Skip to content

fix(llmobs): move listener hooks to enable instead of on init #11889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 10, 2025
Merged
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
14 changes: 11 additions & 3 deletions ddtrace/llmobs/_llmobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ def __init__(self, tracer=None):
self._annotations = []
self._annotation_context_lock = forksafe.RLock()

# Register hooks for span events
core.on("trace.span_start", self._do_annotations)
core.on("trace.span_finish", self._on_span_finish)
def _on_span_start(self, span):
if self.enabled and span.span_type == SpanTypes.LLM:
self._do_annotations(span)

def _on_span_finish(self, span):
if self.enabled and span.span_type == SpanTypes.LLM:
Expand Down Expand Up @@ -270,6 +270,10 @@ def _start_service(self) -> None:
log.debug("Error starting evaluator runner")

def _stop_service(self) -> None:
# Remove listener hooks for span events
core.reset_listeners("trace.span_start", self._on_span_start)
core.reset_listeners("trace.span_finish", self._on_span_finish)

try:
self._evaluator_runner.stop()
# flush remaining evaluation spans & evaluations
Expand Down Expand Up @@ -364,6 +368,10 @@ def enable(
cls.enabled = True
cls._instance.start()

# Register hooks for span events
core.on("trace.span_start", cls._instance._on_span_start)
core.on("trace.span_finish", cls._instance._on_span_finish)

atexit.register(cls.disable)
telemetry_writer.product_activated(TELEMETRY_APM_PRODUCT.LLMOBS, True)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
LLM Observability: Resolves an issue where enabling LLM Observability in agentless mode would result in traces also being sent to the agent proxy endpoint.
28 changes: 28 additions & 0 deletions tests/llmobs/test_llmobs_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,34 @@ def test_activate_distributed_headers_activates_context(llmobs, mock_llmobs_logs
mock_activate.assert_called_once_with(dummy_context)


def test_listener_hooks_enqueue_correct_writer(run_python_code_in_subprocess):
"""
Regression test that ensures that listener hooks enqueue span events to the correct writer,
not the default writer created at startup.
"""
env = os.environ.copy()
pypath = [os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))]
if "PYTHONPATH" in env:
pypath.append(env["PYTHONPATH"])
env.update({"PYTHONPATH": ":".join(pypath), "DD_TRACE_ENABLED": "0"})
out, err, status, pid = run_python_code_in_subprocess(
"""
from ddtrace.llmobs import LLMObs

LLMObs.enable(ml_app="repro-issue", agentless_enabled=True, api_key="foobar.baz", site="datad0g.com")
with LLMObs.agent("dummy"):
pass
""",
env=env,
)
assert status == 0, err
assert out == b""
agentless_writer_log = b"failed to send traces to intake at https://llmobs-intake.datad0g.com/api/v2/llmobs: HTTP error status 403, reason Forbidden\n" # noqa: E501
agent_proxy_log = b"failed to send, dropping 1 traces to intake at http://localhost:8126/evp_proxy/v2/api/v2/llmobs after 5 retries" # noqa: E501
assert err == agentless_writer_log
assert agent_proxy_log not in err


def test_llmobs_fork_recreates_and_restarts_span_writer():
"""Test that forking a process correctly recreates and restarts the LLMObsSpanWriter."""
with mock.patch("ddtrace.internal.writer.HTTPWriter._send_payload"):
Expand Down
Loading