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

store root span info directly in context instead of request attribute #84

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
12 changes: 6 additions & 6 deletions src/Instrumentation/Psr15/src/Psr15Instrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
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;
Expand All @@ -24,10 +23,11 @@
*/
class Psr15Instrumentation
{
public static $rootSpan;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static $rootSpan;
/** @var ContextKeyInterface<SpanInterface> */
public static ContextKeyInterface $rootSpan;

IMO should also be private and only accessible via getter to prevent modification - or use an enum for this context key.


public static function register(): void
{
$instrumentation = new CachedInstrumentation('io.opentelemetry.contrib.php.psr15');

/**
* Create a span for each psr-15 middleware that is executed.
*/
Expand Down Expand Up @@ -68,9 +68,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)
Expand All @@ -92,7 +90,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();
}
Expand Down Expand Up @@ -125,3 +123,5 @@ public static function register(): void
);
}
}

Psr15Instrumentation::$rootSpan ??= Context::createKey('rootSpan');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this won't work with preloading, should be moved into ::register().

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down