Skip to content

Commit 5b99525

Browse files
committed
adding config provider
implement recent additions to the otel spec re: config provider. Config provider should return a ConfigProperties, but we were already returning a ConfigurationRegistry. Added a ConfigProperties interface for ConfigurationRegistry to implement, which gets us pretty close to spec. We don't make ConfigProvider globally available, instead passing the ConfigurationRegistry in to each instrumentation as we register it. The spec allows for this.
1 parent 69825d3 commit 5b99525

File tree

10 files changed

+109
-6
lines changed

10 files changed

+109
-6
lines changed

examples/src/ExampleInstrumentation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace OpenTelemetry\Example;
66

77
use Exception;
8-
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\ConfigurationRegistry;
8+
use OpenTelemetry\API\Configuration\ConfigProperties;
99
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context as InstrumentationContext;
1010
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface;
1111
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Instrumentation;
@@ -15,7 +15,7 @@
1515
final class ExampleInstrumentation implements Instrumentation
1616
{
1717

18-
public function register(HookManagerInterface $hookManager, ConfigurationRegistry $configuration, InstrumentationContext $context): void
18+
public function register(HookManagerInterface $hookManager, ConfigProperties $configuration, InstrumentationContext $context): void
1919
{
2020
$config = $configuration->get(ExampleConfig::class) ?? throw new Exception('example instrumentation must be configured');
2121
if (!$config->enabled) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\API\Configuration;
6+
7+
interface ConfigProperties
8+
{
9+
public function get(string $id): mixed;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\API\Configuration;
6+
7+
interface ConfigProviderInterface
8+
{
9+
public function getInstrumentationConfig(): ConfigProperties;
10+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\API\Configuration\Noop;
6+
7+
use OpenTelemetry\API\Configuration\ConfigProperties;
8+
9+
class NoopConfigProperties implements ConfigProperties
10+
{
11+
12+
public function get(string $id): mixed
13+
{
14+
return null;
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\API\Configuration\Noop;
6+
7+
use OpenTelemetry\API\Configuration\ConfigProperties;
8+
use OpenTelemetry\API\Configuration\ConfigProviderInterface;
9+
10+
class NoopConfigProvider implements ConfigProviderInterface
11+
{
12+
public function getInstrumentationConfig(): ConfigProperties
13+
{
14+
return new NoopConfigProperties();
15+
}
16+
}

src/API/Instrumentation/AutoInstrumentation/ConfigurationRegistry.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace OpenTelemetry\API\Instrumentation\AutoInstrumentation;
66

7-
final class ConfigurationRegistry
7+
use OpenTelemetry\API\Configuration\ConfigProperties;
8+
9+
final class ConfigurationRegistry implements ConfigProperties
810
{
911

1012
private array $configurations = [];
@@ -18,6 +20,7 @@ public function add(InstrumentationConfiguration $configuration): self
1820

1921
/**
2022
* @template C of InstrumentationConfiguration
23+
* @psalm-suppress MoreSpecificImplementedParamType
2124
* @param class-string<C> $id
2225
* @return C|null
2326
*/

src/SDK/SdkAutoloader.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Nevay\SPI\ServiceLoader;
88
use OpenTelemetry\API\Behavior\LogsMessagesTrait;
9+
use OpenTelemetry\API\Configuration\Noop\NoopConfigProperties;
910
use OpenTelemetry\API\Globals;
1011
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context as InstrumentationContext;
1112
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager;
@@ -34,6 +35,7 @@
3435
use OpenTelemetry\SDK\Trace\SamplerFactory;
3536
use OpenTelemetry\SDK\Trace\SpanProcessorFactory;
3637
use OpenTelemetry\SDK\Trace\TracerProviderBuilder;
38+
use RuntimeException;
3739
use Throwable;
3840

3941
/**
@@ -49,6 +51,9 @@ public static function autoload(): bool
4951
return false;
5052
}
5153
if (Configuration::has(Variables::OTEL_EXPERIMENTAL_CONFIG_FILE)) {
54+
if (!class_exists(SdkConfiguration::class)) {
55+
throw new RuntimeException('File-based configuration requires open-telemetry/sdk-configuration');
56+
}
5257
Globals::registerInitializer(fn ($configurator) => self::fileBasedInitializer($configurator));
5358
} else {
5459
Globals::registerInitializer(fn ($configurator) => self::environmentBasedInitializer($configurator));
@@ -131,7 +136,11 @@ private static function registerInstrumentations(): void
131136
$files = Configuration::has(Variables::OTEL_EXPERIMENTAL_CONFIG_FILE)
132137
? Configuration::getList(Variables::OTEL_EXPERIMENTAL_CONFIG_FILE)
133138
: [];
134-
$configuration = SdkInstrumentation::parseFile($files)->create();
139+
if (class_exists(SdkInstrumentation::class)) {
140+
$configuration = SdkInstrumentation::parseFile($files)->create();
141+
} else {
142+
$configuration = new NoopConfigProperties();
143+
}
135144
$hookManager = self::getHookManager();
136145
$tracerProvider = self::createLateBindingTracerProvider();
137146
$meterProvider = self::createLateBindingMeterProvider();
@@ -145,7 +154,6 @@ private static function registerInstrumentations(): void
145154
} catch (Throwable $t) {
146155
self::logError(sprintf('Unable to load instrumentation: %s', $instrumentation::class), ['exception' => $t]);
147156
}
148-
149157
}
150158
}
151159

src/SDK/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
},
5050
"suggest": {
5151
"ext-gmp": "To support unlimited number of synchronous metric readers",
52-
"ext-mbstring": "To increase performance of string operations"
52+
"ext-mbstring": "To increase performance of string operations",
53+
"open-telemetry/sdk-configuration": "File-based OpenTelemetry SDK configuration"
5354
},
5455
"extra": {
5556
"branch-alias": {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Unit\API\Configuration\Noop;
6+
7+
use OpenTelemetry\API\Configuration\Noop\NoopConfigProperties;
8+
use PHPUnit\Framework\Attributes\CoversClass;
9+
use PHPUnit\Framework\TestCase;
10+
11+
#[CoversClass(NoopConfigProperties::class)]
12+
class NoopConfigPropertiesTest extends TestCase
13+
{
14+
public function test_get(): void
15+
{
16+
$properties = new NoopConfigProperties();
17+
$this->assertNull($properties->get('foo'));
18+
}
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Unit\API\Configuration\Noop;
6+
7+
use OpenTelemetry\API\Configuration\Noop\NoopConfigProperties;
8+
use OpenTelemetry\API\Configuration\Noop\NoopConfigProvider;
9+
use PHPUnit\Framework\Attributes\CoversClass;
10+
use PHPUnit\Framework\TestCase;
11+
12+
#[CoversClass(NoopConfigProvider::class)]
13+
class NoopConfigProviderTest extends TestCase
14+
{
15+
public function test_get_instrumentation_config(): void
16+
{
17+
$provider = new NoopConfigProvider();
18+
$this->assertInstanceOf(NoopConfigProperties::class, $provider->getInstrumentationConfig());
19+
}
20+
}

0 commit comments

Comments
 (0)