From c03355416606ba1c784a39f8fb6b656bb45537a4 Mon Sep 17 00:00:00 2001 From: Przemek Delewski Date: Tue, 15 Nov 2022 13:05:54 +0100 Subject: [PATCH 1/3] store root span info directly in context instead of request attribute --- .../Psr15/src/Psr15Instrumentation.php | 12 +++++++----- .../tests/Integration/Psr15InstrumentationTest.php | 7 +++++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Instrumentation/Psr15/src/Psr15Instrumentation.php b/src/Instrumentation/Psr15/src/Psr15Instrumentation.php index ffc4a2a2..bf913292 100644 --- a/src/Instrumentation/Psr15/src/Psr15Instrumentation.php +++ b/src/Instrumentation/Psr15/src/Psr15Instrumentation.php @@ -7,10 +7,10 @@ use OpenTelemetry\API\Common\Instrumentation\CachedInstrumentation; use OpenTelemetry\API\Common\Instrumentation\Globals; use OpenTelemetry\API\Trace\Span; -use OpenTelemetry\API\Trace\SpanInterface; use OpenTelemetry\API\Trace\SpanKind; use OpenTelemetry\API\Trace\StatusCode; use OpenTelemetry\Context\Context; +use OpenTelemetry\Context\ContextKey; use function OpenTelemetry\Instrumentation\hook; use OpenTelemetry\SemConv\TraceAttributes; use Psr\Http\Message\ResponseInterface; @@ -24,6 +24,8 @@ */ class Psr15Instrumentation { + public static ContextKey $rootSpan; + public static function register(): void { $instrumentation = new CachedInstrumentation('io.opentelemetry.contrib.php.psr15'); @@ -68,9 +70,7 @@ public static function register(): void 'handle', pre: static function (RequestHandlerInterface $handler, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($instrumentation) { $request = ($params[0] instanceof ServerRequestInterface) ? $params[0] : null; - $root = $request - ? $request->getAttribute(SpanInterface::class) - : Span::getCurrent(); + $root = Context::getCurrent()->get(Psr15Instrumentation::$rootSpan); $builder = $instrumentation->tracer()->spanBuilder( $root ? sprintf('%s::%s', $class, $function) @@ -92,7 +92,7 @@ public static function register(): void ->setAttribute(TraceAttributes::HTTP_REQUEST_CONTENT_LENGTH, $request->getHeaderLine('Content-Length')) ->setAttribute(TraceAttributes::HTTP_SCHEME, $request->getUri()->getScheme()) ->startSpan(); - $request = $request->withAttribute(SpanInterface::class, $span); + $parent = $parent->with(Psr15Instrumentation::$rootSpan, $span); } else { $span = $builder->startSpan(); } @@ -125,3 +125,5 @@ public static function register(): void ); } } + +Psr15Instrumentation::$rootSpan = Context::createKey('rootSpan'); diff --git a/src/Instrumentation/Psr15/tests/Integration/Psr15InstrumentationTest.php b/src/Instrumentation/Psr15/tests/Integration/Psr15InstrumentationTest.php index e27b90db..8f03a9a7 100644 --- a/src/Instrumentation/Psr15/tests/Integration/Psr15InstrumentationTest.php +++ b/src/Instrumentation/Psr15/tests/Integration/Psr15InstrumentationTest.php @@ -13,7 +13,9 @@ use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator; use OpenTelemetry\API\Trace\Span; use OpenTelemetry\API\Trace\SpanInterface; +use OpenTelemetry\Context\Context; use OpenTelemetry\Context\ScopeInterface; +use OpenTelemetry\Contrib\Instrumentation\Psr15\Psr15Instrumentation; use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter; use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor; use OpenTelemetry\SDK\Trace\TracerProvider; @@ -161,8 +163,9 @@ public function handle(ServerRequestInterface $request): ResponseInterface if ($this->exception) { throw $this->exception; } - $span = $request->getAttribute(SpanInterface::class); - Assert::assertInstanceOf(Span::class, $span); + $rootSpan = Context::getCurrent()->get(Psr15Instrumentation::$rootSpan); + Assert::assertNotNull($rootSpan); + Assert::assertInstanceOf(Span::class, $rootSpan); return new Response(); } From 00374ad2f8267f8935ad8a99b9b17a209fb88b32 Mon Sep 17 00:00:00 2001 From: Przemek Delewski Date: Wed, 16 Nov 2022 15:27:53 +0100 Subject: [PATCH 2/3] fixing property type coercion --- src/Instrumentation/Psr15/src/Psr15Instrumentation.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Instrumentation/Psr15/src/Psr15Instrumentation.php b/src/Instrumentation/Psr15/src/Psr15Instrumentation.php index bf913292..9eb5bba2 100644 --- a/src/Instrumentation/Psr15/src/Psr15Instrumentation.php +++ b/src/Instrumentation/Psr15/src/Psr15Instrumentation.php @@ -10,7 +10,6 @@ use OpenTelemetry\API\Trace\SpanKind; use OpenTelemetry\API\Trace\StatusCode; use OpenTelemetry\Context\Context; -use OpenTelemetry\Context\ContextKey; use function OpenTelemetry\Instrumentation\hook; use OpenTelemetry\SemConv\TraceAttributes; use Psr\Http\Message\ResponseInterface; @@ -24,12 +23,11 @@ */ class Psr15Instrumentation { - public static ContextKey $rootSpan; + public static $rootSpan; public static function register(): void { $instrumentation = new CachedInstrumentation('io.opentelemetry.contrib.php.psr15'); - /** * Create a span for each psr-15 middleware that is executed. */ @@ -126,4 +124,4 @@ public static function register(): void } } -Psr15Instrumentation::$rootSpan = Context::createKey('rootSpan'); +Psr15Instrumentation::$rootSpan ??= Context::createKey('rootSpan'); From bbf87b748eedd25938fc094053fdfd0cd6267fd1 Mon Sep 17 00:00:00 2001 From: Przemek Delewski Date: Fri, 18 Nov 2022 11:15:02 +0100 Subject: [PATCH 3/3] rootSpan is private now --- .../Psr15/src/Psr15Instrumentation.php | 15 ++++++++++----- .../Integration/Psr15InstrumentationTest.php | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Instrumentation/Psr15/src/Psr15Instrumentation.php b/src/Instrumentation/Psr15/src/Psr15Instrumentation.php index 9eb5bba2..ee149cb1 100644 --- a/src/Instrumentation/Psr15/src/Psr15Instrumentation.php +++ b/src/Instrumentation/Psr15/src/Psr15Instrumentation.php @@ -23,10 +23,17 @@ */ class Psr15Instrumentation { - public static $rootSpan; + private static $rootSpan; + + public static function getRootSpan() + { + return Psr15Instrumentation::$rootSpan; + } public static function register(): void { + Psr15Instrumentation::$rootSpan ??= Context::createKey('rootSpan'); + $instrumentation = new CachedInstrumentation('io.opentelemetry.contrib.php.psr15'); /** * Create a span for each psr-15 middleware that is executed. @@ -68,7 +75,7 @@ public static function register(): void 'handle', pre: static function (RequestHandlerInterface $handler, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($instrumentation) { $request = ($params[0] instanceof ServerRequestInterface) ? $params[0] : null; - $root = Context::getCurrent()->get(Psr15Instrumentation::$rootSpan); + $root = Context::getCurrent()->get(Psr15Instrumentation::getRootSpan()); $builder = $instrumentation->tracer()->spanBuilder( $root ? sprintf('%s::%s', $class, $function) @@ -90,7 +97,7 @@ public static function register(): void ->setAttribute(TraceAttributes::HTTP_REQUEST_CONTENT_LENGTH, $request->getHeaderLine('Content-Length')) ->setAttribute(TraceAttributes::HTTP_SCHEME, $request->getUri()->getScheme()) ->startSpan(); - $parent = $parent->with(Psr15Instrumentation::$rootSpan, $span); + $parent = $parent->with(Psr15Instrumentation::getRootSpan(), $span); } else { $span = $builder->startSpan(); } @@ -123,5 +130,3 @@ public static function register(): void ); } } - -Psr15Instrumentation::$rootSpan ??= Context::createKey('rootSpan'); diff --git a/src/Instrumentation/Psr15/tests/Integration/Psr15InstrumentationTest.php b/src/Instrumentation/Psr15/tests/Integration/Psr15InstrumentationTest.php index 8f03a9a7..a3035889 100644 --- a/src/Instrumentation/Psr15/tests/Integration/Psr15InstrumentationTest.php +++ b/src/Instrumentation/Psr15/tests/Integration/Psr15InstrumentationTest.php @@ -163,7 +163,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface if ($this->exception) { throw $this->exception; } - $rootSpan = Context::getCurrent()->get(Psr15Instrumentation::$rootSpan); + $rootSpan = Context::getCurrent()->get(Psr15Instrumentation::getRootSpan()); Assert::assertNotNull($rootSpan); Assert::assertInstanceOf(Span::class, $rootSpan);