-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from MITLibraries/add-sentry-integration
Add sentry integration
- Loading branch information
Showing
6 changed files
with
67 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,32 @@ | ||
import json | ||
import logging | ||
import os | ||
|
||
import sentry_sdk | ||
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration | ||
|
||
logger = logging.getLogger(__name__) | ||
logger.setLevel(logging.DEBUG) | ||
|
||
env = os.getenv("WORKSPACE") | ||
if sentry_dsn := os.getenv("SENTRY_DSN"): | ||
sentry = sentry_sdk.init( | ||
dsn=sentry_dsn, | ||
environment=env, | ||
integrations=[ | ||
AwsLambdaIntegration(), | ||
], | ||
traces_sample_rate=1.0, | ||
) | ||
logger.info("Sentry DSN found, exceptions will be sent to Sentry with env=%s", env) | ||
else: | ||
logger.info("No Sentry DSN found, exceptions will not be sent to Sentry") | ||
|
||
|
||
def lambda_handler(event: dict, context: object) -> str: # noqa | ||
if not os.getenv("WORKSPACE"): | ||
raise RuntimeError("Required env variable WORKSPACE is not set") | ||
|
||
logger.debug(json.dumps(event)) | ||
|
||
return "You have successfully called this lambda!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from importlib import reload | ||
|
||
import pytest | ||
|
||
from lambdas import my_function | ||
|
||
|
||
def test_my_function_configures_sentry_if_dsn_present(caplog, monkeypatch): | ||
monkeypatch.setenv("SENTRY_DSN", "https://[email protected]/123456") | ||
reload(my_function) | ||
assert ( | ||
"Sentry DSN found, exceptions will be sent to Sentry with env=test" | ||
in caplog.text | ||
) | ||
|
||
|
||
def test_my_function_doesnt_configure_sentry_if_dsn_not_present(caplog, monkeypatch): | ||
monkeypatch.delenv("SENTRY_DSN", raising=False) | ||
reload(my_function) | ||
assert "No Sentry DSN found, exceptions will not be sent to Sentry" in caplog.text | ||
|
||
|
||
def test_lambda_handler_missing_workspace_env_raises_error(monkeypatch): | ||
monkeypatch.delenv("WORKSPACE", raising=False) | ||
with pytest.raises(RuntimeError) as error: | ||
my_function.lambda_handler({}, {}) | ||
assert "Required env variable WORKSPACE is not set" in str(error) | ||
|
||
|
||
def test_my_function(): | ||
assert ( | ||
my_function.lambda_handler({}, {}) | ||
== "You have successfully called this lambda!" | ||
) |