Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ FROM public.ecr.aws/lambda/python:3.9

# Copy function code
COPY . ${LAMBDA_TASK_ROOT}/
COPY . ${LAMBDA_TASK_ROOT}/

# Install dependencies
RUN pip3 install pipenv
Expand Down
31 changes: 23 additions & 8 deletions ppod.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
import os
import tarfile
Expand All @@ -8,17 +9,31 @@
import sentry_sdk
import smart_open
from boto3 import client
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

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) -> dict:
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
env = os.environ["WORKSPACE"]
if sentry_dsn := os.getenv("SENTRY_DSN"):
sentry_sdk.init(sentry_dsn, environment=env)
logger.info(
"Sentry DSN found, exceptions will be sent to Sentry with env=%s", env
)
logger.debug(json.dumps(event))

if not os.getenv("WORKSPACE"):
raise RuntimeError("Required env variable WORKSPACE is not set")

bucket = os.environ["BUCKET"]
ssm_client = client("ssm", region_name="us-east-1")
Expand Down
5 changes: 4 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ ignore_missing_imports = True
ignore_missing_imports = True

[mypy-smart_open.*]
ignore_missing_imports = True
ignore_missing_imports = True

[tool:pytest]
log_level = INFO
26 changes: 14 additions & 12 deletions test_ppod.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import logging
from importlib import reload

import pytest
import requests

import ppod
from ppod import (
add_namespaces_to_alma_marcxml,
extract_files_from_tar,
Expand All @@ -12,25 +13,26 @@
)


def test_ppod_configures_sentry_if_dsn_present(
caplog, monkeypatch, request_data_matching_file
):
def test_ppod_configures_sentry_if_dsn_present(caplog, monkeypatch):
monkeypatch.setenv("SENTRY_DSN", "https://1234567890@00000.ingest.sentry.io/123456")
caplog.set_level(logging.INFO)
lambda_handler(request_data_matching_file, {})
reload(ppod)
assert (
"Sentry DSN found, exceptions will be sent to Sentry with env=test"
in caplog.text
)


def test_ppod_doesnt_configure_sentry_if_dsn_not_present(
caplog, monkeypatch, request_data_matching_file
):
def test_ppod_doesnt_configure_sentry_if_dsn_not_present(caplog, monkeypatch):
monkeypatch.delenv("SENTRY_DSN", raising=False)
caplog.set_level(logging.INFO)
lambda_handler(request_data_matching_file, {})
assert "Sentry DSN found" not in caplog.text
reload(ppod)
assert "No Sentry DSN found, exceptions will not be sent to Sentry" in caplog.text


def test_ppod_missing_workspace_env_raises_error(monkeypatch):
monkeypatch.delenv("WORKSPACE", raising=False)
with pytest.raises(RuntimeError) as e:
lambda_handler({}, {})
assert "Required env variable WORKSPACE is not set" in str(e)


def test_ppod_matching_files(request_data_matching_file):
Expand Down