Skip to content

Commit 5451d57

Browse files
authored
Allows API packages to specify file-based and env-based config (#1621)
* Move `ComponentProvider` and related classes to API * Use API `ComponentProvider` * Use API `ComponentProvider` in tests * Add `EnvComponentLoader` API * Add `EnvComponentLoader` example * Ignore deptrac violations * Remove namespace from `ComponentProvider` BC layer * Add API requirement to config package * Fix/suppress unrelated phpstan errors caused by `google/protobuf` `^4.31`
1 parent 95229b7 commit 5451d57

File tree

119 files changed

+970
-449
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+970
-449
lines changed

.phan/config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@
381381
'vendor/google/protobuf/src',
382382
'vendor/ramsey/uuid/src',
383383
'vendor/nyholm/psr7-server/src',
384+
'vendor/symfony/config',
384385
],
385386

386387
// A list of individual files to include in analysis

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
"forward-command": true
127127
},
128128
"spi": {
129-
"OpenTelemetry\\Config\\SDK\\Configuration\\ComponentProvider": [
129+
"OpenTelemetry\\API\\Configuration\\Config\\ComponentProvider": [
130130
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Propagator\\TextMapPropagatorB3",
131131
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Propagator\\TextMapPropagatorB3Multi",
132132
"OpenTelemetry\\Config\\SDK\\ComponentProvider\\Propagator\\TextMapPropagatorBaggage",
@@ -180,6 +180,12 @@
180180
"OpenTelemetry\\Tests\\Integration\\Config\\ComponentProvider\\Propagator\\TextMapPropagatorXray",
181181
"OpenTelemetry\\Tests\\Integration\\Config\\ComponentProvider\\Propagator\\TextMapPropagatorOtTrace"
182182
],
183+
"OpenTelemetry\\API\\Configuration\\ConfigEnv\\EnvComponentLoader": [
184+
"OpenTelemetry\\API\\Instrumentation\\Configuration\\General\\ConfigEnv\\EnvComponentLoaderHttpConfig",
185+
"OpenTelemetry\\API\\Instrumentation\\Configuration\\General\\ConfigEnv\\EnvComponentLoaderPeerConfig",
186+
187+
"OpenTelemetry\\Example\\ExampleConfigLoader"
188+
],
183189
"OpenTelemetry\\API\\Instrumentation\\AutoInstrumentation\\HookManagerInterface": [
184190
"OpenTelemetry\\API\\Instrumentation\\AutoInstrumentation\\ExtensionHookManager"
185191
],

deptrac.baseline.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ deptrac:
66
- OpenTelemetry\SDK\Registry
77
/src/Extension/Propagator/Jaeger/_register.php:
88
- OpenTelemetry\SDK\Registry
9+
OpenTelemetry\API\Configuration\Config\ComponentProvider:
10+
- Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition
11+
- Symfony\Component\Config\Definition\Builder\NodeBuilder
12+
OpenTelemetry\API\Configuration\Config\ComponentProviderRegistry:
13+
- Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition
14+
- Symfony\Component\Config\Definition\Builder\NodeDefinition

examples/instrumentation/configure_instrumentation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
namespace _;
66

77
use Nevay\SPI\ServiceLoader;
8+
use OpenTelemetry\API\Configuration\Context;
89
use OpenTelemetry\API\Globals;
910
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\ExtensionHookManager;
1011
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Instrumentation;
1112
use OpenTelemetry\Config\SDK\Configuration;
12-
use OpenTelemetry\Config\SDK\Configuration\Context;
1313
use OpenTelemetry\Example\Example;
1414
use const PHP_EOL;
1515

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace _;
6+
7+
use OpenTelemetry\Example\Example;
8+
use const PHP_EOL;
9+
use function putenv;
10+
11+
/**
12+
* This example uses SPI (see root composer.json extra.spi) to configure an example auto-instrumentation from environment variables.
13+
*/
14+
// php examples/instrumentation/configure_instrumentation_env.php
15+
putenv('OTEL_PHP_AUTOLOAD_ENABLED=true');
16+
putenv('OTEL_PHP_EXAMPLE_INSTRUMENTATION_SPAN_NAME=example span');
17+
putenv('OTEL_TRACES_EXPORTER=console');
18+
putenv('OTEL_METRICS_EXPORTER=none');
19+
putenv('OTEL_LOGS_EXPORTER=none');
20+
21+
require __DIR__ . '/../../vendor/autoload.php';
22+
23+
echo (new Example())->test(), PHP_EOL;

examples/instrumentation/otel-sdk.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
file_format: '0.3'
1+
file_format: '0.4'
22

33
propagator:
44
composite: []

examples/src/ExampleConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
final class ExampleConfig implements InstrumentationConfiguration
1010
{
1111
public function __construct(
12-
public readonly string $spanName,
12+
public readonly string $spanName = 'example',
1313
public readonly bool $enabled = true,
1414
) {
1515
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Example;
6+
7+
use OpenTelemetry\API\Configuration\ConfigEnv\EnvComponentLoader;
8+
use OpenTelemetry\API\Configuration\ConfigEnv\EnvComponentLoaderRegistry;
9+
use OpenTelemetry\API\Configuration\ConfigEnv\EnvResolver;
10+
use OpenTelemetry\API\Configuration\Context;
11+
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\InstrumentationConfiguration;
12+
13+
/**
14+
* @implements EnvComponentLoader<InstrumentationConfiguration>
15+
*/
16+
final class ExampleConfigLoader implements EnvComponentLoader
17+
{
18+
public function load(EnvResolver $env, EnvComponentLoaderRegistry $registry, Context $context): InstrumentationConfiguration
19+
{
20+
return new ExampleConfig(
21+
spanName: $env->string('OTEL_PHP_EXAMPLE_INSTRUMENTATION_SPAN_NAME') ?? 'example',
22+
);
23+
}
24+
25+
public function name(): string
26+
{
27+
return ExampleConfig::class;
28+
}
29+
}

examples/src/ExampleConfigProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace OpenTelemetry\Example;
66

7+
use OpenTelemetry\API\Configuration\Config\ComponentProvider;
8+
use OpenTelemetry\API\Configuration\Config\ComponentProviderRegistry;
9+
use OpenTelemetry\API\Configuration\Context;
710
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\InstrumentationConfiguration;
8-
use OpenTelemetry\Config\SDK\Configuration\ComponentProvider;
9-
use OpenTelemetry\Config\SDK\Configuration\ComponentProviderRegistry;
10-
use OpenTelemetry\Config\SDK\Configuration\Context;
1111
use OpenTelemetry\Config\SDK\Configuration\Validation;
1212
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
1313
use Symfony\Component\Config\Definition\Builder\NodeBuilder;

examples/src/ExampleInstrumentation.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace OpenTelemetry\Example;
66

7-
use Exception;
87
use OpenTelemetry\API\Configuration\ConfigProperties;
98
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context as InstrumentationContext;
109
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface;
@@ -16,7 +15,7 @@ final class ExampleInstrumentation implements Instrumentation
1615
{
1716
public function register(HookManagerInterface $hookManager, ConfigProperties $configuration, InstrumentationContext $context): void
1817
{
19-
$config = $configuration->get(ExampleConfig::class) ?? throw new Exception('example instrumentation must be configured');
18+
$config = $configuration->get(ExampleConfig::class) ?? new ExampleConfig();
2019
if (!$config->enabled) {
2120
return;
2221
}

0 commit comments

Comments
 (0)