• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

deepset-ai / haystack / 19709118881

26 Nov 2025 03:34PM UTC coverage: 91.563% (+0.1%) from 91.44%
19709118881

Pull #10111

github

web-flow
Merge 35b81da21 into dce828c5a
Pull Request #10111: fix: ensure that headers keys are unique in link_content

13923 of 15206 relevant lines covered (91.56%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.83
haystack/tracing/datadog.py
1
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
2
#
3
# SPDX-License-Identifier: Apache-2.0
4

5
import contextlib
1✔
6
from typing import Any, Iterator, Optional
1✔
7

8
from haystack.lazy_imports import LazyImport
1✔
9
from haystack.tracing import Span, Tracer
1✔
10
from haystack.tracing import utils as tracing_utils
1✔
11

12
with LazyImport("Run 'pip install ddtrace'") as ddtrace_import:
1✔
13
    import ddtrace
1✔
14
    from ddtrace.trace import Span as ddSpan
1✔
15
    from ddtrace.trace import Tracer as ddTracer
1✔
16

17
_COMPONENT_NAME_KEY = "haystack.component.name"
1✔
18
_COMPONENT_TYPE_KEY = "haystack.component.type"
1✔
19
_COMPONENT_RUN_OPERATION_NAME = "haystack.component.run"
1✔
20

21

22
class DatadogSpan(Span):
1✔
23
    def __init__(self, span: "ddSpan") -> None:
1✔
24
        self._span = span
1✔
25

26
    def set_tag(self, key: str, value: Any) -> None:
1✔
27
        """
28
        Set a single tag on the span.
29

30
        :param key: the name of the tag.
31
        :param value: the value of the tag.
32
        """
33
        coerced_value = tracing_utils.coerce_tag_value(value)
1✔
34
        # Although set_tag declares value: Optional[str], its implementation accepts other types.
35
        # https://github.com/DataDog/dd-trace-py/blob/200b33c5221db1af975f6f7017738cd99a2da4a4/ddtrace/_trace/span.py
36
        self._span.set_tag(key, coerced_value)  # type: ignore[arg-type]
1✔
37

38
    def raw_span(self) -> Any:
1✔
39
        """
40
        Provides access to the underlying span object of the tracer.
41

42
        :return: The underlying span object.
43
        """
44
        return self._span
1✔
45

46
    def get_correlation_data_for_logs(self) -> dict[str, Any]:
1✔
47
        """Return a dictionary with correlation data for logs."""
48

49
        # https://docs.datadoghq.com/tracing/other_telemetry/connect_logs_and_traces/python/#no-standard-library-logging
50
        return ddtrace.tracer.get_log_correlation_context()
1✔
51

52

53
class DatadogTracer(Tracer):
1✔
54
    def __init__(self, tracer: "ddTracer") -> None:
1✔
55
        ddtrace_import.check()
1✔
56
        self._tracer = tracer
1✔
57

58
    @staticmethod
1✔
59
    def _get_span_resource_name(operation_name: str, tags: Optional[dict[str, Any]]) -> Optional[str]:
1✔
60
        """
61
        Get the resource name for the Datadog span.
62
        """
63
        if operation_name == _COMPONENT_RUN_OPERATION_NAME and tags:
1✔
64
            component_type = tags.get(_COMPONENT_TYPE_KEY, "")
1✔
65
            component_name = tags.get(_COMPONENT_NAME_KEY, "")
1✔
66

67
            return f"{component_type}: {component_name}"
1✔
68

69
        return None
1✔
70

71
    @contextlib.contextmanager
1✔
72
    def trace(
1✔
73
        self, operation_name: str, tags: Optional[dict[str, Any]] = None, parent_span: Optional[Span] = None
74
    ) -> Iterator[Span]:
75
        """Activate and return a new span that inherits from the current active span."""
76
        resource_name = self._get_span_resource_name(operation_name, tags)
1✔
77

78
        with self._tracer.trace(name=operation_name, resource=resource_name) as span:
1✔
79
            custom_span = DatadogSpan(span)
1✔
80
            if tags:
1✔
81
                custom_span.set_tags(tags)
1✔
82

83
            yield custom_span
1✔
84

85
    def current_span(self) -> Optional[Span]:
1✔
86
        """Return the current active span"""
87
        current_span = self._tracer.current_span()
1✔
88
        if current_span is None:
1✔
89
            return None
×
90

91
        return DatadogSpan(current_span)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc