Skip to content

refactor(telemetry): add span component to the telemetry metrics #12430

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 34 commits into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
912fdb4
Add span component to the telemetry metrics and adjust test expectati…
wantsui Feb 19, 2025
d2ed9d5
Revert back to default int
wantsui Feb 19, 2025
2f719f1
Refactor to replace integration_name with the component if it exists.…
wantsui Feb 24, 2025
89e3b5c
Add a benchmark test for metrics.
wantsui Feb 24, 2025
401f957
Fix benchmark test so it actually runs.
wantsui Feb 24, 2025
0da0929
Add another benchmark.
wantsui Feb 25, 2025
3b68e6d
Revise scenario.
wantsui Feb 26, 2025
a4da33d
Merge branch 'main' into wantsui/add-component-telemetry
wantsui Apr 11, 2025
1c5324f
Use latest benchmarks.
wantsui Apr 11, 2025
adce454
Revert scenarios.
wantsui Apr 11, 2025
015c596
Remove space in comment in config file
wantsui Apr 11, 2025
014348b
Fix spacing in config.yaml comments
wantsui Apr 11, 2025
60a21c0
Remove unneeded scenarios.
wantsui Apr 11, 2025
8664425
Remove batching around the telemetry writer for metrics.
wantsui Apr 11, 2025
fd37515
Merge branch 'main' into wantsui/add-component-telemetry
wantsui Apr 14, 2025
f5057d3
Update ddtrace/_trace/processor/__init__.py
wantsui Apr 18, 2025
4324c76
Update ddtrace/_trace/processor/__init__.py
wantsui Apr 18, 2025
b1b643b
Merge branch 'main' into wantsui/add-component-telemetry
wantsui Apr 30, 2025
a6c684e
Merge branch 'main' into wantsui/add-component-telemetry
wantsui May 1, 2025
320d958
Fix references.
wantsui May 1, 2025
449862f
Merge remote-tracking branch 'origin/main' into wantsui/add-component…
brettlangdon May 20, 2025
7a30a78
Simplify span telemetry metric submission.
wantsui May 20, 2025
100f360
Merge branch 'main' into wantsui/add-component-telemetry
wantsui May 20, 2025
4fecb2b
Fix lint
wantsui May 20, 2025
a2e8088
Add batching back in for performance reasons.
wantsui May 23, 2025
48f5708
Merge branch 'main' into wantsui/add-component-telemetry
wantsui May 23, 2025
64f9322
Merge branch 'main' into wantsui/add-component-telemetry
wantsui May 30, 2025
c2d00c6
Merge branch 'main' into wantsui/add-component-telemetry
wantsui May 30, 2025
e81bdfe
Update ddtrace/_trace/processor/__init__.py
wantsui Jun 5, 2025
831d57e
Update ddtrace/_trace/processor/__init__.py
wantsui Jun 5, 2025
8f247d4
Revert headers change.
wantsui Jun 5, 2025
69d76ca
Merge branch 'main' into wantsui/add-component-telemetry
wantsui Jun 5, 2025
b3c6266
Merge branch 'main' into wantsui/add-component-telemetry
wantsui Jun 5, 2025
dd2c178
Merge branch 'main' into wantsui/add-component-telemetry
brettlangdon Jun 6, 2025
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
8 changes: 6 additions & 2 deletions ddtrace/_trace/processor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from ddtrace.constants import USER_KEEP
from ddtrace.internal import gitmetadata
from ddtrace.internal import telemetry
from ddtrace.internal.constants import COMPONENT
from ddtrace.internal.constants import HIGHER_ORDER_TRACE_ID_BITS
from ddtrace.internal.constants import LAST_DD_PARENT_ID_KEY
from ddtrace.internal.constants import MAX_UINT_64BITS
Expand Down Expand Up @@ -315,12 +316,15 @@ def on_span_start(self, span: Span) -> None:
with self._lock:
trace = self._traces[span.trace_id]
trace.spans.append(span)
self._span_metrics["spans_created"][span._span_api] += 1
integration_name = span._meta.get(COMPONENT, span._span_api)

self._span_metrics["spans_created"][integration_name] += 1
self._queue_span_count_metrics("spans_created", "integration_name")

def on_span_finish(self, span: Span) -> None:
with self._lock:
self._span_metrics["spans_finished"][span._span_api] += 1
integration_name = span._meta.get(COMPONENT, span._span_api)
self._span_metrics["spans_finished"][integration_name] += 1

# Calling finish on a span that we did not see the start for
# DEV: This can occur if the SpanAggregator is recreated while there is a span in progress
Expand Down
6 changes: 4 additions & 2 deletions tests/telemetry/test_telemetry_metrics_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,16 @@ def test_span_creation_and_finished_metrics_datadog(test_agent_session, ddtrace_
code = """
from ddtrace.trace import tracer
for _ in range(10):
with tracer.trace('span1'):
with tracer.trace('span1') as span:
span.set_tag("component", "custom")
pass
"""
env = os.environ.copy()
env["_DD_INSTRUMENTATION_TELEMETRY_TESTS_FORCE_APP_STARTED"] = "true"
_, stderr, status, _ = ddtrace_run_python_code_in_subprocess(code, env=env)
assert status == 0, stderr
metrics_sc = test_agent_session.get_metrics("spans_created")

assert len(metrics_sc) == 1
assert metrics_sc[0]["metric"] == "spans_created"
assert metrics_sc[0]["tags"] == ["integration_name:datadog"]
Expand All @@ -105,7 +107,7 @@ def test_span_creation_and_finished_metrics_datadog(test_agent_session, ddtrace_
metrics_sf = test_agent_session.get_metrics("spans_finished")
assert len(metrics_sf) == 1
assert metrics_sf[0]["metric"] == "spans_finished"
assert metrics_sf[0]["tags"] == ["integration_name:datadog"]
assert metrics_sf[0]["tags"] == ["integration_name:custom"]
assert metrics_sf[0]["points"][0][1] == 10


Expand Down
6 changes: 3 additions & 3 deletions tests/tracer/test_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ def test_span_creation_metrics():

span = Span("span", on_finish=[aggr.on_span_finish])
aggr.on_span_start(span)
span.set_tag("component", "custom")
span.finish()

mock_tm.assert_has_calls(
Expand All @@ -368,12 +369,11 @@ def test_span_creation_metrics():
)
mock_tm.reset_mock()
aggr.shutdown(None)
# On span finished the span has a different integration name:
mock_tm.assert_has_calls(
[
mock.call(TELEMETRY_NAMESPACE.TRACERS, "spans_created", 1, tags=(("integration_name", "datadog"),)),
mock.call(
TELEMETRY_NAMESPACE.TRACERS, "spans_finished", 1, tags=(("integration_name", "datadog"),)
),
mock.call(TELEMETRY_NAMESPACE.TRACERS, "spans_finished", 1, tags=(("integration_name", "custom"),)),
]
)

Expand Down
Loading