OpenTelemetry
Module: Magenx_OpenTelemetry · no GraphQL schema, no database tables, no cron
The storefront already traces its own work, including every call it makes to Magento. This module makes Magento’s side of each call part of the same trace. A slow product page then shows, in one waterfall, the Next.js render, the /api/graphql proxy and the Magento request, named after its GraphQL operation (GraphQL GetProduct). The Magento log entries that request wrote link to it by trace id.
Shoppers see nothing. Spans are exported after the response has been sent, so tracing adds no latency.
How it works
Section titled “How it works”- One span per request. Every Magento web request (GraphQL, shop pages, REST, admin) is recorded as one server span. Its parent is the W3C
traceparentheader that the storefront sends to Magento only. - Named after the GraphQL operation. The name comes from the parsed query document, so the collector’s request-rate, error-rate and latency metrics break down per operation, next to the storefront’s own span for the same operation. Other requests are named
<METHOD> <route>, for exampleGET catalog_product_view. - Log correlation. A Magento log entry written during a sampled request ends with
{"trace_id":"…","span_id":"…"}. The collector readsvar/log/*.log, extracts the ids and links the entry to its trace. - No added latency.
- PHP-FPM: the response is sent to the client before the spans are exported.
- FrankenPHP worker (GraphQL App Server): spans are exported after each response, before the next request.
- Inert until configured. Without the OpenTelemetry environment variables nothing is recorded, whatever the admin setting says.
Spans carry the HTTP method, the URL path (no query string), the area, the store code, the response status, and the GraphQL operation name and type. They never carry customer data or query variables.
Set it up
Section titled “Set it up”The observability guide sets up the collector and OpenObserve. Then:
-
Install the module (see Install below). First allow the SDK’s Composer plugin:
Terminal window composer config allow-plugins.tbachert/spi truecomposer config allow-plugins.php-http/discovery false -
Set the OpenTelemetry environment on PHP-FPM and the FrankenPHP worker (not on the CLI or cron containers):
Terminal window OTEL_PHP_AUTOLOAD_ENABLED=trueOTEL_SERVICE_NAME=magentoOTEL_TRACES_EXPORTER=otlpOTEL_METRICS_EXPORTER=noneOTEL_LOGS_EXPORTER=noneOTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318OTEL_EXPORTER_OTLP_PROTOCOL=http/protobufOTEL_EXPORTER_OTLP_TIMEOUT=1000OTEL_PROPAGATORS=tracecontextOTEL_TRACES_SAMPLER=parentbased_always_onIn the Docker stack this is one switch:
MAGENTO_OTEL_ENABLED="true"in.env. PHP-FPM passes the container environment to PHP only withclear_env = noin the pool; otherwise declare each variable asenv[NAME] = value. -
Optional, for database query spans under each request: install the
opentelemetryPHP extension (built into the Docker stack’s images) andcomposer require open-telemetry/opentelemetry-auto-pdo. -
Restart PHP-FPM and the worker. Send a request through the storefront and open its trace in OpenObserve: the Magento span sits under the storefront’s
graphql <operation>span.
Metrics and logs are deliberately off in the SDK. The collector derives metrics from spans, and it ships the log files itself, so nothing is sent twice.
Limits
Section titled “Limits”- Operation names come from the client’s query document. In this stack Magento’s
/graphqlis private: only the storefront proxy reaches it, and its allowlist pins the documents, so the set of names stays small. Behind a public/graphql, any client could invent names and grow the metric label set; keep it private. - PHP-FPM sends the response early on traced requests. Output written after the response, such as the HTML profiler, is dropped. Turn off Send Response Before Export (PHP-FPM) while you debug such output.
- The worker exports on its own thread after each traced request. Keep
OTEL_EXPORTER_OTLP_TIMEOUTshort (1000 ms): it is the most a down collector can hold a worker. - Only log entries written while the request runs are stamped, and only for sampled traces.
- CLI commands and cron jobs are not traced.
User flows
Section titled “User flows”Traced API request
- A shopper opens a product page; the storefront starts a trace and calls the back office's API with the trace context.
- The back office starts a request span whose parent is the storefront's span.
- The API operation is read from the request document; the span is renamed after it.
- A log entry written while the request runs carries the trace and span ids.
- The response is sent to the shopper; then the span is exported.
- The tracing backend shows the back-office span under the storefront span, and the log entry linked to it.
Investigate a slow page
- An engineer opens a slow trace in the tracing backend.
- The waterfall shows the storefront span, the back-office span named after the operation, and any database spans under it.
- The engineer opens the linked log entries of that back-office span.
Compare operations
- The collector derives request rate, error rate and latency percentiles from spans, labelled by service and API operation.
- A dashboard shows the slowest operations in the back office next to the same operation seen from the storefront.
Turn tracing on or off
- An operator sets the environment settings (endpoint, service name, sampling) on the back office's web processes and restarts them.
- Requests start producing spans.
- An admin user switches tracing off in the admin panel; new requests produce no spans, with no restart.
These always hold:
- Tracing never changes a response's body, headers or status.
- One request's span, attributes or ids never appear in another request, including on a long-running application server.
- Span names and attributes never contain customer data, request variables, URL query strings, ids taken from the URL, or credentials.
- The set of possible span names is bounded by the route names, areas and allowlisted API operations.
- Trace context is only read from the incoming request; this module never sends it on to third parties.
- A storefront trace for a page that calls the back office contains the back-office request span as a child of the storefront's call span.
- A log entry written during that request carries the same trace id as the span.
Edge cases
Section titled “Edge cases”| Situation | What happens |
|---|---|
| Incoming trace context is missing. | New root span; request served. |
| Incoming trace context is malformed. | Ignored; new root span; request served. |
| Incoming trace context is unsampled. | Span not exported; log entries unchanged. |
| Two requests in turn on one long-running worker. | Two spans, two distinct trace ids, nothing carried over. |
| Request throws an unhandled error. | Error passes on unchanged; span ended, marked failed, request state cleared. |
| API request document has a syntax error. | Span keeps its generic name; the API reports the syntax error as usual. |
| Document with two operations and a requested name. | Span named after the requested one. |
| Collector unreachable. | Responses unaffected; at most the export timeout spent per worker after the response. |
| Admin switch off while tracing is configured. | No span; log entries unchanged. |
| Tracing not configured, switch on. | No span, no export attempt; log entries unchanged. |
| Internal forward to a second handler. | One span for the request. |
Who it applies to
Section titled “Who it applies to”- Back-office web requests only, in every area and store view; command-line tasks and scheduled jobs are not traced.
Out of scope
Section titled “Out of scope”- Metrics exported by the back office itself (request rates and latencies are derived from spans by the collector).
- Sending log entries over the trace protocol; log files are shipped by the collector.
- Child spans for individual resolvers, cache calls or service calls written by this module (database spans may come from a generic instrumentation library).
- Tracing command-line tasks and scheduled jobs.
- A storefront or admin user interface for viewing traces.
Glossary
Section titled “Glossary”| Term | Meaning |
|---|---|
| Trace | The tree of spans for one end-to-end request, identified by a trace id. |
| Span | One timed unit of work in a trace, with a name, a parent, attributes and a status. |
| Request span | The single server span this module records for one back-office request. |
| Trace context | The trace id, parent span id and sampling flag carried between services in the W3C Trace Context header. |
| Sampled | The trace context's flag saying the trace is recorded; an unsampled trace is not stored by the backend. |
| API operation | The named operation in an API request document (for example "GetProduct"). |
| Collector | The service that receives spans, reads log files and forwards both to the tracing backend. |
| Export | Sending finished spans to the collector over the OpenTelemetry protocol (OTLP). |
| Per-request process model | Each request runs in a process state that is discarded at its end. |
| Long-running application server | One process serves many requests in turn and keeps state between them. |
| Tracing configured | The environment names an export endpoint and turns the tracing library on. |
Install
Section titled “Install”composer require magenxcommerce/module-open-telemetrybin/magento module:enable Magenx_OpenTelemetrybin/magento setup:upgradebin/magento setup:di:compile # production mode onlybin/magento cache:flush- PHP
~8.4.0||~8.5.0, Magento 2.4.8. - Optional:
ext-opentelemetry- Needed by the auto-instrumentation packages below (pie install open-telemetry/ext-opentelemetry). - Optional:
ext-protobuf- Faster OTLP encoding than the pure-PHP google/protobuf fallback. - Optional:
open-telemetry/opentelemetry-auto-pdo- Database query spans under each request span (needs ext-opentelemetry).
Configuration
Section titled “Configuration”Stores > Configuration > Magenx > OpenTelemetry
General
| Setting | Default | Scope | Notes |
|---|---|---|---|
Request Spansmagenx_open_telemetry/ | Yes | Global | Yes/No. Records one span per request, linked to the storefront's trace. Takes effect only when the OpenTelemetry SDK is configured through the OTEL_* environment variables of PHP-FPM and the FrankenPHP worker; without them nothing is recorded either way. |
Send Response Before Export (PHP-FPM)magenx_open_telemetry/ | Yes | Global | Yes/No. Shown when Request Spans = Yes. On a traced PHP-FPM request, sends the response to the client with fastcgi_finish_request() before spans are exported, so the export adds no latency. Output written after the response (such as the HTML profiler) is then dropped. The FrankenPHP worker exports after each response by itself. |
Admin pages and permissions
Section titled “Admin pages and permissions”Grant these under System > Permissions > User Roles > Role Resources:
| Resource | Under | ID |
|---|---|---|
| OpenTelemetry | Stores > Settings > Configuration | Magenx_OpenTelemetry::config |