Skip to content

Commit

Permalink
Add TextMapPropagator to instrumentation context (#1401)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nevay authored Oct 15, 2024
1 parent c632d96 commit 2c1da9a
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 5 deletions.
4 changes: 1 addition & 3 deletions examples/instrumentation/configure_instrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use OpenTelemetry\API\Globals;
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\ExtensionHookManager;
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Instrumentation;
use OpenTelemetry\API\Logs\NoopLoggerProvider;
use OpenTelemetry\API\Metrics\Noop\NoopMeterProvider;
use OpenTelemetry\Config\SDK\Configuration;
use OpenTelemetry\Config\SDK\Configuration\Context;
use OpenTelemetry\Example\Example;
Expand All @@ -25,7 +23,7 @@
Configuration::parseFile(__DIR__ . '/otel-sdk.yaml')->create(new Context())->setAutoShutdown(true)->buildAndRegisterGlobal();
$configuration = \OpenTelemetry\Config\SDK\Instrumentation::parseFile(__DIR__ . '/otel-sdk.yaml')->create();
$hookManager = new ExtensionHookManager();
$context = new \OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context(Globals::tracerProvider(), new NoopMeterProvider(), new NoopLoggerProvider());
$context = new \OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context(Globals::tracerProvider(), Globals::meterProvider(), Globals::loggerProvider(), Globals::propagator());

foreach (ServiceLoader::load(Instrumentation::class) as $instrumentation) {
$instrumentation->register($hookManager, $configuration, $context);
Expand Down
3 changes: 3 additions & 0 deletions src/API/Instrumentation/AutoInstrumentation/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use OpenTelemetry\API\Metrics\Noop\NoopMeterProvider;
use OpenTelemetry\API\Trace\NoopTracerProvider;
use OpenTelemetry\API\Trace\TracerProviderInterface;
use OpenTelemetry\Context\Propagation\NoopTextMapPropagator;
use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface;

/**
* Context used for component creation.
Expand All @@ -20,6 +22,7 @@ public function __construct(
public readonly TracerProviderInterface $tracerProvider = new NoopTracerProvider(),
public readonly MeterProviderInterface $meterProvider = new NoopMeterProvider(),
public readonly LoggerProviderInterface $loggerProvider = new NoopLoggerProvider(),
public readonly TextMapPropagatorInterface $propagator = new NoopTextMapPropagator(),
) {
}
}
53 changes: 53 additions & 0 deletions src/SDK/Propagation/LateBindingTextMapPropagator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Propagation;

use Closure;
use OpenTelemetry\Context\ContextInterface;
use OpenTelemetry\Context\Propagation\PropagationGetterInterface;
use OpenTelemetry\Context\Propagation\PropagationSetterInterface;
use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface;

/**
* @internal
*/
final class LateBindingTextMapPropagator implements TextMapPropagatorInterface
{

/**
* @param TextMapPropagatorInterface|Closure(): TextMapPropagatorInterface $propagator
*/
public function __construct(
private TextMapPropagatorInterface|Closure $propagator,
) {
}

public function fields(): array
{
if (!$this->propagator instanceof TextMapPropagatorInterface) {
$this->propagator = ($this->propagator)();
}

return $this->propagator->fields();
}

public function inject(mixed &$carrier, ?PropagationSetterInterface $setter = null, ?ContextInterface $context = null): void
{
if (!$this->propagator instanceof TextMapPropagatorInterface) {
$this->propagator = ($this->propagator)();
}

$this->propagator->inject($carrier, $setter, $context);
}

public function extract($carrier, ?PropagationGetterInterface $getter = null, ?ContextInterface $context = null): ContextInterface
{
if (!$this->propagator instanceof TextMapPropagatorInterface) {
$this->propagator = ($this->propagator)();
}

return $this->propagator->extract($carrier, $getter, $context);
}
}
18 changes: 17 additions & 1 deletion src/SDK/SdkAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
use OpenTelemetry\Config\SDK\Configuration as SdkConfiguration;
use OpenTelemetry\Config\SDK\Instrumentation as SdkInstrumentation;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface;
use OpenTelemetry\SDK\Common\Configuration\Configuration;
use OpenTelemetry\SDK\Common\Configuration\Variables;
use OpenTelemetry\SDK\Common\Util\ShutdownHandler;
use OpenTelemetry\SDK\Logs\EventLoggerProviderFactory;
use OpenTelemetry\SDK\Logs\LoggerProviderFactory;
use OpenTelemetry\SDK\Metrics\MeterProviderFactory;
use OpenTelemetry\SDK\Propagation\LateBindingTextMapPropagator;
use OpenTelemetry\SDK\Propagation\PropagatorFactory;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\SDK\Trace\AutoRootSpan;
Expand Down Expand Up @@ -154,7 +156,8 @@ private static function registerInstrumentations(): void
$tracerProvider = self::createLateBindingTracerProvider();
$meterProvider = self::createLateBindingMeterProvider();
$loggerProvider = self::createLateBindingLoggerProvider();
$context = new InstrumentationContext($tracerProvider, $meterProvider, $loggerProvider);
$propagator = self::createLateBindingTextMapPropagator();
$context = new InstrumentationContext($tracerProvider, $meterProvider, $loggerProvider, $propagator);

foreach (ServiceLoader::load(Instrumentation::class) as $instrumentation) {
/** @var Instrumentation $instrumentation */
Expand Down Expand Up @@ -204,6 +207,19 @@ private static function createLateBindingLoggerProvider(): LoggerProviderInterfa
});
}

