Download the PHP package performance-x/opentelemetry-php-instrumentation-trait without Composer
On this page you can find all versions of the php package performance-x/opentelemetry-php-instrumentation-trait. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download performance-x/opentelemetry-php-instrumentation-trait
More information about performance-x/opentelemetry-php-instrumentation-trait
Files in performance-x/opentelemetry-php-instrumentation-trait
Package opentelemetry-php-instrumentation-trait
Short Description A trait to simplify creating OpenTelemetry instrumentations for PHP classes and interfaces
License MIT
Informations about the package opentelemetry-php-instrumentation-trait
OpenTelemetry PHP Instrumentation Trait
A trait to simplify creating OpenTelemetry instrumentations for PHP classes, interfaces, and functions.
Installation
Usage
The trait is designed to be used in instrumentation classes that register OpenTelemetry hooks for specific targets.
Features
- Easy initialization of OpenTelemetry instrumentation
- Support for both class methods and standalone functions
- Configurable span kind (defaults to INTERNAL)
- Automatic prefix for all span attributes via getAttributeName()
- Parameter mapping to span attributes
- Support for pre and post handlers with span access
- Automatic exception handling
- Return value capturing
- Code location attributes (function, namespace, file, line)
Configuration Options
The create()
method accepts:
instrumentation
: Optional pre-configured instrumentation instanceprefix
: Optional prefix for all span attributesspanKind
: Kind of spans to create (default: INTERNAL)className
: Optional target class namename
: Name of the instrumentation if no instrumentation instance provided
At least one of instrumentation
or name
must be provided.
Hook Configuration
The helperHook()
method accepts:
methodName
: The method or function to hookparamMap
: Array of parameters to capture as attributesreturnValueKey
: Optional key for the return value attributepreHandler
: Optional callback for custom span buildingpostHandler
: Optional callback for custom span finishingclassName
: Optional override of target class name
Dynamic Span Names
The trait supports updating span names dynamically during span creation. This is particularly useful when the span name depends on runtime information:
This approach is necessary because span names can only be set during span creation, but sometimes the desired name depends on runtime information available in the pre-handler. By setting the X-PerformanceX-OpenTelemetry-Update-Name
attribute (available as InstrumentationTrait::UPDATE_NAME
), the trait will automatically update the span name during creation.
Parameter Mapping
The paramMap
argument in helperHook()
supports several ways to map method parameters to span attributes:
Value Handling
The trait automatically handles different parameter types appropriately. For example, with Drupal's cache set
method:
Simple Parameter Mapping
Custom Attribute Names
Multiple Parameters with Custom Names
Return Value Handling
The trait can capture method return values. For example, with Drupal's cache get
method:
Combined with Custom Handlers
Error Handling
The trait automatically handles exceptions, but you can customize error handling using post handlers.
Note: While these examples show detailed error capturing, in practice, OpenTelemetry tracing is best combined with proper logging. The trace ID can be added to log entries, allowing you to correlate traces with detailed log messages. This provides better observability than putting log-like information into spans.
Requirements
- PHP 8.2+
- OpenTelemetry PHP SDK
License
MIT License
All versions of opentelemetry-php-instrumentation-trait with dependencies
open-telemetry/api Version ^1.0
open-telemetry/context Version ^1.0
open-telemetry/sem-conv Version ^1.0