PHP Custom Instrumentation

AppSignal is compatible with OpenTelemetry instrumentation packages. Some of these instrumentation packages may require additional setup.

These instrumentations do not not always include all the information you need to debug issues. For more fine-grained reporting, you can add custom instrumentation.

We support OpenTelemetry tracing as laid out in the OpenTelemetry documentation. It's possible to add more data to span using OpenTelemetry span attributes.

There are also AppSignal span attributes that can change how traces are grouped and some attributes that allow for setting sensitive data that is filtered before being sent to our servers.

Creating a new span

Using the tracer from the global tracer provider, create a new span.

PHP
<?php use OpenTelemetry\API\Globals; use OpenTelemetry\API\Trace\Span; function handleRequest() { // Get OpenTelemetry tracerProvider $tracerProvider = Globals::tracerProvider(); $tracer = $tracerProvider->getTracer('my-app'); // Create a new span $span = $tracer->spanBuilder('handleRequest') ->setSpanKind(SpanKind::KIND_SERVER) ->startSpan(); // Set attributes // Close the span $span->end(); }

Note: In these examples we're using the global tracer provider set by OpenTelemetry. You may want to inject the tracer into your classes for better testability.

Get the current span

You can also fetch the current active span:

PHP
<?php use OpenTelemetry\API\Trace\Span; function someFunction() { $span = Span::getCurrent(); // Set attributes on the current span $span->setAttribute('user.id', $userId); $span->setAttribute('operation.name', 'database_query'); }

Note: This will only return a span if one is already active.

More ways to create and get spans are documented on the OpenTelemetry PHP Instrumentation page.

AppSignal attributes

Once you have created, or fetched, a span, you can set certain attributes that are specific to AppSignal to customize how the data arrives in AppSignal.

PHP
<?php $span = // ... $span->setAttribute('attribute_name', 'attribute_value');

See our guides for more information on what custom AppSignal attributes are supported: