Baldinof / roadrunner-bundle

A RoadRunner worker integrated in your Symfony app
MIT License
266 stars 47 forks source link

missing an event that contains the current request #111

Open Gyuroff opened 1 year ago

Gyuroff commented 1 year ago

It would be helpful if you provide an event that contains the current request. It can be used for a telemetry, tracing and so on. It could be named WorkerReceivedRequestEvent or you can find better naming.

Thanks in advance!

Baldinof commented 1 year ago

Hello!

Symfony already has an event for that kernel.request.

If you need to do something before Symfony start handling the request I recommend you to create a middleware.

Gyuroff commented 1 year ago

Hi @Baldinof thanks for your response.

Sorry for not being clear enough on my first post.

I am using go (roadrunner) middleware for OpenTelemetry described here (https://roadrunner.dev/docs/middleware-otel/2.x/en) and it is executed outside and before the symfony kernel, so I cannot take advantage of your suggestion. For the telemetry itself I and not only me need a way to get the request received by the worker in order to get/calculate the trace id. That is why I am suggesting once the worker receives the request to throw an event which is populated with the request itself.

Thanks.

Baldinof commented 1 year ago

I'm not sure to understand, here how you can use otel with a middleware.

Based on the example here, something like this should work:

final class TelemetryMiddleware implements MiddlewareInterface
{
    private TracerInterface $tracer;

    public function __construct(private LoggerInterface $logger)
    {
        $this->tracer = (new TracerProviderFactory)->create()->getTracer('example');
    }

    public function process(Request $request, HttpKernelInterface $next): \Generator
    {
        $context = TraceContextPropagator::getInstance()->extract($request->headers->all());
        $rootSpan = $this->tracer->spanBuilder('root')->setParent($context)->startSpan();
        $scope = $rootSpan->activate();
        try {
            $childSpan = $this->tracer->spanBuilder('child')->startSpan();

            yield $next->handle($request);

        } finally {

            $childSpan->end();
            $rootSpan->end();

            if ($error = $scope->detach()) {
                $this->logger->error('Error detaching scope', ['error' => $error]);
            }
        }
    }
}