Skip to content

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.

traceparentOTLP, after the responsevar/log entry + trace_id

Browser

Storefront
span: graphql GetProduct

Magento
span: GraphQL GetProduct

OTel Collector

OpenObserve
traces · metrics · logs

  • One span per request. Every Magento web request (GraphQL, shop pages, REST, admin) is recorded as one server span. Its parent is the W3C traceparent header 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 example GET catalog_product_view.
  • Log correlation. A Magento log entry written during a sampled request ends with {"trace_id":"…","span_id":"…"}. The collector reads var/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.

The observability guide sets up the collector and OpenObserve. Then:

  1. Install the module (see Install below). First allow the SDK’s Composer plugin:

    Terminal window
    composer config allow-plugins.tbachert/spi true
    composer config allow-plugins.php-http/discovery false
  2. Set the OpenTelemetry environment on PHP-FPM and the FrankenPHP worker (not on the CLI or cron containers):

    Terminal window
    OTEL_PHP_AUTOLOAD_ENABLED=true
    OTEL_SERVICE_NAME=magento
    OTEL_TRACES_EXPORTER=otlp
    OTEL_METRICS_EXPORTER=none
    OTEL_LOGS_EXPORTER=none
    OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318
    OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
    OTEL_EXPORTER_OTLP_TIMEOUT=1000
    OTEL_PROPAGATORS=tracecontext
    OTEL_TRACES_SAMPLER=parentbased_always_on

    In the Docker stack this is one switch: MAGENTO_OTEL_ENABLED="true" in .env. PHP-FPM passes the container environment to PHP only with clear_env = no in the pool; otherwise declare each variable as env[NAME] = value.

  3. Optional, for database query spans under each request: install the opentelemetry PHP extension (built into the Docker stack’s images) and composer require open-telemetry/opentelemetry-auto-pdo.

  4. 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.

  • Operation names come from the client’s query document. In this stack Magento’s /graphql is 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_TIMEOUT short (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.

Traced API request

  1. A shopper opens a product page; the storefront starts a trace and calls the back office's API with the trace context.
  2. The back office starts a request span whose parent is the storefront's span.
  3. The API operation is read from the request document; the span is renamed after it.
  4. A log entry written while the request runs carries the trace and span ids.
  5. The response is sent to the shopper; then the span is exported.
  6. The tracing backend shows the back-office span under the storefront span, and the log entry linked to it.

Investigate a slow page

  1. An engineer opens a slow trace in the tracing backend.
  2. The waterfall shows the storefront span, the back-office span named after the operation, and any database spans under it.
  3. The engineer opens the linked log entries of that back-office span.

Compare operations

  1. The collector derives request rate, error rate and latency percentiles from spans, labelled by service and API operation.
  2. A dashboard shows the slowest operations in the back office next to the same operation seen from the storefront.

Turn tracing on or off

  1. An operator sets the environment settings (endpoint, service name, sampling) on the back office's web processes and restarts them.
  2. Requests start producing spans.
  3. 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.
SituationWhat 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.
  • Back-office web requests only, in every area and store view; command-line tasks and scheduled jobs are not traced.
  • 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.
TermMeaning
TraceThe tree of spans for one end-to-end request, identified by a trace id.
SpanOne timed unit of work in a trace, with a name, a parent, attributes and a status.
Request spanThe single server span this module records for one back-office request.
Trace contextThe trace id, parent span id and sampling flag carried between services in the W3C Trace Context header.
SampledThe trace context's flag saying the trace is recorded; an unsampled trace is not stored by the backend.
API operationThe named operation in an API request document (for example "GetProduct").
CollectorThe service that receives spans, reads log files and forwards both to the tracing backend.
ExportSending finished spans to the collector over the OpenTelemetry protocol (OTLP).
Per-request process modelEach request runs in a process state that is discarded at its end.
Long-running application serverOne process serves many requests in turn and keeps state between them.
Tracing configuredThe environment names an export endpoint and turns the tracing library on.
Terminal window
composer require magenxcommerce/module-open-telemetry
bin/magento module:enable Magenx_OpenTelemetry
bin/magento setup:upgrade
bin/magento setup:di:compile # production mode only
bin/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).

Stores > Configuration > Magenx > OpenTelemetry

General

SettingDefaultScopeNotes
Request Spans
magenx_open_telemetry/general/enabled
YesGlobalYes/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/general/finish_request
YesGlobalYes/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.

Grant these under System > Permissions > User Roles > Role Resources:

ResourceUnderID
OpenTelemetryStores > Settings > ConfigurationMagenx_OpenTelemetry::config