Skip to content

Commit

Permalink
fix docstrints format for tracing package (deepset-ai#7497)
Browse files Browse the repository at this point in the history
  • Loading branch information
masci authored Apr 8, 2024
1 parent aae2b31 commit b20a14d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
14 changes: 14 additions & 0 deletions haystack/tracing/datadog.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,25 @@ def __init__(self, span: "ddtrace.Span") -> None:
self._span = span

def set_tag(self, key: str, value: Any) -> None:
"""
Set a single tag on the span.
:param key: the name of the tag.
:param value: the value of the tag.
"""
coerced_value = tracing_utils.coerce_tag_value(value)
self._span.set_tag(key, coerced_value)

def raw_span(self) -> Any:
"""
Provides access to the underlying span object of the tracer.
:return: The underlying span object.
"""
return self._span

def get_correlation_data_for_logs(self) -> Dict[str, Any]:
"""Return a dictionary with correlation data for logs."""
raw_span = self.raw_span()
if not raw_span:
return {}
Expand All @@ -44,6 +56,7 @@ def __init__(self, tracer: "ddtrace.Tracer") -> None:

@contextlib.contextmanager
def trace(self, operation_name: str, tags: Optional[Dict[str, Any]] = None) -> Iterator[Span]:
"""Activate and return a new span that inherits from the current active span."""
with self._tracer.trace(operation_name) as span:
span = DatadogSpan(span)
if tags:
Expand All @@ -52,6 +65,7 @@ def trace(self, operation_name: str, tags: Optional[Dict[str, Any]] = None) -> I
yield span

def current_span(self) -> Optional[Span]:
"""Return the current active span"""
current_span = self._tracer.current_span()
if current_span is None:
return None
Expand Down
14 changes: 14 additions & 0 deletions haystack/tracing/opentelemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,25 @@ def __init__(self, span: "opentelemetry.trace.Span") -> None:
self._span = span

def set_tag(self, key: str, value: Any) -> None:
"""
Set a single tag on the span.
:param key: the name of the tag.
:param value: the value of the tag.
"""
coerced_value = tracing_utils.coerce_tag_value(value)
self._span.set_attribute(key, coerced_value)

def raw_span(self) -> Any:
"""
Provides access to the underlying span object of the tracer.
:return: The underlying span object.
"""
return self._span

def get_correlation_data_for_logs(self) -> Dict[str, Any]:
"""Return a dictionary with correlation data for logs."""
span_context = self._span.get_span_context()
return {"trace_id": span_context.trace_id, "span_id": span_context.span_id}

Expand All @@ -33,6 +45,7 @@ def __init__(self, tracer: "opentelemetry.trace.Tracer") -> None:

@contextlib.contextmanager
def trace(self, operation_name: str, tags: Optional[Dict[str, Any]] = None) -> Iterator[Span]:
"""Activate and return a new span that inherits from the current active span."""
with self._tracer.start_as_current_span(operation_name) as raw_span:
span = OpenTelemetrySpan(raw_span)
if tags:
Expand All @@ -41,6 +54,7 @@ def trace(self, operation_name: str, tags: Optional[Dict[str, Any]] = None) -> I
yield span

def current_span(self) -> Optional[Span]:
"""Return the current active span"""
current_span = opentelemetry.trace.get_current_span()
if isinstance(current_span, opentelemetry.trace.NonRecordingSpan):
return None
Expand Down
32 changes: 23 additions & 9 deletions haystack/tracing/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class Span(abc.ABC):

@abc.abstractmethod
def set_tag(self, key: str, value: Any) -> None:
"""Set a single tag on the span.
"""
Set a single tag on the span.
Note that the value will be serialized to a string, so it's best to use simple types like strings, numbers, or
booleans.
Expand All @@ -27,15 +28,17 @@ def set_tag(self, key: str, value: Any) -> None:
pass

def set_tags(self, tags: Dict[str, Any]) -> None:
"""Set multiple tags on the span.
"""
Set multiple tags on the span.
:param tags: a mapping of tag names to tag values.
"""
for key, value in tags.items():
self.set_tag(key, value)

def raw_span(self) -> Any:
"""Provides access to the underlying span object of the tracer.
"""
Provides access to the underlying span object of the tracer.
Use this if you need full access to the underlying span object.
Expand All @@ -44,7 +47,8 @@ def raw_span(self) -> Any:
return self

def set_content_tag(self, key: str, value: Any) -> None:
"""Set a single tag containing content information.
"""
Set a single tag containing content information.
Content is sensitive information such as
- the content of a query
Expand All @@ -62,7 +66,8 @@ def set_content_tag(self, key: str, value: Any) -> None:
self.set_tag(key, value)

def get_correlation_data_for_logs(self) -> Dict[str, Any]:
"""Return a dictionary with correlation data for logs.
"""
Return a dictionary with correlation data for logs.
This is useful if you want to correlate logs with traces."""
return {}
Expand All @@ -74,7 +79,8 @@ class Tracer(abc.ABC):
@abc.abstractmethod
@contextlib.contextmanager
def trace(self, operation_name: str, tags: Optional[Dict[str, Any]] = None) -> Iterator[Span]:
"""Trace the execution of a block of code.
"""
Trace the execution of a block of code.
:param operation_name: the name of the operation being traced.
:param tags: tags to apply to the newly created span.
Expand All @@ -84,15 +90,17 @@ def trace(self, operation_name: str, tags: Optional[Dict[str, Any]] = None) -> I

@abc.abstractmethod
def current_span(self) -> Optional[Span]:
"""Returns the currently active span. If no span is active, returns `None`.
"""
Returns the currently active span. If no span is active, returns `None`.
:return: Currently active span or `None` if no span is active.
"""
pass


class ProxyTracer(Tracer):
"""Container for the actual tracer instance.
"""
Container for the actual tracer instance.
This eases
- replacing the actual tracer instance without having to change the global tracer instance
Expand All @@ -105,17 +113,20 @@ def __init__(self, provided_tracer: Tracer) -> None:

@contextlib.contextmanager
def trace(self, operation_name: str, tags: Optional[Dict[str, Any]] = None) -> Iterator[Span]:
"""Activate and return a new span that inherits from the current active span."""
with self.actual_tracer.trace(operation_name, tags=tags) as span:
yield span

def current_span(self) -> Optional[Span]:
"""Return the current active span"""
return self.actual_tracer.current_span()


class NullSpan(Span):
"""A no-op implementation of the `Span` interface. This is used when tracing is disabled."""

def set_tag(self, key: str, value: Any) -> None:
"""Set a single tag on the span."""
pass


Expand All @@ -124,9 +135,11 @@ class NullTracer(Tracer):

@contextlib.contextmanager
def trace(self, operation_name: str, tags: Optional[Dict[str, Any]] = None) -> Iterator[Span]:
"""Activate and return a new span that inherits from the current active span."""
yield NullSpan()

def current_span(self) -> Optional[Span]:
"""Return the current active span"""
return NullSpan()


Expand All @@ -152,7 +165,8 @@ def is_tracing_enabled() -> bool:


def auto_enable_tracing() -> None:
"""Auto-enable the right tracing backend.
"""
Auto-enable the right tracing backend.
This behavior can be disabled by setting the environment variable `HAYSTACK_AUTO_TRACE_ENABLED` to `false`.
Note that it will only work correctly if tracing was configured _before_ Haystack is imported.
Expand Down
3 changes: 2 additions & 1 deletion haystack/tracing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@


def coerce_tag_value(value: Any) -> Union[bool, str, int, float]:
"""Coerces span tag values to compatible types for the tracing backend.
"""
Coerces span tag values to compatible types for the tracing backend.
Most tracing libraries don't support sending complex types to the backend. Hence, we need to convert them to
compatible types.
Expand Down

0 comments on commit b20a14d

Please sign in to comment.