Skip to content

Commit

Permalink
fix(DependencyInjection): pass configured resource info to providers (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
gaelreyrol authored Dec 22, 2024
1 parent 1fb891a commit b50c27a
Show file tree
Hide file tree
Showing 20 changed files with 99 additions and 13 deletions.
8 changes: 8 additions & 0 deletions src/DependencyInjection/OpenTelemetryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
]);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/OpenTelemetryLogsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
]);
}

Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/OpenTelemetryMetricsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
]);
}

Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/OpenTelemetryTracesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
22 changes: 22 additions & 0 deletions src/OpenTelemetry/Resource/ResourceInfoFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\OpenTelemetry\Resource;

use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SemConv\ResourceAttributes;

final class ResourceInfoFactory
{
public static function create(string $namespace, string $name, string $version, string $environment): ResourceInfo
{
$resourceInfo = \OpenTelemetry\SDK\Resource\ResourceInfoFactory::defaultResource();

return $resourceInfo->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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
5 changes: 5 additions & 0 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
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;
use OpenTelemetry\Context\Propagation\NoopTextMapPropagator;
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 {
Expand All @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions tests/Unit/DependencyInjection/OpenTelemetryExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
22 changes: 22 additions & 0 deletions tests/Unit/OpenTelemetry/Resource/ResourceInfoFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Tests\Unit\OpenTelemetry\Resource;

use FriendsOfOpenTelemetry\OpenTelemetryBundle\OpenTelemetry\Resource\ResourceInfoFactory;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(ResourceInfoFactory::class)]
class ResourceInfoFactoryTest extends TestCase
{
public function testCreateResourceInfo(): void
{
$resource = ResourceInfoFactory::create('FriendsOfOpenTelemetry/OpenTelemetry', 'Test', '0.0.0', 'test');

$attributes = $resource->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'));
}
}

0 comments on commit b50c27a

Please sign in to comment.