Skip to content

Commit ee6a347

Browse files
committed
Add Sentry integration
Why these changes are being introduced: We generally want Sentry integration for most apps, and its a bit different for lambda functions than for other Python apps so it's important to have an example integration in this lambda template repo. How this addresses that need: * Adds sentry_sdk dependency to Pipfile. * Adds Sentry lambda integration to my_function module. * Adds check for required WORKSPACE env in lambda_handler function. * Adds tests for new functionality. * Updates README to include instructions for setting up a Sentry project when creating a new app from this template. Relevant ticket(s): * https://mitlibraries.atlassian.net/browse/IN-526
1 parent c60c9ad commit ee6a347

File tree

5 files changed

+63
-6
lines changed

5 files changed

+63
-6
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ verify_ssl = true
44
name = "pypi"
55

66
[packages]
7+
sentry-sdk = "*"
78

89
[dev-packages]
910
bandit = "*"

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ A template repository for creating Python lambda functions.
1515
- Dependabot alerts
1616
- Dependabot security updates
1717
- Secret scanning
18+
7. Create a Sentry project for the app if needed (we want this for most apps):
19+
- Send initial exceptions to Sentry project for dev, stage, and prod environments to create them.
20+
- Create an alert for the prod environment only, with notifications sent to the appropriate team(s).
21+
- If *not* using Sentry, delete Sentry configuration from my_function.py and test_my_function_.py, and remove sentry_sdk from project dependencies.
1822

1923
# my_function
2024

@@ -29,6 +33,7 @@ Description of the function/functions.
2933

3034
## Required ENV
3135

36+
- `SENTRY_DSN` = If set to a valid Sentry DSN, enables Sentry exception monitoring. This is not needed for local development.
3237
- `WORKSPACE` = Set to `dev` for local development, this will be set to `stage` and `prod` in those environments by Terraform.
3338

3439
## Running locally
@@ -44,7 +49,7 @@ Description of the function/functions.
4449
- Run the default handler for the container:
4550

4651
```bash
47-
docker run -p 9000:8080 my_function:latest
52+
docker run -e WORKSPACE=dev -p 9000:8080 my_function:latest
4853
```
4954

5055
- Post to the container:

lambdas/my_function.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
11
import json
22
import logging
3+
import os
4+
5+
import sentry_sdk
6+
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration
37

48
logger = logging.getLogger(__name__)
59
logger.setLevel(logging.DEBUG)
610

11+
env = os.getenv("WORKSPACE")
12+
if sentry_dsn := os.getenv("SENTRY_DSN"):
13+
sentry = sentry_sdk.init(
14+
dsn=sentry_dsn,
15+
environment=env,
16+
integrations=[
17+
AwsLambdaIntegration(),
18+
],
19+
traces_sample_rate=1.0,
20+
)
21+
logger.info("Sentry DSN found, exceptions will be sent to Sentry with env=%s", env)
22+
else:
23+
logger.info("No Sentry DSN found, exceptions will not be sent to Sentry")
24+
725

826
def lambda_handler(event: dict, context: object) -> str: # noqa
27+
if not os.getenv("WORKSPACE"):
28+
raise RuntimeError("Required env variable WORKSPACE is not set")
29+
930
logger.debug(json.dumps(event))
31+
1032
return "You have successfully called this lambda!"

tests/test_function.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/test_my_function.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from importlib import reload
2+
3+
import pytest
4+
5+
from lambdas import my_function
6+
7+
8+
def test_my_function_configures_sentry_if_dsn_present(caplog, monkeypatch):
9+
monkeypatch.setenv("SENTRY_DSN", "https://1234567890@00000.ingest.sentry.io/123456")
10+
reload(my_function)
11+
assert (
12+
"Sentry DSN found, exceptions will be sent to Sentry with env=test"
13+
in caplog.text
14+
)
15+
16+
17+
def test_my_function_doesnt_configure_sentry_if_dsn_not_present(caplog, monkeypatch):
18+
monkeypatch.delenv("SENTRY_DSN", raising=False)
19+
reload(my_function)
20+
assert "No Sentry DSN found, exceptions will not be sent to Sentry" in caplog.text
21+
22+
23+
def test_lambda_handler_missing_workspace_env_raises_error(monkeypatch):
24+
monkeypatch.delenv("WORKSPACE", raising=False)
25+
with pytest.raises(RuntimeError) as error:
26+
my_function.lambda_handler({}, {})
27+
assert "Required env variable WORKSPACE is not set" in str(error)
28+
29+
30+
def test_my_function():
31+
assert (
32+
my_function.lambda_handler({}, {})
33+
== "You have successfully called this lambda!"
34+
)

0 commit comments

Comments
 (0)