Skip to content

Commit

Permalink
fix: Made attributes compatible with OTEL attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
tazarov committed Oct 23, 2023
1 parent 1a8ae67 commit e762178
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions chromadb/telemetry/opentelemetry/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from functools import wraps
from enum import Enum
from typing import Any, Callable, Dict, Optional, Union, cast
from typing import Any, Callable, Dict, Optional, Sequence, Union, cast

from opentelemetry import trace
from opentelemetry.util import types
Expand Down Expand Up @@ -109,14 +109,28 @@ def otel_init(
def trace_method(
trace_name: str,
trace_granularity: OpenTelemetryGranularity,
attributes: Dict[str, Union[str, bool, float, int]] = {},
attributes: Optional[
Dict[
str,
Union[
str,
bool,
float,
int,
Sequence[str],
Sequence[bool],
Sequence[float],
Sequence[int],
],
]
] = None,
) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
"""A decorator that traces a method."""

def decorator(f: Callable[..., Any]) -> Callable[..., Any]:
@wraps(f)
def wrapper(*args: Any, **kwargs: Dict[Any, Any]) -> Any:
global tracer, granularity, _transform_attributes
global tracer, granularity
if trace_granularity < granularity:
return f(*args, **kwargs)
if not tracer:
Expand All @@ -130,13 +144,25 @@ def wrapper(*args: Any, **kwargs: Dict[Any, Any]) -> Any:


def add_attributes_to_current_span(
attributes: Dict[str, Union[str, bool, float, int]]
attributes: Dict[
str,
Union[
str,
bool,
float,
int,
Sequence[str],
Sequence[bool],
Sequence[float],
Sequence[int],
],
]
) -> None:
"""Add attributes to the current span."""
global tracer, granularity, _transform_attributes
global tracer, granularity
if granularity == OpenTelemetryGranularity.NONE:
return
if not tracer:
return
span = trace.get_current_span()
span.set_attributes(_transform_attributes(attributes))
span.set_attributes(attributes)

0 comments on commit e762178

Please sign in to comment.