-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Merged
Merged
Add decorator #6299
Changes from all commits
Commits
Show all changes
97 commits
Select commit
Hold shift + click to select a range
657c34e
add opencensus impl
SuyogSoti 8573e23
dont need to check for noop tracer
SuyogSoti 989c4dc
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-pytho…
SuyogSoti 3f64325
get rid of span and trace id
SuyogSoti 0b04d80
fix span instance
SuyogSoti 9f4e507
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-pytho…
SuyogSoti 7b728d8
added documentation
SuyogSoti bfd355c
added documentation and refactor names of a few variables
SuyogSoti cd06c97
write test for opencensus wrapper
SuyogSoti a9f7a6f
put opencensus in the dev requirements
SuyogSoti b8f63df
initial common
SuyogSoti 408c5ad
only import the wrapper when necessary
SuyogSoti 9baf366
add check for the exporter
SuyogSoti f4c52fd
rework logic and fix some settings
SuyogSoti d439df9
added initial decorator
SuyogSoti f256d62
some mroe documentation
SuyogSoti 9067279
added decorators
SuyogSoti eee5e09
small change
SuyogSoti 6050bd7
some minor fixes
SuyogSoti c9faa4b
test patch happening
SuyogSoti 6d35557
fix space
SuyogSoti 0f6c1f7
share a function
SuyogSoti 2bfffe5
clearer logic for setting span context
SuyogSoti c1f6d69
better logic
SuyogSoti 18890ea
better logic
SuyogSoti 6a4a545
fix environ variable
SuyogSoti e7b5707
test the way opencensus does it
SuyogSoti aaf2152
middle of tests
SuyogSoti 7ccf041
only load if opencensus has already been imported
SuyogSoti 6a01918
fix spelling mistake
SuyogSoti 36572c5
temp
SuyogSoti d9a2d95
Merge branch 'opencensus_impl' into temp
SuyogSoti 280173f
finish writing tests for common
SuyogSoti b3f44fb
charles fixes
SuyogSoti 9cb1bd9
Merge branch 'opencensus_impl' into temp
SuyogSoti 77e0ccf
fix tests
SuyogSoti 1e9c7dc
fix test settings
SuyogSoti d72248e
to header should not take a dict
SuyogSoti 135d1c0
from header should be class method
SuyogSoti 56e8269
Merge branch 'opencensus_impl' into temp
SuyogSoti 73203c2
initial tests
SuyogSoti e304d40
dont create trace and get rid of end_tracer
SuyogSoti ff5627d
dont need to save the trace
SuyogSoti 1e4b03a
more little fixes
SuyogSoti d55bca1
Merge branch 'opencensus_impl' into temp
SuyogSoti 72e0dd4
some intermediatary changes
SuyogSoti aa5b671
fix type annotations
SuyogSoti 0ce1a22
rst fix types
SuyogSoti b9c7d72
add :class:annotations
SuyogSoti 57b2a57
fix line wrapping
SuyogSoti 9b6260e
Merge branch 'opencensus_impl' into temp
SuyogSoti 8b1ed8f
added tests for decorator
SuyogSoti c041818
rename opencensus wrapper
SuyogSoti 8da57ec
Merge branch 'opencensus_impl' into overall
SuyogSoti 56d2e7d
intermediate changes
SuyogSoti de6f552
use spans the right way
SuyogSoti 691eb3d
some formatting
SuyogSoti 6a1f733
Merge branch 'opencensus_impl' into overall
SuyogSoti 20d5afe
some grammar
SuyogSoti c55c901
Merge branch 'opencensus_impl' into overall
SuyogSoti 3e80b1e
restructure settings and make tests pass
SuyogSoti 3f9f4ae
rename get_parent
SuyogSoti 412bf7a
fix typings
SuyogSoti 3c6ab1c
use protocol and from_headers becomes links
SuyogSoti de8b5f4
Merge branch 'opencensus_impl' into overall
SuyogSoti de41353
ramifications of opencensus wrapper being a protocol
SuyogSoti db2f865
add tests for link
SuyogSoti 7c637e6
Merge branch 'opencensus_impl' into overall
SuyogSoti 27c9876
added async tests
SuyogSoti ac9ff09
delete the unit test thing
SuyogSoti f0ba4ee
fix merge conflict
SuyogSoti ff07c55
added add_attribute
SuyogSoti 30fd165
added add_attribute
SuyogSoti 644fe2a
added tests for add attributes
SuyogSoti fc6a284
Merge branch 'opencensus_impl' into add_decorator
SuyogSoti 8f137c1
remove unused import
SuyogSoti 8e8aa21
Merge branch 'master' into add_decorator
SuyogSoti a373e40
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-pytho…
SuyogSoti 0c37b38
added docstrings
SuyogSoti b925782
minor docstring formatting
SuyogSoti 4dd6ba9
fix pylint errors
SuyogSoti fae494a
don't rely on opencensus children to check
SuyogSoti e484bcf
use exporter to not rely on parent.children
SuyogSoti 76c75bc
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-pytho…
SuyogSoti 9d0159c
test should only propagate
SuyogSoti aa14218
made test tracing helper
SuyogSoti bc35a5f
decrease flakiness of test
SuyogSoti 85e5e48
simplify get parent
SuyogSoti 91d1d25
calling a decorator decorator is redundant
SuyogSoti 603d4f6
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-pytho…
SuyogSoti 056bc2e
fix settings
SuyogSoti 720c44e
more elegant code
SuyogSoti f503b6e
add await for async
SuyogSoti 6743bd1
should only have to wait a 1/1000 of a second
SuyogSoti c070b77
fix tests spans too short
SuyogSoti df7f09e
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-pytho…
SuyogSoti d106a16
accidentally deleted setup.cfg
SuyogSoti 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
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
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,92 @@ | ||
| # -------------------------------------------------------------------------- | ||
| # | ||
| # 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. | ||
| # | ||
| # -------------------------------------------------------------------------- | ||
| """Common functions shared by both the sync and the async decorators.""" | ||
|
|
||
| from azure.core.tracing.context import tracing_context | ||
| from azure.core.tracing.abstract_span import AbstractSpan | ||
| from azure.core.settings import settings | ||
|
|
||
|
|
||
| try: | ||
| from typing import TYPE_CHECKING | ||
| except ImportError: | ||
| TYPE_CHECKING = False | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Any, Optional | ||
|
|
||
|
|
||
| def set_span_contexts(wrapped_span, span_instance=None): | ||
| # type: (AbstractSpan, Optional[AbstractSpan]) -> None | ||
| """ | ||
| Set the sdk context and the implementation context. `span_instance` will be used to set the implementation context | ||
| if passed in else will use `wrapped_span.span_instance`. | ||
|
|
||
| :param wrapped_span: The `AbstractSpan` to set as the sdk context | ||
| :type wrapped_span: `azure.core.tracing.abstract_span.AbstractSpan` | ||
| :param span_instance: The span to set as the current span for the implementation context | ||
| """ | ||
| tracing_context.current_span.set(wrapped_span) | ||
| impl_wrapper = settings.tracing_implementation() | ||
| if wrapped_span is not None: | ||
| span_instance = wrapped_span.span_instance | ||
| if impl_wrapper is not None: | ||
| impl_wrapper.set_current_span(span_instance) | ||
SuyogSoti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def get_parent_span(parent_span): | ||
| # type: (Any) -> Tuple(AbstractSpan, AbstractSpan, Any) | ||
| """ | ||
| Returns the current span so that the function's span will be its child. It will create a new span if there is | ||
| no current span in any of the context. | ||
|
|
||
| :param parent_span: The parent_span arg that the user passes into the top level function | ||
| :returns: the parent_span of the function to be traced | ||
| :rtype: `azure.core.tracing.abstract_span.AbstractSpan` | ||
| """ | ||
| wrapper_class = settings.tracing_implementation() | ||
| if wrapper_class is None: | ||
| return None | ||
|
|
||
| orig_wrapped_span = tracing_context.current_span.get() | ||
| # 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 | ||
|
|
||
|
|
||
| def should_use_trace(parent_span): | ||
| # type: (AbstractSpan) -> bool | ||
| """Given Parent Span Returns whether the function should be traced""" | ||
| only_propagate = settings.tracing_should_only_propagate() | ||
| return bool(parent_span and not only_propagate) | ||
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,64 @@ | ||
| # -------------------------------------------------------------------------- | ||
| # | ||
| # 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. | ||
| # | ||
| # -------------------------------------------------------------------------- | ||
| """The decorator to apply if you want the given function traced.""" | ||
|
|
||
| import functools | ||
|
|
||
| import azure.core.tracing.common as common | ||
| from azure.core.settings import settings | ||
| from azure.core.tracing.context import tracing_context | ||
|
|
||
|
|
||
| def distributed_trace(func): | ||
| # type: (Callable[[Any], Any]) -> Callable[[Any], Any] | ||
| @functools.wraps(func) | ||
| def wrapper_use_tracer(self, *args, **kwargs): | ||
| # type: (Any) -> Any | ||
| passed_in_parent = kwargs.pop("parent_span", None) | ||
| orig_wrapped_span = tracing_context.current_span.get() | ||
| wrapper_class = settings.tracing_implementation() | ||
| original_span_instance = None | ||
| if wrapper_class is not None: | ||
| original_span_instance = wrapper_class.get_current_span() | ||
| parent_span = common.get_parent_span(passed_in_parent) | ||
| ans = None | ||
| if common.should_use_trace(parent_span): | ||
| 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 orig_wrapped_span is None and passed_in_parent is None: | ||
| parent_span.finish() | ||
| common.set_span_contexts(orig_wrapped_span, span_instance=original_span_instance) | ||
| else: | ||
| ans = func(self, *args, **kwargs) | ||
| return ans | ||
|
|
||
| return wrapper_use_tracer |
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.