Skip to content

Commit af255f0

Browse files
chore(llmobs): handle error from getting agent info [backport 3.4] (#13089)
Backport 9e21081 from #13049 to 3.4. Handles an error when trying to get agent info. This should be included with the initial change to check agent info to determine if LLM Observability should run agentless or agent-proxy. Direct fix for #12911, which hasn't landed yet, hence making this a `chore`. The original release note for the above PR should suffice for the release. ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) Co-authored-by: Sam Brenner <[email protected]>
1 parent 306489d commit af255f0

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

ddtrace/llmobs/_writer.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ def should_use_agentless(user_defined_agentless_enabled: Optional[bool] = None)
7575
if user_defined_agentless_enabled is not None:
7676
return user_defined_agentless_enabled
7777

78-
agent_info: Optional[Dict[str, Any]] = agent.info()
78+
agent_info: Optional[Dict[str, Any]]
79+
80+
try:
81+
agent_info = agent.info()
82+
except Exception:
83+
agent_info = None
7984

8085
if agent_info is None:
8186
return True

tests/llmobs/conftest.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -305,23 +305,23 @@ def llmobs_events(llmobs, llmobs_span_writer):
305305

306306
@pytest.fixture
307307
def agent():
308-
patcher = mock.patch("ddtrace.internal.agent.info", return_value={"endpoints": ["/evp_proxy/v2/"]})
309-
patcher.start()
310-
yield
311-
patcher.stop()
308+
with mock.patch("ddtrace.internal.agent.info", return_value={"endpoints": ["/evp_proxy/v2/"]}):
309+
yield
312310

313311

314312
@pytest.fixture
315313
def agent_missing_proxy():
316-
patcher = mock.patch("ddtrace.internal.agent.info", return_value={"endpoints": []})
317-
patcher.start()
318-
yield
319-
patcher.stop()
314+
with mock.patch("ddtrace.internal.agent.info", return_value={"endpoints": []}):
315+
yield
316+
317+
318+
@pytest.fixture
319+
def no_agent_info():
320+
with mock.patch("ddtrace.internal.agent.info", return_value=None):
321+
yield
320322

321323

322324
@pytest.fixture
323325
def no_agent():
324-
patcher = mock.patch("ddtrace.internal.agent.info", return_value=None)
325-
patcher.start()
326-
yield
327-
patcher.stop()
326+
with mock.patch("ddtrace.internal.agent.info", side_effect=Exception):
327+
yield

tests/llmobs/test_llmobs_service.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ def test_enable_agent_proxy_when_agent_is_available(agent):
8888
llmobs_service.disable()
8989

9090

91+
def test_enable_agentless_when_agent_info_is_not_available(no_agent_info):
92+
with override_global_config(dict(_dd_api_key="<not-a-real-api-key>", _llmobs_ml_app="<ml-app-name>")):
93+
dummy_tracer = DummyTracer()
94+
llmobs_service.enable(_tracer=dummy_tracer)
95+
llmobs_instance = llmobs_service._instance
96+
assert llmobs_instance is not None
97+
assert llmobs_service.enabled
98+
assert isinstance(llmobs_instance._llmobs_span_writer._clients[0], LLMObsAgentlessEventClient)
99+
100+
llmobs_service.disable()
101+
102+
91103
def test_enable_agentless_when_agent_is_not_available(no_agent):
92104
with override_global_config(dict(_dd_api_key="<not-a-real-api-key>", _llmobs_ml_app="<ml-app-name>")):
93105
dummy_tracer = DummyTracer()

0 commit comments

Comments
 (0)