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
33 changes: 6 additions & 27 deletions src/thumbnailer/lambda.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from logging import debug, info
from logging import debug, info, warn
from os import environ
from os.path import splitext
from tempfile import NamedTemporaryFile

from botocore.errorfactory import ClientError
from sentry_sdk import configure_scope

from thumbnailer import version
from thumbnailer.image import resize
from thumbnailer.responses import (
generate_binary_response,
Expand All @@ -14,15 +14,11 @@
generate_version_response,
)
from thumbnailer.s3 import KeyNotFound, download_file_from_s3, upload_file_to_s3
from thumbnailer.util import configure_logging, DEFAULT_WIDTH, DEFAULT_HEIGHT
from sentry_sdk import init, configure_scope
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration
from thumbnailer.util import init_monitoring, configure_logging, DEFAULT_WIDTH, DEFAULT_HEIGHT


MEDIA_BUCKET_ENV_KEY = 'MEDIA_UPLOAD_BUCKET_NAME'
THUMBS_BUCKET_ENV_KEY = 'MEDIA_THUMBS_BUCKET_NAME'
MONITORING_DSN = 'SENTRY_DSN'
OPERATING_ENV = 'OPERATING_ENV'


def parse_path(path):
Expand Down Expand Up @@ -70,29 +66,11 @@ def setup_verbose_logging(evt):
configure_logging(verboseLogging)


def init_monitoring():
dsn = environ.get(MONITORING_DSN)
env = environ.get(OPERATING_ENV)

if not dsn:
return

init(
dsn=dsn,
integrations=[AwsLambdaIntegration()],
release=f'v{version}',
send_default_pii=False,
traces_sample_rate=0.50,
environment=env,
_experiments={'auto_enabling_integrations': True},
)

init_monitoring()

def handler(event, context):
setup_verbose_logging(event)

init_monitoring()

debug(event)

url_path = event.get('path')
Expand All @@ -101,7 +79,8 @@ def handler(event, context):
scope.set_extra("thumbnail_event", event)

if environ.get('TRIGGER_ERROR'):
1 / 0
scope.set_tag('TRIGGER_ERROR', environ.get('TRIGGER_ERROR'))
raise Exception('test event')

if not url_path:
return generate_json_respone(400, f'url path not set')
Expand Down
31 changes: 30 additions & 1 deletion src/thumbnailer/util.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
from logging import getLogger, basicConfig, DEBUG, INFO, debug, info
from os import environ
from logging import getLogger, basicConfig, DEBUG, INFO, debug, info, warning

from sentry_sdk import init
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration

from thumbnailer import version


DEFAULT_WIDTH = 222
DEFAULT_HEIGHT = 222
MONITORING_DSN = 'SENTRY_DSN'
OPERATING_ENV = 'OPERATING_ENV'


def configure_logging(verbose):
Expand All @@ -22,3 +30,24 @@ def configure_logging(verbose):
datefmt='%Y/%m/%d %H:%M:%S',
level=level,
)


def init_monitoring():
dsn = environ.get(MONITORING_DSN)
env = environ.get(OPERATING_ENV)

if not dsn:
warning(f'DSN not found in envronment under key {MONITORING_DSN}')
return

info(f'Configuring monitoring via DSN: {dsn}')
init(
dsn=dsn,
integrations=[AwsLambdaIntegration()],
release=f'v{version}',
send_default_pii=False,
traces_sample_rate=0.50,
environment=env,
_experiments={'auto_enabling_integrations': True},
)