Skip to content

Commit c319db5

Browse files
committed
feat: add resource name for Haystack Component Datadog spans
1 parent e3f9da1 commit c319db5

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

haystack/tracing/datadog.py

Lines changed: 21 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,28 @@ 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+
resource_name = f"{component_type} {component_name}".strip()
76+
77+
return resource_name if resource_name else None
78+
79+
return None
80+
6381
@contextlib.contextmanager
6482
def trace(
6583
self, operation_name: str, tags: Optional[Dict[str, Any]] = None, parent_span: Optional[Span] = None
6684
) -> Iterator[Span]:
6785
"""Activate and return a new span that inherits from the current active span."""
68-
with self._tracer.trace(operation_name) as span:
86+
resource_name = self._get_span_resource_name(operation_name, tags)
87+
88+
with self._tracer.trace(name=operation_name, resource=resource_name) as span:
6989
custom_span = DatadogSpan(span)
7090
if tags:
7191
custom_span.set_tags(tags)

0 commit comments

Comments
 (0)