接入案例:ip-server 与 ip-server-client
以两个真实项目演示 OTel 模块的完整接入:RPC 服务端 ip-server (查询 IP 归属地)与它的客户端 ip-server-client(命令行 + 网页)。 端到端链路:
console query / 网页(ip-server-client)
│ TraceRpcClient:CLIENT span,注入 traceparent
▼ jsonrpc over HTTP(POST /rpc)
ip-server
│ TraceRpcServer:SERVER span,提取 traceparent,接续同一条 trace
├─ 阿里云 IP 定位 API(TraceGuzzleRequest:CLIENT span)
└─ Redis 缓存客户端的 CLIENT span 与服务端的 SERVER span 归属同一条 trace, 全部指标与 span 落到本地可观测性栈(VictoriaMetrics / VictoriaTraces)。
项目侧接入步骤
两个项目的接入动作相同,共三步:
1. 引入 OTel 模块
kuiper v0.9 从本仓库(otel 分支)以 composer path repository 引入。Kuiper 是 monorepo, 按组件独立发包,路径仓库用通配符指向各子包目录,composer.json 中加:
"repositories": [
{
"type": "path",
"url": "<kuiper-worktree 路径>/*",
"options": { "symlink": true }
}
],
"require": {
"kuiper/runtime": "^0.9",
"kuiper/rpc": "^0.9",
"kuiper/otel": "^0.9"
}Composer 会读取通配到的每个子包目录里的 composer.json,以其中的包名 (kuiper/runtime、kuiper/rpc、kuiper/otel 等)和版本(^0.9 系列)解析依赖。
OTel PHP SDK 依赖(open-telemetry/sdk、open-telemetry/exporter-otlp) 由 kuiper/otel 自带,应用无需显式声明。
2. 注册配置类
src/container.php 的 configuration 数组加入 OtelConfiguration。 注意它要排在消费 application.rpc.* / application.web.middleware 配置的配置类之前(它负责把埋点中间件写进这些配置节):
'configuration' => [
kuiper\logger\LoggerConfiguration::class,
kuiper\http\client\HttpClientConfiguration::class,
// 先写入埋点中间件配置……
kuiper\otel\OtelConfiguration::class,
// ……再由这些配置类消费
kuiper\web\WebConfiguration::class,
kuiper\rpc\RpcServerConfiguration::class,
],3. 环境变量启用
.env(或进程环境变量,php -S 下需走 .env):
OTEL_ENABLED=true
OTEL_SERVICE_NAME=ip-server # 每个 服务 一个名字
OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4318之后 HTTP 入/出站、数据库、Redis、RPC 客户端/服务端的 trace 与 metrics 自动生效,业务代码零改动。全部配置项见 配置指南。
业务自定义埋点示例
自动埋点覆盖框架链路;业务维度(缓存命中、查询结果)用 Telemetry 门面自己记。两个项目的示例:
ip-server:缓存 counter + 结果 span
IpServantImpl(构造器注入 Telemetry,OTel 未启用时全部 Noop、零开销):
use kuiper\otel\Telemetry;
use OpenTelemetry\API\Metrics\CounterInterface;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
$this->cacheOperations = $telemetry->meter('winwin.ip')->createCounter(
'ip.server.cache.operations',
'{operation}',
'IP 查询缓存命中/回源次数'
);
// 缓存命中/回源各记一次
$this->cacheOperations->add(1, ['cache.hit' => true]);
// 业务子 span:记录结果属性。IP 地址是高基数属性,只进 span 不进 metrics
$span = $this->telemetry->tracer('winwin.ip')
->spanBuilder('resolve ip info')
->setSpanKind(SpanKind::KIND_INTERNAL)
->setAttribute('ip.address', $ip)
->startSpan();
$scope = $span->activate();
try {
$ipInfo = $this->resolveFromRemote($ip);
$span->setAttribute('ip.country', $ipInfo->country)
->setAttribute('ip.province', $ipInfo->province);
$span->end();
return $ipInfo;
} catch (\Throwable $e) {
$span->recordException($e);
$span->setStatus(StatusCode::STATUS_ERROR, $e->getMessage());
$span->end();
throw $e;
} finally {
$scope->detach();
}ip-server-client:查询结果 counter
QueryIpCommand(命令行每次查询按结果维度计数):
$this->queries = $telemetry->meter('winwin.ip.client')->createCounter(
'ip.client.queries',
'{query}',
'IP 查询成功/失败次数'
);
try {
$info = $this->ipServant->query($ip);
$this->queries->add(1, ['result' => 'success']);
} catch (RpcResponseException $e) {
$this->queries->add(1, ['result' => 'error']);
// ……
}指标维度遵循低基数原则:只有
cache.hit/result这类枚举值, IP 地址、查询词等高基数信息放进 span 属性。
本地运行与验证
# 1. 启动观测栈(deploy/otel/docker-compose.yml 不在本仓库,在 otel 相关分支/配套仓库)
docker compose -f deploy/otel/docker-compose.yml up -d
# 2. 启动 ip-server(OTel 变量在 .env)
cd <ip-server 项目目录>
php -S 127.0.0.1:8001 public/index.php
# 3. 客户端查询(OTel 变量在 .env,RPC 地址指向 127.0.0.1:8001/rpc)
cd <ip-server-client 项目目录>
./console query 8.8.8.8验证 trace —— 客户端 CLIENT span 与服务端 SERVER span 在同一条 trace:
# 服务列表应出现 ip-server 与 ip-server-client
curl -s "http://127.0.0.1:9428/select/jaeger/api/services"
# 查最近 2 小时的 trace(时间参数为微秒);浏览器 UI:http://127.0.0.1:9428/select/vmui/
NOW=$(date +%s); curl -s "http://127.0.0.1:9428/select/jaeger/api/traces?service=ip-server-client&start=$(( (NOW-7200)*1000000 ))&end=$(( NOW*1000000 ))&limit=5"一条完整 trace 包含三个 span:
| 服务 | span | 来源 |
|---|---|---|
| ip-server-client | IpServantProxy/query | TraceRpcClient(CLIENT) |
| ip-server | POST /rpc | 入站 HTTP |
| ip-server | IpServant/query | TraceRpcServer(SERVER) |
验证 metrics:
# 业务 counter(PromQL 里点号转下划线)
curl -s "http://127.0.0.1:8428/api/v1/query" --get \
--data-urlencode 'query=ip_client_queries_total'
curl -s "http://127.0.0.1:8428/api/v1/query" --get \
--data-urlencode 'query=ip_server_cache_operations_total'
# 框架自动指标
curl -s "http://127.0.0.1:8428/api/v1/query" --get \
--data-urlencode 'query=http_server_requests_total'CLI 是一次性进程:trace 在进程退出时冲刷,metrics 按
OTEL_METRIC_EXPORT_INTERVAL_MS(默认 60s)或在下次记录时惰性导出。
从 v0.8 升级客户端的注意点
ip-server-client 从 kuiper v0.8 分包升级到 v0.9 时顺带接入 OTel, 踩到的差异对其他 v0.8 项目同样适用(详见 迁移说明):
- 生成代码用 tars-generator ≥0.9 重新生成(
#[JsonRpcClient]→#[RpcClient]); 或按迁移文档在项目里放一个继承RpcClient的 3 行兼容 attribute kuiper\jsonrpc\config\JsonRpcClientConfiguration→kuiper\rpc\RpcClientConfiguration(server 侧同理);kuiper\resilience\ResilienceConfiguration在 v0.9 中仍然保留,可继续使用- RPC 单端口
/rpc路径分发取代 v0.8 的双端口 listener; 测试里APP_PHP_CONFIG_FILE换成APP_CONFIG_FILE - v0.9 客户端把服务端业务异常还原为
RpcResponseException(业务码仍在getCode()),不再是 v0.8 的ServerException - php -S / 子进程注意:v0.9 读取环境变量合并
getenv()与$_ENV, 端口等配置由.env(Runtime 自动加载)或显式导出提供