Skip to content

Commit f911459

Browse files
lan666assjrl
andauthored
feat: add resource name for Haystack Component Datadog spans (#9337)
* feat: add resource name for Haystack Component Datadog spans * fest: format resource name Signed-off-by: Ahmad Zidan <ahmad.zidan@traveloka.com> * feat: add release notes Signed-off-by: Ahmad Zidan <ahmad.zidan@traveloka.com> --------- Signed-off-by: Ahmad Zidan <ahmad.zidan@traveloka.com> Co-authored-by: Sebastian Husch Lee <10526848+sjrl@users.noreply.github.com>
1 parent 3784889 commit f911459

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

haystack/tracing/datadog.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
from ddtrace.trace import Span as ddSpan
1515
from ddtrace.trace import Tracer as ddTracer
1616

17+
_COMPONENT_NAME_KEY = "haystack.component.name"
18+
_COMPONENT_TYPE_KEY = "haystack.component.type"
19+
_COMPONENT_RUN_OPERATION_NAME = "haystack.component.run"
20+
1721

1822
class DatadogSpan(Span):
1923
def __init__(self, span: "ddSpan") -> None:
@@ -60,12 +64,27 @@ def __init__(self, tracer: "ddTracer") -> None:
6064
ddtrace_import.check()
6165
self._tracer = tracer
6266

67+
@staticmethod
68+
def _get_span_resource_name(operation_name: str, tags: Optional[Dict[str, Any]]) -> Optional[str]:
69+
"""
70+
Get the resource name for the Datadog span.
71+
"""
72+
if operation_name == _COMPONENT_RUN_OPERATION_NAME and tags:
73+
component_type = tags.get(_COMPONENT_TYPE_KEY, "")
74+
component_name = tags.get(_COMPONENT_NAME_KEY, "")
75+
76+
return f"{component_type}: {component_name}"
77+
78+
return None
79+
6380
@contextlib.contextmanager
6481
def trace(
6582
self, operation_name: str, tags: Optional[Dict[str, Any]] = None, parent_span: Optional[Span] = None
6683
) -> Iterator[Span]:
6784
"""Activate and return a new span that inherits from the current active span."""
68-
with self._tracer.trace(operation_name) as span:
85+
resource_name = self._get_span_resource_name(operation_name, tags)
86+
87+
with self._tracer.trace(name=operation_name, resource=resource_name) as span:
6988
custom_span = DatadogSpan(span)
7089
if tags:
7190
custom_span.set_tags(tags)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
enhancements:
3+
- |
4+
For component run Datadog tracing, set the span resource name to the component name instead of the operation name.

test/tracing/test_datadog.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,24 @@ class TestDatadogTracer:
4747
def test_opentelemetry_tracer(self, datadog_tracer: ddTracer, capfd: CaptureFixture) -> None:
4848
tracer = DatadogTracer(datadog_tracer)
4949

50-
with tracer.trace("test") as span:
50+
component_tags = {
51+
"haystack.component.name": "test_component",
52+
"haystack.component.type": "TestType",
53+
"haystack.component.input": {"input_key": "input_value"},
54+
"haystack.component.output": {"output_key": "output_value"},
55+
}
56+
57+
with tracer.trace("haystack.component.run", tags=component_tags) as span:
5158
span.set_tag("key", "value")
5259

5360
traces = get_traces_from_console(capfd)
5461
assert len(traces) == 1
5562

5663
trace = traces[0]
5764

58-
assert trace["name"] == "test"
65+
assert trace["name"] == "haystack.component.run"
66+
assert "test_component" in trace["resource"]
67+
assert "TestType" in trace["resource"]
5968

6069
def test_tagging(self, datadog_tracer: ddTracer, capfd: CaptureFixture) -> None:
6170
tracer = DatadogTracer(datadog_tracer)

0 commit comments

Comments
 (0)