Last active
April 7, 2023 07:13
-
-
Save TheBigRoomXXL/06e4966141ea8135652a5d547002109e to your computer and use it in GitHub Desktop.
Tracing decorator for OpenTelemetry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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