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

missing an event that contains the current request #111

Open
Gyuroff opened this issue May 4, 2023 · 3 comments
Open

missing an event that contains the current request #111

Gyuroff opened this issue May 4, 2023 · 3 comments

Comments

@Gyuroff
Copy link

Gyuroff commented May 4, 2023

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
Copy link
Owner

Baldinof commented May 4, 2023

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
Copy link
Author

Gyuroff commented May 9, 2023

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
Copy link
Owner

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]);
            }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants