Skip to content

Commit

Permalink
🔧(project) improve sentry integration
Browse files Browse the repository at this point in the history
Sentry configuration is now documented.
We can now ignore health check routes of the API to avoid pollution.
  • Loading branch information
wilbrdt committed May 17, 2023
1 parent 07999c9 commit a683344
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ RALPH_BACKENDS__DATABASE__CLICKHOUSE__TEST_TABLE_NAME=test_xapi_events_all

# RALPH_SENTRY_DSN=
# RALPH_EXECUTION_ENVIRONMENT=development
# RALPH_SENTRY_CLI_TRACES_SAMPLE_RATE=0.1
# RALPH_SENTRY_LRS_TRACES_SAMPLE_RATE=0.1
# RALPH_SENTRY_IGNORE_HEALTH_CHECKS=True

# Converter

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to
as per the xAPI specification
- Use batch/v1 api in cronjob_pipeline manifest
- Use autoscaling/v2 in HorizontalPodAutoscaler manifest
- Add the ability to ignore health check routes for Sentry transactions

### Fixed

Expand Down
24 changes: 24 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,27 @@ RALPH_XAPI_FORWARDINGS='
]
'
```

### Sentry configuration

Ralph provides Sentry integration to monitor its LRS server and its CLI.
To activate Sentry integration, one should define the following environment variables:

```bash
RALPH_SENTRY_DSN={PROTOCOL}://{PUBLIC_KEY}:{SECRET_KEY}@{HOST}{PATH}/{PROJECT_ID}
RALPH_EXECUTION_ENVIRONMENT=development
```

The Sentry DSN (Data Source Name) can be found in your project settings from Sentry application. The execution environment should reflect the environment Ralph has been deployed in (_e.g._ `production`).

You may also want to [monitor the performance](https://develop.sentry.dev/sdk/performance/) of Ralph by configuring the CLI and LRS traces sample rates:
```bash
RALPH_SENTRY_CLI_TRACES_SAMPLE_RATE=0.1
RALPH_SENTRY_LRS_TRACES_SAMPLE_RATE=0.3
```
> Note that a sampling rate of `1.0` means 100% of transactions are sent to sentry and `0.1` only 10%.
If you want to lower noisy transactions (_e.g._ in a Kubernetes cluster), you can disable health checks related ones:
```bash
RALPH_SENTRY_IGNORE_HEALTH_CHECKS=True
```
2 changes: 2 additions & 0 deletions src/helm/ralph/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ spec:
value: "/app/.ralph"
- name: RALPH_AUTH_FILE
value: "/var/run/ralph/auth.json"
- name: RALPH_SENTRY_IGNORE_HEALTH_CHECKS
value: "{{ .Values.ralph.sentry_ignore_health_checks }}"
- name: RALPH_BACKENDS__DATABASE__ES__CLIENT_OPTIONS__ca_certs
value: "/usr/local/share/ca-certificates/es-cluster.pem"
envFrom:
Expand Down
1 change: 1 addition & 0 deletions src/helm/ralph/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ ralph:
prefix: "traefik"
acme_enabled_route_prefix: "traefik.acme"
secret_name: "secret-ralph"
sentry_ignore_health_checks: false
volume_history:
size: 2Gi
access_modes: ReadWriteMany
Expand Down
21 changes: 21 additions & 0 deletions src/ralph/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""Main module for Ralph's LRS API."""
from functools import lru_cache
from urllib.parse import urlparse

import sentry_sdk
from fastapi import Depends, FastAPI

Expand All @@ -8,13 +11,31 @@
from .auth import AuthenticatedUser, authenticated_user
from .routers import health, statements


@lru_cache(maxsize=None)
def get_health_check_routes():
"""Return the health check routes."""
return [route.path for route in health.router.routes]


def filter_transactions(event, hint): # pylint: disable=unused-argument
"""Filter transactions for Sentry."""
url = urlparse(event["request"]["url"])

if settings.SENTRY_IGNORE_HEALTH_CHECKS and url.path in get_health_check_routes():
return None

return event


if settings.SENTRY_DSN is not None:
sentry_sdk.init(
dsn=settings.SENTRY_DSN,
traces_sample_rate=settings.SENTRY_LRS_TRACES_SAMPLE_RATE,
release=__version__,
environment=settings.EXECUTION_ENVIRONMENT,
max_breadcrumbs=50,
before_send_transaction=filter_transactions,
)

app = FastAPI()
Expand Down
5 changes: 3 additions & 2 deletions src/ralph/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,10 @@ class Config(BaseSettingsConfig):
RUNSERVER_MAX_SEARCH_HITS_COUNT: int = 100
RUNSERVER_POINT_IN_TIME_KEEP_ALIVE: str = "1m"
RUNSERVER_PORT: int = 8100
SENTRY_CLI_TRACES_SAMPLE_RATE: float = 1.0
SENTRY_DSN: str = None
SENTRY_CLI_TRACES_SAMPLE_RATE = 1.0
SENTRY_LRS_TRACES_SAMPLE_RATE = 0.1
SENTRY_IGNORE_HEALTH_CHECKS: bool = False
SENTRY_LRS_TRACES_SAMPLE_RATE: float = 1.0
XAPI_FORWARDINGS: List[XapiForwardingConfigurationSettings] = []

@property
Expand Down
2 changes: 2 additions & 0 deletions src/tray/templates/services/app/deploy.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ spec:
value: "/app/.ralph"
- name: RALPH_AUTH_FILE
value: "/var/run/ralph/auth.json"
- name: RALPH_SENTRY_IGNORE_HEALTH_CHECKS
value: "{{ ralph_sentry_ignore_health_checks }}"
{% if ralph_mount_es_ca_secret %}
- name: RALPH_BACKENDS__DATABASE__ES__CLIENT_OPTIONS__ca_certs
value: "/usr/local/share/ca-certificates/es-cluster.pem"
Expand Down
1 change: 1 addition & 0 deletions src/tray/vars/all/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ ralph_lrs_command:
- "8000"
ralph_lrs_auth_secret_name: "ralph-lrs-auth-{{ ralph_vault_checksum | default('undefined_ralph_vault_checksum') }}"
ralph_ingress_class_name: "{{ default_ingress_class_name }}"
ralph_sentry_ignore_health_checks: false

0 comments on commit a683344

Please sign in to comment.