|
14 | 14 | from ddtrace.trace import Span as ddSpan |
15 | 15 | from ddtrace.trace import Tracer as ddTracer |
16 | 16 |
|
| 17 | +_COMPONENT_NAME_KEY = "haystack.component.name" |
| 18 | +_COMPONENT_TYPE_KEY = "haystack.component.type" |
| 19 | +_COMPONENT_RUN_OPERATION_NAME = "haystack.component.run" |
| 20 | + |
17 | 21 |
|
18 | 22 | class DatadogSpan(Span): |
19 | 23 | def __init__(self, span: "ddSpan") -> None: |
@@ -60,12 +64,28 @@ def __init__(self, tracer: "ddTracer") -> None: |
60 | 64 | ddtrace_import.check() |
61 | 65 | self._tracer = tracer |
62 | 66 |
|
| 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 | + |
63 | 81 | @contextlib.contextmanager |
64 | 82 | def trace( |
65 | 83 | self, operation_name: str, tags: Optional[Dict[str, Any]] = None, parent_span: Optional[Span] = None |
66 | 84 | ) -> Iterator[Span]: |
67 | 85 | """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: |
69 | 89 | custom_span = DatadogSpan(span) |
70 | 90 | if tags: |
71 | 91 | custom_span.set_tags(tags) |
|
0 commit comments