Error tracking
The orchestrator-core
supports Sentry for
error tracking and performance monitoring. Sentry is an application monitoring
platform that helps developers identify, debug, and resolve issues in their applications by
providing real-time error tracking, performance monitoring, and distributed tracing capabilities.
In order to initialize Sentry (assuming you have already set up a Sentry project), perform the following steps:
1. Update your own Settings
class
Add the following attributes to the Settings
object in your own orchestrator's settings.py
:
TRACING_ENABLED: bool = False
SENTRY_DSN: str = ""
TRACE_SAMPLE_RATE: float = 0.1
# settings.py
from pydantic_settings import BaseSettings
class MySettings(BaseSettings):
TRACING_ENABLED: bool = False
SENTRY_DSN: str = ""
TRACE_SAMPLE_RATE: float = 0.1
my_settings = MySettings()
2. Set environment variables
TRACING_ENABLED=True
SENTRY_DSN = "your_sentry_dsn" # should be obtained from Sentry
TRACE_SAMPLE_RATE = 0.1 # should be a float between 0 and 1
TRACING_ENABLED
to True
will enable tracing for the application, allowing you to monitor
performance and errors more effectively.
Note
- SENTRY_DSN: The Data Source Name (DSN) is a unique URL provided by Sentry. It connects your application to your Sentry project so errors and performance data are sent to the correct place.
- TRACE_SAMPLE_RATE: A float between 0 and 1 that controls what percentage of transactions are sent to Sentry for performance monitoring (e.g., 0.5 means 50% of traces are sampled). See Sentry documentation for more details on sampling.
3. Update main.py
file
Add the following code to the main.py
file of the orchestrator-core
application:
from orchestrator import OrchestratorCore
from orchestrator.cli.main import app as core_cli
from orchestrator.settings import AppSettings
from my_orchestrator.settings import my_settings
app = OrchestratorCore(base_settings=AppSettings())
if app.base_settings.TRACING_ENABLED and app.base_settings.ENVIRONMENT != "local":
from orchestrator.app import sentry_integrations
from sentry_sdk.integrations.httpx import HttpxIntegration
sentry_integrations.append(HttpxIntegration())
app.add_sentry(
my_settings.SENTRY_DSN,
my_settings.TRACE_SAMPLE_RATE,
app.base_settings.SERVICE_NAME,
app.base_settings.ENVIRONMENT,
)
if __name__ == "__main__":
core_cli()
Note
- It's recommended to use separate Sentry projects for different environments (production, staging, etc.) to avoid cluttering production error tracking with development errors. Many teams skip Sentry integration in local development environments.
- Make sure to adjust the
TRACE_SAMPLE_RATE
according to your needs and test the integration in a development environment before deploying it to production.
4. Test the Sentry integration
You can test the Sentry integration by triggering an error in your application. For example, you can create a route that raises an exception when the endpoint is triggered:
from fastapi import FastAPI
app = FastAPI()
@app.get("/debug-sentry")
def debug_sentry():
division_by_zero = 1 / 0
Warning
Make sure your environment variable ENVIRONMENT
is NOT set to local
when testing the Sentry integration.
See also