2020
2121from __future__ import annotations
2222
23+ from dataclasses import replace
2324from logging import getLogger
2425from typing import TYPE_CHECKING , Any
2526
@@ -52,8 +53,18 @@ class ObservabilityManager:
5253 config: App-wide backend selection by name. ``None`` selects nothing
5354 (concerns without an instance stay NullObject) — the default for a
5455 bare ``AppSpec``.
55- redactor: Cross-cutting sensitive-data filter threaded into BOTH the
56- logging and error-tracking sinks.
56+ redactor: Cross-cutting sensitive-data filter threaded into the
57+ logging, error-tracking and tracing sinks (every payload surface;
58+ metrics carry no payloads and are exempt).
59+ log_redactor: Per-surface override for the logging sink; wins over
60+ ``redactor`` there. Surfaces differ in volume by orders of
61+ magnitude — this is how a cheap masker goes on every log line
62+ while an expensive one guards only the error path.
63+ error_redactor: Per-surface override for the error-tracking sink.
64+ The natural home for ML-grade maskers: events are rare, shipped
65+ off the request path, and leak the most (stack-frame locals,
66+ request bodies).
67+ trace_redactor: Per-surface override for span attributes.
5768 metrics: Ready metrics sink instance (wins over ``config.metrics``).
5869 tracing: Ready tracing sink instance (wins over ``config.tracing``).
5970 error_tracking: Ready error-tracking sink instance (wins over
@@ -66,13 +77,21 @@ def __init__(
6677 config : ObsConfig | None = None ,
6778 * ,
6879 redactor : Redactor | None = None ,
80+ log_redactor : Redactor | None = None ,
81+ error_redactor : Redactor | None = None ,
82+ trace_redactor : Redactor | None = None ,
6983 metrics : MetricsSinkProtocol | None = None ,
7084 tracing : TracingSinkProtocol | None = None ,
7185 error_tracking : ErrorTrackingSinkProtocol | None = None ,
7286 logging : LoggingSinkProtocol | None = None ,
7387 ) -> None :
7488 self ._config = config
7589 self ._redactor = redactor
90+ self ._surface_redactors : dict [str , Redactor | None ] = {
91+ "logging" : log_redactor ,
92+ "error_tracking" : error_redactor ,
93+ "tracing" : trace_redactor ,
94+ }
7695 self ._configured = False
7796 # (concern, sink) pairs in setup order; sinks are registry-resolved and
7897 # therefore dynamically typed at this boundary.
@@ -143,7 +162,10 @@ def configure(self, settings: BaseServiceSettingsProtocol, *, service_name: str
143162 if backend is None or not gates [concern ](settings ):
144163 continue
145164 sink = resolve_sink (concern , backend )()
146- sink .setup (ctx )
165+ # Each sink sees the redactor resolved for its own surface: the
166+ # per-surface override, else the cross-cutting one. Metrics carry
167+ # no payloads, so their ctx never advertises a redactor.
168+ sink .setup (replace (ctx , redactor = self ._redactor_for (concern )))
147169 self ._active .append ((concern , sink ))
148170 setattr (self , f"_{ concern } " , sink )
149171 logger .info (
@@ -177,6 +199,12 @@ def shutdown(self) -> None:
177199 self ._logging = NullLoggingSink ()
178200 self ._configured = False
179201
202+ def _redactor_for (self , concern : str ) -> Redactor | None :
203+ if concern == "metrics" :
204+ return None
205+ override = self ._surface_redactors .get (concern )
206+ return override if override is not None else self ._redactor
207+
180208 def _build_setup_context (self , settings : BaseServiceSettingsProtocol , service_name : str ) -> ObsSetupContext :
181209 environment = getattr (settings , "environment" , None )
182210 if not environment :
0 commit comments