Skip to content

Commit 985e803

Browse files
authored
Merge pull request #3 from MITLibraries/add-sentry-integration
Add sentry integration
2 parents c60c9ad + 5559212 commit 985e803

File tree

6 files changed

+67
-6
lines changed

6 files changed

+67
-6
lines changed

Pipfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ verify_ssl = true
44
name = "pypi"
55

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

89
[dev-packages]
910
bandit = "*"
1011
black = "*"
1112
coverage = "*"
1213
coveralls = "*"
1314
mypy = "*"
15+
pyflakes = "==2.4.0"
1416
pylama = {extras = ["all"], version = "*"}
1517
pytest = "*"
1618

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!"

setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@ max_line_length = 90
1111
[pylama:isort]
1212
profile = black
1313

14+
[pylama:pydocstyle]
15+
convention = pep257
16+
1417
[tool:pytest]
1518
log_level = DEBUG

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)