Resource Detectors in OpenTelemetry PHP
Resource detectors are used to automatically detect and collect information about the environment in which your application is running. This information is then attached to the telemetry data (traces, metrics, and logs) to provide additional context.
The PHP SDK detects resources from a variety of sources, and by default will use all available resource detectors: environment (OTEL_RESOURCE_ATTRIBUTES, OTEL_SERVICE_NAME), host, operating system, and process information.
By default, all SDK resource detectors are used, but you can use the environment variable OTEL_PHP_DETECTORS to enable only certain detectors, or completely disable them:
# Use only specific detectors
env OTEL_PHP_DETECTORS=container,env,os php example.php
# Disable all detectors
env OTEL_PHP_DETECTORS=none php example.php
Built-in detectors
The PHP SDK includes several built-in resource detectors:
- Environment - reads
OTEL_RESOURCE_ATTRIBUTESandOTEL_SERVICE_NAME - Host - detects host information
- Operating System - detects OS details
- Process - detects process information
Available detector packages
Resource detectors for generic platforms or vendor-specific environments can be installed as composer packages.
Container
This package provides an OpenTelemetry ResourceDetector which will detect docker container id at runtime, using either V1 (cgroup) or V2 (mountinfo). It should work with docker, kubernetes, and podman containers.
composer require open-telemetry/detector-container
env OTEL_PHP_DETECTORS=container php example.php
Azure
This package provides OpenTelemetry ResourceDetectors which will detect resource attributes for these Azure services: App Service, Container Apps, Virtual Machines.
composer require open-telemetry/detector-azure
DigitalOcean
composer require open-telemetry/detector-digitalocean
The detector will be automatically registered as part of composer autoloading. By default, all built-in and registered custom resource detectors are used, and will be added to the default resources associated with traces, metrics, and logs.
Custom resource via environment
If there is not an SDK detector for the resource you need, you can add arbitrary resources via the OTEL_RESOURCE_ATTRIBUTES environment variable, which is interpreted by the env detector:
env OTEL_RESOURCE_ATTRIBUTES="service.name=my_service,service.namespace=demo,service.version=1.0,deployment.environment=development" php example.php
Custom resource in code
Custom resources can also be configured in your code. Here, the default resources (detected as described above) are merged with custom resources:
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SemConv\ResourceAttributes;
$resource = ResourceInfoFactory::defaultResource()->merge(
ResourceInfo::create(Attributes::create([
ResourceAttributes::SERVICE_NAMESPACE => 'foo',
ResourceAttributes::SERVICE_NAME => 'bar',
ResourceAttributes::SERVICE_INSTANCE_ID => 1,
ResourceAttributes::SERVICE_VERSION => '0.1',
ResourceAttributes::DEPLOYMENT_ENVIRONMENT_NAME => 'development',
]))
);
$tracerProvider = new TracerProvider(
new SimpleSpanProcessor(
(new ConsoleSpanExporterFactory())->create()
),
null,
$resource
);
Custom resource detector
If you need to detect custom information, you can create your own resource detector by implementing the ResourceDetectorInterface:
<?php
use OpenTelemetry\SDK\Resource\ResourceDetectorInterface;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
class CustomDetector implements ResourceDetectorInterface
{
public function getResource(): ResourceInfo
{
return ResourceInfo::create(Attributes::create([
'custom.key' => 'custom-value',
'environment.type' => 'production',
]));
}
}
// Register and use the custom detector
$resource = ResourceInfoFactory::defaultResource()->merge(
(new CustomDetector())->getResource()
);