Skip to content

Instantly share code, notes, and snippets.

@TheBigRoomXXL
Last active April 7, 2023 07:13
Show Gist options
  • Save TheBigRoomXXL/06e4966141ea8135652a5d547002109e to your computer and use it in GitHub Desktop.
Save TheBigRoomXXL/06e4966141ea8135652a5d547002109e to your computer and use it in GitHub Desktop.
Tracing decorator for OpenTelemetry
# Inspired by https://stackoverflow.com/a/309000
# With some help for typing from https://rednafi.github.io/reflections/static-typing-python-decorators.html
from functools import wraps
from collections.abc import Callable
from typing import ParamSpec, TypeVar
from opentelemetry import trace
P = ParamSpec("P")
R = TypeVar("R")
def traced(func: Callable[P, R]) -> Callable[P, R]:
@wraps(func)
def with_tracing(*args:P.args, **kwargs:P.kwargs) -> R:
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span(func.__name__):
return func(*args, **kwargs)
return with_tracing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment