-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Add decorator #6299
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
Add decorator #6299
Changes from 7 commits
657c34e
8573e23
989c4dc
3f64325
0b04d80
9f4e507
7b728d8
bfd355c
cd06c97
a9f7a6f
b8f63df
408c5ad
9baf366
f4c52fd
d439df9
f256d62
9067279
eee5e09
6050bd7
c9faa4b
6d35557
0f6c1f7
2bfffe5
c1f6d69
18890ea
6a4a545
e7b5707
aaf2152
7ccf041
6a01918
36572c5
d9a2d95
280173f
b3f44fb
9cb1bd9
77e0ccf
1e9c7dc
d72248e
135d1c0
56e8269
73203c2
e304d40
ff5627d
1e4b03a
d55bca1
72e0dd4
aa5b671
0ce1a22
b9c7d72
57b2a57
9b6260e
8b1ed8f
c041818
8da57ec
56d2e7d
de6f552
691eb3d
6a1f733
20d5afe
c55c901
3e80b1e
3f9f4ae
412bf7a
3c6ab1c
de8b5f4
de41353
db2f865
7c637e6
27c9876
ac9ff09
f0ba4ee
ff07c55
30fd165
644fe2a
fc6a284
8f137c1
8e8aa21
a373e40
0c37b38
b925782
4dd6ba9
fae494a
e484bcf
76c75bc
9d0159c
aa14218
bc35a5f
85e5e48
91d1d25
603d4f6
056bc2e
720c44e
f503b6e
6743bd1
c070b77
df7f09e
d106a16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| # -------------------------------------------------------------------------- | ||
|
SuyogSoti marked this conversation as resolved.
|
||
| # | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # | ||
| # The MIT License (MIT) | ||
| # | ||
| # Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| # of this software and associated documentation files (the ""Software""), to deal | ||
| # in the Software without restriction, including without limitation the rights | ||
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| # copies of the Software, and to permit persons to whom the Software is | ||
| # furnished to do so, subject to the following conditions: | ||
| # | ||
| # The above copyright notice and this permission notice shall be included in | ||
| # all copies or substantial portions of the Software. | ||
| # | ||
| # THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| # THE SOFTWARE. | ||
| # | ||
| # -------------------------------------------------------------------------- | ||
| from os import environ | ||
| import re | ||
|
|
||
| from azure.core.tracing.context import tracing_context | ||
| from azure.core.tracing.abstract_span import AbstractSpan | ||
| from azure.core.settings import settings | ||
|
|
||
|
|
||
| def _get_opencensus_wrapper(): | ||
| # type: () -> OpencensusWrapper | ||
| """Returns the OpencensusWrapper if opencensus is installed else returns None""" | ||
| try: | ||
| from azure.core.tracing.ext.opencensus_wrapper import OpencensusWrapper | ||
|
|
||
| return OpencensusWrapper | ||
| except ImportError: | ||
| return None | ||
|
SuyogSoti marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| def set_span_contexts(wrapped_span, span_instance=None, impl_wrapper=None): | ||
| # type: (AbstractSpan, AbstractSpan) -> None | ||
|
SuyogSoti marked this conversation as resolved.
Outdated
|
||
| tracing_context.current_span.set(wrapped_span) | ||
| impl_wrapper = impl_wrapper or wrapped_span | ||
| tracing_context.tracing_impl.set(impl_wrapper.__class__) | ||
| if wrapped_span is not None or ( | ||
| span_instance is not None and impl_wrapper is not None | ||
| ): | ||
| span_instance = span_instance or wrapped_span.span_instance | ||
| impl_wrapper.set_current_span(span_instance) | ||
|
SuyogSoti marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def get_parent(kwargs, *args): | ||
| # type: (Any) -> Tuple(Any, Any, Any) | ||
| """Returns the parent span that of the span that represents the function and the spans before that parent span""" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused by this function. The name suggests it returns one span. The docstring is tough to parse and kind of supports that (a serial comma would help clarify). However, the type hint promises an arbitrary triple, and the function secretly accepts a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. renamed to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I fixed it to get_parent_span |
||
| parent_span = kwargs.pop("parent_span", None) # type: AbstractSpan | ||
|
SuyogSoti marked this conversation as resolved.
Outdated
|
||
| orig_wrapped_span = tracing_context.current_span.get() | ||
|
|
||
| # wrapper class get from tracing_context, settings or assume OpencensusWrapper if opencesus is installed | ||
| wrapper_class = ( | ||
| tracing_context.tracing_impl.get() | ||
| or settings.tracing_implementation() | ||
| or _get_opencensus_wrapper() | ||
| ) | ||
| if wrapper_class is None: | ||
| return None, orig_wrapped_span, None | ||
|
|
||
| # parent span is given, get from my context, get from the implementation context or make our own | ||
| parent_span = ( | ||
| orig_wrapped_span if parent_span is None else wrapper_class(parent_span) | ||
| ) | ||
| if parent_span is None: | ||
| current_span = wrapper_class.get_current_span() | ||
| parent_span = ( | ||
| wrapper_class(span=current_span) | ||
| if current_span | ||
| else wrapper_class(name="azure-sdk-for-python-first_parent_span") | ||
| ) | ||
|
|
||
| return parent_span, orig_wrapped_span, wrapper_class.get_current_span() | ||
|
|
||
|
|
||
| def should_use_trace(parent_span): | ||
| # type: (AbstractSpan, List[str], str) | ||
|
SuyogSoti marked this conversation as resolved.
Outdated
|
||
| """Given Parent Span Returns whether the function should be traced""" | ||
| only_propagate = settings.tracing_should_only_propagate() | ||
| return parent_span and not only_propagate | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # -------------------------------------------------------------------------- | ||
| # | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # | ||
| # The MIT License (MIT) | ||
| # | ||
| # Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| # of this software and associated documentation files (the ""Software""), to deal | ||
| # in the Software without restriction, including without limitation the rights | ||
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| # copies of the Software, and to permit persons to whom the Software is | ||
| # furnished to do so, subject to the following conditions: | ||
| # | ||
| # The above copyright notice and this permission notice shall be included in | ||
| # all copies or substantial portions of the Software. | ||
| # | ||
| # THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| # THE SOFTWARE. | ||
| # | ||
| # -------------------------------------------------------------------------- | ||
| import functools | ||
| import azure.core.tracing.common as common | ||
|
|
||
| def distributed_tracing_decorator(func): | ||
|
SuyogSoti marked this conversation as resolved.
Outdated
|
||
| # type: (Callable[[Any], Any]) -> Callable[[Any], Any] | ||
| @functools.wraps(func) | ||
| def wrapper_use_tracer(self, *args, **kwargs): | ||
| # type: (Any) -> Any | ||
| parent_span, original_span_from_sdk_context, original_span_instance = common.get_parent( | ||
| kwargs | ||
| ) | ||
| ans = None | ||
| if common.should_use_trace(parent_span, func.__name__): | ||
|
SuyogSoti marked this conversation as resolved.
Outdated
|
||
| common.set_span_contexts(parent_span) | ||
| name = self.__class__.__name__ + "." + func.__name__ | ||
| child = parent_span.span(name=name) | ||
| child.start() | ||
| common.set_span_contexts(child) | ||
| ans = func(self, *args, **kwargs) | ||
| child.finish() | ||
| common.set_span_contexts(parent_span) | ||
| if getattr(parent_span, "was_created_by_azure_sdk", False): | ||
| parent_span.finish() | ||
| common.set_span_contexts( | ||
| original_span_from_sdk_context, | ||
| span_instance=original_span_instance, | ||
| impl_wrapper=parent_span, | ||
| ) | ||
| else: | ||
| ans = func(self, *args, **kwargs) | ||
| return ans | ||
|
|
||
| return wrapper_use_tracer | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # -------------------------------------------------------------------------- | ||
| # | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # | ||
| # The MIT License (MIT) | ||
| # | ||
| # Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| # of this software and associated documentation files (the ""Software""), to deal | ||
| # in the Software without restriction, including without limitation the rights | ||
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| # copies of the Software, and to permit persons to whom the Software is | ||
| # furnished to do so, subject to the following conditions: | ||
| # | ||
| # The above copyright notice and this permission notice shall be included in | ||
| # all copies or substantial portions of the Software. | ||
| # | ||
| # THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| # THE SOFTWARE. | ||
| # | ||
| # -------------------------------------------------------------------------- | ||
| import functools | ||
| import azure.core.tracing.common as common | ||
|
|
||
| def distributed_tracing_decorator_async(func): | ||
| # type: (Callable[[Any], Any]) -> Callable[[Any], Any] | ||
| @functools.wraps(func) | ||
| async def wrapper_use_tracer(self, *args, **kwargs): | ||
| # type: (Any) -> Any | ||
| parent_span, original_span_from_sdk_context, original_span_instance = common.get_parent( | ||
| kwargs | ||
| ) | ||
| ans = None | ||
| if common.should_use_trace(parent_span, func.__name__): | ||
| common.set_span_contexts(parent_span) | ||
| name = self.__class__.__name__ + "." + func.__name__ | ||
| child = parent_span.span(name=name) | ||
| child.start() | ||
| common.set_span_contexts(child) | ||
| ans = await func(self, *args, **kwargs) | ||
| child.finish() | ||
| common.set_span_contexts(parent_span) | ||
| if getattr(parent_span, "was_created_by_azure_sdk", False): | ||
| parent_span.finish() | ||
| common.set_span_contexts( | ||
| original_span_from_sdk_context, | ||
| span_instance=original_span_instance, | ||
| impl_wrapper=parent_span, | ||
| ) | ||
| else: | ||
| ans = await func(self, *args, **kwargs) | ||
| return ans | ||
|
|
||
| return wrapper_use_tracer |
Uh oh!
There was an error while loading. Please reload this page.