getsentry / raven-python

Raven is the legacy Python client for Sentry (getsentry.com) — replaced by sentry-python
https://sentry.io
BSD 3-Clause "New" or "Revised" License
1.68k stars 657 forks source link

AWS Lambda uses ThreadedHTTPTransport instead of HTTPTransport #1063

Open jcerjak opened 7 years ago

jcerjak commented 7 years ago

In AWS Lambda environment, HTTPTransport should be used instead of the default ThreadedHTTPTransport (this was added in #828). Which HTTP transport to use is determined based on the presence of the LAMBDA_TASK_ROOT environment variable. But in my case ThreadedHTTPTransport is used, even though LAMBDA_TASK_ROOT is there.

I discovered this when noticing that some of the errors were not being reported to Sentry.

Why this happens?

It seems that when instantiating the Client from DSN, the default transport is used, and the one determined by discover_default_transport is ignored.

How to fix this?

The discover_default_transport should be taken into account when using RemoteConfig.from_string. If this is not acceptable, the docs should mention that transport needs to be manually set to raven.transport.http.HTTPTransport when using Sentry on AWS Lambda.

dcramer commented 7 years ago

If we truly cant use the threaded transport, then this seems like an easy fix in the discovery (the env check)

jcerjak commented 7 years ago

Not sure why it doesn't work, maybe I'm doing something wrong..

Steps to reproduce the issue

  1. Create a lambda function with content:
    
    import os
    from raven import Client

client = Client(os.environ['SENTRY_DSN'])

@client.capture_exceptions def lambda_handler(event, context): raise Exception('Error!')

2. Package the lambda for Python 2.7 as per [AWS instructions](http://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html), together with dependencies (raven==6.1.0 and contextlib2==0.5.5). I've built the package in the [AWS environment](http://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html), though in this case it probably doesn't make a difference, since there are no C extensions.
3. Deploy the lambda package to AWS, with Python 2.7 environment. Set SENTRY_DSN environment variable (`https://...`)
4. Trigger the lambda. No errors are logged in Sentry.
5. Replace the lambda code with:

import os from raven import Client from raven.transport.http import HTTPTransport

client = Client(os.environ['SENTRY_DSN'], transport=HTTPTransport)

@client.capture_exceptions def lambda_handler(event, context): raise Exception('Error!')


6. Trigger the lambda again. Now the error should show up in Sentry.
ashwoods commented 7 years ago

Im porting https://github.com/Netflix-Skunkworks/raven-python-lambda to raven, but in the meanwhile you can Check out their implementation. On Wed, 23 Aug 2017 at 12:08, Jure Cerjak notifications@github.com wrote:

Not sure why it doesn't work, maybe I'm doing something wrong..

Steps to reproduce the issue

  1. Create a lambda function with content:

import os from raven import Client

client = Client(os.environ['SENTRY_DSN'])

@client.capture_exceptions def lambda_handler(event, context): raise Exception('Error!')

  1. Package the lambda for Python 2.7 as per AWS instructions http://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html, together with dependencies (raven==6.1.0 and contextlib2==0.5.5). I've built the package in the AWS environment http://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html, though in this case it probably doesn't make a difference, since there are no C extensions.
  2. Deploy the lambda package to AWS, with Python 2.7 environment. Set SENTRY_DSN environment variable (https://...)
  3. Trigger the lambda. No errors are logged in Sentry.
  4. Replace the lambda code with:

import os from raven import Client from raven.transport.http import HTTPTransport

client = Client(os.environ['SENTRY_DSN'], transport=HTTPTransport)

@client.capture_exceptions def lambda_handler(event, context): raise Exception('Error!')

  1. Trigger the lambda again. Now the error should show up in Sentry.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/getsentry/raven-python/issues/1063#issuecomment-324284102, or mute the thread https://github.com/notifications/unsubscribe-auth/AAW4tclk1F6JgFPn8szQjDqMPb4-UP3Mks5sa_oZgaJpZM4O-0XS .

jcerjak commented 7 years ago

It seems that they are also using HTTPTransport: https://github.com/Netflix-Skunkworks/raven-python-lambda/blob/master/raven_python_lambda/__init__.py#L49

dcramer commented 7 years ago

If it's purely the default transport that breaks it we should just get that ito core. We also might want to explore if we should only enable threads in certain situations based on feature detect?

riteshshrv commented 6 years ago

SentryHandler does not send errors to sentry when log level is set to ERROR on Lambda. I'm using dictConfig to configure logging with this handler, is there something I need to do to get this working?