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

SentryContext's middleware is not handled #299

Open
vertisan opened this issue Nov 6, 2019 · 1 comment
Labels

Comments

@vertisan
Copy link

@vertisan vertisan commented Nov 6, 2019

Hi,
I need to push additional, custom tags sometimes into exeption report but it's not handled:
Handler class (App/Exceptions)

    public function report(Exception $exception)
    {
        if ($this->shouldReport($exception) && app()->environment() === 'production') {
            app('sentry')->captureException($exception);
        }

        parent::report($exception);
    }

SentryContext class (App/Http/Middleware)

public function handle($request, Closure $next)
{
    if (app()->bound('sentry')) {
        /** @var \Raven_Client $sentry */
        $sentry = app('sentry');

        $userContext = ['id' => null, 'email' => null];
        $tagsContext = [
            'ip' =>  $request->getClientIp(),
            'server_time' => date('Y-m-d H:i:s')
        ];

        if (auth()->check()) {
            $userContext = [
                'id' => auth()->user()->id,
                'email' => auth()->user()->email,
            ];
        }

        if ($request->headers->has(self::IMPORTER_RESOURCE_HEADER)) {
            $tagsContext['importer_resource'] = $request->headers->get(self::IMPORTER_RESOURCE_HEADER);
            $tagsContext['importer'] = $request->headers->get(self::IMPORTER_HEADER);
        }

        $sentry->user_context($userContext);
        $sentry->tags_context($tagsContext);
    }

    return $next($request);
}

Kernel class (App/Http)

protected $middlewareGroups = [
    'web' => [
        //...
        // in $middleware it's not working too
        \App\Http\Middleware\SentryContext::class,
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

Laravel: 5.4
Sentry Laravel: 0.7.0

Where is the problem? Currently I cannot upgrade Laravel or Sentry's vendor.

@stayallive

This comment has been minimized.

Copy link
Member

@stayallive stayallive commented Nov 7, 2019

This should work correctly AFAIK, however depending on where the request fails it may never have reached the middleware causing the info not to be added.

For example (if I remember correctly) when a route fails because the class does not exists middleware have not been executed yet because the error occurred before the route was resolved.

You could try and see if a request even makes it to the middleware (locally) to make sure it executes when your error occurs.

The next best thing would be to set the tags and other context just before the error is passed to sentry in the report method of the error handler.

I don't think upgrading Sentry would help you here, since the feature is so basic there is no reason it shouldn't work so I think the exceptions occur before middleware ar even executed.

@stayallive stayallive added the support label Nov 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.