honeybadger-io / honeybadger-python

Send Python and Django errors to Honeybadger.
https://www.honeybadger.io/
MIT License
15 stars 25 forks source link

Error using python logging integration in Chalice AWSLambda #95

Closed bradsgreen closed 2 years ago

bradsgreen commented 3 years ago

I am trying to use the logging plugin in a Chalice lambda.

It works just fine, in that I see the logs in Cloudwatch & the error log appears as an error in honeybadger,

However, I get the following warning each time I register the logger: [WARNING] 2021-08-31T21:46:25.138Z Lambda function not wrapped by honeybadger: module 'main' has no attribute 'handle_http_request'

Any guidance as to what is going on?

Here is my (very simple) code:

logger = ...
hb_handler = HoneybadgerHandler(api_key='---my api key---')
hb_handler.setLevel(logging.ERROR)
logger.addHandler(hb_handler)

@foo.lambda_function()
def honeybadger_error_test(event, context):
    logger.error('Something went wrong!!!')
    logger.info('But this is OK!')
Kelvin4664 commented 3 years ago

Hey @bradsgreen The problem is not with the logging plugin. By default, honeybadger tries to detect and monitor errors when configured in a Lambda environment. But in your case it detected a lambda environment but was unable to wrap around your code to report errors. Can you specify your lambda runtime? and any third party library you use that may be interfering with how lambda runs (maybe a deployment tool?) for a possible fix.

bradsgreen commented 3 years ago

Perhaps the difference is that I am using blueprints:

app.py:

from chalice import Chalice

from chalicelib.foo import foo_product

app = Chalice(app_name='foo')
app.register_blueprint(foo_product)

foo_product.py:

import os

from chalice import Blueprint, Cron
from chalicelib import chalice_logging_workaround

foo_product = Blueprint(__name__)
logger = chalice_logging_workaround.get_logger_for_blueprint(
    __name__, api_key='--my key--')

@foo_product.lambda_function()
def honeybadger_error_test(event, context):
    logger.error('Something went wrong!!!')
    logger.info('But this is OK!')

chalice_logging_workaround.py:

import logging
import sys

from honeybadger.contrib.logger import HoneybadgerHandler

def get_logger_for_blueprint(name, api_key=''):
    # TODO: This may not be needed anymore when https://github.com/aws/chalice/issues/1566 is fixed
    log = logging.getLogger(name)
    log.setLevel(logging.DEBUG)
    handler = logging.StreamHandler(sys.stdout)
    # Timestamp is handled by lambda itself so the
    # default FORMAT_STRING doesn't need to include it.h
    FORMAT_STRING = '%(name)s - %(levelname)s - %(message)s'
    formatter = logging.Formatter(FORMAT_STRING)
    handler.setFormatter(formatter)
    log.propagate = False
    log.addHandler(handler)

    # Install a honeybadger handler to report 'error' logs to HB
    hb_handler = HoneybadgerHandler(api_key=api_key)
    hb_handler.setLevel(logging.ERROR)
    log.addHandler(hb_handler)

    return log

the logging workaround is due to a current chalice bug which causes logging to not work in Blueprints if you access it from the app environment there.

Everything else is bog simple. I deploy using chalice deploy, my requirements.txt is:

boto3==1.14.43
honeybadger==0.7.0
psutil==5.8.0
six==1.16.0
bradsgreen commented 3 years ago

FYI I see what you mean now. I changed my test to:

def honeybadger_error_test(event, context):
    logger.error('Something went wrong!!!')
    logger.info('But this is OK!')
    raise AssertionError("BOOM!")

And the logger.error() reported an error to HB, but the exception is not reported because the lambda was not wrapped successfully as you mentioned.

Kelvin4664 commented 3 years ago

One more thing @bradsgreen what python version are you running?

bradsgreen commented 3 years ago

(venv) xxx % python --version Python 3.8.2

bradsgreen commented 3 years ago

I simplified my usage to not use the Blueprints in Chalice but it did not help:

app = Chalice(app_name='myapp')

# Install a honeybadger handler to report 'error' logs to HB
hb_handler = HoneybadgerHandler(api_key='---my key')
hb_handler.setLevel(logging.ERROR)
app.log.addHandler(hb_handler)

@app.lambda_function()
def honeybadger_error_test(event, context):
    app.log.error('Something went wrong!!!')
    app.log.info('But this is OK!')
    raise AssertionError("BOOM!")

Still results in


2021-09-08T22:36:20.039ZCopy[WARNING]   2021-09-08T22:36:20.039Z        Lambda function not wrapped by honeybadger: module '__main__' has no attribute 'handle_http_request'
--

and the assertion not being reported.

bradsgreen commented 3 years ago

OK, this has nothing to do with the logging integration at all:

from honeybadger import honeybadger
honeybadger.configure(api_key='--mykey--')

@app.lambda_function()
def honeybadger_error_test(event, context):
    app.log.error('Something went wrong!!!')
    app.log.info('But this is OK!')
    raise AssertionError("BOOM!")

results in the following logs:

START RequestId: cbc62e52-ae16-4a40-ba5f-edb75e0d8908 Version: $LATEST
--
[WARNING]   2021-09-09T00:15:20.095Z        Lambda function not wrapped by honeybadger: module '__main__' has no attribute 'handle_http_request'
kds - ERROR - Something went wrong!!!
[ERROR] AssertionError: BOOM!Traceback (most recent call last):  File "/var/task/chalice/app.py", line 1564, in __call__    return self.handler(event_obj)  File "/var/task/chalice/app.py", line 1517, in __call__    return self._original_func(event.to_dict(), event.context)  File "/var/task/app.py", line 29, in honeybadger_error_test    raise AssertionError("BOOM!")
END RequestId: cbc62e52-ae16-4a40-ba5f-edb75e0d8908
REPORT RequestId: cbc62e52-ae16-4a40-ba5f-edb75e0d8908  Duration: 2.85 ms   Billed Duration: 3 ms   Memory Size: 5120 MB    Max Memory Used: 71 MB  Init Duration: 640.74 ms

Does the AWS Lambda integration simply not work in Chalice at all? Or am I doing something wrong?

Kelvin4664 commented 3 years ago

You are not doing something wrong. it has more to do with the python version on lambda, not chalice. I will setup a dummy project to verify and make a fix.

joshuap commented 2 years ago

Fix released in 0.7.1