Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
97 commits
Select commit Hold shift + click to select a range
657c34e
add opencensus impl
SuyogSoti Jul 9, 2019
8573e23
dont need to check for noop tracer
SuyogSoti Jul 9, 2019
989c4dc
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-pytho…
SuyogSoti Jul 9, 2019
3f64325
get rid of span and trace id
SuyogSoti Jul 9, 2019
0b04d80
fix span instance
SuyogSoti Jul 9, 2019
9f4e507
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-pytho…
SuyogSoti Jul 9, 2019
7b728d8
added documentation
SuyogSoti Jul 9, 2019
bfd355c
added documentation and refactor names of a few variables
SuyogSoti Jul 9, 2019
cd06c97
write test for opencensus wrapper
SuyogSoti Jul 10, 2019
a9f7a6f
put opencensus in the dev requirements
SuyogSoti Jul 10, 2019
b8f63df
initial common
SuyogSoti Jul 10, 2019
408c5ad
only import the wrapper when necessary
SuyogSoti Jul 10, 2019
9baf366
add check for the exporter
SuyogSoti Jul 10, 2019
f4c52fd
rework logic and fix some settings
SuyogSoti Jul 10, 2019
d439df9
added initial decorator
SuyogSoti Jul 10, 2019
f256d62
some mroe documentation
SuyogSoti Jul 10, 2019
9067279
added decorators
SuyogSoti Jul 10, 2019
eee5e09
small change
SuyogSoti Jul 10, 2019
6050bd7
some minor fixes
SuyogSoti Jul 10, 2019
c9faa4b
test patch happening
SuyogSoti Jul 10, 2019
6d35557
fix space
SuyogSoti Jul 10, 2019
0f6c1f7
share a function
SuyogSoti Jul 10, 2019
2bfffe5
clearer logic for setting span context
SuyogSoti Jul 10, 2019
c1f6d69
better logic
SuyogSoti Jul 10, 2019
18890ea
better logic
SuyogSoti Jul 10, 2019
6a4a545
fix environ variable
SuyogSoti Jul 11, 2019
e7b5707
test the way opencensus does it
SuyogSoti Jul 11, 2019
aaf2152
middle of tests
SuyogSoti Jul 11, 2019
7ccf041
only load if opencensus has already been imported
SuyogSoti Jul 11, 2019
6a01918
fix spelling mistake
SuyogSoti Jul 11, 2019
36572c5
temp
SuyogSoti Jul 11, 2019
d9a2d95
Merge branch 'opencensus_impl' into temp
SuyogSoti Jul 11, 2019
280173f
finish writing tests for common
SuyogSoti Jul 11, 2019
b3f44fb
charles fixes
SuyogSoti Jul 11, 2019
9cb1bd9
Merge branch 'opencensus_impl' into temp
SuyogSoti Jul 11, 2019
77e0ccf
fix tests
SuyogSoti Jul 11, 2019
1e9c7dc
fix test settings
SuyogSoti Jul 11, 2019
d72248e
to header should not take a dict
SuyogSoti Jul 11, 2019
135d1c0
from header should be class method
SuyogSoti Jul 11, 2019
56e8269
Merge branch 'opencensus_impl' into temp
SuyogSoti Jul 11, 2019
73203c2
initial tests
SuyogSoti Jul 11, 2019
e304d40
dont create trace and get rid of end_tracer
SuyogSoti Jul 11, 2019
ff5627d
dont need to save the trace
SuyogSoti Jul 11, 2019
1e4b03a
more little fixes
SuyogSoti Jul 11, 2019
d55bca1
Merge branch 'opencensus_impl' into temp
SuyogSoti Jul 11, 2019
72e0dd4
some intermediatary changes
SuyogSoti Jul 11, 2019
aa5b671
fix type annotations
SuyogSoti Jul 11, 2019
0ce1a22
rst fix types
SuyogSoti Jul 11, 2019
b9c7d72
add :class:annotations
SuyogSoti Jul 11, 2019
57b2a57
fix line wrapping
SuyogSoti Jul 11, 2019
9b6260e
Merge branch 'opencensus_impl' into temp
SuyogSoti Jul 11, 2019
8b1ed8f
added tests for decorator
SuyogSoti Jul 12, 2019
c041818
rename opencensus wrapper
SuyogSoti Jul 12, 2019
8da57ec
Merge branch 'opencensus_impl' into overall
SuyogSoti Jul 12, 2019
56d2e7d
intermediate changes
SuyogSoti Jul 12, 2019
de6f552
use spans the right way
SuyogSoti Jul 12, 2019
691eb3d
some formatting
SuyogSoti Jul 12, 2019
6a1f733
Merge branch 'opencensus_impl' into overall
SuyogSoti Jul 12, 2019
20d5afe
some grammar
SuyogSoti Jul 12, 2019
c55c901
Merge branch 'opencensus_impl' into overall
SuyogSoti Jul 12, 2019
3e80b1e
restructure settings and make tests pass
SuyogSoti Jul 12, 2019
3f9f4ae
rename get_parent
SuyogSoti Jul 12, 2019
412bf7a
fix typings
SuyogSoti Jul 12, 2019
3c6ab1c
use protocol and from_headers becomes links
SuyogSoti Jul 12, 2019
de8b5f4
Merge branch 'opencensus_impl' into overall
SuyogSoti Jul 12, 2019
de41353
ramifications of opencensus wrapper being a protocol
SuyogSoti Jul 12, 2019
db2f865
add tests for link
SuyogSoti Jul 12, 2019
7c637e6
Merge branch 'opencensus_impl' into overall
SuyogSoti Jul 12, 2019
27c9876
added async tests
SuyogSoti Jul 12, 2019
ac9ff09
delete the unit test thing
SuyogSoti Jul 12, 2019
f0ba4ee
fix merge conflict
SuyogSoti Jul 12, 2019
ff07c55
added add_attribute
SuyogSoti Jul 13, 2019
30fd165
added add_attribute
SuyogSoti Jul 13, 2019
644fe2a
added tests for add attributes
SuyogSoti Jul 13, 2019
fc6a284
Merge branch 'opencensus_impl' into add_decorator
SuyogSoti Jul 13, 2019
8f137c1
remove unused import
SuyogSoti Jul 13, 2019
8e8aa21
Merge branch 'master' into add_decorator
SuyogSoti Jul 13, 2019
a373e40
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-pytho…
SuyogSoti Jul 15, 2019
0c37b38
added docstrings
SuyogSoti Jul 15, 2019
b925782
minor docstring formatting
SuyogSoti Jul 15, 2019
4dd6ba9
fix pylint errors
SuyogSoti Jul 15, 2019
fae494a
don't rely on opencensus children to check
SuyogSoti Jul 15, 2019
e484bcf
use exporter to not rely on parent.children
SuyogSoti Jul 15, 2019
76c75bc
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-pytho…
SuyogSoti Jul 15, 2019
9d0159c
test should only propagate
SuyogSoti Jul 16, 2019
aa14218
made test tracing helper
SuyogSoti Jul 16, 2019
bc35a5f
decrease flakiness of test
SuyogSoti Jul 16, 2019
85e5e48
simplify get parent
SuyogSoti Jul 16, 2019
91d1d25
calling a decorator decorator is redundant
SuyogSoti Jul 16, 2019
603d4f6
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-pytho…
SuyogSoti Jul 16, 2019
056bc2e
fix settings
SuyogSoti Jul 16, 2019
720c44e
more elegant code
SuyogSoti Jul 16, 2019
f503b6e
add await for async
SuyogSoti Jul 16, 2019
6743bd1
should only have to wait a 1/1000 of a second
SuyogSoti Jul 16, 2019
c070b77
fix tests spans too short
SuyogSoti Jul 17, 2019
df7f09e
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-pytho…
SuyogSoti Jul 17, 2019
d106a16
accidentally deleted setup.cfg
SuyogSoti Jul 17, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
some intermediatary changes
  • Loading branch information
