Skip to content

Commit feea76e

Browse files
authored
reduce default set of resource detectors (#1471)
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.
1 parent a19455c commit feea76e

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

src/SDK/Common/Configuration/Defaults.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ interface Defaults
113113
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md#language-specific-environment-variables
114114
*/
115115
public const OTEL_PHP_TRACES_PROCESSOR = 'batch';
116-
public const OTEL_PHP_DETECTORS = 'all';
116+
public const OTEL_PHP_DETECTORS = 'default';
117117
public const OTEL_PHP_AUTOLOAD_ENABLED = 'false';
118118
public const OTEL_PHP_INTERNAL_METRICS_ENABLED = 'false';
119119
public const OTEL_PHP_DISABLED_INSTRUMENTATIONS = [];

src/SDK/Common/Configuration/KnownValues.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ interface KnownValues
189189
public const VALUE_STDOUT = 'stdout';
190190
public const VALUE_PSR3 = 'psr3';
191191
public const VALUE_EMPTY = '';
192+
public const VALUE_DETECTORS_DEFAULT = 'default';
192193
public const VALUE_DETECTORS_ENVIRONMENT = 'env';
193194
public const VALUE_DETECTORS_HOST = 'host';
194195
public const VALUE_DETECTORS_OS = 'os';
@@ -199,6 +200,7 @@ interface KnownValues
199200
public const VALUE_DETECTORS_SERVICE = 'service';
200201
public const VALUE_DETECTORS_COMPOSER = 'composer';
201202
public const OTEL_PHP_DETECTORS = [
203+
self::VALUE_DETECTORS_DEFAULT,
202204
self::VALUE_ALL,
203205
self::VALUE_DETECTORS_ENVIRONMENT,
204206
self::VALUE_DETECTORS_HOST,

src/SDK/Resource/ResourceInfoFactory.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public static function defaultResource(): ResourceInfo
4242

4343
foreach ($detectors as $detector) {
4444
switch ($detector) {
45+
case Values::VALUE_DETECTORS_DEFAULT:
46+
$resourceDetectors[] = new Detectors\Sdk();
47+
$resourceDetectors[] = new Detectors\SdkProvided();
48+
$resourceDetectors[] = new Detectors\Environment();
49+
50+
break;
4551
case Values::VALUE_DETECTORS_SERVICE:
4652
$resourceDetectors[] = new Detectors\Service();
4753

tests/Integration/SDK/Resource/ResourceInfoFactoryTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ResourceInfoFactoryTest extends TestCase
1818

1919
public function test_all_default_resources(): void
2020
{
21+
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', 'all');
2122
$resource = ResourceInfoFactory::defaultResource();
2223

2324
$this->assertSame(ResourceAttributes::SCHEMA_URL, $resource->getSchemaUrl());

tests/Unit/SDK/Resource/ResourceInfoFactoryTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public static function schemaUrlsToMergeProvider(): Generator
8484
#[Group('trace-compliance')]
8585
public function test_resource_service_name_default(): void
8686
{
87+
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', 'all');
8788
$resource = ResourceInfoFactory::defaultResource();
8889
$this->assertEquals('open-telemetry/opentelemetry', $resource->getAttributes()->get('service.name'));
8990
}
@@ -106,6 +107,7 @@ public function test_resource_with_invalid_environment_variable(): void
106107
#[Group('compliance')]
107108
public function test_resource_from_environment_service_name_takes_precedence_over_resource_attribute(): void
108109
{
110+
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', 'all');
109111
$this->setEnvironmentVariable('OTEL_RESOURCE_ATTRIBUTES', 'service.name=bar');
110112
$this->setEnvironmentVariable('OTEL_SERVICE_NAME', 'foo');
111113
$resource = ResourceInfoFactory::defaultResource();
@@ -115,6 +117,7 @@ public function test_resource_from_environment_service_name_takes_precedence_ove
115117
#[Group('compliance')]
116118
public function test_resource_from_environment_resource_attribute_takes_precedence_over_default(): void
117119
{
120+
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', 'all');
118121
$this->setEnvironmentVariable('OTEL_RESOURCE_ATTRIBUTES', 'service.name=foo');
119122
$resource = ResourceInfoFactory::defaultResource();
120123
$this->assertEquals('foo', $resource->getAttributes()->get('service.name'));
@@ -162,6 +165,35 @@ public function test_default_with_all_sdk_detectors(): void
162165
}
163166
}
164167

168+
/**
169+
* From SDK 2.x, the default detectors are reduced to: sdk, sdk_provided, env
170+
*/
171+
#[DataProvider('defaultProvider')]
172+
public function test_default_detectors(?string $value): void
173+
{
174+
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', $value);
175+
$resource = ResourceInfoFactory::defaultResource();
176+
$keys = array_keys($resource->getAttributes()->toArray());
177+
$expected = [
178+
'telemetry.sdk.name',
179+
'telemetry.sdk.language',
180+
'telemetry.sdk.version',
181+
'telemetry.distro.name',
182+
'telemetry.distro.version',
183+
'service.name',
184+
];
185+
186+
$this->assertEquals($expected, $keys);
187+
}
188+
189+
public static function defaultProvider(): array
190+
{
191+
return [
192+
['default'],
193+
[null],
194+
];
195+
}
196+
165197
public function test_default_with_none_detectors(): void
166198
{
167199
$this->setEnvironmentVariable('OTEL_PHP_DETECTORS', 'none');

0 commit comments

Comments
 (0)