-
Notifications
You must be signed in to change notification settings - Fork 0
OpenTelemetry tracing decorators and helpers #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a7cf5c7
Add OpenTelemetry API package and tracing helpers.
jongeorge1 df261d2
Apply instrumentation
jongeorge1 632afdd
Added information on the tracing code to README
jongeorge1 edfc1a1
README tweak
jongeorge1 98cc17d
Readme tweak
jongeorge1 3142b35
Merge remote-tracking branch 'origin/main' into feature/tracing-decor…
jongeorge1 4fc2e21
Merge branch 'main' into feature/tracing-decorators
MikeEvansLarah 2606bd9
Fix import
MikeEvansLarah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,5 @@ | ||
| from .tracing import all_methods_start_new_current_span_with_method_name | ||
|
|
||
| __all__ = [ | ||
| "all_methods_start_new_current_span_with_method_name", | ||
| ] |
This file contains hidden or 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,102 @@ | ||
| from opentelemetry import trace | ||
| from functools import wraps | ||
|
|
||
|
|
||
| def start_as_current_span_with_method_name(tracer: trace.Tracer): | ||
| """ | ||
| Function decorator which starts a new span with the full name of the method (i.e. class_name.method_name for | ||
| methods within classes, or just method_name for standalone functions) as the span name. The span is then set as | ||
| the current span for the duration of the method call and can be accessed using trace.get_current_span(). | ||
|
|
||
| Args: | ||
| tracer (trace.Tracer): The tracer to use for starting the span. Create a tracer for the source file using | ||
| trace.get_tracer(__name__) and pass it to this decorator. | ||
| """ | ||
|
|
||
| def decorator(func): | ||
| @wraps(func) | ||
| def wrapper(*args, **kwargs): | ||
| with tracer.start_as_current_span(name=func.__qualname__): | ||
| return func(*args, **kwargs) | ||
|
|
||
| return wrapper | ||
|
|
||
| return decorator | ||
|
|
||
|
|
||
| def all_methods_start_new_current_span_with_method_name(tracer: trace.Tracer): | ||
| """ | ||
| Class decorator which applies start_as_current_span_with_method_name to all methods within the class. | ||
|
|
||
| Args: | ||
| tracer (trace.Tracer): The tracer to use for starting the span. Create a tracer for the source file using | ||
| trace.get_tracer(__name__) and pass it to this decorator. | ||
| """ | ||
|
|
||
| decorator = start_as_current_span_with_method_name(tracer) | ||
|
|
||
| def decorate(cls): | ||
| for attr in cls.__dict__: | ||
| item = getattr(cls, attr) | ||
| if callable(item): | ||
| setattr(cls, attr, decorator(item)) | ||
|
|
||
| return cls | ||
|
|
||
| return decorate | ||
|
|
||
|
|
||
| def add_attributes_to_span(span: trace.Span, **kwargs: dict[str, any]): | ||
| """ | ||
| Adds the specified key-value pairs to the specified span as attributes. | ||
|
|
||
| For example, calling: | ||
| add_attributes_to_span(span, key1="value1", key2="value2") | ||
| is equivalent to calling: | ||
| span.set_attributes({"key1": "value1", "key2": "value2"}) | ||
|
|
||
| Args: | ||
| **kwargs: The key-value pairs to add to the span as attributes. | ||
| """ | ||
| if span is not None: | ||
| kwargs_as_strings = {k: str(v) for k, v in kwargs.items()} | ||
| span.set_attributes(kwargs_as_strings) | ||
|
|
||
|
|
||
| def add_attributes_to_current_span(**kwargs: dict[str, any]): | ||
| """ | ||
| Adds the specified key-value pairs to the current span as attributes. | ||
|
|
||
| Args: | ||
| **kwargs: The key-value pairs to add to the span as attributes. | ||
| """ | ||
| add_attributes_to_span(trace.get_current_span(), **kwargs) | ||
|
|
||
|
|
||
| def add_kwargs_to_span(span: trace.Span, keys: list[str], source_kwargs: dict[str, any]): | ||
| """ | ||
| Adds the specified keys from the source_kwargs dictionary to the span as attributes. | ||
|
|
||
| Args: | ||
| span (trace.Span): The span to add the attributes to. | ||
| keys (list[str]): The keys from the source_kwargs to add to the span. These are manually specified to avoid | ||
| adding sensitive information to the span. | ||
| source_kwargs (dict[str, any]): The dictionary to get the values from. This is typically the kwargs | ||
| dictionary of the method being traced. | ||
| """ | ||
| kwargs_to_add = {key: source_kwargs[key] for key in keys if key in source_kwargs} | ||
| add_attributes_to_span(span, **kwargs_to_add) | ||
|
|
||
|
|
||
| def add_kwargs_to_current_span(keys: list[str], source_kwargs: dict[str, any]): | ||
| """ | ||
| Adds the specified keys from the source_kwargs dictionary to the current span as attributes. | ||
|
|
||
| Args: | ||
| keys (list[str]): The keys from the source_kwargs to add to the span. These are manually specified to avoid | ||
| adding sensitive information to the span. | ||
| source_kwargs (dict[str, any]): The dictionary to get the values from. This is typically the kwargs | ||
| dictionary of the method being traced. | ||
| """ | ||
| span = trace.get_current_span() | ||
| add_kwargs_to_span(span, keys, source_kwargs) |
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.