diff --git a/sdk/core/azure-core-tracing-opencensus/CHANGELOG.md b/sdk/core/azure-core-tracing-opencensus/CHANGELOG.md index f07f01cb65e8..dbf4f6cf0218 100644 --- a/sdk/core/azure-core-tracing-opencensus/CHANGELOG.md +++ b/sdk/core/azure-core-tracing-opencensus/CHANGELOG.md @@ -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. diff --git a/sdk/core/azure-core-tracing-opencensus/azure/core/tracing/ext/opencensus_span/__init__.py b/sdk/core/azure-core-tracing-opencensus/azure/core/tracing/ext/opencensus_span/__init__.py index 89cfbf342f9d..6aa9f9974582 100644 --- a/sdk/core/azure-core-tracing-opencensus/azure/core/tracing/ext/opencensus_span/__init__.py +++ b/sdk/core/azure-core-tracing-opencensus/azure/core/tracing/ext/opencensus_span/__init__.py @@ -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[ @@ -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): diff --git a/sdk/core/azure-core-tracing-opencensus/setup.py b/sdk/core/azure-core-tracing-opencensus/setup.py index 7ab9fb4027cb..c70433333ca1 100644 --- a/sdk/core/azure-core-tracing-opencensus/setup.py +++ b/sdk/core/azure-core-tracing-opencensus/setup.py @@ -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'], diff --git a/sdk/core/azure-core-tracing-opencensus/tests/test_tracing_implementations.py b/sdk/core/azure-core-tracing-opencensus/tests/test_tracing_implementations.py index ecaf8ac96ee4..885f00816d0e 100644 --- a/sdk/core/azure-core-tracing-opencensus/tests/test_tracing_implementations.py +++ b/sdk/core/azure-core-tracing-opencensus/tests/test_tracing_implementations.py @@ -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 @@ -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()) diff --git a/shared_requirements.txt b/shared_requirements.txt index eb6118a522c0..9b63a6c413fb 100644 --- a/shared_requirements.txt +++ b/shared_requirements.txt @@ -129,7 +129,7 @@ pyjwt>=1.7.1 #override azure azure-keyvault~=1.0 #override azure-mgmt-core azure-core<2.0.0,>=1.13.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