Here's how I instrumented my Python Lambda function. If you are using Node environment, there are parallel docs.
Step 1: Create a dummy function - I created a simple function that tries Division By Zero. (Skip this if you already have your function)
import json
def lambda_handler(event, context):
message = 'Hello {} {}!'.format(event['first_name'],
event['last_name'])
x=3/0
return {
'message' : message
}
Step 2: Add Sentry to the Python function folder using pip
pip install --upgrade sentry-sdk
Step 3: Initialize Sentry - This required me to import Sentry's serverless package and wrap my handler.
import json
import sentry_sdk
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration
sentry_sdk.init(
dsn="Your dsn from sentry.io",
integrations=[AwsLambdaIntegration()],
traces_sample_rate=1.0,
)
def lambda_handler(event, context):
message = 'Hello {} {}!'.format(event['first_name'],
event['last_name'])
x=3/0
return {
'message' : message
}
That's it. I now have complete visibility into my Lambda functions.
The stack traces are detailed and point me to the line of code that's causing the exception.
Sentry captured my function transactions automatically (although, the transaction is pretty empty because my function doesn't do much)
Function context like aws_request_id
and remaining_time_in_milis
are super helpful. In addition, I have deep links to CloudWatch logs.
In addition, I made couple customizations to my error reporting:
-
I really care about timeout issues. They end up bleeding into unknown issues that are difficult to debug. So, I enabled
timeout_warning
.
sentry_sdk.init( dsn="Your dsn from sentry.io", integrations=[AwsLambdaIntegration(timeout_warning=True)], traces_sample_rate=1.0, )
Also, while Sentry automatically captured tons of context, I did want to look at my payload to the function. So, I enabled
send_default_pii
python
sentry_sdk.init(
dsn="Your dsn from sentry.io",
integrations=[AwsLambdaIntegration(timeout_warning=True)],
traces_sample_rate=1.0,
send_default_pii=True,
)
With this instrumentation, I have visibility that's unparalleled to any other out there. I can quickly debug issues and identify root causes for slow executing functions. Thus, saving tons of time and unnecessary lambda cost.
Top comments (0)