Skip to content

Commit

Permalink
✨(project) integrate Sentry
Browse files Browse the repository at this point in the history
This is a requirement to track production issues.
  • Loading branch information
jmaupetit committed Jul 4, 2024
1 parent b8cfda9 commit efcc6c2
Showing 6 changed files with 110 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -8,6 +8,10 @@ and this project adheres to

## [Unreleased]

### Added

- Integrate Sentry

### Fixed

- Fix documentation typos
24 changes: 24 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
@@ -147,6 +147,30 @@ Default: `None` (required)

---

#### `EXECUTION_ENVIRONMENT`

Used by [Sentry](https://sentry.io/) to track the environment of raised issue.

Default: `None`

---

#### `SENTRY_DSN`

The DSN of your Sentry project, _e.g._ `https://account@sentry.io/project_id`. When not set, Sentry integration is not active.

Default: `None`

---

#### `SENTRY_TRACES_SAMPLE_RATE`

The sample rate of traces sent to sentry: 1.0 means 100% while 0.1 means 10%.

Default: `1.0`

---

### `.secrets.yaml`

---
55 changes: 53 additions & 2 deletions poetry.lock

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

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ python = "^3.11"
starlette = "^0.37.2"
typer = "^0.12.3"
uvicorn = {extras = ["standard"], version = "^0.30.1"}
sentry-sdk = {extras = ["starlette"], version = "^2.7.1"}

[tool.poetry.group.dev.dependencies]
black = "^24.4.2"
16 changes: 16 additions & 0 deletions src/data7/app.py
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

import contextlib
import csv
import importlib.metadata
import logging
from dataclasses import dataclass
from enum import StrEnum
@@ -11,7 +12,9 @@

import databases
import pyarrow as pa
import sentry_sdk
from pyarrow import parquet as pq
from sentry_sdk.integrations.starlette import StarletteIntegration
from starlette.applications import Starlette
from starlette.exceptions import HTTPException
from starlette.middleware import Middleware
@@ -213,6 +216,19 @@ async def lifespan(app):
"""Application lifespan."""
await database.connect()
app.state.datasets = await populate_datasets()

if settings.SENTRY_DSN is not None:
sentry_sdk.init(
dsn=str(settings.SENTRY_DSN),
enable_tracing=True,
traces_sample_rate=settings.SENTRY_TRACES_SAMPLE_RATE,
release=importlib.metadata.version("data7"),
environment=settings.EXECUTION_ENVIRONMENT,
integrations=[
StarletteIntegration(),
],
)

yield
await database.disconnect()

12 changes: 12 additions & 0 deletions src/data7/settings.yaml.dist
Original file line number Diff line number Diff line change
@@ -19,20 +19,31 @@ default:
# host:
# port:

# Sentry
sentry_dsn: null
sentry_traces_sample_rate: 1.0

# ---- PRODUCTION ------------------------------
production:
execution_environment: production

# Set debug to true for development, never for production!
debug: false

# Server
# host: data7.example.com
# port: 8080

# Sentry
# sentry_dsn:
# sentry_traces_sample_rate: 1.0

#
# /!\ FEEL FREE TO REMOVE ENVIRONMENTS BELOW /!\
#
# ---- DEVELOPMENT -----------------------------
development:
execution_environment: development
debug: true

# Server
@@ -41,3 +52,4 @@ development:

# ---- TESTING ---------------------------------
testing:
execution_environment: testing

0 comments on commit efcc6c2

Please sign in to comment.