authentik.lib.tracing
authentik error reporting: one active tracer handles spans/tags, but every configured backend gets exceptions
1"""authentik error reporting: one active tracer handles spans/tags, but every 2configured backend gets exceptions""" 3 4from authentik.lib.config import CONFIG 5from authentik.lib.tracing.common import ( 6 TRACER_DEFER_POSTFORK_ENV_VAR as TRACER_DEFER_POSTFORK_ENV_VAR, 7) 8from authentik.lib.tracing.common import Span as Span 9from authentik.lib.tracing.common import Tracer 10from authentik.lib.tracing.exceptions import TracingIgnoredException as TracingIgnoredException 11from authentik.lib.tracing.exceptions import should_ignore_exception as should_ignore_exception 12 13_enabled = CONFIG.get_bool("error_reporting.enabled", False) 14# Backends are only imported when configured, as their SDKs are heavy 15_otel_tracer: Tracer | None = None 16if _enabled and CONFIG.get("error_reporting.otel_endpoint"): 17 from authentik.lib.tracing.otel import OpenTelemetryTracer 18 19 _otel_tracer = OpenTelemetryTracer() 20# Every configured backend gets exceptions; only one of them handles spans/tags, since 21# authentik only ever needs one trace tree and picking one avoids double request overhead. 22# With both configured, Sentry only reports errors and OTel does all tracing 23_sentry_tracer: Tracer | None = None 24if _enabled and CONFIG.get("error_reporting.sentry_dsn"): 25 from authentik.lib.tracing.sentry import SentryTracer 26 27 _sentry_tracer = SentryTracer(tracing=_otel_tracer is None) 28_error_tracers: list[Tracer] = [t for t in (_otel_tracer, _sentry_tracer) if t is not None] 29_active_tracer: Tracer = _otel_tracer or _sentry_tracer or Tracer() 30 31 32def sentry_enabled() -> bool: 33 """Check if Sentry is configured""" 34 return _sentry_tracer is not None 35 36 37def active_tracer() -> Tracer: 38 """Get the tracer used for spans, tags, and trace-context propagation headers""" 39 return _active_tracer 40 41 42def setup_pre_fork() -> None: 43 """Initialize every configured backend before any fork""" 44 for backend in _error_tracers: 45 backend.setup_pre_fork() 46 47 48def setup_post_fork() -> None: 49 """Finish initializing every configured backend after a fork""" 50 for backend in _error_tracers: 51 backend.setup_post_fork() 52 53 54def init() -> None: 55 """Full init for single-process entrypoints that never fork afterwards. Gunicorn's 56 preloaded web server calls setup_pre_fork()/setup_post_fork() separately instead""" 57 setup_pre_fork() 58 setup_post_fork() 59 60 61def record_exception(exc: Exception) -> None: 62 """Record an exception on every configured backend""" 63 for backend in _error_tracers: 64 backend.record_exception(exc)
def
sentry_enabled() -> bool:
33def sentry_enabled() -> bool: 34 """Check if Sentry is configured""" 35 return _sentry_tracer is not None
Check if Sentry is configured
38def active_tracer() -> Tracer: 39 """Get the tracer used for spans, tags, and trace-context propagation headers""" 40 return _active_tracer
Get the tracer used for spans, tags, and trace-context propagation headers
def
setup_pre_fork() -> None:
43def setup_pre_fork() -> None: 44 """Initialize every configured backend before any fork""" 45 for backend in _error_tracers: 46 backend.setup_pre_fork()
Initialize every configured backend before any fork
def
setup_post_fork() -> None:
49def setup_post_fork() -> None: 50 """Finish initializing every configured backend after a fork""" 51 for backend in _error_tracers: 52 backend.setup_post_fork()
Finish initializing every configured backend after a fork
def
init() -> None:
55def init() -> None: 56 """Full init for single-process entrypoints that never fork afterwards. Gunicorn's 57 preloaded web server calls setup_pre_fork()/setup_post_fork() separately instead""" 58 setup_pre_fork() 59 setup_post_fork()
Full init for single-process entrypoints that never fork afterwards. Gunicorn's preloaded web server calls setup_pre_fork()/setup_post_fork() separately instead
def
record_exception(exc: Exception) -> None:
62def record_exception(exc: Exception) -> None: 63 """Record an exception on every configured backend""" 64 for backend in _error_tracers: 65 backend.record_exception(exc)
Record an exception on every configured backend