From e16097895edf75464b60b040af49576432a357d0 Mon Sep 17 00:00:00 2001 From: Ian Fukushima Date: Sun, 8 Dec 2024 12:21:06 -0300 Subject: [PATCH 1/2] feat: warn if LangfuseTracer initialized without tracing enabled --- .../haystack_integrations/tracing/langfuse/tracer.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/integrations/langfuse/src/haystack_integrations/tracing/langfuse/tracer.py b/integrations/langfuse/src/haystack_integrations/tracing/langfuse/tracer.py index d6f2535c7..6af05633e 100644 --- a/integrations/langfuse/src/haystack_integrations/tracing/langfuse/tracer.py +++ b/integrations/langfuse/src/haystack_integrations/tracing/langfuse/tracer.py @@ -7,7 +7,8 @@ from haystack import logging from haystack.components.generators.openai_utils import _convert_message_to_openai_format from haystack.dataclasses import ChatMessage -from haystack.tracing import Span, Tracer, tracer +from haystack.tracing import Span, Tracer +from haystack.tracing import tracer as proxy_tracer from haystack.tracing import utils as tracing_utils import langfuse @@ -78,7 +79,7 @@ def set_content_tag(self, key: str, value: Any) -> None: :param key: The content tag key. :param value: The content tag value. """ - if not tracer.is_content_tracing_enabled: + if not proxy_tracer.is_content_tracing_enabled: return if key.endswith(".input"): if "messages" in value: @@ -126,6 +127,12 @@ def __init__(self, tracer: "langfuse.Langfuse", name: str = "Haystack", public: be publicly accessible to anyone with the tracing URL. If set to `False`, the tracing data will be private and only accessible to the Langfuse account owner. """ + if not proxy_tracer.is_content_tracing_enabled: + logger.warning( + "Traces will not be logged to Langfuse because Haystack tracing is disabled. " + "To enable, set the HAYSTACK_CONTENT_TRACING_ENABLED environment variable to true " + "before importing Haystack." + ) self._tracer = tracer self._context: List[LangfuseSpan] = [] self._name = name From 4465c18ffc1a6eba26ae67462cdc63ec9b9c75a8 Mon Sep 17 00:00:00 2001 From: Ian Fukushima Date: Mon, 9 Dec 2024 09:36:01 -0300 Subject: [PATCH 2/2] test: warn when lagnfuse tracer init with tracing disabled --- integrations/langfuse/tests/test_tracer.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/integrations/langfuse/tests/test_tracer.py b/integrations/langfuse/tests/test_tracer.py index 42ae1d07d..d9790ea36 100644 --- a/integrations/langfuse/tests/test_tracer.py +++ b/integrations/langfuse/tests/test_tracer.py @@ -1,4 +1,6 @@ import datetime +import logging +import sys from unittest.mock import MagicMock, Mock, patch from haystack.dataclasses import ChatMessage @@ -149,3 +151,17 @@ def test_context_is_empty_after_tracing(self): pass assert tracer._context == [] + + def test_init_with_tracing_disabled(self, monkeypatch, caplog): + # Clear haystack modules because ProxyTracer is initialized whenever haystack is imported + modules_to_clear = [name for name in sys.modules if name.startswith('haystack')] + for name in modules_to_clear: + sys.modules.pop(name, None) + + # Re-import LangfuseTracer and instantiate it with tracing disabled + with caplog.at_level(logging.WARNING): + monkeypatch.setenv("HAYSTACK_CONTENT_TRACING_ENABLED", "false") + from haystack_integrations.tracing.langfuse import LangfuseTracer + + LangfuseTracer(tracer=MockTracer(), name="Haystack", public=False) + assert "tracing is disabled" in caplog.text