SuyogSoti committed Jul 11, 2019
commit 72e0dd465684bc914f422759ad7536e87122d3ab
8 changes: 4 additions & 4 deletions sdk/core/azure-core/azure/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ def convert_logging(value):


def get_opencensus_wrapper():
# type: () -> OpencensusSpanWrapper
"""Returns the OpencensusSpanWrapper if opencensus is installed else returns None"""
# type: () -> OpenCensusSpan
"""Returns the OpenCensusSpan if opencensus is installed else returns None"""
try:
from azure.core.tracing.ext.opencensus_wrapper import OpencensusSpanWrapper
from azure.core.tracing.ext.opencensus_wrapper import OpenCensusSpan

return OpencensusSpanWrapper
return OpenCensusSpan
except ImportError:
return None

Expand Down
4 changes: 2 additions & 2 deletions sdk/core/azure-core/azure/core/tracing/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def set_span_contexts(wrapped_span, span_instance=None):
impl_wrapper.set_current_span(span_instance)


def get_parent(*args, **kwargs):
def get_parent(kwargs):
# type: (Any) -> Tuple(AbstractSpan, AbstractSpan, Any)
"""Returns the parent span that of the span that represents the function and the spans before that parent span"""
Copy link
Member

Choose a reason for hiding this comment

The 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 parent_span argument. If I already have parent_span, do I call get_parent to get its parent? What's the triple for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed to get_parent_and_original_contexts

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -80,7 +80,7 @@ def get_parent(*args, **kwargs):


