Skip to content

Commit

Permalink
Attach build and environment info to Sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-z committed Jan 21, 2024
1 parent 700f30c commit 67e2cfd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/push-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ jobs:
with:
context: .
push: true
build-args: |
DOCKER_METADATA_OUTPUT_JSON
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
FROM python:3.11.5-slim

# Pass information about the build to the container
ARG DOCKER_METADATA_OUTPUT_JSON='{}'
ENV DOCKER_METADATA_OUTPUT_JSON=${DOCKER_METADATA_OUTPUT_JSON}

COPY requirements.txt /app/
WORKDIR /app

Expand Down
55 changes: 42 additions & 13 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,57 @@
LOG_LEVEL = os.environ.get("LOG_LEVEL", "DEBUG")
PORT = os.environ.get("PORT", 5000)
HOST = os.environ.get("HOST", "0.0.0.0")
SENTRY_DSN = os.environ.get("SENTRY_DSN", "")

logging.basicConfig(level=LOG_LEVEL)
if SENTRY_DSN:
logging.info("Sentry DSN provided, enabling Sentry SDK")

# BUILD_INFO is generated by the build pipeline (e.g. docker/metadata-action).
# It looks like:
# {"tags":["ghcr.io/watonomous/repo-ingestion:main"],"labels":{"org.opencontainers.image.title":"repo-ingestion","org.opencontainers.image.description":"Simple server to receive file changes and open GitHub pull requests","org.opencontainers.image.url":"https://github.com/WATonomous/repo-ingestion","org.opencontainers.image.source":"https://github.com/WATonomous/repo-ingestion","org.opencontainers.image.version":"main","org.opencontainers.image.created":"2024-01-20T16:10:39.421Z","org.opencontainers.image.revision":"1d55b62b15c78251e0560af9e97927591e260a98","org.opencontainers.image.licenses":""}}
BUILD_INFO=json.loads(os.getenv("DOCKER_METADATA_OUTPUT_JSON", "{}"))


# Set up Sentry
if os.getenv("SENTRY_DSN"):
build_labels = BUILD_INFO.get("labels", {})
image_title = build_labels.get("org.opencontainers.image.title", "unknown_image")
image_version = build_labels.get("org.opencontainers.image.version", "unknown_version")
image_rev = build_labels.get("org.opencontainers.image.revision", "unknown_rev")

sentry_config = {
"dsn": os.getenv("SENTRY_DSN"),
"environment": os.getenv("SENTRY_ENVIRONMENT", "unknown"),
"release": os.getenv("SENTRY_RELEASE", f'{image_title}:{image_version}@{image_rev}'),
}

logging.info(f"Sentry DSN found. Setting up Sentry with config: {sentry_config}")

sentry_logging = LoggingIntegration(
level=logging.INFO, # Capture info and above as breadcrumbs
event_level=logging.ERROR # Send errors as events
)

def sentry_traces_sampler(sampling_context):
# Inherit parent sampling decision
if sampling_context["parent_sampled"] is not None:
return sampling_context["parent_sampled"]

# Don't need to sample health checks
if sampling_context.get("asgi_scope", {}).get("path", "").startswith("/health"):
return 0

# Sample everything else
return 1

sentry_sdk.init(
dsn=SENTRY_DSN,
integrations=[
sentry_logging,
],

# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
# We recommend adjusting this value in production,
traces_sample_rate=1.0,
**sentry_config,
integrations=[sentry_logging],

traces_sampler=sentry_traces_sampler,

enable_tracing=True,
)
else:
logging.info("No Sentry DSN provided, Sentry SDK disabled")
logging.info("No Sentry DSN found. Skipping Sentry setup.")

def split_dsn(dsn):
"""
Expand Down

0 comments on commit 67e2cfd

Please sign in to comment.