Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create text map propagators from registry #885

Merged
merged 3 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions examples/autoload_sdk.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion examples/autoload_sdk_with_custom_transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion src/Contrib/Grpc/_register.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

declare(strict_types=1);
\OpenTelemetry\SDK\FactoryRegistry::registerTransportFactory('grpc', \OpenTelemetry\Contrib\Grpc\GrpcTransportFactory::class);
\OpenTelemetry\SDK\Registry::registerTransportFactory('grpc', \OpenTelemetry\Contrib\Grpc\GrpcTransportFactory::class);
2 changes: 1 addition & 1 deletion src/Contrib/Newrelic/_register.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

declare(strict_types=1);

\OpenTelemetry\SDK\FactoryRegistry::registerSpanExporterFactory('newrelic', \OpenTelemetry\Contrib\Newrelic\SpanExporterFactory::class);
\OpenTelemetry\SDK\Registry::registerSpanExporterFactory('newrelic', \OpenTelemetry\Contrib\Newrelic\SpanExporterFactory::class);
4 changes: 2 additions & 2 deletions src/Contrib/Otlp/MetricExporterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use OpenTelemetry\SDK\Common\Configuration\KnownValues;
use OpenTelemetry\SDK\Common\Configuration\Variables;
use OpenTelemetry\SDK\Common\Export\TransportInterface;
use OpenTelemetry\SDK\FactoryRegistry;
use OpenTelemetry\SDK\Metrics\MetricExporterFactoryInterface;
use OpenTelemetry\SDK\Metrics\MetricExporterInterface;
use OpenTelemetry\SDK\Registry;
use UnexpectedValueException;

class MetricExporterFactory implements MetricExporterFactoryInterface
Expand Down Expand Up @@ -44,7 +44,7 @@ private function buildTransport(string $protocol): TransportInterface
: Configuration::getMap(Variables::OTEL_EXPORTER_OTLP_HEADERS);
$headers += OtlpUtil::getUserAgentHeader();

$factory = FactoryRegistry::transportFactory($protocol);
$factory = Registry::transportFactory($protocol);
switch ($protocol) {
case KnownValues::VALUE_GRPC:
return $factory->create(
Expand Down
4 changes: 2 additions & 2 deletions src/Contrib/Otlp/SpanExporterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/Contrib/Otlp/_register.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

declare(strict_types=1);
\OpenTelemetry\SDK\FactoryRegistry::registerSpanExporterFactory('otlp', \OpenTelemetry\Contrib\Otlp\SpanExporterFactory::class);
\OpenTelemetry\SDK\FactoryRegistry::registerMetricExporterFactory('otlp', \OpenTelemetry\Contrib\Otlp\MetricExporterFactory::class);
\OpenTelemetry\SDK\Registry::registerSpanExporterFactory('otlp', \OpenTelemetry\Contrib\Otlp\SpanExporterFactory::class);
\OpenTelemetry\SDK\Registry::registerMetricExporterFactory('otlp', \OpenTelemetry\Contrib\Otlp\MetricExporterFactory::class);

\OpenTelemetry\SDK\FactoryRegistry::registerTransportFactory('http', \OpenTelemetry\Contrib\Otlp\OtlpHttpTransportFactory::class);
\OpenTelemetry\SDK\Registry::registerTransportFactory('http', \OpenTelemetry\Contrib\Otlp\OtlpHttpTransportFactory::class);
2 changes: 1 addition & 1 deletion src/Contrib/Zipkin/_register.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

declare(strict_types=1);

\OpenTelemetry\SDK\FactoryRegistry::registerSpanExporterFactory('zipkin', \OpenTelemetry\Contrib\Zipkin\SpanExporterFactory::class);
\OpenTelemetry\SDK\Registry::registerSpanExporterFactory('zipkin', \OpenTelemetry\Contrib\Zipkin\SpanExporterFactory::class);
16 changes: 16 additions & 0 deletions src/Extension/Propagator/B3/_register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use OpenTelemetry\Extension\Propagator\B3\B3Propagator;
use OpenTelemetry\SDK\Common\Configuration\KnownValues;
use OpenTelemetry\SDK\Registry;

Registry::registerTextMapPropagator(
KnownValues::VALUE_B3,
B3Propagator::getB3SingleHeaderInstance()
);
Registry::registerTextMapPropagator(
KnownValues::VALUE_B3_MULTI,
B3Propagator::getB3MultiHeaderInstance()
);
5 changes: 4 additions & 1 deletion src/Extension/Propagator/B3/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
"autoload": {
"psr-4": {
"OpenTelemetry\\Extension\\Propagator\\B3\\": "."
}
},
"files": [
"_register.php"
]
}
}
4 changes: 2 additions & 2 deletions src/SDK/Metrics/MeterProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
use OpenTelemetry\SDK\Common\Configuration\KnownValues;
use OpenTelemetry\SDK\Common\Configuration\Variables;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\FactoryRegistry;
use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilter\AllExemplarFilter;
use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilter\NoneExemplarFilter;
use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilter\WithSampledTraceExemplarFilter;
use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilterInterface;
use OpenTelemetry\SDK\Metrics\MetricExporter\NoopMetricExporter;
use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader;
use OpenTelemetry\SDK\Registry;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\SDK\Sdk;

