Skip to content

Commit

Permalink
Support kind keyword in Opencensus (Azure#19548)
Browse files Browse the repository at this point in the history
* support kind keyword

* changelog

* Unsupport kind

* FIx kind kwarg support during instantiating

* test

* links

* test fixx

* update azure core

* shared req
  • Loading branch information
Rakshith Bhyravabhotla authored and rakshith91 committed Jul 16, 2021
1 parent 1cb71d4 commit 1242dc2
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
4 changes: 4 additions & 0 deletions sdk/core/azure-core-tracing-opencensus/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

# Release History

## 1.0.0b8 (Unreleased)

- Fix for supporting `kind` keyword while instantiating the span.

## 1.0.0b7 (2021-04-08)

- `Link` and `SpanKind` can now be added while creating the span instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
TYPE_CHECKING = False

if TYPE_CHECKING:
from typing import Dict, Optional, Union, Callable, Sequence
from typing import Dict, Optional, Union, Callable, Sequence, Any

from azure.core.pipeline.transport import HttpRequest, HttpResponse
AttributeValue = Union[
Expand Down Expand Up @@ -61,7 +61,38 @@ def __init__(self, span=None, name="span", **kwargs):
:paramtype links: list[~azure.core.tracing.Link]
"""
tracer = self.get_current_tracer()
value = kwargs.pop('kind', None)
kind = (
OpenCensusSpanKind.CLIENT if value == SpanKind.CLIENT else
OpenCensusSpanKind.CLIENT if value == SpanKind.PRODUCER else # No producer in opencensus
OpenCensusSpanKind.SERVER if value == SpanKind.SERVER else
OpenCensusSpanKind.CLIENT if value == SpanKind.CONSUMER else # No consumer in opencensus
OpenCensusSpanKind.UNSPECIFIED if value == SpanKind.INTERNAL else # No internal in opencensus
OpenCensusSpanKind.UNSPECIFIED if value == SpanKind.UNSPECIFIED else
None
) # type: SpanKind
if value and kind is None:
raise ValueError("Kind {} is not supported in OpenCensus".format(value))

links = kwargs.pop('links', None)
self._span_instance = span or tracer.start_span(name=name, **kwargs)
if kind is not None:
self._span_instance.span_kind = kind

if links:
try:
for link in links:
ctx = trace_context_http_header_format.TraceContextPropagator().from_headers(link.headers)
self._span_instance.add_link(
Link(
trace_id=ctx.trace_id,
span_id=ctx.span_id,
attributes=link.attributes
))
except AttributeError:
# we will just send the links as is if it's not ~azure.core.tracing.Link without any validation
# assuming user knows what they are doing.
self._span_instance.links = links

@property
def span_instance(self):
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/azure-core-tracing-opencensus/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
'opencensus>=0.6.0',
'opencensus-ext-azure>=0.3.1',
'opencensus-ext-threading',
'azure-core<2.0.0,>=1.0.0',
'azure-core<2.0.0,>=1.13.0',
],
extras_require={
":python_version<'3.5'": ['typing'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import mock

from azure.core.tracing.ext.opencensus_span import OpenCensusSpan
from azure.core.tracing import SpanKind
from azure.core.tracing import SpanKind, Link
from opencensus.trace import tracer as tracer_module
from opencensus.trace.attributes import Attributes
from opencensus.trace.span import SpanKind as OpenCensusSpanKind
Expand Down Expand Up @@ -129,6 +129,47 @@ def test_add_attribute(self):
assert wrapped_class.span_instance.attributes["test"] == "test2"
assert parent.attributes["test"] == "test2"

def test_passing_kind_in_ctor(self):
with ContextHelper() as ctx:
trace = tracer_module.Tracer(sampler=AlwaysOnSampler())
parent = trace.start_span()
wrapped_class = OpenCensusSpan(kind=SpanKind.CLIENT)
assert wrapped_class.kind == SpanKind.CLIENT

def test_passing_links_in_ctor(self):
with ContextHelper() as ctx:
trace = tracer_module.Tracer(sampler=AlwaysOnSampler())
parent = trace.start_span()
wrapped_class = OpenCensusSpan(
links=[Link(
headers= {"traceparent": "00-2578531519ed94423ceae67588eff2c9-231ebdc614cb9ddd-01"}
)
]
)
assert len(wrapped_class.span_instance.links) == 1
link = wrapped_class.span_instance.links[0]
assert link.trace_id == "2578531519ed94423ceae67588eff2c9"
assert link.span_id == "231ebdc614cb9ddd"

def test_passing_links_in_ctor_with_attr(self):
attributes = {"attr1": 1}
with ContextHelper() as ctx:
trace = tracer_module.Tracer(sampler=AlwaysOnSampler())
parent = trace.start_span()
wrapped_class = OpenCensusSpan(
links=[Link(
headers= {"traceparent": "00-2578531519ed94423ceae67588eff2c9-231ebdc614cb9ddd-01"},
attributes=attributes
)
]
)
assert len(wrapped_class.span_instance.links) == 1
link = wrapped_class.span_instance.links[0]
assert link.attributes is not None
assert link.trace_id == "2578531519ed94423ceae67588eff2c9"
assert link.span_id == "231ebdc614cb9ddd"


def test_set_http_attributes(self):
with ContextHelper():
trace = tracer_module.Tracer(sampler=AlwaysOnSampler())
Expand Down
2 changes: 1 addition & 1 deletion shared_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pyjwt>=1.7.1
#override azure azure-keyvault~=1.0
#override azure-mgmt-core azure-core<2.0.0,>=1.15.0
#override azure-containerregistry azure-core>=1.4.0,<2.0.0
#override azure-core-tracing-opencensus azure-core<2.0.0,>=1.0.0
#override azure-core-tracing-opencensus azure-core<2.0.0,>=1.13.0
#override azure-core-tracing-opentelemetry azure-core<2.0.0,>=1.13.0
#override azure-cosmos azure-core<2.0.0,>=1.0.0
#override azure-data-tables azure-core<2.0.0,>=1.14.0
Expand Down

0 comments on commit 1242dc2

Please sign in to comment.