def should_use_trace(parent_span):
# type: (AbstractSpan, List[str], str)
# 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)
1 change: 1 addition & 0 deletions sdk/core/azure-core/azure/core/tracing/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#
# --------------------------------------------------------------------------
import functools

import azure.core.tracing.common as common


Expand Down
73 changes: 37 additions & 36 deletions sdk/core/azure-core/tests/test_tracing_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,39 @@
from azure.core.pipeline import Pipeline, PipelineResponse
from azure.core.pipeline.policies import HTTPPolicy
from azure.core.pipeline.transport import HttpTransport
from azure.core.tracing import common, AbstractSpan
from azure.core.tracing import common
from azure.core.tracing.context import tracing_context
from azure.core.tracing.decorator import distributed_tracing_decorator
from azure.core.settings import settings
from azure.core.tracing.ext.opencensus_wrapper import OpencensusSpanWrapper
from azure.core.tracing.ext.opencensus_wrapper import OpenCensusSpan
from opencensus.trace import tracer as tracer_module
from opencensus.trace.samplers import AlwaysOnSampler
import pytest


class ContextHelper(object):
def __init__(self, environ={}, tracer_to_use=None):
self.orig_tracer = OpencensusSpanWrapper.get_current_tracer()
self.orig_current_span = OpencensusSpanWrapper.get_current_span()
self.orig_tracer = OpenCensusSpan.get_current_tracer()
self.orig_current_span = OpenCensusSpan.get_current_span()
self.orig_sdk_context_span = tracing_context.current_span.get()
self.orig_sdk_context_tracing_impl = tracing_context.tracing_impl.get()
self.os_env = mock.patch.dict(os.environ, environ)
self.tracer_to_use = tracer_to_use