private static function createLateBindingTextMapPropagator(): TextMapPropagatorInterface
{
return new LateBindingTextMapPropagator(static function (): TextMapPropagatorInterface {
$scope = Context::getRoot()->activate();

try {
return Globals::propagator();
} finally {
$scope->detach();
}
});
}

private static function getHookManager(): HookManagerInterface
{
/** @var HookManagerInterface $hookManager */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use OpenTelemetry\API\Logs\NoopLoggerProvider;
use OpenTelemetry\API\Metrics\Noop\NoopMeterProvider;
use OpenTelemetry\API\Trace\NoopTracerProvider;
use OpenTelemetry\Context\Propagation\NoopTextMapPropagator;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

Expand All @@ -20,5 +21,6 @@ public function test_default_noops(): void
$this->assertInstanceOf(NoopTracerProvider::class, $context->tracerProvider);
$this->assertInstanceOf(NoopMeterProvider::class, $context->meterProvider);
$this->assertInstanceOf(NoopLoggerProvider::class, $context->loggerProvider);
$this->assertInstanceOf(NoopTextMapPropagator::class, $context->propagator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace OpenTelemetry\Tests\Unit\API\Instrumentation\AutoInstrumentation;

use function assert;
use Nevay\SPI\ServiceLoader;
use OpenTelemetry\API\Behavior\Internal\Logging;
use OpenTelemetry\API\Configuration\ConfigProperties;
Expand All @@ -25,11 +26,13 @@
use OpenTelemetry\API\Trace\LateBindingTracerProvider;
use OpenTelemetry\API\Trace\TracerInterface;
use OpenTelemetry\API\Trace\TracerProviderInterface;
use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface;
use OpenTelemetry\SDK\Common\Configuration\Variables;
use OpenTelemetry\SDK\SdkAutoloader;
use OpenTelemetry\Tests\TestState;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

#[CoversClass(LateBindingLoggerProvider::class)]
#[CoversClass(LateBindingLogger::class)]
Expand All @@ -45,6 +48,7 @@
class LateBindingProviderTest extends TestCase
{
use TestState;
use ProphecyTrait;

public function setUp(): void
{
Expand Down Expand Up @@ -77,6 +81,12 @@ public function getLogger(): LoggerInterface

return self::$context->loggerProvider->getLogger('test');
}
public function getPropagator(): TextMapPropagatorInterface
{
assert(self::$context !== null);

return self::$context->propagator;
}
};
$this->setEnvironmentVariable(Variables::OTEL_PHP_AUTOLOAD_ENABLED, 'true');
$tracer_accessed = false;
Expand All @@ -101,14 +111,17 @@ public function getLogger(): LoggerInterface

return $this->createMock(LoggerInterface::class);
});
$propagator = $this->prophesize(TextMapPropagatorInterface::class);

ServiceLoader::register(Instrumentation::class, $instrumentation::class);
$this->assertTrue(SdkAutoloader::autoload());
//initializer added _after_ autoloader has run and instrumentation registered
Globals::registerInitializer(function (Configurator $configurator) use ($tracerProvider, $loggerProvider, $meterProvider): Configurator {
Globals::registerInitializer(function (Configurator $configurator) use ($tracerProvider, $loggerProvider, $meterProvider, $propagator): Configurator {
return $configurator
->withTracerProvider($tracerProvider)
->withMeterProvider($meterProvider)
->withLoggerProvider($loggerProvider)
->withPropagator($propagator->reveal())
;
});

Expand All @@ -129,5 +142,11 @@ public function getLogger(): LoggerInterface
$this->assertFalse($logger_accessed);
$logger->emit(new LogRecord()); /** @phpstan-ignore-next-line */
$this->assertTrue($logger_accessed);

/** @phpstan-ignore-next-line */
$propagator->fields()->shouldNotHaveBeenCalled();
$instrumentation->getPropagator()->fields();
/** @phpstan-ignore-next-line */
$propagator->fields()->shouldHaveBeenCalledOnce();
}
}

0 comments on commit 2c1da9a

Please sign in to comment.