getsentry / sentry-laravel

The official Laravel SDK for Sentry (sentry.io)
https://sentry.io
MIT License
1.25k stars 189 forks source link

Tracing all (vendors included) curl / Guzzle requests #448

Closed illambo closed 2 years ago

illambo commented 3 years ago

Hello! I know that perhaps it is not the most suitable place to ask for this advice but I try anyway, I would like to trace in the performance all the spans deriving from the curl or guzzle calls including those of the vendors (not manageable at the application level). does anyone experience a way to (php extension or other mode) that allows such a request?

On the google cloud platform I know that there are extensions such as opencentus with dedicated integrations that, in combination with google cloud trace, also monitor these aspects, but do I find alternatives or other even simpler ways?

Thanks and in case you can close the request if you consider it irrelevant.

stayallive commented 3 years ago

I find this request highly relevant but I don't think this will be possible unless those libraries have global ways to inject tracing capabilities or fire events if they do requests.

This "problem" is that we are doing tracing fully native in PHP and don't use a PHP extension. This method has a few upsides but also a few downsides like not being able to inject our code in any and all objects unless they provide a public API for it.

And as far as I know curl and Guzzle do not have those, so I'm open to ideas on how to globally trace requests but this might simply not be possible at the moment.

illambo commented 3 years ago

I think guzzle/guzzle#2128 may be inherent so I leave the annotation. Thanks for the support.

illambo commented 3 years ago

Perhaps a possible future integration with OpenTelemetry (merging of OpenCensus and OpenTracing) it might be useful. There is also a pre-alpha php sdk at open-telemetry/opentelemetry-php.

robmellett commented 3 years ago

Hey @stayallive,

I'm just a random lurker and I happened to notice this issue.

We just implemented something similar in our laravel app. We you can log all request/response data with Guzzle middleare. https://docs.guzzlephp.org/en/stable/handlers-and-middleware.html

A laravel example would look like this.

// GuzzleClient
$client = new Client([
            'handler' => $this->createHandler(),
        ]);

This will wireup a guzzle client to write requests/responses to the configured laravel sentry log channel.

/**
     * This is used to log all the requests & responses
     * to the log provider.
     * @return HandlerStack
     */
    private function createHandler()
    {
        $handlerStack = HandlerStack::create();

        $id = Str::random();

        $messageFormats = [
            "{$id} REQUEST: {method} - {uri} - HTTP/{version} - {req_headers} - {req_body}",
            "{$id} RESPONSE: {code} - {res_body}",
        ];

        collect($messageFormats)->each(function ($messageFormat) use ($handlerStack) {
            $handlerStack->unshift(
                Middleware::log(
                    App::get('log')->channel('sentry'),
                    new SecureMessageFormatter($messageFormat),
                    'debug'
                )
            );
        });

        return $handlerStack;
    }

You can configure a custom log driver with this. I think it's possible with Sentry (It's been a while since I've looked at the docs).

/// config/logging.php

'sentry' => [
            'driver' => 'custom',
            'via'    => \App\Support\SentryLogger::class,
            'host' => ********,
            'port' => '514',
        ],

Here, SentryLogger::class would be a class that extends the Monolog driver.

Just putting some thoughts out there. Feel free to shoot them down of course :)

stayallive commented 3 years ago

Much appreciated you sharing your thoughts!

The "issue" is more that we would like it to be automatic if possible so that every cURL, Guzzle and/or Laravel Http request is profiled without needing the user to setup a handler stack and such.

For Guzzle specific we already have a middleware that traces requests, but like your solution you need to manually add that to your Guzzle client, there is no global middleware for Guzzle (or any of the other major Http clients).

stayallive commented 2 years ago

As mentioned we have a middleware you can add to your Guzzle calls.

AFAIK there is no way to globally and automatically hook into Guzzle / cURL so I am closing this issue since it cannot be solved unfortunately unless Guzzle / cURL / PHP supplies us with a way of hooking into them.

If there are developments in the future we will for sure revisit this!