Skip to content

Commit

Permalink
Amend bundle kernel listener example
Browse files Browse the repository at this point in the history
The example only hooked into the TerminateEvent so the constructor only got called
late in the request lifecycle (this happened with symfony/symfony 5.3).

By explicitly hooking in to the RequestEvent and calling startSpan there, the span
covers all other spans created during the request.
  • Loading branch information
wadjei committed May 6, 2022
1 parent 030c370 commit c69a5de
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Symfony/OtelSdkBundle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\SDK\Trace\Tracer;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
class TracingKernelSubscriber implements EventSubscriberInterface
Expand All @@ -221,12 +222,15 @@ class TracingKernelSubscriber implements EventSubscriberInterface
// store a reference to the Tracer instance in case we want to create
// more spans on different events (not covered in this example)
$this->tracer = $tracer;
}
public function onRequestEvent(RequestEvent $event)
{
// Create our main span and activate it
$this->mainSpan = $tracer->spanBuilder('main')->startSpan();
$this->mainSpan = $this->tracer->spanBuilder('main')->startSpan();
$this->mainSpan->activate();
}
public function onTerminateEvent(TerminateEvent $event): void
{
// end our main span once the request has been processed and the kernel terminates.
Expand All @@ -236,9 +240,11 @@ class TracingKernelSubscriber implements EventSubscriberInterface
public static function getSubscribedEvents(): array
{
// return the subscribed events, their methods and priorities
// use a very low negative integer for the priority, so the listener
// will be the last one to be called.
// use a very high integer for the Request priority and a
// very low negative integer for the Terminate priority, so the listener
// will be the first and last one to be called respectively.
return [
KernelEvents::TERMINATE => [['onRequestEvent', 10000]],
KernelEvents::TERMINATE => [['onTerminateEvent', -10000]],
];
}
Expand Down

0 comments on commit c69a5de

Please sign in to comment.