Skip to content

Commit 57e9798

Browse files
author
Rakshith Bhyravabhotla
authored
Support kind keyword in Opencensus (Azure#19548)
* support kind keyword * changelog * Unsupport kind * FIx kind kwarg support during instantiating * test * links * test fixx * update azure core * shared req
1 parent 5f9a50c commit 57e9798

File tree

5 files changed

+80
-4
lines changed

5 files changed

+80
-4
lines changed

sdk/core/azure-core-tracing-opencensus/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11

22
# Release History
33

4+
## 1.0.0b8 (Unreleased)
5+
6+
- Fix for supporting `kind` keyword while instantiating the span.
7+
48
## 1.0.0b7 (2021-04-08)
59

610
- `Link` and `SpanKind` can now be added while creating the span instance.

sdk/core/azure-core-tracing-opencensus/azure/core/tracing/ext/opencensus_span/__init__.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
TYPE_CHECKING = False
2323

2424
if TYPE_CHECKING:
25-
from typing import Dict, Optional, Union, Callable, Sequence
25+
from typing import Dict, Optional, Union, Callable, Sequence, Any
2626

2727
from azure.core.pipeline.transport import HttpRequest, HttpResponse
2828
AttributeValue = Union[
@@ -61,7 +61,38 @@ def __init__(self, span=None, name="span", **kwargs):
6161
:paramtype links: list[~azure.core.tracing.Link]
6262
"""
6363
tracer = self.get_current_tracer()
64+
value = kwargs.pop('kind', None)
65+
kind = (
66+
OpenCensusSpanKind.CLIENT if value == SpanKind.CLIENT else
67+
OpenCensusSpanKind.CLIENT if value == SpanKind.PRODUCER else # No producer in opencensus
68+
OpenCensusSpanKind.SERVER if value == SpanKind.SERVER else
69+
OpenCensusSpanKind.CLIENT if value == SpanKind.CONSUMER else # No consumer in opencensus
70+
OpenCensusSpanKind.UNSPECIFIED if value == SpanKind.INTERNAL else # No internal in opencensus
71+
OpenCensusSpanKind.UNSPECIFIED if value == SpanKind.UNSPECIFIED else
72+
None
73+
) # type: SpanKind
74+
if value and kind is None:
75+
raise ValueError("Kind {} is not supported in OpenCensus".format(value))
76+
77+
links = kwargs.pop('links', None)
6478
self._span_instance = span or tracer.start_span(name=name, **kwargs)
79+
if kind is not None:
80+
self._span_instance.span_kind = kind
81+
82+
if links:
83+
try:
84+
for link in links:
85+
ctx = trace_context_http_header_format.TraceContextPropagator().from_headers(link.headers)
86+
self._span_instance.add_link(
87+
Link(
88+
trace_id=ctx.trace_id,
89+
span_id=ctx.span_id,
90+
attributes=link.attributes
91+
))
92+
except AttributeError:
93+
# we will just send the links as is if it's not ~azure.core.tracing.Link without any validation
94+
# assuming user knows what they are doing.
95+
self._span_instance.links = links
6596

6697
@property
6798
def span_instance(self):

sdk/core/azure-core-tracing-opencensus/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
'opencensus>=0.6.0',
6262
'opencensus-ext-azure>=0.3.1',
6363
'opencensus-ext-threading',
64-
'azure-core<2.0.0,>=1.0.0',
64+
'azure-core<2.0.0,>=1.13.0',
6565
],
6666
extras_require={
6767
":python_version<'3.5'": ['typing'],

sdk/core/azure-core-tracing-opencensus/tests/test_tracing_implementations.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import mock
1313

1414
from azure.core.tracing.ext.opencensus_span import OpenCensusSpan
15-
from azure.core.tracing import SpanKind
15+
from azure.core.tracing import SpanKind, Link
1616
from opencensus.trace import tracer as tracer_module
1717
from opencensus.trace.attributes import Attributes
1818
from opencensus.trace.span import SpanKind as OpenCensusSpanKind
@@ -129,6 +129,47 @@ def test_add_attribute(self):
129129
assert wrapped_class.span_instance.attributes["test"] == "test2"
130130
assert parent.attributes["test"] == "test2"
131131

132+
def test_passing_kind_in_ctor(self):
133+
with ContextHelper() as ctx:
134+
trace = tracer_module.Tracer(sampler=AlwaysOnSampler())
135+
parent = trace.start_span()
136+
wrapped_class = OpenCensusSpan(kind=SpanKind.CLIENT)
137+
assert wrapped_class.kind == SpanKind.CLIENT
138+
139+
def test_passing_links_in_ctor(self):
140+
with ContextHelper() as ctx:
141+
trace = tracer_module.Tracer(sampler=AlwaysOnSampler())
142+
parent = trace.start_span()
143+
wrapped_class = OpenCensusSpan(
144+
links=[Link(
145+
headers= {"traceparent": "00-2578531519ed94423ceae67588eff2c9-231ebdc614cb9ddd-01"}
146+
)
147+
]
148+
)
149+
assert len(wrapped_class.span_instance.links) == 1
150+
link = wrapped_class.span_instance.links[0]
151+
assert link.trace_id == "2578531519ed94423ceae67588eff2c9"
152+
assert link.span_id == "231ebdc614cb9ddd"
153+
154+
def test_passing_links_in_ctor_with_attr(self):
155+
attributes = {"attr1": 1}
156+
with ContextHelper() as ctx:
157+
trace = tracer_module.Tracer(sampler=AlwaysOnSampler())
158+
parent = trace.start_span()
159+
wrapped_class = OpenCensusSpan(
160+
links=[Link(
161+
headers= {"traceparent": "00-2578531519ed94423ceae67588eff2c9-231ebdc614cb9ddd-01"},
162+
attributes=attributes
163+
)
164+
]
165+
)
166+
assert len(wrapped_class.span_instance.links) == 1
167+
link = wrapped_class.span_instance.links[0]
168+
assert link.attributes is not None
169+
assert link.trace_id == "2578531519ed94423ceae67588eff2c9"
170+
assert link.span_id == "231ebdc614cb9ddd"
171+
172+
132173
def test_set_http_attributes(self):
133174
with ContextHelper():
134175
trace = tracer_module.Tracer(sampler=AlwaysOnSampler())

shared_requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ pyjwt>=1.7.1
129129
#override azure azure-keyvault~=1.0
130130
#override azure-mgmt-core azure-core<2.0.0,>=1.15.0
131131
#override azure-containerregistry azure-core>=1.4.0,<2.0.0
132-
#override azure-core-tracing-opencensus azure-core<2.0.0,>=1.0.0
132+
#override azure-core-tracing-opencensus azure-core<2.0.0,>=1.13.0
133133
#override azure-core-tracing-opentelemetry azure-core<2.0.0,>=1.13.0
134134
#override azure-cosmos azure-core<2.0.0,>=1.0.0
135135
#override azure-data-tables azure-core<2.0.0,>=1.14.0

0 commit comments

Comments
 (0)