def __enter__(self):
self.orig_tracer = OpencensusSpanWrapper.get_current_tracer()
self.orig_current_span = OpencensusSpanWrapper.get_current_span()
self.orig_tracer = OpenCensusSpan.get_current_tracer()
self.orig_current_span = OpenCensusSpan.get_current_span()
self.orig_sdk_context_span = tracing_context.current_span.get()
self.orig_sdk_context_tracing_impl = tracing_context.tracing_impl.get()
settings.tracing_implementation.set_value(self.tracer_to_use)
self.os_env.start()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
OpencensusSpanWrapper.set_current_tracer(self.orig_tracer)
OpencensusSpanWrapper.set_current_span(self.orig_current_span)
OpenCensusSpan.set_current_tracer(self.orig_tracer)
OpenCensusSpan.set_current_span(self.orig_current_span)
tracing_context.current_span.set(self.orig_sdk_context_span)
tracing_context.tracing_impl.set(self.orig_sdk_context_tracin
tracing_context.tracing_impl.set(self.orig_sdk_context_tracing_impl)
settings.tracing_implementation.unset_value()
self.os_env.stop()

Expand Down Expand Up @@ -94,15 +94,16 @@ def test_get_opencensus_wrapper_if_opencensus_is_imported(self):
sys.modules["opencensus"] = opencensus
assert (
common.get_opencensus_wrapper_if_opencensus_is_imported()
is OpencensusSpanWrapper
is OpenCensusSpan
)

def test_set_span_context(self):
with ContextHelper(environ={"AZURE_SDK_TRACING_IMPLEMENTATION": "opencensus"}):
wrapper = settings.tracing_implementation()
assert wrapper == OpenCensusSpan
assert tracing_context.current_span.get() is None
assert wrapper.get_current_span() is None
parent = OpencensusSpanWrapper()
parent = OpenCensusSpan()
common.set_span_contexts(parent)
assert parent.span_instance == wrapper.get_current_span()
assert tracing_context.current_span.get() == parent
Expand All @@ -112,62 +113,62 @@ def test_get_parent(self):
opencensus = sys.modules["opencensus"]
del sys.modules["opencensus"]

parent, orig_tracing_context, orig_span_inst = common.get_parent()
parent, orig_tracing_context, orig_span_inst = common.get_parent({})
assert orig_span_inst is None
assert parent is None
assert orig_tracing_context is None

sys.modules["opencensus"] = opencensus
parent, orig_tracing_context, orig_span_inst = common.get_parent()
parent, orig_tracing_context, orig_span_inst = common.get_parent({})
assert orig_span_inst is None
assert parent.span_instance.name == "azure-sdk-for-python-first_parent_span"
assert orig_tracing_context is None

tracer = tracer_module.Tracer(sampler=AlwaysOnSampler())
parent, orig_tracing_context, orig_span_inst = common.get_parent()
parent, orig_tracing_context, orig_span_inst = common.get_parent({})
assert orig_span_inst is None
assert parent.span_instance.name == "azure-sdk-for-python-first_parent_span"
assert orig_tracing_context is None
parent.finish()

some_span = tracer.start_span(name="some_span")
new_parent, orig_tracing_context, orig_span_inst = common.get_parent()
new_parent, orig_tracing_context, orig_span_inst = common.get_parent({})
assert orig_span_inst == some_span
assert new_parent.span_instance.name == "some_span"
assert orig_tracing_context is None

should_be_old_parent, orig_tracing_context, orig_span_inst = common.get_parent(
parent_span=parent.span_instance
)
kwarg = {"parent_span": parent.span_instance}
should_be_old_parent, orig_tracing_context, orig_span_inst = common.get_parent(kwarg)
assert kwarg.get("parent_span") is None
assert orig_span_inst == some_span
assert should_be_old_parent.span_instance == parent.span_instance
assert orig_tracing_context is None

def test_should_use_trace(self):
with ContextHelper(environ={"AZURE_TRACING_ONLY_PROPAGATE": "yes"}):
parent_span = OpencensusSpanWrapper()
parent_span = OpenCensusSpan()
assert common.should_use_trace(parent_span) == False
assert common.should_use_trace(None) == False
parent_span = OpencensusSpanWrapper()
parent_span = OpenCensusSpan()
assert common.should_use_trace(parent_span)
assert common.should_use_trace(None) == False


class TestDecorator(unittest.TestCase):
def test_with_nothing_imported(self):
with ContextHelper():
opencensus = sys.modules["opencensus"]
del sys.modules["opencensus"]
client = MockClient(assert_current_span=True)
with pytest.raises(AssertionError):
client.make_request(3)
sys.modules["opencensus"] = opencensus

def test_with_nothing_imported(self):
with ContextHelper():
client = MockClient(assert_current_span=True)
with pytest.raises(AssertionError):
client.make_request(3)
#
# class TestDecorator(unittest.TestCase):
# def test_with_nothing_imported(self):
# with ContextHelper():
# opencensus = sys.modules["opencensus"]
# del sys.modules["opencensus"]
# client = MockClient(assert_current_span=True)
# with pytest.raises(AssertionError):
# client.make_request(3)
# sys.modules["opencensus"] = opencensus
#
# def test_with_nothing_imported(self):
# with ContextHelper():
# client = MockClient(assert_current_span=True)
# with pytest.raises(AssertionError):
# client.make_request(3)

if __name__ == "__main__":
unittest.main()