Skip to content

Commit

Permalink
reduce default set of resource detectors
Browse files Browse the repository at this point in the history
Limit the default detectors from everything to:
- sdk
- sdk_provided
- env

This is controlled by a new detector key, OTEL_PHP_DETECTORS=default (which is
the default if no value is provided). The previous behaviour can be restored
by explicitly setting OTEL_PHP_DETECTORS=all.
  • Loading branch information
brettmc committed Jan 9, 2025
1 parent c6f742d commit 4368d31
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/SDK/Common/Configuration/Defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ interface Defaults
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md#language-specific-environment-variables
*/
public const OTEL_PHP_TRACES_PROCESSOR = 'batch';
public const OTEL_PHP_DETECTORS = 'all';
public const OTEL_PHP_DETECTORS = 'default';
public const OTEL_PHP_AUTOLOAD_ENABLED = 'false';
public const OTEL_PHP_INTERNAL_METRICS_ENABLED = 'false';
public const OTEL_PHP_DISABLED_INSTRUMENTATIONS = [];
Expand Down
2 changes: 2 additions & 0 deletions src/SDK/Common/Configuration/KnownValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ interface KnownValues
public const VALUE_STDOUT = 'stdout';
public const VALUE_PSR3 = 'psr3';
public const VALUE_EMPTY = '';
public const VALUE_DETECTORS_DEFAULT = 'default';
public const VALUE_DETECTORS_ENVIRONMENT = 'env';
public const VALUE_DETECTORS_HOST = 'host';
public const VALUE_DETECTORS_OS = 'os';
Expand All @@ -195,6 +196,7 @@ interface KnownValues
public const VALUE_DETECTORS_SERVICE = 'service';
public const VALUE_DETECTORS_COMPOSER = 'composer';
public const OTEL_PHP_DETECTORS = [
self::VALUE_DETECTORS_DEFAULT,
self::VALUE_ALL,
self::VALUE_DETECTORS_ENVIRONMENT,
self::VALUE_DETECTORS_HOST,
Expand Down
6 changes: 6 additions & 0 deletions src/SDK/Resource/ResourceInfoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public static function defaultResource(): ResourceInfo

foreach ($detectors as $detector) {
switch ($detector) {
case Values::VALUE_DETECTORS_DEFAULT:
$resourceDetectors[] = new Detectors\Sdk();
$resourceDetectors[] = new Detectors\SdkProvided();
$resourceDetectors[] = new Detectors\Environment();

break;
case Values::VALUE_DETECTORS_SERVICE:
$resourceDetectors[] = new Detectors\Service();

Expand Down
1 change: 1 addition & 0 deletions tests/Integration/SDK/Resource/ResourceInfoFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ResourceInfoFactoryTest extends TestCase

public function test_all_default_resources(): void
{
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', 'all');
$resource = ResourceInfoFactory::defaultResource();

$this->assertSame(ResourceAttributes::SCHEMA_URL, $resource->getSchemaUrl());
Expand Down
32 changes: 32 additions & 0 deletions tests/Unit/SDK/Resource/ResourceInfoFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public static function schemaUrlsToMergeProvider(): Generator
#[Group('trace-compliance')]
public function test_resource_service_name_default(): void
{
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', 'all');
$resource = ResourceInfoFactory::defaultResource();
$this->assertEquals('open-telemetry/opentelemetry', $resource->getAttributes()->get('service.name'));
}
Expand All @@ -106,6 +107,7 @@ public function test_resource_with_invalid_environment_variable(): void
#[Group('compliance')]
public function test_resource_from_environment_service_name_takes_precedence_over_resource_attribute(): void
{
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', 'all');
$this->setEnvironmentVariable('OTEL_RESOURCE_ATTRIBUTES', 'service.name=bar');
$this->setEnvironmentVariable('OTEL_SERVICE_NAME', 'foo');
$resource = ResourceInfoFactory::defaultResource();
Expand All @@ -115,6 +117,7 @@ public function test_resource_from_environment_service_name_takes_precedence_ove
#[Group('compliance')]
public function test_resource_from_environment_resource_attribute_takes_precedence_over_default(): void
{
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', 'all');
$this->setEnvironmentVariable('OTEL_RESOURCE_ATTRIBUTES', 'service.name=foo');
$resource = ResourceInfoFactory::defaultResource();
$this->assertEquals('foo', $resource->getAttributes()->get('service.name'));
Expand Down Expand Up @@ -162,6 +165,35 @@ public function test_default_with_all_sdk_detectors(): void
}
}

/**
* From SDK 2.x, the default detectors are reduced to: sdk, sdk_provided, env
*/
#[DataProvider('defaultProvider')]
public function test_default_detectors(?string $value): void
{
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', $value);
$resource = ResourceInfoFactory::defaultResource();
$keys = array_keys($resource->getAttributes()->toArray());
$expected = [
'telemetry.sdk.name',
'telemetry.sdk.language',
'telemetry.sdk.version',
'telemetry.distro.name',
'telemetry.distro.version',
'service.name',
];

$this->assertEquals($expected, $keys);
}

public static function defaultProvider(): array
{
return [
['default'],
[null],
];
}

public function test_default_with_none_detectors(): void
{
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', 'none');
Expand Down

0 comments on commit 4368d31

Please sign in to comment.