Automatically instrument your PHP application with OpenTelemetry

This walkthrough shows how to add observability to your PHP application using the OpenTelemetry PHP libraries and tools.

Enrichment with OneAgent

It is currently not possible to enrich automatically instrumented services with host-relevant information. To achieve this, you'd need to switch to manual instrumentation.

Step 1 Get the Dynatrace access details

Determine the API base URL

For details on how to assemble the base OTLP endpoint URL, see Export with OTLP. The URL should end in /api/v2/otlp.

Get API access token

The access token for ingesting traces, logs, and metrics can be generated under Access Tokens.

Export with OTLP has more details on the format and the necessary access scopes.

Step 2 Instrument your application

  1. Ensure you have an adequate build environment for your system set up, consisting of GCC, Make, and Autoconfig.

  2. Build and install the instrumentation library, using pickle.

    php pickle.phar install opentelemetry
  3. Add the newly compiled library as extension to your php.ini.

    extension=opentelemetry.so
  4. Restart PHP and verify that the extension was loaded.

    • From the command line, with php -m
    • As part of a web server, by calling phpinfo()
  5. Install the SDK and other dependencies.

    • required Install the SDK for OpenTelemetry PHP.
    • optional Depending on the libraries your application is using, you might want to add other instrumentation libraries to the dependencies. You'll find the list of supported libraries in the OpenTelemetry Registry.
    • required You must use composer autoloading, as this is the mechanism that all auto-instrumentation packages use to register themselves.
  6. Configure the following environment variables.

    OTEL_PHP_AUTOLOAD_ENABLED=true
    OTEL_SERVICE_NAME=php-quickstart
    OTEL_PROPAGATORS=baggage,tracecontext
    OTEL_EXPORTER=otlp
    OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
    OTEL_EXPORTER_OTLP_ENDPOINT=[URL]
    OTEL_EXPORTER_OTLP_HEADERS="Authorization=Api-Token [TOKEN]"
    OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE=delta

    Substitute [URL] and [TOKEN] with the respective values.

Step 3 Ensure context propagation

Context propagation is particularly important when network calls (for example, REST) are involved.

With automatic instrumentation, this should be automatically taken care of by the instrumentation libraries. If the used network libraries are not be covered by that, you would need to switch to manual instrumentation instead and handle propagation manually.

Step 4 optional Configure data capture to meet privacy requirements optional

While Dynatrace automatically captures all OpenTelemetry attributes, only attribute values specified in the allowlist are stored and displayed in the Dynatrace web UI. This prevents accidental storage of personal data, so you can meet your privacy requirements and control the amount of monitoring data stored.

To view your custom attributes, you need to allow them in the Dynatrace web UI first. To learn how to configure attribute storage and masking, see Attribute redaction.

Step 5 Verify data ingestion into Dynatrace

Once you have finished the instrumentation of your application, perform a couple of test actions to create and send demo traces, metrics, and logs and verify that they were correctly ingested into Dynatrace.

To do that for traces, go to Distributed Traces or Distributed Traces Classic (latest Dynatrace) and select the Ingested traces tab. If you use OneAgent, select PurePaths instead.

For metrics and logs, go to Metrics or Logs or Logs & Events (latest Dynatrace).