Skip to content

Commit

Permalink
Add environment and release to Sentry, and ignore healthcheck traces
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-z committed Jan 21, 2024
1 parent 801784a commit 2f04c75
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 23 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/push-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ on:
- main

env:
IMAGE_NAME: discord-provisioner-bot
IMAGE_REGISTRY: ghcr.io/${{ github.repository_owner }}
IMAGE_NAME: ${{ github.repository }}
IMAGE_REGISTRY: ghcr.io
REGISTRY_USER: ${{ github.actor }}
REGISTRY_PASSWORD: ${{ github.token }}

Expand Down 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 }}
labels: ${{ steps.meta.outputs.labels }}
8 changes: 7 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
FROM python:3.11-alpine3.18

RUN apk add curl

RUN mkdir /app
WORKDIR /app
COPY . /app
COPY requirements.txt /app/

RUN pip install -r /app/requirements.txt

COPY . /app/

HEALTHCHECK --interval=10s --timeout=5s --retries=3 CMD curl --fail http://localhost:8000/health || exit 1

CMD ["python3", "/app/main.py"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This is a simple Discord bot used to trigger WATO's infrastructure provisioning

```bash
docker build . -t discord-provisioner-bot
docker run -e DISCORD_TOKEN=<DISCORD_TOKEN> -e GITHUB_TOKEN=<GITHUB_TOKEN> -e SENTRY_DSN=<SENTRY_DSN> discord-provisioner-bot
docker run --rm -it -e DISCORD_TOKEN=<DISCORD_TOKEN> -e GITHUB_TOKEN=<GITHUB_TOKEN> -e SENTRY_DSN=<SENTRY_DSN> discord-provisioner-bot
```

## Deployment
Expand Down
72 changes: 54 additions & 18 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import aiohttp
import discord
import json
import logging
import os
import sentry_sdk
Expand All @@ -10,31 +11,66 @@
from time import perf_counter, sleep


sentry_logging = LoggingIntegration(
level=logging.INFO, # Capture info and above as breadcrumbs
event_level=logging.ERROR # Send errors as events
)
sentry_sdk.init(
dsn=os.environ["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,
)
# 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}'),
}

print(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
aiohttp_request = sampling_context.get("aiohttp_request")
if aiohttp_request is not None and aiohttp_request.path == "/health":
return 0

# Sample everything else
return 1

sentry_sdk.init(
**sentry_config,
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,
traces_sampler=sentry_traces_sampler,

enable_tracing=True,
)
else:
print("No Sentry DSN found. Skipping Sentry setup.")

logger = logging.getLogger('discord.wato-provisioner')

intents = discord.Intents.none()
intents.members = True

client = discord.Client(intents=intents)

# TODO: sentry integration for logging:
# https://docs.sentry.io/platforms/python/guides/logging/

@client.event
async def on_member_join(member):
logger.info(f'{member} has joined the server.')
Expand Down

0 comments on commit 2f04c75

Please sign in to comment.