定制 traceway/opentelemetry-symfony 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

traceway/opentelemetry-symfony

Composer 安装命令:

composer require traceway/opentelemetry-symfony

包简介

Pure-PHP OpenTelemetry instrumentation for Symfony — automatic HTTP, Console, HttpClient, Messenger, Doctrine DBAL, Cache, Twig tracing and Monolog log-trace correlation with response propagation, a lightweight Tracing helper, route templates, and semantic conventions. No C extension required (ext-p

README 文档

README

Traceway Logo

OpenTelemetry Symfony Bundle

CI codecov Packagist Version Packagist Downloads PHP Version Symfony Version License Discord

Pure-PHP OpenTelemetry instrumentation for Symfony. Automatic tracing for HTTP, Console, HttpClient, Messenger, Mailer, Scheduler, Doctrine DBAL, Cache, and Twig — plus Monolog log-trace correlation, OTel log export, and opt-in metrics. No C extension required.

Works with any OpenTelemetry-compatible backend: Traceway, Jaeger, Zipkin, Datadog, Grafana Tempo, Honeycomb, AWS X-Ray, and more.

  • Pure PHP — installs on every managed Symfony host
  • Production-ready — stable since v1.0, PHPStan level 10 with no baseline, Symfony 6.4 LTS through 8.x
  • Correct under load — Messenger context propagates across async boundaries, DBAL 3 and 4 CI-tested, re-entrance guards on HttpClient and the log handler

Installation

composer require traceway/opentelemetry-symfony

Symfony Flex registers the bundle automatically. Without Flex, add it to config/bundles.php:

return [
    // ...
    Traceway\OpenTelemetryBundle\OpenTelemetryBundle::class => ['all' => true],
];

Quick Start

OTEL_PHP_AUTOLOAD_ENABLED=true
OTEL_SERVICE_NAME=my-symfony-app
OTEL_TRACES_EXPORTER=otlp
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
OTEL_EXPORTER_OTLP_PROTOCOL=http/json

That's it. Every HTTP request, console command, outgoing call, Messenger job, DB query, cache operation, and Twig render is now traced.

It is also possible to use Symfony's DotEnv component in combination with the open_telemetry.sdk bundle configuration section for OTEL_PHP_AUTOLOAD_ENABLED, OTEL_RESOURCE_ATTRIBUTES and OTEL_EXPORTER_OTLP_HEADERS to set the relevant SDK configuration, see docs/configuration.md for more details. Using configuration setting open_telemetry.sdk.autoload_enabled registers the OpenTelemetry SDK without needing to set OTEL_PHP_AUTOLOAD_ENABLED before Composer's autoload is loaded. Furthermore, using open_telemetry.sdk.resource_attributes allows OTEL_RESOURCE_ATTRIBUTES to be set more conveniently and also supports the use of Symfony Secrets and the same applies with open_telemetry.sdk.exporter_otlp_headers for OTEL_EXPORTER_OTLP_HEADERS.

Verify the wiring:

bin/console traceway:doctor

What Gets Traced

Component Span Kind What's captured
HTTP requests SERVER Route templates (GET /api/items/{id}), status codes, body sizes, client IP, exceptions, sub-requests
Console commands INTERNAL Command name, argv, pid, exit code, exceptions
HttpClient CLIENT Outgoing requests with W3C context propagation, OTLP endpoint auto-excluded, re-entrance guard
Messenger PRODUCER/CONSUMER Message class, transport, W3C context propagation across async boundaries
Scheduler CONSUMER Schedule name, trigger, next-run, cancellation marker. Requires symfony/scheduler
Mailer PRODUCER + CLIENT create span on MailerInterface::send, send span on the transport. Recipient count, message-id, X-Transport routing
Doctrine DBAL CLIENT Parameterised SQL, transactions, db system/namespace auto-detection. DBAL 3.6+ and 4.x CI-tested
Cache INTERNAL get (hit/miss), delete, invalidateTags with pool name. Requires symfony/cache
Twig INTERNAL Template name, nested includes. Requires twig/twig
Monolog Inject trace_id, span_id + trace_flags into every log record. Opt-in OTel Logs API export with per-channel scope

Also: Server-Timing response headers, full OTel semantic conventions.

Semantic Conventions

This bundle tracks the current OTel semantic conventions, including details most instrumentations skip: _OTHER method normalization, url.full credential/query redaction, default-port inference, error.type on every failure path, stable db.system.name values, SQLSTATE as db.response.status_code, and the per-signal histogram bucket advisories.

Deliberate deviations, chosen so task-oriented backends group telemetry usefully:

Where Spec says We do Why
Messenger span name send {transport} send {MessageClass} Tasks group per message type, not per queue
Console span name {process.executable.name} the command name app:import beats php (allowed low-cardinality alternative)
Consumer parenting span links by default parent-child (links with root_spans: true) end-to-end traces out of the box
db.system, db.statement, … deprecated dual-emitted alongside the stable keys migration aid for older backends, removal in v3.0
db.query.text sanitize by default recorded verbatim (prepared statements are placeholder-safe) disable with traces.doctrine.record_statements: false

Custom attributes (console.command, cache.*, twig.*, scheduler.*, messaging.message.class) cover areas with no registered convention yet.

Configuration

All options are optional — the bundle works out of the box. A minimal config/packages/open_telemetry.yaml:

open_telemetry:
    traces:
        excluded_paths: [/health, /_profiler, /_wdt]
    metrics:
        enabled: true
    logs:
        correlation:
            enabled: true

See docs/configuration.md for the full reference and environment variables.

Manual Instrumentation

Inject TracingInterface for one-liner span creation:

use Traceway\OpenTelemetryBundle\TracingInterface;

class OrderService
{
    public function __construct(private readonly TracingInterface $tracing) {}

    public function process(int $orderId): void
    {
        $this->tracing->trace('order.validate', fn () => $this->validate($orderId));
        $this->tracing->trace('order.fulfill', function () {
            $this->tracing->trace('inventory.reserve', fn () => $this->reserve());
            $this->tracing->trace('payment.charge', fn () => $this->charge());
        });
    }
}

Mock in tests with $this->createStub(TracingInterface::class) and have trace() invoke the callback directly.

Metrics

Opt-in OpenTelemetry metrics — Messenger, Doctrine DBAL, HTTP server/client, and Mailer — alongside a MeterRegistryInterface for custom counters/histograms. See docs/metrics.md.

Doctor

bin/console traceway:doctor runs diagnostic checks against the bundle's wiring, SDK environment variables, and OTLP endpoint reachability — text or JSON, scriptable in CI.

Runtime
  ✓ ext-opentelemetry not loaded (no conflict risk)
SDK configuration
  ✓ OTEL_SERVICE_NAME = "my-symfony-app"
  ✓ OTEL_TRACES_EXPORTER = otlp
Connectivity
  ✓ OTLP endpoint reachable (HTTP 404, 7ms)
Results: 9 ok, 0 warning, 0 error, 3 skipped, 0 info

See docs/doctor.md for flags, JSON envelope schema, and custom checks.

Documentation

Configuration Full config reference and environment variables
Metrics Instrument list, manual metrics, exemplars
AWS X-Ray Native propagator / id_generator keys
Doctor traceway:doctor flags, JSON output, custom checks
Performance Overhead, sampling, exporter choice
Upgrade from v1.x Flat → nested config migration
Changelog Release history

Contributing

git clone https://github.com/tracewayapp/opentelemetry-symfony-bundle.git
cd opentelemetry-symfony-bundle
composer install
vendor/bin/phpunit
vendor/bin/phpstan analyse

See CONTRIBUTING.md. Join the conversation on Discord.

License

MIT

traceway/opentelemetry-symfony 适用场景与选型建议

traceway/opentelemetry-symfony 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.39k 次下载、GitHub Stars 达 77, 最近一次更新时间为 2026 年 03 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「logging」 「symfony」 「twig」 「cache」 「monolog」 「console」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 traceway/opentelemetry-symfony 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 traceway/opentelemetry-symfony 我们能提供哪些服务?
定制开发 / 二次开发

基于 traceway/opentelemetry-symfony 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 17.39k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 77
  • 点击次数: 43
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 77
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-14