From ac161be94609adbc8b80fe8e2758f6f0129fb4b4 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Mon, 17 Oct 2022 10:19:56 +1100 Subject: [PATCH 1/4] initial sdk builder work --- examples/sdk_builder.php | 80 +++++++++++++++++++ src/SDK/Metrics/MeterProvider.php | 9 ++- src/SDK/Metrics/MeterProviderBuilder.php | 78 ++++++++++++++++++ src/SDK/Metrics/NoopMeterProvider.php | 26 ++++++ src/SDK/SDK.php | 19 ----- src/SDK/Sdk.php | 56 +++++++++++++ src/SDK/SdkBuilder.php | 64 +++++++++++++++ .../BatchSpanProcessorBuilder.php | 50 ++++++++++++ src/SDK/Trace/TracerProvider.php | 9 ++- src/SDK/Trace/TracerProviderBuilder.php | 66 +++++++++++++++ src/SDK/Trace/TracerProviderFactory.php | 4 +- tests/Unit/SDK/Resource/Detectors/SdkTest.php | 2 +- tests/Unit/SDK/Resource/ResourceInfoTest.php | 2 +- tests/Unit/SDK/SdkBuilderTest.php | 51 ++++++++++++ tests/Unit/SDK/SdkTest.php | 28 ++++++- 15 files changed, 513 insertions(+), 31 deletions(-) create mode 100644 examples/sdk_builder.php create mode 100644 src/SDK/Metrics/MeterProviderBuilder.php create mode 100644 src/SDK/Metrics/NoopMeterProvider.php delete mode 100644 src/SDK/SDK.php create mode 100644 src/SDK/Sdk.php create mode 100644 src/SDK/SdkBuilder.php create mode 100644 src/SDK/Trace/SpanProcessor/BatchSpanProcessorBuilder.php create mode 100644 src/SDK/Trace/TracerProviderBuilder.php create mode 100644 tests/Unit/SDK/SdkBuilderTest.php diff --git a/examples/sdk_builder.php b/examples/sdk_builder.php new file mode 100644 index 000000000..02a2cac09 --- /dev/null +++ b/examples/sdk_builder.php @@ -0,0 +1,80 @@ +create('http://collector:4318/v1/metrics', 'application/x-protobuf') + ), + ClockFactory::getDefault() +); + +$meterProvider = MeterProvider::builder() + ->setResource($resource) + ->addReader($reader) + ->setAutoShutdown(true) + ->build(); + +$tracerProvider = TracerProvider::builder() + ->addSpanProcessor( + (new BatchSpanProcessorBuilder($spanExporter)) + ->setMeterProvider($meterProvider) + ->build() + ) + ->setResource($resource) + ->setSampler(new ParentBased(new AlwaysOnSampler())) + ->setAutoShutdown(true) + ->build(); + +Sdk::builder() + ->setTracerProvider($tracerProvider) + ->setMeterProvider($meterProvider) + ->setPropagator(TraceContextPropagator::getInstance()) + ->buildAndRegisterGlobal(); + +$instrumentation = new CachedInstrumentation('example'); +$tracer = $instrumentation->tracer(); + +$root = $tracer->spanBuilder('root')->startSpan(); +$scope = $root->activate(); +for ($i=0; $i < 100; $i++) { + if ($i%8 === 0) { + $reader->collect(); + } + $tracer->spanBuilder('span-' . $i) + ->startSpan() + ->end(); + usleep(50000); +} +$scope->detach(); +$root->end(); +$reader->shutdown(); + +echo 'Finished SDK builder example' . PHP_EOL; diff --git a/src/SDK/Metrics/MeterProvider.php b/src/SDK/Metrics/MeterProvider.php index 6352d701c..32810997d 100644 --- a/src/SDK/Metrics/MeterProvider.php +++ b/src/SDK/Metrics/MeterProvider.php @@ -13,7 +13,7 @@ use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilterInterface; use OpenTelemetry\SDK\Metrics\MetricFactory\StreamFactory; use OpenTelemetry\SDK\Resource\ResourceInfo; -use OpenTelemetry\SDK\SDK; +use OpenTelemetry\SDK\Sdk; final class MeterProvider implements MeterProviderInterface { @@ -65,7 +65,7 @@ public function getMeter( ?string $schemaUrl = null, iterable $attributes = [] ): MeterInterface { - if ($this->closed || SDK::isDisabled()) { + if ($this->closed || Sdk::isDisabled()) { return new NoopMeter(); } @@ -117,4 +117,9 @@ public function forceFlush(): bool return $success; } + + public function builder(): MeterProviderBuilder + { + return new MeterProviderBuilder(); + } } diff --git a/src/SDK/Metrics/MeterProviderBuilder.php b/src/SDK/Metrics/MeterProviderBuilder.php new file mode 100644 index 000000000..df2015ff5 --- /dev/null +++ b/src/SDK/Metrics/MeterProviderBuilder.php @@ -0,0 +1,78 @@ + + private array $metricReaders = []; + private ?ResourceInfo $resource = null; + private bool $autoShutdown = false; + + public function registerMetricReader(MetricReaderInterface $reader): self + { + $this->metricReaders[] = $reader; + + return $this; + } + + public function setResource(ResourceInfo $resource): self + { + $this->resource = $resource; + + return $this; + } + + public function addReader(MetricReaderInterface $reader): self + { + $this->metricReaders[] = $reader; + + return $this; + } + + /** + * Automatically shut down the tracer provider on process completion. If not set, the user is responsible for calling `shutdown`. + */ + public function setAutoShutdown(bool $shutdown): self + { + $this->autoShutdown = $shutdown; + + return $this; + } + + /** + * @psalm-suppress PossiblyInvalidArgument + */ + public function build(): MeterProviderInterface + { + $meterProvider = new MeterProvider( + null, + $this->resource ?? ResourceInfoFactory::emptyResource(), + ClockFactory::getDefault(), + Attributes::factory(), + new InstrumentationScopeFactory(Attributes::factory()), + $this->metricReaders, + new CriteriaViewRegistry(), + new WithSampledTraceExemplarFilter(), + new ImmediateStalenessHandlerFactory(), + ); + if ($this->autoShutdown) { + ShutdownHandler::register(fn (?CancellationInterface $cancellation = null): bool => $meterProvider->shutdown()); + } + + return $meterProvider; + } +} diff --git a/src/SDK/Metrics/NoopMeterProvider.php b/src/SDK/Metrics/NoopMeterProvider.php new file mode 100644 index 000000000..2efb484d3 --- /dev/null +++ b/src/SDK/Metrics/NoopMeterProvider.php @@ -0,0 +1,26 @@ +tracerProvider = $tracerProvider; + $this->meterProvider = $meterProvider; + $this->propagator = $propagator; + } + + public static function isDisabled(): bool + { + return Accessor::getBool(Variables::OTEL_SDK_DISABLED); + } + + public static function builder(): SdkBuilder + { + return new SdkBuilder(); + } + + public function getTracerProvider(): TracerProviderInterface + { + return $this->tracerProvider; + } + + public function getMeterProvider(): MeterProviderInterface + { + return $this->meterProvider; + } + + public function getPropagator(): TextMapPropagatorInterface + { + return $this->propagator; + } +} diff --git a/src/SDK/SdkBuilder.php b/src/SDK/SdkBuilder.php new file mode 100644 index 000000000..c9b452504 --- /dev/null +++ b/src/SDK/SdkBuilder.php @@ -0,0 +1,64 @@ +tracerProvider = $provider; + + return $this; + } + + public function setMeterProvider(MeterProviderInterface $meterProvider): self + { + $this->meterProvider = $meterProvider; + + return $this; + } + + public function setPropagator(TextMapPropagatorInterface $propagator): self + { + $this->propagator = $propagator; + + return $this; + } + + public function build(): Sdk + { + return new Sdk( + $this->tracerProvider ?? new NoopTracerProvider(), + $this->meterProvider ?? new NoopMeterProvider(), + $this->propagator ?? NoopTextMapPropagator::getInstance(), + ); + } + + public function buildAndRegisterGlobal(): ScopeInterface + { + $sdk = $this->build(); + $context = Configurator::create() + ->withPropagator($sdk->getPropagator()) + ->withTracerProvider($sdk->getTracerProvider()) + ->withMeterProvider($sdk->getMeterProvider()) + ->storeInContext(); + + return Context::storage()->attach($context); + } +} diff --git a/src/SDK/Trace/SpanProcessor/BatchSpanProcessorBuilder.php b/src/SDK/Trace/SpanProcessor/BatchSpanProcessorBuilder.php new file mode 100644 index 000000000..8c10483c0 --- /dev/null +++ b/src/SDK/Trace/SpanProcessor/BatchSpanProcessorBuilder.php @@ -0,0 +1,50 @@ +exporter = $exporter; + } + + public function setMeterProvider(MeterProviderInterface $meterProvider): self + { + $this->meterProvider = $meterProvider; + + return $this; + } + + public function setClock(ClockInterface $clock): self + { + $this->clock = $clock; + + return $this; + } + + public function build(): BatchSpanProcessor + { + return new BatchSpanProcessor( + $this->exporter, + $this->clock ?? ClockFactory::getDefault(), + BatchSpanProcessor::DEFAULT_MAX_QUEUE_SIZE, + BatchSpanProcessor::DEFAULT_SCHEDULE_DELAY, + BatchSpanProcessor::DEFAULT_EXPORT_TIMEOUT, + BatchSpanProcessor::DEFAULT_MAX_EXPORT_BATCH_SIZE, + true, + $this->meterProvider + ); + } +} diff --git a/src/SDK/Trace/TracerProvider.php b/src/SDK/Trace/TracerProvider.php index eeb3cfdb2..f60169327 100644 --- a/src/SDK/Trace/TracerProvider.php +++ b/src/SDK/Trace/TracerProvider.php @@ -13,7 +13,7 @@ use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactoryInterface; use OpenTelemetry\SDK\Resource\ResourceInfo; use OpenTelemetry\SDK\Resource\ResourceInfoFactory; -use OpenTelemetry\SDK\SDK; +use OpenTelemetry\SDK\Sdk; use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler; use OpenTelemetry\SDK\Trace\Sampler\ParentBased; @@ -66,7 +66,7 @@ public function getTracer( ?string $schemaUrl = null, iterable $attributes = [] ): API\TracerInterface { - if ($this->tracerSharedState->hasShutdown() || SDK::isDisabled()) { + if ($this->tracerSharedState->hasShutdown() || Sdk::isDisabled()) { return NoopTracer::getInstance(); } @@ -92,4 +92,9 @@ public function shutdown(?CancellationInterface $cancellation = null): bool return $this->tracerSharedState->shutdown($cancellation); } + + public static function builder(): TracerProviderBuilder + { + return new TracerProviderBuilder(); + } } diff --git a/src/SDK/Trace/TracerProviderBuilder.php b/src/SDK/Trace/TracerProviderBuilder.php new file mode 100644 index 000000000..f2bf7d05f --- /dev/null +++ b/src/SDK/Trace/TracerProviderBuilder.php @@ -0,0 +1,66 @@ + + private ?array $spanProcessors = []; + private ?ResourceInfo $resource = null; + private ?SamplerInterface $sampler = null; + private bool $autoShutdown = false; + + public function addSpanProcessor(SpanProcessorInterface $spanProcessor): self + { + $this->spanProcessors[] = $spanProcessor; + + return $this; + } + + public function setResource(ResourceInfo $resource): self + { + $this->resource = $resource; + + return $this; + } + + /** + * Automatically shut down the tracer provider on process completion. If not set, the user is responsible for calling `shutdown`. + */ + public function setAutoShutdown(bool $shutdown): self + { + $this->autoShutdown = $shutdown; + + return $this; + } + + public function setSampler(SamplerInterface $sampler): self + { + $this->sampler = $sampler; + + return $this; + } + + public function build(): TracerProviderInterface + { + $tracerProvider = new TracerProvider( + $this->spanProcessors, + $this->sampler ?? new ParentBased(new AlwaysOnSampler()), + $this->resource ?? ResourceInfoFactory::defaultResource(), + ); + if ($this->autoShutdown) { + ShutdownHandler::register(fn (?CancellationInterface $cancellation = null): bool => $tracerProvider->shutdown($cancellation)); + } + + return $tracerProvider; + } +} diff --git a/src/SDK/Trace/TracerProviderFactory.php b/src/SDK/Trace/TracerProviderFactory.php index 55be3dde2..49e7c81cd 100644 --- a/src/SDK/Trace/TracerProviderFactory.php +++ b/src/SDK/Trace/TracerProviderFactory.php @@ -5,7 +5,7 @@ namespace OpenTelemetry\SDK\Trace; use OpenTelemetry\SDK\Behavior\LogsMessagesTrait; -use OpenTelemetry\SDK\SDK; +use OpenTelemetry\SDK\Sdk; final class TracerProviderFactory { @@ -28,7 +28,7 @@ public function __construct( public function create(): TracerProviderInterface { - if (SDK::isDisabled()) { + if (Sdk::isDisabled()) { return new NoopTracerProvider(); } diff --git a/tests/Unit/SDK/Resource/Detectors/SdkTest.php b/tests/Unit/SDK/Resource/Detectors/SdkTest.php index 0cb9f6f96..6f09ce705 100644 --- a/tests/Unit/SDK/Resource/Detectors/SdkTest.php +++ b/tests/Unit/SDK/Resource/Detectors/SdkTest.php @@ -18,7 +18,7 @@ public function test_sdk_get_resource(): void { $resouceDetector = new Detectors\Sdk(); $resource = $resouceDetector->getResource(); - $version = InstalledVersions::getVersion('open-telemetry/opentelemetry'); + $version = InstalledVersions::getPrettyVersion('open-telemetry/opentelemetry'); $this->assertSame(ResourceAttributes::SCHEMA_URL, $resource->getSchemaUrl()); $this->assertSame('opentelemetry', $resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_NAME)); diff --git a/tests/Unit/SDK/Resource/ResourceInfoTest.php b/tests/Unit/SDK/Resource/ResourceInfoTest.php index a1dd98b4f..881e1ff02 100644 --- a/tests/Unit/SDK/Resource/ResourceInfoTest.php +++ b/tests/Unit/SDK/Resource/ResourceInfoTest.php @@ -35,7 +35,7 @@ public function test_get_attributes(): void new Detectors\SdkProvided(), ]))->getResource(); - $version = InstalledVersions::getVersion('open-telemetry/opentelemetry'); + $version = InstalledVersions::getPrettyVersion('open-telemetry/opentelemetry'); $name = $resource->getAttributes()->get('name'); $sdkname = $resource->getAttributes()->get(ResourceAttributes::TELEMETRY_SDK_NAME); diff --git a/tests/Unit/SDK/SdkBuilderTest.php b/tests/Unit/SDK/SdkBuilderTest.php new file mode 100644 index 000000000..58d9960a7 --- /dev/null +++ b/tests/Unit/SDK/SdkBuilderTest.php @@ -0,0 +1,51 @@ +propagator = $this->createMock(TextMapPropagatorInterface::class); + $this->tracerProvider = $this->createMock(TracerProviderInterface::class); + $this->meterProvider = $this->createMock(MeterProviderInterface::class); + $this->builder = (new SdkBuilder()) + ->setMeterProvider($this->meterProvider) + ->setPropagator($this->propagator) + ->setTracerProvider($this->tracerProvider); + } + + public function test_build(): void + { + $sdk = $this->builder->build(); + $this->assertSame($this->meterProvider, $sdk->getMeterProvider()); + $this->assertSame($this->propagator, $sdk->getPropagator()); + $this->assertSame($this->tracerProvider, $sdk->getTracerProvider()); + } + + public function test_build_and_register_global(): void + { + $scope = $this->builder->buildAndRegisterGlobal(); + $this->assertSame($this->meterProvider, Globals::meterProvider()); + $this->assertSame($this->propagator, Globals::propagator()); + $this->assertSame($this->tracerProvider, Globals::tracerProvider()); + $scope->detach(); + } +} diff --git a/tests/Unit/SDK/SdkTest.php b/tests/Unit/SDK/SdkTest.php index 23c2c9081..69047f31c 100644 --- a/tests/Unit/SDK/SdkTest.php +++ b/tests/Unit/SDK/SdkTest.php @@ -5,11 +5,15 @@ namespace OpenTelemetry\Tests\Unit\SDK; use AssertWell\PHPUnitGlobalState\EnvironmentVariables; -use OpenTelemetry\SDK\SDK; +use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface; +use OpenTelemetry\SDK\Metrics\MeterProviderInterface; +use OpenTelemetry\SDK\Sdk; +use OpenTelemetry\SDK\SdkBuilder; +use OpenTelemetry\SDK\Trace\TracerProviderInterface; use PHPUnit\Framework\TestCase; /** - * @covers \OpenTelemetry\SDK\SDK + * @covers \OpenTelemetry\SDK\Sdk */ class SdkTest extends TestCase { @@ -22,7 +26,7 @@ public function tearDown(): void public function test_is_not_disabled_by_default(): void { - $this->assertFalse(SDK::isDisabled()); + $this->assertFalse(Sdk::isDisabled()); } /** @@ -31,7 +35,7 @@ public function test_is_not_disabled_by_default(): void public function test_is_disabled(string $value, bool $expected): void { self::setEnvironmentVariable('OTEL_SDK_DISABLED', $value); - $this->assertSame($expected, SDK::isDisabled()); + $this->assertSame($expected, Sdk::isDisabled()); } public function disabledProvider(): array { @@ -42,4 +46,20 @@ public function disabledProvider(): array ['0', false], ]; } + + public function test_builder(): void + { + $this->assertInstanceOf(SdkBuilder::class, Sdk::builder()); + } + + public function test_getters(): void + { + $propagator = $this->createMock(TextMapPropagatorInterface::class); + $meterProvider = $this->createMock(MeterProviderInterface::class); + $tracerProvider = $this->createMock(TracerProviderInterface::class); + $sdk = new Sdk($tracerProvider, $meterProvider, $propagator); + $this->assertSame($propagator, $sdk->getPropagator()); + $this->assertSame($meterProvider, $sdk->getMeterProvider()); + $this->assertSame($tracerProvider, $sdk->getTracerProvider()); + } } From a0ed88a00f571abc78d3e09cb320bd618b766037 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Tue, 25 Oct 2022 14:34:02 +1100 Subject: [PATCH 2/4] fixing static function --- src/SDK/Metrics/MeterProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SDK/Metrics/MeterProvider.php b/src/SDK/Metrics/MeterProvider.php index 32810997d..9ef6c848c 100644 --- a/src/SDK/Metrics/MeterProvider.php +++ b/src/SDK/Metrics/MeterProvider.php @@ -118,7 +118,7 @@ public function forceFlush(): bool return $success; } - public function builder(): MeterProviderBuilder + public static function builder(): MeterProviderBuilder { return new MeterProviderBuilder(); } From 0f5c00955f80e083e321eba2a56fa08bba24296a Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Wed, 26 Oct 2022 20:15:19 +1100 Subject: [PATCH 3/4] review feedback --- examples/sdk_builder.php | 3 +-- src/SDK/Metrics/MeterProvider.php | 2 +- src/SDK/Metrics/MeterProviderBuilder.php | 20 +------------- src/SDK/SdkBuilder.php | 25 ++++++++++++++--- .../BatchSpanProcessorBuilder.php | 11 +------- src/SDK/Trace/TracerProvider.php | 3 +-- src/SDK/Trace/TracerProviderBuilder.php | 27 +++---------------- tests/Integration/SDK/TracerTest.php | 5 ++-- 8 files changed, 32 insertions(+), 64 deletions(-) diff --git a/examples/sdk_builder.php b/examples/sdk_builder.php index 02a2cac09..037c39cad 100644 --- a/examples/sdk_builder.php +++ b/examples/sdk_builder.php @@ -39,7 +39,6 @@ $meterProvider = MeterProvider::builder() ->setResource($resource) ->addReader($reader) - ->setAutoShutdown(true) ->build(); $tracerProvider = TracerProvider::builder() @@ -50,13 +49,13 @@ ) ->setResource($resource) ->setSampler(new ParentBased(new AlwaysOnSampler())) - ->setAutoShutdown(true) ->build(); Sdk::builder() ->setTracerProvider($tracerProvider) ->setMeterProvider($meterProvider) ->setPropagator(TraceContextPropagator::getInstance()) + ->setAutoShutdown(true) ->buildAndRegisterGlobal(); $instrumentation = new CachedInstrumentation('example'); diff --git a/src/SDK/Metrics/MeterProvider.php b/src/SDK/Metrics/MeterProvider.php index 9ef6c848c..572069ec2 100644 --- a/src/SDK/Metrics/MeterProvider.php +++ b/src/SDK/Metrics/MeterProvider.php @@ -65,7 +65,7 @@ public function getMeter( ?string $schemaUrl = null, iterable $attributes = [] ): MeterInterface { - if ($this->closed || Sdk::isDisabled()) { + if ($this->closed || Sdk::isDisabled()) { //@todo create meter provider from factory, and move Sdk::isDisabled() there return new NoopMeter(); } diff --git a/src/SDK/Metrics/MeterProviderBuilder.php b/src/SDK/Metrics/MeterProviderBuilder.php index df2015ff5..e05750b56 100644 --- a/src/SDK/Metrics/MeterProviderBuilder.php +++ b/src/SDK/Metrics/MeterProviderBuilder.php @@ -5,10 +5,8 @@ namespace OpenTelemetry\SDK\Metrics; use OpenTelemetry\SDK\Common\Attribute\Attributes; -use OpenTelemetry\SDK\Common\Future\CancellationInterface; use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory; use OpenTelemetry\SDK\Common\Time\ClockFactory; -use OpenTelemetry\SDK\Common\Util\ShutdownHandler; use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilter\WithSampledTraceExemplarFilter; use OpenTelemetry\SDK\Metrics\StalenessHandler\ImmediateStalenessHandlerFactory; use OpenTelemetry\SDK\Metrics\View\CriteriaViewRegistry; @@ -20,7 +18,6 @@ class MeterProviderBuilder // @var array private array $metricReaders = []; private ?ResourceInfo $resource = null; - private bool $autoShutdown = false; public function registerMetricReader(MetricReaderInterface $reader): self { @@ -43,22 +40,12 @@ public function addReader(MetricReaderInterface $reader): self return $this; } - /** - * Automatically shut down the tracer provider on process completion. If not set, the user is responsible for calling `shutdown`. - */ - public function setAutoShutdown(bool $shutdown): self - { - $this->autoShutdown = $shutdown; - - return $this; - } - /** * @psalm-suppress PossiblyInvalidArgument */ public function build(): MeterProviderInterface { - $meterProvider = new MeterProvider( + return new MeterProvider( null, $this->resource ?? ResourceInfoFactory::emptyResource(), ClockFactory::getDefault(), @@ -69,10 +56,5 @@ public function build(): MeterProviderInterface new WithSampledTraceExemplarFilter(), new ImmediateStalenessHandlerFactory(), ); - if ($this->autoShutdown) { - ShutdownHandler::register(fn (?CancellationInterface $cancellation = null): bool => $meterProvider->shutdown()); - } - - return $meterProvider; } } diff --git a/src/SDK/SdkBuilder.php b/src/SDK/SdkBuilder.php index c9b452504..b944aa103 100644 --- a/src/SDK/SdkBuilder.php +++ b/src/SDK/SdkBuilder.php @@ -5,20 +5,32 @@ namespace OpenTelemetry\SDK; use OpenTelemetry\API\Common\Instrumentation\Configurator; -use OpenTelemetry\API\Metrics\MeterProviderInterface; -use OpenTelemetry\API\Metrics\Noop\NoopMeterProvider; -use OpenTelemetry\API\Trace\NoopTracerProvider; -use OpenTelemetry\API\Trace\TracerProviderInterface; use OpenTelemetry\Context\Context; use OpenTelemetry\Context\Propagation\NoopTextMapPropagator; use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface; use OpenTelemetry\Context\ScopeInterface; +use OpenTelemetry\SDK\Common\Util\ShutdownHandler; +use OpenTelemetry\SDK\Metrics\MeterProviderInterface; +use OpenTelemetry\SDK\Metrics\NoopMeterProvider; +use OpenTelemetry\SDK\Trace\NoopTracerProvider; +use OpenTelemetry\SDK\Trace\TracerProviderInterface; class SdkBuilder { private ?TracerProviderInterface $tracerProvider = null; private ?MeterProviderInterface $meterProvider = null; private ?TextMapPropagatorInterface $propagator = null; + private bool $autoShutdown = false; + + /** + * Automatically shut down providers on process completion. If not set, the user is responsible for calling `shutdown`. + */ + public function setAutoShutdown(bool $shutdown): self + { + $this->autoShutdown = $shutdown; + + return $this; + } public function setTracerProvider(TracerProviderInterface $provider): self { @@ -43,6 +55,11 @@ public function setPropagator(TextMapPropagatorInterface $propagator): self public function build(): Sdk { + if ($this->autoShutdown) { + $this->tracerProvider && ShutdownHandler::register([$this->tracerProvider, 'shutdown']); + $this->meterProvider && ShutdownHandler::register([$this->meterProvider, 'shutdown']); + } + return new Sdk( $this->tracerProvider ?? new NoopTracerProvider(), $this->meterProvider ?? new NoopMeterProvider(), diff --git a/src/SDK/Trace/SpanProcessor/BatchSpanProcessorBuilder.php b/src/SDK/Trace/SpanProcessor/BatchSpanProcessorBuilder.php index 8c10483c0..8e81e7dd6 100644 --- a/src/SDK/Trace/SpanProcessor/BatchSpanProcessorBuilder.php +++ b/src/SDK/Trace/SpanProcessor/BatchSpanProcessorBuilder.php @@ -5,7 +5,6 @@ namespace OpenTelemetry\SDK\Trace\SpanProcessor; use OpenTelemetry\SDK\Common\Time\ClockFactory; -use OpenTelemetry\SDK\Common\Time\ClockInterface; use OpenTelemetry\SDK\Metrics\MeterProviderInterface; use OpenTelemetry\SDK\Trace\SpanExporterInterface; @@ -13,7 +12,6 @@ class BatchSpanProcessorBuilder { private SpanExporterInterface $exporter; private ?MeterProviderInterface $meterProvider = null; - private ?ClockInterface $clock = null; public function __construct(SpanExporterInterface $exporter) { @@ -27,18 +25,11 @@ public function setMeterProvider(MeterProviderInterface $meterProvider): self return $this; } - public function setClock(ClockInterface $clock): self - { - $this->clock = $clock; - - return $this; - } - public function build(): BatchSpanProcessor { return new BatchSpanProcessor( $this->exporter, - $this->clock ?? ClockFactory::getDefault(), + ClockFactory::getDefault(), BatchSpanProcessor::DEFAULT_MAX_QUEUE_SIZE, BatchSpanProcessor::DEFAULT_SCHEDULE_DELAY, BatchSpanProcessor::DEFAULT_EXPORT_TIMEOUT, diff --git a/src/SDK/Trace/TracerProvider.php b/src/SDK/Trace/TracerProvider.php index f60169327..fdae4aea2 100644 --- a/src/SDK/Trace/TracerProvider.php +++ b/src/SDK/Trace/TracerProvider.php @@ -13,7 +13,6 @@ use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactoryInterface; use OpenTelemetry\SDK\Resource\ResourceInfo; use OpenTelemetry\SDK\Resource\ResourceInfoFactory; -use OpenTelemetry\SDK\Sdk; use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler; use OpenTelemetry\SDK\Trace\Sampler\ParentBased; @@ -66,7 +65,7 @@ public function getTracer( ?string $schemaUrl = null, iterable $attributes = [] ): API\TracerInterface { - if ($this->tracerSharedState->hasShutdown() || Sdk::isDisabled()) { + if ($this->tracerSharedState->hasShutdown()) { return NoopTracer::getInstance(); } diff --git a/src/SDK/Trace/TracerProviderBuilder.php b/src/SDK/Trace/TracerProviderBuilder.php index f2bf7d05f..8dcfdc700 100644 --- a/src/SDK/Trace/TracerProviderBuilder.php +++ b/src/SDK/Trace/TracerProviderBuilder.php @@ -4,12 +4,7 @@ namespace OpenTelemetry\SDK\Trace; -use OpenTelemetry\SDK\Common\Future\CancellationInterface; -use OpenTelemetry\SDK\Common\Util\ShutdownHandler; use OpenTelemetry\SDK\Resource\ResourceInfo; -use OpenTelemetry\SDK\Resource\ResourceInfoFactory; -use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler; -use OpenTelemetry\SDK\Trace\Sampler\ParentBased; class TracerProviderBuilder { @@ -17,7 +12,6 @@ class TracerProviderBuilder private ?array $spanProcessors = []; private ?ResourceInfo $resource = null; private ?SamplerInterface $sampler = null; - private bool $autoShutdown = false; public function addSpanProcessor(SpanProcessorInterface $spanProcessor): self { @@ -33,16 +27,6 @@ public function setResource(ResourceInfo $resource): self return $this; } - /** - * Automatically shut down the tracer provider on process completion. If not set, the user is responsible for calling `shutdown`. - */ - public function setAutoShutdown(bool $shutdown): self - { - $this->autoShutdown = $shutdown; - - return $this; - } - public function setSampler(SamplerInterface $sampler): self { $this->sampler = $sampler; @@ -52,15 +36,10 @@ public function setSampler(SamplerInterface $sampler): self public function build(): TracerProviderInterface { - $tracerProvider = new TracerProvider( + return new TracerProvider( $this->spanProcessors, - $this->sampler ?? new ParentBased(new AlwaysOnSampler()), - $this->resource ?? ResourceInfoFactory::defaultResource(), + $this->sampler, + $this->resource, ); - if ($this->autoShutdown) { - ShutdownHandler::register(fn (?CancellationInterface $cancellation = null): bool => $tracerProvider->shutdown($cancellation)); - } - - return $tracerProvider; } } diff --git a/tests/Integration/SDK/TracerTest.php b/tests/Integration/SDK/TracerTest.php index 39b1e2daa..606fcd408 100644 --- a/tests/Integration/SDK/TracerTest.php +++ b/tests/Integration/SDK/TracerTest.php @@ -20,6 +20,7 @@ use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor; use OpenTelemetry\SDK\Trace\SpanProcessorInterface; use OpenTelemetry\SDK\Trace\TracerProvider; +use OpenTelemetry\SDK\Trace\TracerProviderFactory; use OpenTelemetry\SemConv\TraceAttributes; use PHPUnit\Framework\TestCase; @@ -107,10 +108,10 @@ public function test_returns_noop_span_builder_after_shutdown(): void $this->assertInstanceOf(API\NoopSpanBuilder::class, $tracer->spanBuilder('baz')); } - public function test_returns_noop_tracer_when_sdk_disabled(): void + public function test_factory_returns_noop_tracer_when_sdk_disabled(): void { self::setEnvironmentVariable('OTEL_SDK_DISABLED', 'true'); - $tracerProvider = new TracerProvider(); + $tracerProvider = (new TracerProviderFactory('test'))->create(); $tracer = $tracerProvider->getTracer('foo'); $this->assertInstanceOf(API\NoopTracer::class, $tracer); } From 7293011ff1c0805e4df335f257431fea36227152 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Fri, 28 Oct 2022 14:26:43 +1100 Subject: [PATCH 4/4] fixing build failures --- src/SDK/Sdk.php | 2 -- src/SDK/SdkBuilder.php | 11 +++++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/SDK/Sdk.php b/src/SDK/Sdk.php index ce4c0750c..6d863582e 100644 --- a/src/SDK/Sdk.php +++ b/src/SDK/Sdk.php @@ -7,9 +7,7 @@ use OpenTelemetry\API\Metrics\MeterProviderInterface; use OpenTelemetry\API\Trace\TracerProviderInterface; use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface; -use OpenTelemetry\SDK\Common\Environment\Accessor; use OpenTelemetry\SDK\Common\Environment\EnvironmentVariables; -use OpenTelemetry\SDK\Common\Environment\EnvironmentVariablesTrait; use OpenTelemetry\SDK\Common\Environment\Variables; class Sdk diff --git a/src/SDK/SdkBuilder.php b/src/SDK/SdkBuilder.php index b944aa103..301a3e158 100644 --- a/src/SDK/SdkBuilder.php +++ b/src/SDK/SdkBuilder.php @@ -9,6 +9,7 @@ use OpenTelemetry\Context\Propagation\NoopTextMapPropagator; use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface; use OpenTelemetry\Context\ScopeInterface; +use OpenTelemetry\SDK\Common\Future\CancellationInterface; use OpenTelemetry\SDK\Common\Util\ShutdownHandler; use OpenTelemetry\SDK\Metrics\MeterProviderInterface; use OpenTelemetry\SDK\Metrics\NoopMeterProvider; @@ -55,14 +56,16 @@ public function setPropagator(TextMapPropagatorInterface $propagator): self public function build(): Sdk { + $tracerProvider = $this->tracerProvider ?? new NoopTracerProvider(); + $meterProvider = $this->meterProvider ?? new NoopMeterProvider(); if ($this->autoShutdown) { - $this->tracerProvider && ShutdownHandler::register([$this->tracerProvider, 'shutdown']); - $this->meterProvider && ShutdownHandler::register([$this->meterProvider, 'shutdown']); + ShutdownHandler::register(fn (?CancellationInterface $cancellation = null): bool => $tracerProvider->shutdown($cancellation)); + ShutdownHandler::register(fn (): bool => $meterProvider->shutdown()); } return new Sdk( - $this->tracerProvider ?? new NoopTracerProvider(), - $this->meterProvider ?? new NoopMeterProvider(), + $tracerProvider, + $meterProvider, $this->propagator ?? NoopTextMapPropagator::getInstance(), ); }