Skip to content

Commit

Permalink
store root span info directly in context instead of request attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Przemek Delewski committed Nov 15, 2022
1 parent f0e6db3 commit da19a11
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
12 changes: 7 additions & 5 deletions src/Instrumentation/Psr15/src/Psr15Instrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,6 +24,8 @@
*/
class Psr15Instrumentation
{
public static ContextKey $rootSpanExists;

public static function register(): void
{
$instrumentation = new CachedInstrumentation('io.opentelemetry.contrib.php.psr15');
Expand Down Expand Up @@ -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::$rootSpanExists);
$builder = $instrumentation->tracer()->spanBuilder(
$root
? sprintf('%s::%s', $class, $function)
Expand All @@ -85,14 +85,14 @@ public static function register(): void
if (!$root && $request) {
//create http root span
$parent = Globals::propagator()->extract($request->getHeaders());
$parent = $parent->with(Psr15Instrumentation::$rootSpanExists, true);
$span = $builder
->setParent($parent)
->setAttribute(TraceAttributes::HTTP_URL, $request->getUri()->__toString())
->setAttribute(TraceAttributes::HTTP_METHOD, $request->getMethod())
->setAttribute(TraceAttributes::HTTP_REQUEST_CONTENT_LENGTH, $request->getHeaderLine('Content-Length'))
->setAttribute(TraceAttributes::HTTP_SCHEME, $request->getUri()->getScheme())
->startSpan();
$request = $request->withAttribute(SpanInterface::class, $span);
} else {
$span = $builder->startSpan();
}
Expand Down Expand Up @@ -125,3 +125,5 @@ public static function register(): void
);
}
}

Psr15Instrumentation::$rootSpanExists = Context::createKey('rootSpanExists');
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OpenTelemetry\API\Trace\Span;
use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\Context\ScopeInterface;
use OpenTelemetry\Context\Context;
use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\SDK\Trace\TracerProvider;
Expand All @@ -23,6 +24,7 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use OpenTelemetry\Contrib\Instrumentation\Psr15\Psr15Instrumentation;

class Psr15InstrumentationTest extends TestCase
{
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);
$rootSpanExists = Context::getCurrent()->get(Psr15Instrumentation::$rootSpanExists);
Assert::assertNotNull($rootSpanExists);
Assert::assertTrue($rootSpanExists);

return new Response();
}
Expand Down

0 comments on commit da19a11

Please sign in to comment.