cascata/hyperf-opentelemetry
Composer 安装命令:
composer require cascata/hyperf-opentelemetry
包简介
Drop-in OpenTelemetry integration for Hyperf 3.1+ (traces, metrics and logs via OTLP).
关键字:
README 文档
README
Drop-in OpenTelemetry integration for Hyperf 3.1+ — traces, metrics and logs via OTLP, with sensible Swoole defaults.
Out of the box you get:
- HTTP server spans for every inbound request (
SERVERkind, W3C TraceContext propagation) #[Traced]attribute to wrap any service/use-case/repository method in a span- Correlation ID middleware (propagates
X-Correlation-Id) - Monolog handler that ships every log via OTLP, with
trace_id/span_idinjected automatically - Coroutine-safe trace context (survives the end of individual spans, so logs emitted by exception handlers still correlate)
- Optional Docker Compose stack with OTel Collector → Tempo / Prometheus / Loki / Grafana
Requirements
| PHP | >= 8.1 |
| Hyperf | ~3.1.0 |
| Swoole | >= 5.0 |
| ext-grpc | not required (uses OTLP HTTP) |
Install
composer require cascata/hyperf-opentelemetry
Register the tracing middleware in config/autoload/middlewares.php:
return [ 'http' => [ \Cascata\HyperfOpenTelemetry\Middleware\CorrelationIdMiddleware::class, \Cascata\HyperfOpenTelemetry\Middleware\TracingMiddleware::class, // ... your other middlewares ], ];
Add the Monolog handler to config/autoload/logger.php (your existing config):
use Cascata\HyperfOpenTelemetry\Logging\OtelLogHandler; use Cascata\HyperfOpenTelemetry\Logging\OtelTraceContextProcessor; use Monolog\Formatter\JsonFormatter; use Monolog\Handler\StreamHandler; use Monolog\Logger; return [ 'default' => [ 'handlers' => [ [ 'class' => StreamHandler::class, 'constructor' => [ 'stream' => BASE_PATH . '/runtime/logs/hyperf.log', 'level' => Logger::DEBUG, ], 'formatter' => [ 'class' => JsonFormatter::class, 'constructor' => [], ], ], [ 'class' => OtelLogHandler::class, 'constructor' => [ 'level' => Logger::INFO, ], ], ], 'processors' => [ ['class' => OtelTraceContextProcessor::class], ], ], ];
Set environment variables:
OTEL_ENABLED=true OTEL_SERVICE_NAME=my-service OTEL_SERVICE_NAMESPACE=mycompany OTEL_SERVICE_VERSION=1.0.0 OTEL_DEPLOYMENT_ENV=dev OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318 OTEL_TRACES_SAMPLER_ARG=1.0
Clear the DI proxy cache and you're done:
rm -rf runtime/container
Usage
Annotate methods you want traced
use Cascata\HyperfOpenTelemetry\Tracing\Annotation\Traced; class CreateUser { #[Traced(name: 'usecase.user.create')] public function execute(CreateUserInput $input): CreateUserOutput { // automatic span: usecase.user.create } }
Span kinds:
#[Traced(name: 'mq.welcome.publish', kind: 'producer')] // PRODUCER (publish) #[Traced(name: 'mq.welcome.consume', kind: 'consumer')] // CONSUMER (read) #[Traced(name: 'http.payment.charge', kind: 'client')] // CLIENT (outbound) #[Traced(name: 'usecase.foo')] // INTERNAL (default)
Aspects only work on methods that:
- are public, non-static
- belong to classes resolved by the Hyperf container (DI)
Methods called via new ClassName() won't be intercepted.
Custom metrics
use OpenTelemetry\API\Globals; $meter = Globals::meterProvider()->getMeter('my-service.business'); $counter = $meter->createCounter('orders_created_total'); $counter->add(1, ['tenant' => $tenantId]);
Publish optional resources
The package ships an optional config file and a complete Docker Compose stack.
# Publish opentelemetry.php to config/autoload/ php bin/hyperf.php vendor:publish cascata/hyperf-opentelemetry --id=config # Publish Tempo + Prometheus + Loki + Grafana + OTel Collector docker stack php bin/hyperf.php vendor:publish cascata/hyperf-opentelemetry --id=observability-stack
The published stack lives at BASE_PATH/observability/. Bring it up with:
docker network create hyperf-network # only once cd observability && docker compose up -d
Grafana: http://localhost:3000 (anonymous Admin).
Architecture
Application code
│ $logger->info() or handler emits span
▼
Cascata\HyperfOpenTelemetry ──► global TracerProvider / MeterProvider / LoggerProvider
│
▼
OTLP HTTP (4318)
│
▼
OTel Collector ──► Tempo (traces)
──► Prom (metrics, via spanmetrics connector)
──► Loki (logs)
│
▼
Grafana
The application only ever speaks OTLP to the collector. Swapping Tempo for Jaeger, or Loki for Elasticsearch, only changes the collector config — your code does not change.
Configuration reference
All values can be set via env or by publishing opentelemetry.php.
| Env var | Default | Purpose |
|---|---|---|
OTEL_ENABLED |
true |
Master switch |
OTEL_SERVICE_NAME |
hyperf-app |
service.name resource attribute |
OTEL_SERVICE_NAMESPACE |
default |
service.namespace |
OTEL_SERVICE_VERSION |
1.0.0 |
service.version |
OTEL_DEPLOYMENT_ENV |
dev |
deployment.environment.name |
OTEL_EXPORTER_OTLP_ENDPOINT |
http://otel-collector:4318 |
OTLP HTTP base URL |
OTEL_TRACES_SAMPLER_ARG |
1.0 |
TraceID-ratio sampler (0.0 – 1.0) |
Troubleshooting
No spans / logs / metrics arriving in Grafana
Verify the collector is reachable from inside the app container:
docker exec <your-app> curl -s -o /dev/null -w "%{http_code}\n" \ http://otel-collector:4318/v1/traces -X POST \ -H "Content-Type: application/x-protobuf" --data "test" # expected: 400 (means the endpoint is up, payload was rejected as expected)
Enable internal debug logging:
OTEL_LOG_LEVEL=debug OTEL_PHP_INTERNAL_LOG_LEVEL=debug
Span::getCurrent() returns no-op span in exception handlers
That's why this package also stores trace_id/span_id in
Hyperf\Context — logs emitted after a span closes still correlate. No
action needed on your side.
Changes to #[Traced] annotations don't take effect
The Hyperf DI proxy cache must be regenerated:
rm -rf runtime/container && php bin/hyperf.php start
Batch processor doesn't flush in Swoole
This package uses SimpleSpanProcessor / SimpleLogRecordProcessor by
default — they export synchronously, which is the safe choice in long-running
Swoole workers. For very high-throughput services, consider implementing a
periodic flush listener instead.
Versioning
| Branch | Hyperf | OTel SDK |
|---|---|---|
1.x |
~3.1.0 |
^1.1 |
Follows SemVer.
License
MIT
cascata/hyperf-opentelemetry 适用场景与选型建议
cascata/hyperf-opentelemetry 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 05 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Metrics」 「logs」 「swoole」 「prometheus」 「tracing」 「grafana」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cascata/hyperf-opentelemetry 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cascata/hyperf-opentelemetry 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cascata/hyperf-opentelemetry 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The tenancy/tenancy logs mutations
Laravel 5.x.x library for integration Monolog Sentry
A abstraction for generating metrics in Laravel.
Laravel SDK for working with prometheus metrics
Production-ready Laravel Prometheus metrics package with built-in collectors for HTTP, database, cache, queue, events, errors, filesystem, and mail monitoring
Tool to show code coverage metrics, measured by PHP Unit in Cobertura format, in console and CI/CD pipeline
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 49
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-05-14