Expand All @@ -37,7 +37,7 @@ public function create(): MeterProviderInterface
$exporterName = $exporters[0];

try {
$factory = FactoryRegistry::metricExporterFactory($exporterName);
$factory = Registry::metricExporterFactory($exporterName);
$exporter = $factory->create();
} catch (\Throwable $t) {
self::logWarning(sprintf('Unable to create %s meter provider: %s', $exporterName, $t->getMessage()));
Expand Down
4 changes: 2 additions & 2 deletions src/SDK/Metrics/MetricExporter/_register.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
48 changes: 10 additions & 38 deletions src/SDK/Propagation/PropagatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,21 @@
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);
switch (count($propagators)) {
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));
}
Expand All @@ -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();
}
}
16 changes: 16 additions & 0 deletions src/SDK/Propagation/_register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

\OpenTelemetry\SDK\Registry::registerTextMapPropagator(
\OpenTelemetry\SDK\Common\Configuration\KnownValues::VALUE_BAGGAGE,
\OpenTelemetry\API\Baggage\Propagation\BaggagePropagator::getInstance()
);
\OpenTelemetry\SDK\Registry::registerTextMapPropagator(
\OpenTelemetry\SDK\Common\Configuration\KnownValues::VALUE_TRACECONTEXT,
\OpenTelemetry\API\Trace\Propagation\TraceContextPropagator::getInstance()
);
\OpenTelemetry\SDK\Registry::registerTextMapPropagator(
\OpenTelemetry\SDK\Common\Configuration\KnownValues::VALUE_NONE,
\OpenTelemetry\Context\Propagation\NoopTextMapPropagator::getInstance()
);
25 changes: 24 additions & 1 deletion src/SDK/FactoryRegistry.php → src/SDK/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@

namespace OpenTelemetry\SDK;

use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface;
use OpenTelemetry\SDK\Common\Export\TransportFactoryInterface;
use OpenTelemetry\SDK\Metrics\MetricExporterFactoryInterface;
use OpenTelemetry\SDK\Trace\SpanExporter\SpanExporterFactoryInterface;
use RuntimeException;

class FactoryRegistry
/**
* A registry to enable central registration of components that the SDK requires but which may be provided
* by non-SDK modules, such as contrib and extension.
*/
class Registry
{
private static array $spanExporterFactories = [];
private static array $transportFactories = [];
private static array $metricExporterFactories = [];
private static array $textMapPropagators = [];

/**
* @param TransportFactoryInterface|class-string<TransportFactoryInterface> $factory
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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];
}
}
4 changes: 2 additions & 2 deletions src/SDK/Trace/ExporterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,7 +26,7 @@ public function create(): ?SpanExporterInterface
if ($exporter === 'none') {
return null;
}
$factory = FactoryRegistry::spanExporterFactory($exporter);
$factory = Registry::spanExporterFactory($exporter);

return $factory->create();
}
Expand Down
4 changes: 2 additions & 2 deletions src/SDK/Trace/SpanExporter/ConsoleSpanExporterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/SDK/Trace/SpanExporter/_register.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

declare(strict_types=1);
\OpenTelemetry\SDK\FactoryRegistry::registerSpanExporterFactory('console', \OpenTelemetry\SDK\Trace\SpanExporter\ConsoleSpanExporterFactory::class);
\OpenTelemetry\SDK\FactoryRegistry::registerSpanExporterFactory('memory', \OpenTelemetry\SDK\Trace\SpanExporter\InMemorySpanExporterFactory::class);
\OpenTelemetry\SDK\Registry::registerSpanExporterFactory('console', \OpenTelemetry\SDK\Trace\SpanExporter\ConsoleSpanExporterFactory::class);
\OpenTelemetry\SDK\Registry::registerSpanExporterFactory('memory', \OpenTelemetry\SDK\Trace\SpanExporter\InMemorySpanExporterFactory::class);

\OpenTelemetry\SDK\FactoryRegistry::registerTransportFactory('stream', \OpenTelemetry\SDK\Common\Export\Stream\StreamTransportFactory::class);
\OpenTelemetry\SDK\Registry::registerTransportFactory('stream', \OpenTelemetry\SDK\Common\Export\Stream\StreamTransportFactory::class);
1 change: 1 addition & 0 deletions src/SDK/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"files": [
"Common/Util/functions.php",
"Metrics/MetricExporter/_register.php",
"Propagation/_register.php",
"Trace/SpanExporter/_register.php",
"_autoload.php"
]
Expand Down
Loading