Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/traceloop-sdk/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,34 @@ def on_start(self, span, parent_context=None):
TracerWrapper.instance = _trace_wrapper_instance


@pytest.fixture
def exporter_with_trace_content_override():
# Clear singleton if existed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initialize _trace_wrapper_instance to None (e.g. before the if-check) so that the teardown code doesn’t raise a NameError if the attribute isn’t present.

Suggested change
# Clear singleton if existed
_trace_wrapper_instance = None

_trace_wrapper_instance = None
if hasattr(TracerWrapper, "instance"):
_trace_wrapper_instance = TracerWrapper.instance
del TracerWrapper.instance

# Set environment variable to false
os.environ["TRACELOOP_TRACE_CONTENT"] = "false"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Preserve the original environment variable value.

The fixture unconditionally deletes TRACELOOP_TRACE_CONTENT in cleanup, which could raise KeyError if the test deletes it, or could lose a pre-existing value set outside the test.

Apply this diff:

+    _original_trace_content = os.environ.get("TRACELOOP_TRACE_CONTENT")
     # Set environment variable to false
     os.environ["TRACELOOP_TRACE_CONTENT"] = "false"
     
     ...
     
     # Clean up environment
-    del os.environ["TRACELOOP_TRACE_CONTENT"]
+    if _original_trace_content is not None:
+        os.environ["TRACELOOP_TRACE_CONTENT"] = _original_trace_content
+    else:
+        os.environ.pop("TRACELOOP_TRACE_CONTENT", None)

Also applies to: 250-250

🤖 Prompt for AI Agents
In packages/traceloop-sdk/tests/conftest.py around lines 234 and 250, the
fixture unconditionally deletes TRACELOOP_TRACE_CONTENT in cleanup which can
raise KeyError or clobber an existing external value; capture the original
os.environ.get("TRACELOOP_TRACE_CONTENT") before setting it, and in teardown
restore it (if original is None remove safely using
os.environ.pop("TRACELOOP_TRACE_CONTENT", None) or if original had a value set
it back) so the environment is restored without raising errors or losing
pre-existing values.


# Create exporter with trace_content=True to override env var
exporter = InMemorySpanExporter()
Traceloop.init(
exporter=exporter,
disable_batch=True,
trace_content=True # This should override the env var
)

yield exporter

# Restore singleton if any
if _trace_wrapper_instance:
TracerWrapper.instance = _trace_wrapper_instance
# Clean up environment
del os.environ["TRACELOOP_TRACE_CONTENT"]


@pytest.fixture
def datasets():
"""Create a Datasets instance with HTTP client for VCR recording/playback"""
Expand Down
3 changes: 2 additions & 1 deletion packages/traceloop-sdk/traceloop/sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def init(
sampler: Optional[Sampler] = None,
traceloop_sync_enabled: bool = False,
should_enrich_metrics: bool = True,
trace_content: bool = True,
resource_attributes: dict = {},
instruments: Optional[Set[Instruments]] = None,
block_instruments: Optional[Set[Instruments]] = None,
Expand Down Expand Up @@ -94,7 +95,7 @@ def init(
print(Fore.YELLOW + "Tracing is disabled" + Fore.RESET)
return

enable_content_tracing = is_content_tracing_enabled()
enable_content_tracing = trace_content or is_content_tracing_enabled()

if exporter or processor:
print(Fore.GREEN + "Traceloop exporting traces to a custom exporter")
Expand Down