Skip to content

Commit

Permalink
Add Sentry support
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchrisadams committed Nov 8, 2024
1 parent 8235f3a commit 8409923
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
17 changes: 17 additions & 0 deletions docs/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,20 @@ Django version 5.1.3, using settings 'local_config'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
```


### Monitoring and tracking errors with Sentry

The Django application served by the carbon.txt validator is set up to support instrumentation with Sentry.

To activate it, you need to declare the `SENTRY_DSN`, `SENTRY_TRACE_SAMPLE_RATE` and `SENTRY_PROFILE_SAMPLE_RATE` environment variables when running the web server.

The easiest way to do this is declare them in an .env file like so:

```
# .env
SENTRY_DSN="https://LONG_ALPHANUMERIC_STRING.ingest.de.sentry.io/NUMERIC_ID"
SENTRY_TRACE_SAMPLE_RATE="1.0"
SENTRY_PROFILE_SAMPLE_RATE="1.0"
```
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "carbon-txt"
version = "0.0.6"
version = "0.0.7"
description = "A command line tool containing a validator for carbon.txt files, by the Green Web Foundation"
authors = [
{ name = "Chris Adams" },
Expand All @@ -19,6 +19,7 @@ dependencies = [
"granian>=1.6.2",
"httpx>=0.27.2",
"rich>=13.9.2",
"sentry-sdk[django]>=2.18.0",
# faster, but not yet supported by python 3.13 yet
# "rtoml>=0.11.0",
"typer>=0.12.5",
Expand Down
23 changes: 20 additions & 3 deletions src/carbon_txt/web/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
https://docs.djangoproject.com/en/5.1/ref/settings/
"""

import pathlib
import environ
import logging
import os
import pathlib

import logging
import environ
import sentry_sdk

from carbon_txt.exceptions import InsecureKeyException

Expand All @@ -30,6 +31,9 @@
DEBUG=(bool, False),
SECRET_KEY=(str, os.getenv("SECRET_KEY", DEFAULT_SECRET_KEY)),
ENV_PATH=(str, ".env"),
SENTRY_DSN=(str, ""),
SENTRY_TRACE_SAMPLE_RATE=(float, 0),
SENTRY_PROFILE_SAMPLE_RATE=(float, 0),
)

# fetch environment variables from .env file
Expand Down Expand Up @@ -162,3 +166,16 @@
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"


if env("SENTRY_DSN"):
sentry_sdk.init(
dsn=env("SENTRY_DSN"),
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for tracing.
traces_sample_rate=env("SENTRY_TRACE_SAMPLE_RATE"),
# Set profiles_sample_rate to 1.0 to profile 100%
# of sampled transactions.
# We recommend adjusting this value in production.
profiles_sample_rate=env("SENTRY_PROFILE_SAMPLE_RATE"),
)
22 changes: 21 additions & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8409923

Please sign in to comment.