Skip to content
Merged
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
added documentation
  • Loading branch information
SuyogSoti committed Jul 9, 2019
commit 7b728d8bf27fc187920f272a494f5de9d69e7fa6
75 changes: 68 additions & 7 deletions sdk/core/azure-core/azure/core/tracing/ext/opencensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,31 @@
# ------------------------------------
import os

from azure.core.tracing.abstract_span import AbstractSpan
from azure.core.tracing import AbstractSpan
from opencensus.trace import execution_context
from opencensus.trace import tracer as tracer_module, Span
from opencensus.trace.propagation import trace_context_http_header_format
from opencensus.trace.samplers import ProbabilitySampler

try:
from typing import TYPE_CHECKING
except ImportError:
TYPE_CHECKING = False

if TYPE_CHECKING:
from typing import Any, Dict, Union


class OpencensusWrapper(AbstractSpan):
"""Wraps a given Opencensus Span so that it implements azure.core.tracing.AbstractSpan"""

def __init__(self, span=None, name="parent_span"):
# type: (Any) -> None
# type: (Span, str) -> None
"""
:param span: The opencensus span to wrap
:param name: The name of the opencensus span to create if a new span is needed
"""
super(OpencensusWrapper, self).__init__(span, name)
tracer = self.get_current_tracer()
self.was_created_by_azure_sdk = False
if span is None:
Expand All @@ -33,32 +48,57 @@ def __init__(self, span=None, name="parent_span"):
span = tracer.span(name=name)

self.tracer = tracer
self.span_instance = span
self._span_instance = span

@property
def span_instance(self):
# type: () -> Span
"""
:return: The openencensus span that is being wrapped.
"""
return self._span_instance

def _get_environ(self, key):
# type: (str) -> str
# type: (str) -> Union[str, None]
"""
Gets the Environment Variable given. Will return None if no such environment variable is found.
:param key: The name of the environment variable
:return: The value of the variable or None if the variable does not exist.
"""
if key in os.environ:
return os.environ[key]
return None

def span(self, name="child_span"):
# type: (str) -> OpencensusWrapper
"""
Create a child span for the current span and append it to the child spans list in the span instance.
:param name: Name of the child span
:return: The OpencensusWrapper that is wrapping the child span instance
"""
child = self.span_instance.span(name=name)
wrapped_child = OpencensusWrapper(child)
return wrapped_child

def start(self):
# type: () -> None
"""Set the start time for a span."""
self.span_instance.start()

def finish(self):
# type: () -> None
"""Set the end time for a span."""
self.span_instance.finish()
if self.was_created_by_azure_sdk:
self.end_tracer(self.tracer)

def to_header(self, headers):
# type: (Dict[str, str]) -> str
"""
Returns a dictionary with the header labels and values.
:param headers: A key value pair dictionary
:return: A key value pair dictionary
"""
tracer_from_context = self.get_current_tracer()
temp_headers = {}
if tracer_from_context is not None:
Expand All @@ -68,31 +108,52 @@ def to_header(self, headers):

def from_header(self, headers):
# type: (Dict[str, str]) -> Any
"""
Given a dictionary returns a new tracer with the span context extracted from that dictionary.
:param headers: A key value pair dictionary
:return: A tracer initialized with the span context extraction from headers.
"""
ctx = trace_context_http_header_format.TraceContextPropagator().from_headers(headers)
return tracer_module.Tracer(span_context=ctx)

@classmethod
def end_tracer(cls, tracer):
# type: (Tracer) -> None
# type: (tracer_module.Tracer) -> None
"""
If a tracer exists, exports and ends the tracer.
:param tracer: The tracer to export and end
"""
if tracer is not None:
tracer.end_span()

@classmethod
def get_current_span(cls):
# type: () -> Span
"""
Get the current span from the execution context. Return None otherwise.
"""
return execution_context.get_current_span()

@classmethod
def get_current_tracer(cls):
# type: () -> Tracer
# type: () -> tracer_module.Tracer
"""
Get the current tracer from the execution context. Return None otherwise.
"""
return execution_context.get_opencensus_tracer()

@classmethod
def set_current_span(cls, span):
# type: (Span) -> None
"""
Set the given span as the current span in the execution context.
"""
return execution_context.set_current_span(span)

@classmethod
def set_current_tracer(cls, tracer):
# type: (Tracer) -> None
# type: (tracer_module.Tracer) -> None
"""
Set the given tracer as the current tracer in the execution context.
"""
return execution_context.set_opencensus_tracer(tracer)