-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating a decorator to be used in async class methods (#1049)
* Creating a decorator to be used in async class methods to create a new OTEL span
- Loading branch information
1 parent
5f2e2b7
commit 93e1b9a
Showing
7 changed files
with
502 additions
and
429 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""This utility class is to hold any utilities that are needed for async operations.""" | ||
from typing import Any, Callable, Coroutine, Union | ||
from opentelemetry import trace | ||
|
||
|
||
tracer = trace.get_tracer("synapseclient") | ||
|
||
|
||
def otel_trace_method( | ||
method_to_trace_name: Union[Callable[..., str], None] = None | ||
) -> Callable[..., Callable[..., Coroutine[Any, Any, None]]]: | ||
""" | ||
Decorator to trace a method with OpenTelemetry in an async environment. This function | ||
is specifically written to be used on a method within a class. | ||
This will pass the class instance as the first argument to the method. This allows | ||
you to modify the name of the trace to include information about the class instance. | ||
Example: Decorating a method within a class that will be traced with OpenTelemetry. | ||
Setting the trace name: | ||
@otel_trace_method(method_to_trace_name=lambda self, **kwargs: f"Project_Store: {self.name}") | ||
async def store(self): | ||
Args: | ||
method_to_trace_name: A callable that takes the class instance as the first argument | ||
and returns a string to be used as the trace name. If this is not provided, | ||
the trace name will be set to the method name. | ||
Returns: | ||
A callable decorator that will trace the method with OpenTelemetry. | ||
""" | ||
|
||
def decorator(f) -> Callable[..., Coroutine[Any, Any, None]]: | ||
"""Function decorator.""" | ||
|
||
async def wrapper(self, *arg, **kwargs) -> None: | ||
"""Wrapper for the function to be traced.""" | ||
trace_name = ( | ||
method_to_trace_name(self, *arg, **kwargs) | ||
if method_to_trace_name | ||
else None | ||
) | ||
with tracer.start_as_current_span(trace_name or f"Synaspse::{f.__name__}"): | ||
return await f(self, *arg, **kwargs) | ||
|
||
return wrapper | ||
|
||
return decorator |
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
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
Oops, something went wrong.