msstc4symfony / tracing-bridge-otlp
Exports msstc4symfony/profiling-bundle spans with msstc4symfony/tracing-bundle ids over OTLP
Package info
github.com/msstc4symfony/tracing-bridge-otlp
Type:symfony-bundle
pkg:composer/msstc4symfony/tracing-bridge-otlp
Requires
- php: >=8.2
- composer-runtime-api: >=2.2
- msstc4symfony/profiling-bundle: ^2.3.1
- msstc4symfony/tracing-bridge-profiling: ^1.3
- msstc4symfony/tracing-bundle: ^2.4
- nyholm/psr7: ^1.8
- open-telemetry/exporter-otlp: ^1.4
- open-telemetry/sdk: ^1.15
- psr/cache: ^2.0|^3.0
- psr/clock: ^1.0
- psr/log: ^3.0
- symfony/cache: ^7.4|^8.0
- symfony/clock: ^7.4|^8.0
- symfony/config: ^7.4|^8.0
- symfony/dependency-injection: ^7.4|^8.0
- symfony/event-dispatcher: ^7.4|^8.0
- symfony/http-client: ^7.4|^8.0
- symfony/http-kernel: ^7.4|^8.0
- symfony/polyfill-mbstring: ^1.23
- symfony/service-contracts: ^3.5
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.76
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.1
- phpstan/phpstan-strict-rules: ^2.0
- phpstan/phpstan-symfony: ^2.0
- phpunit/phpunit: >=11.0
- rector/rector: ^2.1
- roave/security-advisories: dev-latest
- symfony/console: ^7.4|^8.0
- symfony/filesystem: ^7.4|^8.0
- symfony/framework-bundle: ^7.4|^8.0
- symfony/messenger: ^7.4|^8.0
- symfony/monolog-bundle: ^3.11|^4.0
- symfony/process: ^7.4|^8.0
Suggests
- ext-curl: bounds DNS resolution by timeout_ms (the native HTTP client resolves names without a timeout)
- monolog/monolog: logs.enabled: Monolog records over OTLP
- open-telemetry/transport-grpc: protocol: grpc (needs ext-grpc)
Provides
None
Conflicts
- monolog/monolog: <3.0
- symfony/symfony: *
Replaces
None
README
Exports the spans of profiling-bundle to any OTLP receiver
(OpenTelemetry Collector, Grafana Alloy or Tempo, Jaeger, a vendor) with the trace and span ids of
tracing-bundle: the ids in the logs, in traceparent headers and in
Messenger stamps are the ids of the exported spans. It uses the official OpenTelemetry OTLP exporter, but the
OpenTelemetry SDK does not run the spans: it would make up ids of its own.
Compatibility
| Bundle | PHP | Symfony | profiling-bundle | tracing-bundle | tracing-bridge-profiling |
|---|---|---|---|---|---|
| 1.0 | 8.2+ | 7.4, 8.x | ^2.2 | ^2.2 | ^1.2 |
| 1.1 | 8.2+ | 7.4, 8.x | ^2.2 | ^2.2 | ^1.2 |
| 1.2 | 8.2+ | 7.4, 8.x | ^2.3.1 | ^2.4 | ^1.3 |
64-bit PHP. Symfony 8.x needs PHP 8.4; on PHP 8.2 and 8.3 Composer installs Symfony 7.4.
Installation
composer require msstc4symfony/tracing-bridge-otlp
Register the four bundles in config/bundles.php (order does not matter):
Msstc4Symfony\TracingBundle\TracingBundle::class => ['all' => true], Msstc4Symfony\ProfilingBundle\ProfilingBundle::class => ['all' => true], Msstc4Symfony\TracingBridgeProfiling\TracingBridgeProfilingBundle::class => ['all' => true], Msstc4Symfony\TracingBridgeOtlp\TracingBridgeOtlpBundle::class => ['all' => true],
For Messenger, tracing-bundle's OutgoingTraceMiddleware and IncomingTraceMiddleware must be on the bus (see its
README); without them a consumed message starts a new trace.
What is exported
Every span profiling records, once it ends, when its trace is sampled:
| Span | Span id, parent | Kind |
|---|---|---|
Unit of work: request <route>, cli command <name>, message <class>, action <class> (with tracing-bridge-logic) |
the unit's span of tracing-bundle; parent: the caller's span from traceparent / TraceStamp (remote), the enclosing unit, or none |
SERVER, INTERNAL, CONSUMER, INTERNAL |
| A span tracing-bridge-profiling traced (yours, DBAL, Action) | its trace_span_id / trace_parent_span_id |
CLIENT for profiling's automatic DBAL spans, else INTERNAL |
| Detached HTTP client span of profiling | the span id tracing-bridge-profiling reserved for the request (the one its traceparent names), under the span current when the request started |
CLIENT |
- Name: the span message; bytes that are not UTF-8 become
?(the OTLP encoder rejects them). Times: start and duration of profiling, microsecond precision. - Attributes: every scalar context value as
profiling.<key>, plus the semantic convention namesexception.type(a non-empty stringexception),http.response.status_code(an integerstatus, and the response code of a request span),db.namespace(connectionof a DBAL span) andmessaging.destination.name(transport). Arrays, objects, enums,null, NAN and INF are dropped, and so are keys that are not UTF-8; strings are cut to 4096 characters (bytes for a string that is not UTF-8) (OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT/OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT), so one huge value cannot push the batch over the receiver's size limit. The bridge adds nothing profiling did not record (no SQL text, no query string). - Status: ERROR with a non-empty
exception,message_failed: trueor an HTTP status of 500 and above; otherwise unset. - Events: a span with a non-empty
exceptionrecords anexceptionevent (attributeexception.type) at its end. - Links: a
message …CONSUMER span links to the sender's span (its remote parent), as the messaging conventions ask. - Resource:
service.namefromservice_name, elseOTEL_SERVICE_NAME/OTEL_RESOURCE_ATTRIBUTES, else the name of the project directory; the SDK's resource detectors add the rest. Scope:msstc4symfony/tracing-bridge-otlp.
Not exported:
- spans of an unsampled trace. A new trace is sampled by tracing-bundle's
sampling.ratio; a continued one keeps the caller's flag; - profiling's
NullSpan(spans its decision makers filtered out); - spans opened outside a unit of work tracing-bridge-profiling knows, and spans of a route, command or message that is
not in profiling's
routes/commands/messageslists: there is no unit span, so the children point at a parent that is never exported. Such a span name is logged once atdebug.
Delivery
Spans wait in memory for the end of their unit of work: kernel.terminate, console.terminate, the worker moving on
after a message (WorkerRunningEvent) or stopping. Then one OTLP request sends them; a unit with more than
max_buffered_spans spans sends early. Spans profiling ends on kernel.reset (left open by the unit) go with the next
export.
The export never throws into the application. A failed export (receiver down, timeout, rejected or partially rejected
batch, a wrong endpoint or protocol) logs one warning on the otlp channel with the number of dropped spans, and
exporting pauses for 30 seconds: the spans of that time are dropped and counted in the next log record (info when
exporting resumes). There are no retries. Without pause_pool, the pause is shared through a filesystem cache in
%kernel.share_dir%/msstc4symfony_tracing_bridge_otlp (%kernel.cache_dir%/… when the kernel has no share dir), so
the PHP-FPM children, cron commands and workers using that directory wait too: a PHP-FPM request reads it once, a
long-running worker at most once a second. The application's cache pools are not used: their lookups are metrics. When
the directory is not writable the pause holds per process. Spans and log records pause separately, so a receiver without a
log pipeline still gets the spans. The cost of a down receiver is at most timeout_ms once per pause, signal and
directory: typically a host or a container, several hosts when share_dir is a shared volume.
To share the pause between hosts, point pause_pool at a PSR-6 pool they share, for example a dedicated pool on Redis:
framework: cache: pools: otlp_pause: adapter: cache.adapter.redis provider: '%env(REDIS_DSN)%' msstc4symfony_tracing_bridge_otlp: pause_pool: otlp_pause
Then a down receiver costs timeout_ms once per pause and signal for the whole cluster. The value is a service id without @.
The hosts must share framework.cache.prefix_seed: a pool's key namespace derives from it, and it defaults to the
project directory, which differs between release directories. Prefer a pool of its own, left out of cache metrics
(metrics-bundle cache.pools): a counted pool records one lookup per exported signal and PHP-FPM request, and at most
one a second per signal and worker. The hosts compare the pause with their clocks, so they need synced clocks (NTP). Each of those lookups is a
round trip, at the end of the unit of work: give the Redis provider a short
timeout. A pool that fails holds the pause per process.
The exporter has its own HTTP client: the application's http_client is traced and profiled, so the export would trace
itself. Install ext-curl: the native HTTP client resolves host names without a timeout.
Configuration
# config/packages/msstc4symfony_tracing_bridge_otlp.yaml msstc4symfony_tracing_bridge_otlp: enabled: true # false: no service is registered service_name: ~ # ~: OTEL_SERVICE_NAME / OTEL_RESOURCE_ATTRIBUTES, else the project directory name endpoint: ~ # base URL, /v1/traces is appended for HTTP; ~: the variables below, else http://localhost:4318 protocol: ~ # http/protobuf | http/json | grpc; ~: the variables below, else http/protobuf timeout_ms: 1000 # whole export request max_buffered_spans: 2048 # spans per unit of work before an early export pause_pool: ~ # PSR-6 pool service id for the export pause; ~: files in kernel.share_dir, else kernel.cache_dir logs: enabled: false # true: Monolog records over OTLP (see Logs) level: info # lowest Monolog level exported max_buffered_logs: 2048
service_name and endpoint take env placeholders ('%env(OTLP_ENDPOINT)%'); an empty value means unset, as an
empty OTEL_* variable does. pause_pool is a service id, fixed when the container is built: no placeholder; an
empty one keeps the filesystem cache.
Environment variables honoured: OTEL_EXPORTER_OTLP_TRACES_ENDPOINT (used as is) and OTEL_EXPORTER_OTLP_ENDPOINT,
OTEL_EXPORTER_OTLP[_TRACES]_PROTOCOL (http/protobuf, http/json, grpc), OTEL_EXPORTER_OTLP[_TRACES]_HEADERS,
OTEL_EXPORTER_OTLP[_TRACES]_COMPRESSION (none, gzip), OTEL_EXPORTER_OTLP[_TRACES]_CERTIFICATE,
OTEL_EXPORTER_OTLP[_TRACES]_CLIENT_CERTIFICATE, OTEL_EXPORTER_OTLP[_TRACES]_CLIENT_KEY,
OTEL_[SPAN_]ATTRIBUTE_VALUE_LENGTH_LIMIT, OTEL_SERVICE_NAME, OTEL_RESOURCE_ATTRIBUTES. The configuration wins over
them. OTEL_EXPORTER_OTLP_TIMEOUT is ignored: the SDK's 10 seconds would hold every request and message that long
when the receiver hangs.
protocol: grpc needs open-telemetry/transport-grpc and ext-grpc; configured without them the container does not
compile. gRPC chosen through the variables fails at the first export instead (one warning per pause).
The OpenTelemetry SDK also reports a failed export through its own logger (OTEL_PHP_LOG_DESTINATION, by default
PHP's error log). Set OTEL_PHP_LOG_DESTINATION=none to keep only the bridge's warning.
Logs
With logs.enabled the bundle registers the Monolog handler msstc4symfony_tracing_bridge_otlp.logs_handler; add it
to your handlers (it needs monolog/monolog):
monolog: handlers: otlp: type: service id: msstc4symfony_tracing_bridge_otlp.logs_handler
Each record becomes an OTLP log record of the span it was logged in: the trace_id / span_id that tracing-bundle's
TraceLogProcessor put in its extra, so a buffering handler (fingers_crossed, buffer) that passes records on
later keeps them; without them, the span current when the handler writes it. The sampled flag is that of the trace when
it is still current, also for an unsampled trace. Records written outside a unit of work (an idle worker) carry a trace
of their own without spans.
- Body: the message, cleaned of bytes that are not UTF-8 but not cut. Severity: from the level.
- Attributes:
monolog.channel,exception.type/exception.messagefor aThrowableincontext['exception'], then the scalarextraandcontextvalues (context wins;trace_idandspan_idbecome the trace context instead). The bridge's own names come first and win over keys spelled the same. Values are cut and cleaned like span attributes (OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT/OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT). - Every scalar value goes out, including what processors add (
ip,urlof WebProcessor, user ids): export only what you would log. - The records go with the spans at the end of the unit of work, or earlier when
max_buffered_logsis reached. A failed log export logs onewarningand pauses the log export, not the spans; records of the bridge's ownotlpchannel are not exported. - The endpoint and protocol are the span ones;
OTEL_EXPORTER_OTLP_LOGS_*take the place of the_TRACES_variables.
Limitations
- A Guzzle redirect is a new pass through the handler stack: its
traceparentnames the span around the call, not the CLIENT span (Symfony HttpClient keeps the CLIENT span on every redirect). - A fatal error ends PHP before the export: the spans of that unit of work are lost.
- A
service_namewhose env variable is missing at runtime loses the export silently: spans and log records are not built, the application goes on. - Log records written after the unit's export and before
kernel.reset(akernel.terminate/console.terminatelistener below -8192, aWorkerRunningEventlistener between -512 and -1024) are dropped with the reset. - One buffer serves the container: spans of concurrent Fibers share it.
- With an asynchronous Messenger execution strategy the worker may move on before the handler ran: the
message …span then gets the worker's idle trace instead of the message's. - A create span processor of the application that wraps spans must run before the bridge's (priority above -65536), or its spans are not exported.
Public API
The bundle class Msstc4Symfony\TracingBridgeOtlp\TracingBridgeOtlpBundle and its configuration. Everything else is
@internal.
Local development
composer-ci.json installs profiling-bundle, tracing-bundle and tracing-bridge-profiling.
make install-ci # composer-ci.json profile with every CI tool COMPOSER=composer-ci.json make check # lint, PHPStan, CS, composer validate/audit, Rector, deptrac make test # unit + integration (a local php -S OTLP receiver)
License
MIT — see LICENSE.