diff --git a/composer.json b/composer.json index cca5f6935..4f7709898 100644 --- a/composer.json +++ b/composer.json @@ -67,7 +67,9 @@ "src/Contrib/Grpc/_register.php", "src/Contrib/Newrelic/_register.php", "src/Contrib/Zipkin/_register.php", + "src/Extension/Propagator/B3/_register.php", "src/SDK/Metrics/MetricExporter/_register.php", + "src/SDK/Propagation/_register.php", "src/SDK/Trace/SpanExporter/_register.php", "src/SDK/Common/Dev/Compatibility/_load.php", "src/SDK/Common/Util/functions.php", diff --git a/examples/autoload_sdk.php b/examples/autoload_sdk.php index 74ccbb2a1..e2a896785 100644 --- a/examples/autoload_sdk.php +++ b/examples/autoload_sdk.php @@ -9,6 +9,7 @@ putenv('OTEL_EXPORTER_OTLP_METRICS_PROTOCOL=grpc'); putenv('OTEL_EXPORTER_OTLP_ENDPOINT=http://collector:4317'); putenv('OTEL_PHP_TRACES_PROCESSOR=batch'); +putenv('OTEL_PROPAGATORS=b3,baggage,tracecontext'); echo 'autoloading SDK example starting...' . PHP_EOL; diff --git a/examples/autoload_sdk_with_custom_transport.php b/examples/autoload_sdk_with_custom_transport.php index 3852b90a4..133722066 100644 --- a/examples/autoload_sdk_with_custom_transport.php +++ b/examples/autoload_sdk_with_custom_transport.php @@ -40,7 +40,7 @@ public function forceFlush(?\OpenTelemetry\SDK\Common\Future\CancellationInterfa } }; -\OpenTelemetry\SDK\FactoryRegistry::registerTransportFactory('grpc', $factory, true); +\OpenTelemetry\SDK\Registry::registerTransportFactory('grpc', $factory, true); $instrumentation = new \OpenTelemetry\API\Common\Instrumentation\CachedInstrumentation('demo'); diff --git a/src/Contrib/Grpc/_register.php b/src/Contrib/Grpc/_register.php index 2ba6b3f99..94883eebf 100644 --- a/src/Contrib/Grpc/_register.php +++ b/src/Contrib/Grpc/_register.php @@ -1,4 +1,4 @@ create( diff --git a/src/Contrib/Otlp/SpanExporterFactory.php b/src/Contrib/Otlp/SpanExporterFactory.php index 6b4a25a94..3902dba73 100644 --- a/src/Contrib/Otlp/SpanExporterFactory.php +++ b/src/Contrib/Otlp/SpanExporterFactory.php @@ -12,7 +12,7 @@ use OpenTelemetry\SDK\Common\Export\TransportFactoryInterface; use OpenTelemetry\SDK\Common\Export\TransportInterface; use OpenTelemetry\SDK\Common\Otlp\HttpEndpointResolver; -use OpenTelemetry\SDK\FactoryRegistry; +use OpenTelemetry\SDK\Registry; use OpenTelemetry\SDK\Trace\SpanExporter\SpanExporterFactoryInterface; use OpenTelemetry\SDK\Trace\SpanExporterInterface; @@ -51,7 +51,7 @@ private function buildTransport(): TransportInterface $headers = $this->getHeaders(); $compression = $this->getCompression(); - $factoryClass = FactoryRegistry::transportFactory($protocol); + $factoryClass = Registry::transportFactory($protocol); $factory = $this->transportFactory ?: new $factoryClass(); return $factory->create($endpoint, $contentType, $headers, $compression); diff --git a/src/Contrib/Otlp/_register.php b/src/Contrib/Otlp/_register.php index 10c011139..657c7ce67 100644 --- a/src/Contrib/Otlp/_register.php +++ b/src/Contrib/Otlp/_register.php @@ -1,7 +1,7 @@ create(); } catch (\Throwable $t) { self::logWarning(sprintf('Unable to create %s meter provider: %s', $exporterName, $t->getMessage())); diff --git a/src/SDK/Metrics/MetricExporter/_register.php b/src/SDK/Metrics/MetricExporter/_register.php index d23f95892..04c2143f6 100644 --- a/src/SDK/Metrics/MetricExporter/_register.php +++ b/src/SDK/Metrics/MetricExporter/_register.php @@ -2,5 +2,5 @@ declare(strict_types=1); -\OpenTelemetry\SDK\FactoryRegistry::registerMetricExporterFactory('memory', \OpenTelemetry\SDK\Metrics\MetricExporter\InMemoryExporterFactory::class); -\OpenTelemetry\SDK\FactoryRegistry::registerMetricExporterFactory('none', \OpenTelemetry\SDK\Metrics\MetricExporter\NoopMetricExporterFactory::class); +\OpenTelemetry\SDK\Registry::registerMetricExporterFactory('memory', \OpenTelemetry\SDK\Metrics\MetricExporter\InMemoryExporterFactory::class); +\OpenTelemetry\SDK\Registry::registerMetricExporterFactory('none', \OpenTelemetry\SDK\Metrics\MetricExporter\NoopMetricExporterFactory::class); diff --git a/src/SDK/Propagation/PropagatorFactory.php b/src/SDK/Propagation/PropagatorFactory.php index 8415f2e04..7b251003a 100644 --- a/src/SDK/Propagation/PropagatorFactory.php +++ b/src/SDK/Propagation/PropagatorFactory.php @@ -9,20 +9,13 @@ use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface; use OpenTelemetry\SDK\Behavior\LogsMessagesTrait; use OpenTelemetry\SDK\Common\Configuration\Configuration; -use OpenTelemetry\SDK\Common\Configuration\KnownValues; use OpenTelemetry\SDK\Common\Configuration\Variables; +use OpenTelemetry\SDK\Registry; class PropagatorFactory { use LogsMessagesTrait; - private const KNOWN_PROPAGATORS = [ - KnownValues::VALUE_TRACECONTEXT => ['\OpenTelemetry\API\Trace\Propagation\TraceContextPropagator', 'getInstance'], - KnownValues::VALUE_BAGGAGE => ['\OpenTelemetry\API\Baggage\Propagation\BaggagePropagator', 'getInstance'], - KnownValues::VALUE_B3 => ['\OpenTelemetry\Extension\Propagator\B3\B3Propagator', 'getB3SingleHeaderInstance'], - KnownValues::VALUE_B3_MULTI => ['\OpenTelemetry\Extension\Propagator\B3\B3Propagator', 'getB3MultiHeaderInstance'], - ]; - public function create(): TextMapPropagatorInterface { $propagators = Configuration::getList(Variables::OTEL_PROPAGATORS); @@ -30,7 +23,7 @@ public function create(): TextMapPropagatorInterface case 0: return new NoopTextMapPropagator(); case 1: - return $this->buildPropagator($propagators[0]) ?? new NoopTextMapPropagator(); + return $this->buildPropagator($propagators[0]); default: return new MultiTextMapPropagator($this->buildPropagators($propagators)); } @@ -43,41 +36,20 @@ private function buildPropagators(array $names): array { $propagators = []; foreach ($names as $name) { - $propagator = $this->buildPropagator($name); - if ($propagator !== null) { - $propagators[] = $propagator; - } + $propagators[] = $this->buildPropagator($name); } return $propagators; } - private function buildPropagator(string $name): ?TextMapPropagatorInterface + private function buildPropagator(string $name): TextMapPropagatorInterface { - switch ($name) { - case KnownValues::VALUE_NONE: - return null; - case KnownValues::VALUE_XRAY: - case KnownValues::VALUE_OTTRACE: - self::logWarning('Unimplemented propagator: ' . $name); - - return null; - default: - if (!array_key_exists($name, self::KNOWN_PROPAGATORS)) { - self::logWarning('Unknown propagator: ' . $name); - - return null; - } - $parts = self::KNOWN_PROPAGATORS[$name]; - - try { - return call_user_func($parts); - } catch (\Throwable $e) { - self::logError(sprintf('Unable to create %s propagator: %s', $name, $e->getMessage())); - - return null; - } - + try { + return Registry::textMapPropagator($name); + } catch (\RuntimeException $e) { + self::logWarning($e->getMessage()); } + + return NoopTextMapPropagator::getInstance(); } } diff --git a/src/SDK/Propagation/_register.php b/src/SDK/Propagation/_register.php new file mode 100644 index 000000000..fd90da184 --- /dev/null +++ b/src/SDK/Propagation/_register.php @@ -0,0 +1,16 @@ + $factory @@ -84,6 +90,14 @@ public static function registerMetricExporterFactory(string $exporter, $factory, self::$metricExporterFactories[$exporter] = $factory; } + public static function registerTextMapPropagator(string $name, TextMapPropagatorInterface $propagator, bool $clobber = false): void + { + if (!$clobber && array_key_exists($name, self::$textMapPropagators)) { + return; + } + self::$textMapPropagators[$name] = $propagator; + } + public static function spanExporterFactory(string $exporter): SpanExporterFactoryInterface { if (!array_key_exists($exporter, self::$spanExporterFactories)) { @@ -124,4 +138,13 @@ public static function metricExporterFactory(string $exporter): MetricExporterFa return $factory; } + + public static function textMapPropagator(string $name): TextMapPropagatorInterface + { + if (!array_key_exists($name, self::$textMapPropagators)) { + throw new RuntimeException('Text map propagator not registered for: ' . $name); + } + + return self::$textMapPropagators[$name]; + } } diff --git a/src/SDK/Trace/ExporterFactory.php b/src/SDK/Trace/ExporterFactory.php index 0ea019490..20a3e0dd4 100644 --- a/src/SDK/Trace/ExporterFactory.php +++ b/src/SDK/Trace/ExporterFactory.php @@ -7,7 +7,7 @@ use InvalidArgumentException; use OpenTelemetry\SDK\Common\Configuration\Configuration; use OpenTelemetry\SDK\Common\Configuration\Variables; -use OpenTelemetry\SDK\FactoryRegistry; +use OpenTelemetry\SDK\Registry; use RuntimeException; class ExporterFactory @@ -26,7 +26,7 @@ public function create(): ?SpanExporterInterface if ($exporter === 'none') { return null; } - $factory = FactoryRegistry::spanExporterFactory($exporter); + $factory = Registry::spanExporterFactory($exporter); return $factory->create(); } diff --git a/src/SDK/Trace/SpanExporter/ConsoleSpanExporterFactory.php b/src/SDK/Trace/SpanExporter/ConsoleSpanExporterFactory.php index b7eaf95be..7e45fb549 100644 --- a/src/SDK/Trace/SpanExporter/ConsoleSpanExporterFactory.php +++ b/src/SDK/Trace/SpanExporter/ConsoleSpanExporterFactory.php @@ -4,14 +4,14 @@ namespace OpenTelemetry\SDK\Trace\SpanExporter; -use OpenTelemetry\SDK\FactoryRegistry; +use OpenTelemetry\SDK\Registry; use OpenTelemetry\SDK\Trace\SpanExporterInterface; class ConsoleSpanExporterFactory implements SpanExporterFactoryInterface { public function create(): SpanExporterInterface { - $transport = FactoryRegistry::transportFactory('stream')->create('php://stdout', 'application/json'); + $transport = Registry::transportFactory('stream')->create('php://stdout', 'application/json'); return new ConsoleSpanExporter($transport); } diff --git a/src/SDK/Trace/SpanExporter/_register.php b/src/SDK/Trace/SpanExporter/_register.php index b1fcfaee2..aad07be42 100644 --- a/src/SDK/Trace/SpanExporter/_register.php +++ b/src/SDK/Trace/SpanExporter/_register.php @@ -1,7 +1,7 @@ assertInstanceOf(TransportFactoryInterface::class, $factory); } @@ -41,7 +41,7 @@ public function transportProtocolsProvider(): array */ public function test_default_span_exporter_factories(string $name): void { - $factory = FactoryRegistry::spanExporterFactory($name); + $factory = Registry::spanExporterFactory($name); $this->assertInstanceOf(SpanExporterFactoryInterface::class, $factory); } @@ -61,7 +61,7 @@ public function spanExporterProvider(): array */ public function test_default_metric_exporter_factories(string $name): void { - $factory = FactoryRegistry::metricExporterFactory($name); + $factory = Registry::metricExporterFactory($name); $this->assertInstanceOf(MetricExporterFactoryInterface::class, $factory); } @@ -80,7 +80,7 @@ public function metricExporterProvider(): array public function test_register_invalid_transport_factory($factory): void { $this->expectWarning(); - FactoryRegistry::registerTransportFactory('http', $factory, true); + Registry::registerTransportFactory('http', $factory, true); } /** @@ -89,7 +89,7 @@ public function test_register_invalid_transport_factory($factory): void public function test_register_invalid_span_exporter_factory($factory): void { $this->expectWarning(); - FactoryRegistry::registerSpanExporterFactory('foo', $factory, true); + Registry::registerSpanExporterFactory('foo', $factory, true); } /** @@ -98,7 +98,7 @@ public function test_register_invalid_span_exporter_factory($factory): void public function test_register_invalid_metric_exporter_factory($factory): void { $this->expectWarning(); - FactoryRegistry::registerMetricExporterFactory('foo', $factory, true); + Registry::registerMetricExporterFactory('foo', $factory, true); } public function invalidFactoryProvider(): array