Skip to content

Commit 1ef3be1

Browse files
github-actions[bot]Yun-Kimbrettlangdon
authored
chore(tracer): simplify llmobs injection assertion on tracer propagation tests [backport 2.9] (#9468)
Backport f1c78e8 from #9458 to 2.9. #9455 fixed a failing test in the tracer test suite due to not being tested when llmobs made some changes to `_inject_llmobs_parent_id()`, and also ensured that tracer tests run on llmobs changes moving forward. This PR also changes the corresponding tracer tests to be less focused on exact implementation of `_inject_llmobs_parent_id()`, as `tests/llmobs/test_propagation.py` is responsible for testing the actual injection logic. We only care on the tracer propagation test level that `_inject_llmobs_parent_id()` is called if llmobs is enabled. ## Checklist - [x] Change(s) are motivated and described in the PR description - [x] Testing strategy is described if automated tests are not included in the PR - [x] Risks are described (performance impact, potential for breakage, maintainability) - [x] Change is maintainable (easy to change, telemetry, documentation) - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) are followed or label `changelog/no-changelog` is set - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)) - [x] Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) - [x] If this PR changes the public interface, I've notified `@DataDog/apm-tees`. ## Reviewer Checklist - [x] Title is accurate - [x] All changes are related to the pull request's stated goal - [x] Description motivates each change - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - [x] Testing strategy adequately addresses listed risks - [x] Change is maintainable (easy to change, telemetry, documentation) - [x] Release note makes sense to a user of the library - [x] Author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - [x] 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: Yun Kim <[email protected]> Co-authored-by: Brett Langdon <[email protected]>
1 parent 97b336d commit 1ef3be1

File tree

1 file changed

+18
-62
lines changed

1 file changed

+18
-62
lines changed

tests/tracer/test_propagation.py

Lines changed: 18 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import pickle
66

7+
import mock
78
import pytest
89

910
from ddtrace import tracer as ddtracer
@@ -2670,69 +2671,24 @@ def test_DD_TRACE_PROPAGATION_STYLE_INJECT_overrides_DD_TRACE_PROPAGATION_STYLE(
26702671
assert result == expected_headers
26712672

26722673

2673-
def test_llmobs_enabled_injects_parent_id(run_python_code_in_subprocess):
2674-
code = """
2675-
from ddtrace import tracer
2676-
from ddtrace.ext import SpanTypes
2677-
from ddtrace.llmobs._constants import PROPAGATED_PARENT_ID_KEY
2678-
from ddtrace.propagation.http import HTTPPropagator
2679-
2680-
with tracer.trace("LLMObs span", span_type=SpanTypes.LLM) as root_span:
2681-
with tracer.trace("Non-LLMObs span") as child_span:
2682-
headers = {}
2683-
HTTPPropagator.inject(child_span.context, headers)
2684-
2685-
assert "{}={}".format(PROPAGATED_PARENT_ID_KEY, root_span.span_id) in headers["x-datadog-tags"]
2686-
"""
2687-
2688-
env = os.environ.copy()
2689-
env["DD_LLMOBS_ENABLED"] = "1"
2690-
env["DD_TRACE_ENABLED"] = "0"
2691-
stdout, stderr, status, _ = run_python_code_in_subprocess(code=code, env=env)
2692-
assert status == 0, (stdout, stderr)
2693-
assert stderr == b"", (stdout, stderr)
2694-
2695-
2696-
def test_llmobs_disabled_does_not_inject_parent_id(run_python_code_in_subprocess):
2697-
code = """
2698-
from ddtrace import tracer
2699-
from ddtrace.ext import SpanTypes
2700-
from ddtrace.llmobs._constants import PROPAGATED_PARENT_ID_KEY
2701-
from ddtrace.propagation.http import HTTPPropagator
2702-
2703-
with tracer.trace("LLMObs span", span_type=SpanTypes.LLM) as root_span:
2704-
with tracer.trace("Non-LLMObs span") as child_span:
2705-
headers = {}
2706-
HTTPPropagator.inject(child_span.context, headers)
2707-
2708-
assert "{}".format(PROPAGATED_PARENT_ID_KEY) not in headers["x-datadog-tags"]
2709-
"""
2674+
def test_llmobs_enabled_injects_llmobs_parent_id():
2675+
with override_global_config(dict(_llmobs_enabled=True)):
2676+
with mock.patch("ddtrace.llmobs._utils._inject_llmobs_parent_id") as mock_llmobs_inject:
2677+
context = Context(trace_id=1, span_id=2)
2678+
HTTPPropagator.inject(context, {})
2679+
mock_llmobs_inject.assert_called_once_with(context)
27102680

2711-
env = os.environ.copy()
2712-
env["DD_LLMOBS_ENABLED"] = "0"
2713-
env["DD_TRACE_ENABLED"] = "0"
2714-
stdout, stderr, status, _ = run_python_code_in_subprocess(code=code, env=env)
2715-
assert status == 0, (stdout, stderr)
2716-
assert stderr == b"", (stdout, stderr)
27172681

2682+
def test_llmobs_disabled_does_not_inject_parent_id():
2683+
with override_global_config(dict(_llmobs_enabled=False)):
2684+
with mock.patch("ddtrace.llmobs._utils._inject_llmobs_parent_id") as mock_llmobs_inject:
2685+
context = Context(trace_id=1, span_id=2)
2686+
HTTPPropagator.inject(context, {})
2687+
mock_llmobs_inject.assert_not_called()
27182688

2719-
def test_llmobs_enabled_does_not_inject_parent_id_if_no_llm_span(run_python_code_in_subprocess):
2720-
code = """
2721-
from ddtrace import tracer
2722-
from ddtrace.llmobs._constants import PROPAGATED_PARENT_ID_KEY
2723-
from ddtrace.propagation.http import HTTPPropagator
27242689

2725-
with tracer.trace("Non-LLMObs span") as root_span:
2726-
with tracer.trace("Non-LLMObs span") as child_span:
2727-
headers = {}
2728-
HTTPPropagator.inject(child_span.context, headers)
2729-
2730-
assert f"{PROPAGATED_PARENT_ID_KEY}=undefined" in headers["x-datadog-tags"]
2731-
"""
2732-
2733-
env = os.environ.copy()
2734-
env["DD_LLMOBS_ENABLED"] = "1"
2735-
env["DD_TRACE_ENABLED"] = "0"
2736-
stdout, stderr, status, _ = run_python_code_in_subprocess(code=code, env=env)
2737-
assert status == 0, (stdout, stderr)
2738-
assert stderr == b"", (stdout, stderr)
2690+
def test_llmobs_parent_id_not_injected_by_default():
2691+
with mock.patch("ddtrace.llmobs._utils._inject_llmobs_parent_id") as mock_llmobs_inject:
2692+
context = Context(trace_id=1, span_id=2)
2693+
HTTPPropagator.inject(context, {})
2694+
mock_llmobs_inject.assert_not_called()

0 commit comments

Comments
 (0)