feat(sdk): expose trace_content via Traceloop.init()#4195
Conversation
Adds a `trace_content: Optional[bool] = None` argument to `Traceloop.init()` that mirrors the `TRACELOOP_TRACE_CONTENT` environment variable. When omitted, the env var stays authoritative; when set to True/False, the argument overrides the env var so that applications can disable prompt/completion capture without relying on process-level environment configuration. Closes traceloop#137
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThis PR introduces a new Changestrace_content Feature
🎯 2 (Simple) | ⏱️ ~12 minutes
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Document the three non-obvious aspects of trace_content surfaced in review: (1) writing TRACELOOP_TRACE_CONTENT is process-wide and inherited by subprocesses, (2) the override is sticky across subsequent init() calls with trace_content=None, (3) per-span override via override_enable_content_tracing still works because instrumentations treat env and context as OR. Adds a test pinning (2) so a future save/restore refactor would force a docs update. Assisted-by: Claude Opus 4.7
…test fixture Address self-review nits on the trace_content init arg: * Docstring: clarify that the env override is not applied when enabled=False or tracing is disabled via TRACELOOP_TRACING_ENABLED, since init() returns early before the write would happen. Pins the documented surface to the actual code path. * Test fixture: clear TRACELOOP_TRACE_CONTENT before yielding (not just on teardown) so trace_content tests start from a known-unset state. A CI env that pre-sets the var would otherwise mask bugs in tests that exercise the "env not set" default path. No behavior change. Assisted-by: Claude Opus 4.7
Summary
Adds a
trace_content: Optional[bool] = Noneargument toTraceloop.init()that mirrors the existingTRACELOOP_TRACE_CONTENTenvironment variable, addressing #137 (open since 2023).trace_content=None(default): theTRACELOOP_TRACE_CONTENTenv var stays authoritative — no behavior change for existing deployments.trace_content=True/False: the argument explicitly overrides the env var so applications can toggle prompt/completion capture without relying on process-level configuration.The implementation sets
os.environ["TRACELOOP_TRACE_CONTENT"]before any instrumentor initializes, so every bundled instrumentation that reads the env var (bedrock, watsonx, alephalpha, writer, …) picks up the value transparently without further changes.Design notes
use_attributes/use_legacy_attributes, which also useOptional[bool] = Noneto signal "not explicitly set").Behavior to be aware of
os.environ["TRACELOOP_TRACE_CONTENT"], child processes that inherit the env and any other code reading that variable will see the override too. This is the same surface area the existing env var already has — the arg just adds a programmatic entry point.trace_content=True/Falsehas been passed, a laterTraceloop.init(trace_content=None)will not restore the previous env-driven value — the most recent explicit setting keeps winning until the env var is changed externally. Fine for the typical "init once at startup" flow; documented in the docstring.trace_content=Falsedoes not block the existingoverride_enable_content_tracingOTel context value — instrumentations treat env and context as OR, so individual spans can opt back in while the global default stays off. This preserves the current semantics intentionally.Test plan
test_trace_content_false_disables_content_tracing— explicit False overrides env=truetest_trace_content_true_overrides_env— explicit True overrides env=falsetest_trace_content_none_honors_env— omitting the arg preserves env-driven behaviortest_sdk_initialization.py+test_privacy_no_prompts.py(32 tests) still passnx run traceloop-sdk:lintpassesCloses #137
Summary by CodeRabbit
Release Notes
trace_contentparameter to SDK initialization for controlling sensitive data capture in distributed traces. Once configured, the setting persists across subsequent initialization calls, ensuring consistent tracing behavior throughout the application lifecycle. Individual spans can still override the global configuration as needed.