Skip to content

Commit c69a5de

Browse files
committed
Amend bundle kernel listener example
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.
1 parent 030c370 commit c69a5de

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/Symfony/OtelSdkBundle/README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ use OpenTelemetry\API\Trace\SpanInterface;
209209
use OpenTelemetry\SDK\Trace\Tracer;
210210
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
211211
use Symfony\Component\HttpKernel\KernelEvents;
212+
use Symfony\Component\HttpKernel\Event\RequestEvent;
212213
use Symfony\Component\HttpKernel\Event\TerminateEvent;
213214
214215
class TracingKernelSubscriber implements EventSubscriberInterface
@@ -221,12 +222,15 @@ class TracingKernelSubscriber implements EventSubscriberInterface
221222
// store a reference to the Tracer instance in case we want to create
222223
// more spans on different events (not covered in this example)
223224
$this->tracer = $tracer;
225+
}
224226
227+
public function onRequestEvent(RequestEvent $event)
228+
{
225229
// Create our main span and activate it
226-
$this->mainSpan = $tracer->spanBuilder('main')->startSpan();
230+
$this->mainSpan = $this->tracer->spanBuilder('main')->startSpan();
227231
$this->mainSpan->activate();
228232
}
229-
233+
230234
public function onTerminateEvent(TerminateEvent $event): void
231235
{
232236
// end our main span once the request has been processed and the kernel terminates.
@@ -236,9 +240,11 @@ class TracingKernelSubscriber implements EventSubscriberInterface
236240
public static function getSubscribedEvents(): array
237241
{
238242
// return the subscribed events, their methods and priorities
239-
// use a very low negative integer for the priority, so the listener
240-
// will be the last one to be called.
243+
// use a very high integer for the Request priority and a
244+
// very low negative integer for the Terminate priority, so the listener
245+
// will be the first and last one to be called respectively.
241246
return [
247+
KernelEvents::TERMINATE => [['onRequestEvent', 10000]],
242248
KernelEvents::TERMINATE => [['onTerminateEvent', -10000]],
243249
];
244250
}

0 commit comments

Comments
 (0)