From b50c27a56ffb00b03540d922d46d9bcfa2b6b9d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Reyrol?= Date: Sun, 22 Dec 2024 15:02:44 +0100 Subject: [PATCH] fix(DependencyInjection): pass configured resource info to providers (#106) --- .../OpenTelemetryExtension.php | 8 +++++++ .../OpenTelemetryLogsExtension.php | 1 + .../OpenTelemetryMetricsExtension.php | 1 + .../OpenTelemetryTracesExtension.php | 1 + .../DefaultLoggerProviderFactory.php | 5 +++-- .../LoggerProviderFactoryInterface.php | 3 ++- .../NoopLoggerProviderFactory.php | 3 ++- .../DefaultMeterProviderFactory.php | 6 ++--- .../MeterProviderFactoryInterface.php | 3 ++- .../NoopMeterProviderFactory.php | 3 ++- .../Resource/ResourceInfoFactory.php | 22 +++++++++++++++++++ .../DefaultTracerProviderFactory.php | 5 +++-- .../NoopTracerProviderFactory.php | 3 ++- .../TracerProviderFactoryInterface.php | 3 ++- src/Resources/config/services.php | 5 +++++ .../OpenTelemetryExtensionTest.php | 3 +++ .../OpenTelemetryLogsExtensionTest.php | 5 +++++ .../OpenTelemetryMetricsExtensionTest.php | 5 +++++ .../OpenTelemetryTracesExtensionTest.php | 5 +++++ .../Resource/ResourceInfoFactoryTest.php | 22 +++++++++++++++++++ 20 files changed, 99 insertions(+), 13 deletions(-) create mode 100644 src/OpenTelemetry/Resource/ResourceInfoFactory.php create mode 100644 tests/Unit/OpenTelemetry/Resource/ResourceInfoFactoryTest.php diff --git a/src/DependencyInjection/OpenTelemetryExtension.php b/src/DependencyInjection/OpenTelemetryExtension.php index 2512924..ea71ccd 100644 --- a/src/DependencyInjection/OpenTelemetryExtension.php +++ b/src/DependencyInjection/OpenTelemetryExtension.php @@ -57,6 +57,14 @@ private function loadServiceParams(array $config, ContainerBuilder $container): $container->setParameter('open_telemetry.service.name', $config['name']); $container->setParameter('open_telemetry.service.version', $config['version']); $container->setParameter('open_telemetry.service.environment', $config['environment']); + + $container->getDefinition('open_telemetry.resource_info') + ->setArguments([ + $config['namespace'], + $config['name'], + $config['version'], + $config['environment'], + ]); } /** diff --git a/src/DependencyInjection/OpenTelemetryLogsExtension.php b/src/DependencyInjection/OpenTelemetryLogsExtension.php index 54f450f..90b4fe8 100644 --- a/src/DependencyInjection/OpenTelemetryLogsExtension.php +++ b/src/DependencyInjection/OpenTelemetryLogsExtension.php @@ -118,6 +118,7 @@ private function loadLogProvider(string $name, array $config): void ->setFactory([new Reference(sprintf('open_telemetry.logs.provider_factory.%s', $config['type'])), 'createProvider']) ->setArguments([ isset($config['processor']) ? new Reference($config['processor']) : null, + new Reference('open_telemetry.resource_info'), ]); } diff --git a/src/DependencyInjection/OpenTelemetryMetricsExtension.php b/src/DependencyInjection/OpenTelemetryMetricsExtension.php index 96d42f3..87e833c 100644 --- a/src/DependencyInjection/OpenTelemetryMetricsExtension.php +++ b/src/DependencyInjection/OpenTelemetryMetricsExtension.php @@ -101,6 +101,7 @@ private function loadMetricProvider(string $name, array $config): void ->setArguments([ isset($config['exporter']) ? new Reference($config['exporter']) : null, $filter, + new Reference('open_telemetry.resource_info'), ]); } diff --git a/src/DependencyInjection/OpenTelemetryTracesExtension.php b/src/DependencyInjection/OpenTelemetryTracesExtension.php index e8d7038..fd786da 100644 --- a/src/DependencyInjection/OpenTelemetryTracesExtension.php +++ b/src/DependencyInjection/OpenTelemetryTracesExtension.php @@ -127,6 +127,7 @@ private function loadTraceProvider(string $name, array $config): void ->setArguments([ $sampler, isset($config['processors']) ? array_map(fn (string $processor) => new Reference($processor), $config['processors']) : null, + new Reference('open_telemetry.resource_info'), ]); } diff --git a/src/OpenTelemetry/Log/LoggerProvider/DefaultLoggerProviderFactory.php b/src/OpenTelemetry/Log/LoggerProvider/DefaultLoggerProviderFactory.php index 7966c76..33885d0 100644 --- a/src/OpenTelemetry/Log/LoggerProvider/DefaultLoggerProviderFactory.php +++ b/src/OpenTelemetry/Log/LoggerProvider/DefaultLoggerProviderFactory.php @@ -7,15 +7,16 @@ use OpenTelemetry\SDK\Logs\LoggerProviderInterface; use OpenTelemetry\SDK\Logs\LogRecordLimitsBuilder; use OpenTelemetry\SDK\Logs\LogRecordProcessorInterface; +use OpenTelemetry\SDK\Resource\ResourceInfo; final class DefaultLoggerProviderFactory extends AbstractLoggerProviderFactory { - public function createProvider(?LogRecordProcessorInterface $processor = null): LoggerProviderInterface + public function createProvider(?LogRecordProcessorInterface $processor = null, ?ResourceInfo $resource = null): LoggerProviderInterface { $instrumentationScopeFactory = new InstrumentationScopeFactory((new LogRecordLimitsBuilder())->build()->getAttributeFactory()); assert($processor instanceof LogRecordProcessorInterface); - return new LoggerProvider($processor, $instrumentationScopeFactory); + return new LoggerProvider($processor, $instrumentationScopeFactory, $resource); } } diff --git a/src/OpenTelemetry/Log/LoggerProvider/LoggerProviderFactoryInterface.php b/src/OpenTelemetry/Log/LoggerProvider/LoggerProviderFactoryInterface.php index 0d36f71..6eb4093 100644 --- a/src/OpenTelemetry/Log/LoggerProvider/LoggerProviderFactoryInterface.php +++ b/src/OpenTelemetry/Log/LoggerProvider/LoggerProviderFactoryInterface.php @@ -4,8 +4,9 @@ use OpenTelemetry\SDK\Logs\LoggerProviderInterface; use OpenTelemetry\SDK\Logs\LogRecordProcessorInterface; +use OpenTelemetry\SDK\Resource\ResourceInfo; interface LoggerProviderFactoryInterface { - public function createProvider(?LogRecordProcessorInterface $processor = null): LoggerProviderInterface; + public function createProvider(?LogRecordProcessorInterface $processor = null, ?ResourceInfo $resource = null): LoggerProviderInterface; } diff --git a/src/OpenTelemetry/Log/LoggerProvider/NoopLoggerProviderFactory.php b/src/OpenTelemetry/Log/LoggerProvider/NoopLoggerProviderFactory.php index f9d8ac2..9640dec 100644 --- a/src/OpenTelemetry/Log/LoggerProvider/NoopLoggerProviderFactory.php +++ b/src/OpenTelemetry/Log/LoggerProvider/NoopLoggerProviderFactory.php @@ -5,10 +5,11 @@ use OpenTelemetry\SDK\Logs\LoggerProviderInterface; use OpenTelemetry\SDK\Logs\LogRecordProcessorInterface; use OpenTelemetry\SDK\Logs\NoopLoggerProvider; +use OpenTelemetry\SDK\Resource\ResourceInfo; final class NoopLoggerProviderFactory extends AbstractLoggerProviderFactory { - public function createProvider(?LogRecordProcessorInterface $processor = null): LoggerProviderInterface + public function createProvider(?LogRecordProcessorInterface $processor = null, ?ResourceInfo $resource = null): LoggerProviderInterface { return new NoopLoggerProvider(); } diff --git a/src/OpenTelemetry/Metric/MeterProvider/DefaultMeterProviderFactory.php b/src/OpenTelemetry/Metric/MeterProvider/DefaultMeterProviderFactory.php index ea32cb0..a612b17 100644 --- a/src/OpenTelemetry/Metric/MeterProvider/DefaultMeterProviderFactory.php +++ b/src/OpenTelemetry/Metric/MeterProvider/DefaultMeterProviderFactory.php @@ -7,17 +7,17 @@ use OpenTelemetry\SDK\Metrics\MeterProviderInterface; use OpenTelemetry\SDK\Metrics\MetricExporterInterface; use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader; +use OpenTelemetry\SDK\Resource\ResourceInfo; use OpenTelemetry\SDK\Resource\ResourceInfoFactory; final class DefaultMeterProviderFactory extends AbstractMeterProviderFactory { - public function createProvider(MetricExporterInterface $exporter, ExemplarFilterInterface $filter): MeterProviderInterface + public function createProvider(MetricExporterInterface $exporter, ExemplarFilterInterface $filter, ?ResourceInfo $resource = null): MeterProviderInterface { $reader = new ExportingReader($exporter); - $resource = ResourceInfoFactory::defaultResource(); return MeterProvider::builder() - ->setResource($resource) + ->setResource($resource ?? ResourceInfoFactory::defaultResource()) ->addReader($reader) ->setExemplarFilter($filter) ->build(); diff --git a/src/OpenTelemetry/Metric/MeterProvider/MeterProviderFactoryInterface.php b/src/OpenTelemetry/Metric/MeterProvider/MeterProviderFactoryInterface.php index 7d50ad2..187c9f3 100644 --- a/src/OpenTelemetry/Metric/MeterProvider/MeterProviderFactoryInterface.php +++ b/src/OpenTelemetry/Metric/MeterProvider/MeterProviderFactoryInterface.php @@ -5,8 +5,9 @@ use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilterInterface; use OpenTelemetry\SDK\Metrics\MeterProviderInterface; use OpenTelemetry\SDK\Metrics\MetricExporterInterface; +use OpenTelemetry\SDK\Resource\ResourceInfo; interface MeterProviderFactoryInterface { - public function createProvider(MetricExporterInterface $exporter, ExemplarFilterInterface $filter): MeterProviderInterface; + public function createProvider(MetricExporterInterface $exporter, ExemplarFilterInterface $filter, ?ResourceInfo $resource = null): MeterProviderInterface; } diff --git a/src/OpenTelemetry/Metric/MeterProvider/NoopMeterProviderFactory.php b/src/OpenTelemetry/Metric/MeterProvider/NoopMeterProviderFactory.php index a810a3b..ef28c82 100644 --- a/src/OpenTelemetry/Metric/MeterProvider/NoopMeterProviderFactory.php +++ b/src/OpenTelemetry/Metric/MeterProvider/NoopMeterProviderFactory.php @@ -6,10 +6,11 @@ use OpenTelemetry\SDK\Metrics\MeterProviderInterface; use OpenTelemetry\SDK\Metrics\MetricExporterInterface; use OpenTelemetry\SDK\Metrics\NoopMeterProvider; +use OpenTelemetry\SDK\Resource\ResourceInfo; final class NoopMeterProviderFactory extends AbstractMeterProviderFactory { - public function createProvider(?MetricExporterInterface $exporter = null, ?ExemplarFilterInterface $filter = null): MeterProviderInterface + public function createProvider(?MetricExporterInterface $exporter = null, ?ExemplarFilterInterface $filter = null, ?ResourceInfo $resource = null): MeterProviderInterface { return new NoopMeterProvider(); } diff --git a/src/OpenTelemetry/Resource/ResourceInfoFactory.php b/src/OpenTelemetry/Resource/ResourceInfoFactory.php new file mode 100644 index 0000000..c067a0e --- /dev/null +++ b/src/OpenTelemetry/Resource/ResourceInfoFactory.php @@ -0,0 +1,22 @@ +merge(ResourceInfo::create(Attributes::create([ + ResourceAttributes::SERVICE_NAMESPACE => $namespace, + ResourceAttributes::SERVICE_NAME => $name, + ResourceAttributes::SERVICE_VERSION => $version, + ResourceAttributes::DEPLOYMENT_ENVIRONMENT_NAME => $environment, + ]), ResourceAttributes::SCHEMA_URL)); + } +} diff --git a/src/OpenTelemetry/Trace/TracerProvider/DefaultTracerProviderFactory.php b/src/OpenTelemetry/Trace/TracerProvider/DefaultTracerProviderFactory.php index 9711191..d0ca7d5 100644 --- a/src/OpenTelemetry/Trace/TracerProvider/DefaultTracerProviderFactory.php +++ b/src/OpenTelemetry/Trace/TracerProvider/DefaultTracerProviderFactory.php @@ -2,18 +2,19 @@ namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\OpenTelemetry\Trace\TracerProvider; +use OpenTelemetry\SDK\Resource\ResourceInfo; use OpenTelemetry\SDK\Trace\SamplerInterface; use OpenTelemetry\SDK\Trace\TracerProvider; use OpenTelemetry\SDK\Trace\TracerProviderInterface; final readonly class DefaultTracerProviderFactory extends AbstractTracerProviderFactory { - public function createProvider(?SamplerInterface $sampler = null, array $processors = []): TracerProviderInterface + public function createProvider(?SamplerInterface $sampler = null, array $processors = [], ?ResourceInfo $info = null): TracerProviderInterface { if (0 >= count($processors)) { throw new \InvalidArgumentException('Processors should not be empty'); } - return new TracerProvider($processors, $sampler); + return new TracerProvider($processors, $sampler, $info); } } diff --git a/src/OpenTelemetry/Trace/TracerProvider/NoopTracerProviderFactory.php b/src/OpenTelemetry/Trace/TracerProvider/NoopTracerProviderFactory.php index 23e8d36..47b8f0b 100644 --- a/src/OpenTelemetry/Trace/TracerProvider/NoopTracerProviderFactory.php +++ b/src/OpenTelemetry/Trace/TracerProvider/NoopTracerProviderFactory.php @@ -2,13 +2,14 @@ namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\OpenTelemetry\Trace\TracerProvider; +use OpenTelemetry\SDK\Resource\ResourceInfo; use OpenTelemetry\SDK\Trace\NoopTracerProvider; use OpenTelemetry\SDK\Trace\SamplerInterface; use OpenTelemetry\SDK\Trace\TracerProviderInterface; final readonly class NoopTracerProviderFactory extends AbstractTracerProviderFactory { - public function createProvider(?SamplerInterface $sampler = null, array $processors = []): TracerProviderInterface + public function createProvider(?SamplerInterface $sampler = null, array $processors = [], ?ResourceInfo $info = null): TracerProviderInterface { return new NoopTracerProvider(); } diff --git a/src/OpenTelemetry/Trace/TracerProvider/TracerProviderFactoryInterface.php b/src/OpenTelemetry/Trace/TracerProvider/TracerProviderFactoryInterface.php index bd91cd5..05dd3b8 100644 --- a/src/OpenTelemetry/Trace/TracerProvider/TracerProviderFactoryInterface.php +++ b/src/OpenTelemetry/Trace/TracerProvider/TracerProviderFactoryInterface.php @@ -2,6 +2,7 @@ namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\OpenTelemetry\Trace\TracerProvider; +use OpenTelemetry\SDK\Resource\ResourceInfo; use OpenTelemetry\SDK\Trace\SamplerInterface; use OpenTelemetry\SDK\Trace\SpanProcessorInterface; use OpenTelemetry\SDK\Trace\TracerProviderInterface; @@ -11,5 +12,5 @@ interface TracerProviderFactoryInterface /** * @param SpanProcessorInterface[] $processors */ - public function createProvider(?SamplerInterface $sampler = null, array $processors = []): TracerProviderInterface; + public function createProvider(?SamplerInterface $sampler = null, array $processors = [], ?ResourceInfo $info = null): TracerProviderInterface; } diff --git a/src/Resources/config/services.php b/src/Resources/config/services.php index 99d05c6..1bffd70 100644 --- a/src/Resources/config/services.php +++ b/src/Resources/config/services.php @@ -3,6 +3,7 @@ use FriendsOfOpenTelemetry\OpenTelemetryBundle\OpenTelemetry\Context\Propagator\HeadersPropagator as HeadersPropagationGetter; use FriendsOfOpenTelemetry\OpenTelemetryBundle\OpenTelemetry\Exporter\ExporterDsn; use FriendsOfOpenTelemetry\OpenTelemetryBundle\OpenTelemetry\Exporter\OtlpExporterOptions; +use FriendsOfOpenTelemetry\OpenTelemetryBundle\OpenTelemetry\Resource\ResourceInfoFactory; use FriendsOfOpenTelemetry\OpenTelemetryBundle\OpenTelemetryBundle; use OpenTelemetry\Context\Propagation\ArrayAccessGetterSetter; use OpenTelemetry\Context\Propagation\MultiTextMapPropagator; @@ -10,6 +11,7 @@ use OpenTelemetry\Context\Propagation\SanitizeCombinedHeadersPropagationGetter; use OpenTelemetry\Contrib\Propagation\ServerTiming\ServerTimingPropagator; use OpenTelemetry\Contrib\Propagation\TraceResponse\TraceResponsePropagator; +use OpenTelemetry\SDK\Resource\ResourceInfo; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; return static function (ContainerConfigurator $container): void { @@ -23,6 +25,9 @@ ->defaults() ->private() + ->set('open_telemetry.resource_info', ResourceInfo::class) + ->factory([ResourceInfoFactory::class, 'create']) + ->set('open_telemetry.propagator.server_timing', ServerTimingPropagator::class) ->set('open_telemetry.propagator.trace_response', TraceResponsePropagator::class) diff --git a/tests/Unit/DependencyInjection/OpenTelemetryExtensionTest.php b/tests/Unit/DependencyInjection/OpenTelemetryExtensionTest.php index 9a5e1db..8b788f0 100644 --- a/tests/Unit/DependencyInjection/OpenTelemetryExtensionTest.php +++ b/tests/Unit/DependencyInjection/OpenTelemetryExtensionTest.php @@ -19,6 +19,7 @@ use OpenTelemetry\Context\Propagation\SanitizeCombinedHeadersPropagationGetter; use OpenTelemetry\Contrib\Propagation\ServerTiming\ServerTimingPropagator; use OpenTelemetry\Contrib\Propagation\TraceResponse\TraceResponsePropagator; +use OpenTelemetry\SDK\Resource\ResourceInfo; use PHPUnit\Framework\Attributes\CoversClass; use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -67,6 +68,8 @@ public function testDefaultServices(): void { $this->load(); + self::assertContainerBuilderHasService('open_telemetry.resource_info', ResourceInfo::class); + self::assertContainerBuilderHasService('open_telemetry.propagator.server_timing', ServerTimingPropagator::class); self::assertContainerBuilderHasService('open_telemetry.propagator.trace_response', TraceResponsePropagator::class); diff --git a/tests/Unit/DependencyInjection/OpenTelemetryLogsExtensionTest.php b/tests/Unit/DependencyInjection/OpenTelemetryLogsExtensionTest.php index 9f598d4..699f96a 100644 --- a/tests/Unit/DependencyInjection/OpenTelemetryLogsExtensionTest.php +++ b/tests/Unit/DependencyInjection/OpenTelemetryLogsExtensionTest.php @@ -346,6 +346,11 @@ public function testProviders(string $type, ?string $processor): void 0, null !== $processor ? new Reference($processor) : null, ); + self::assertContainerBuilderHasServiceDefinitionWithArgument( + 'open_telemetry.logs.providers.main', + 1, + new Reference('open_telemetry.resource_info'), + ); $provider = $this->container->getDefinition('open_telemetry.logs.providers.main'); self::assertEquals([new Reference(sprintf('open_telemetry.logs.provider_factory.%s', $type)), 'createProvider'], $provider->getFactory()); } diff --git a/tests/Unit/DependencyInjection/OpenTelemetryMetricsExtensionTest.php b/tests/Unit/DependencyInjection/OpenTelemetryMetricsExtensionTest.php index a9fda7c..1d20c0f 100644 --- a/tests/Unit/DependencyInjection/OpenTelemetryMetricsExtensionTest.php +++ b/tests/Unit/DependencyInjection/OpenTelemetryMetricsExtensionTest.php @@ -251,6 +251,11 @@ public function testProviders(string $type, ?string $exporter, string $filter): 1, (new ChildDefinition('open_telemetry.metrics.exemplar_filter_factory'))->setArguments([$filter]), ); + self::assertContainerBuilderHasServiceDefinitionWithArgument( + 'open_telemetry.metrics.providers.main', + 2, + new Reference('open_telemetry.resource_info'), + ); $provider = $this->container->getDefinition('open_telemetry.metrics.providers.main'); self::assertEquals([new Reference(sprintf('open_telemetry.metrics.provider_factory.%s', $type)), 'createProvider'], $provider->getFactory()); } diff --git a/tests/Unit/DependencyInjection/OpenTelemetryTracesExtensionTest.php b/tests/Unit/DependencyInjection/OpenTelemetryTracesExtensionTest.php index 63974e0..770e14f 100644 --- a/tests/Unit/DependencyInjection/OpenTelemetryTracesExtensionTest.php +++ b/tests/Unit/DependencyInjection/OpenTelemetryTracesExtensionTest.php @@ -357,6 +357,11 @@ public function testProviders(string $type, ?array $sampler, ?array $processors) 1, null !== $processors ? array_map(fn (string $processor) => new Reference($processor), $processors) : [], ); + self::assertContainerBuilderHasServiceDefinitionWithArgument( + 'open_telemetry.traces.providers.main', + 2, + new Reference('open_telemetry.resource_info'), + ); $provider = $this->container->getDefinition('open_telemetry.traces.providers.main'); self::assertEquals([new Reference(sprintf('open_telemetry.traces.provider_factory.%s', $type)), 'createProvider'], $provider->getFactory()); } diff --git a/tests/Unit/OpenTelemetry/Resource/ResourceInfoFactoryTest.php b/tests/Unit/OpenTelemetry/Resource/ResourceInfoFactoryTest.php new file mode 100644 index 0000000..31458e9 --- /dev/null +++ b/tests/Unit/OpenTelemetry/Resource/ResourceInfoFactoryTest.php @@ -0,0 +1,22 @@ +getAttributes(); + self::assertSame('FriendsOfOpenTelemetry/OpenTelemetry', $attributes->get('service.namespace')); + self::assertSame('Test', $attributes->get('service.name')); + self::assertSame('0.0.0', $attributes->get('service.version')); + self::assertSame('test', $attributes->get('deployment.environment.name')); + } +}