From 69b6e14f8f33186845f19e3814025176d1994978 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Mon, 28 Jun 2021 11:02:14 -0700 Subject: [PATCH 1/9] support kind keyword --- .../tracing/ext/opencensus_span/__init__.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) 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..93067221ae7d 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 @@ -17,7 +17,7 @@ from ._version import VERSION try: - from typing import TYPE_CHECKING + from typing import TYPE_CHECKING, Any except ImportError: TYPE_CHECKING = False @@ -61,7 +61,20 @@ def __init__(self, span=None, name="span", **kwargs): :paramtype links: list[~azure.core.tracing.Link] """ tracer = self.get_current_tracer() - self._span_instance = span or tracer.start_span(name=name, **kwargs) + 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)) + + self._span_instance = span or tracer.start_span(name=name, span_kind=kind, **kwargs) @property def span_instance(self): From ea78b450e0841fefdf09b25fca8a8f4b8c926920 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Mon, 28 Jun 2021 11:03:12 -0700 Subject: [PATCH 2/9] changelog --- sdk/core/azure-core-tracing-opencensus/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sdk/core/azure-core-tracing-opencensus/CHANGELOG.md b/sdk/core/azure-core-tracing-opencensus/CHANGELOG.md index f07f01cb65e8..d4856e4aee8a 100644 --- a/sdk/core/azure-core-tracing-opencensus/CHANGELOG.md +++ b/sdk/core/azure-core-tracing-opencensus/CHANGELOG.md @@ -3,6 +3,10 @@ ## 1.0.0b7 (2021-04-08) +- `kind` keyword is now supported while creating the span instance. + +## 1.0.0b7 (2021-04-08) + - `Link` and `SpanKind` can now be added while creating the span instance. ## 1.0.0b6 (2020-05-04) From aba29ba84a2fd3567ab559876006fca22bc6bc74 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Mon, 28 Jun 2021 11:35:44 -0700 Subject: [PATCH 3/9] Unsupport kind --- .../CHANGELOG.md | 4 ++-- .../tracing/ext/opencensus_span/__init__.py | 19 +++---------------- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/sdk/core/azure-core-tracing-opencensus/CHANGELOG.md b/sdk/core/azure-core-tracing-opencensus/CHANGELOG.md index d4856e4aee8a..9470b39c7021 100644 --- a/sdk/core/azure-core-tracing-opencensus/CHANGELOG.md +++ b/sdk/core/azure-core-tracing-opencensus/CHANGELOG.md @@ -1,9 +1,9 @@ # Release History -## 1.0.0b7 (2021-04-08) +## 1.0.0b8 (Unreleased) -- `kind` keyword is now supported while creating the span instance. +- `kind` keyword is not supported while instantiating the span. ## 1.0.0b7 (2021-04-08) 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 93067221ae7d..8645285bdf0e 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 @@ -56,25 +56,12 @@ def __init__(self, span=None, name="span", **kwargs): :type span: :class: opencensus.trace.Span :param name: The name of the OpenCensus span to create if a new span is needed :type name: str - :keyword SpanKind kind: The span kind of this span. - :keyword links: The list of links to be added to the span. :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)) - - self._span_instance = span or tracer.start_span(name=name, span_kind=kind, **kwargs) + # start_span doesn't support kind argument + kind = kwargs.pop("kind", SpanKind.UNSPECIFIED) # pylint: disable=unused-variable + self._span_instance = span or tracer.start_span(name=name, **kwargs) @property def span_instance(self): From a2d17088882d475fc2fbd17caeedd9bb08907eb0 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Mon, 28 Jun 2021 12:20:25 -0700 Subject: [PATCH 4/9] FIx kind kwarg support during instantiating --- .../CHANGELOG.md | 2 +- .../tracing/ext/opencensus_span/__init__.py | 22 +++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/sdk/core/azure-core-tracing-opencensus/CHANGELOG.md b/sdk/core/azure-core-tracing-opencensus/CHANGELOG.md index 9470b39c7021..dbf4f6cf0218 100644 --- a/sdk/core/azure-core-tracing-opencensus/CHANGELOG.md +++ b/sdk/core/azure-core-tracing-opencensus/CHANGELOG.md @@ -3,7 +3,7 @@ ## 1.0.0b8 (Unreleased) -- `kind` keyword is not supported while instantiating the span. +- Fix for supporting `kind` keyword while instantiating the span. ## 1.0.0b7 (2021-04-08) 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 8645285bdf0e..dd39f588711a 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 @@ -17,12 +17,12 @@ from ._version import VERSION try: - from typing import TYPE_CHECKING, Any + from typing import TYPE_CHECKING except ImportError: 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[ @@ -56,12 +56,26 @@ def __init__(self, span=None, name="span", **kwargs): :type span: :class: opencensus.trace.Span :param name: The name of the OpenCensus span to create if a new span is needed :type name: str + :keyword SpanKind kind: The span kind of this span. + :keyword links: The list of links to be added to the span. :paramtype links: list[~azure.core.tracing.Link] """ tracer = self.get_current_tracer() - # start_span doesn't support kind argument - kind = kwargs.pop("kind", SpanKind.UNSPECIFIED) # pylint: disable=unused-variable + 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)) self._span_instance = span or tracer.start_span(name=name, **kwargs) + if kind is not None: + self._span_instance.span_kind = kind @property def span_instance(self): From 457c5d143235e690b8e6136261817ac77c7e79fb Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Mon, 28 Jun 2021 13:43:52 -0700 Subject: [PATCH 5/9] test --- .../tests/test_tracing_implementations.py | 7 +++++++ 1 file changed, 7 insertions(+) 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..aa697ead0bc2 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 @@ -129,6 +129,13 @@ 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_set_http_attributes(self): with ContextHelper(): trace = tracer_module.Tracer(sampler=AlwaysOnSampler()) From 832be3ff2cbbe9d1ee32c45c62e54149fdb5b842 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Mon, 28 Jun 2021 15:12:06 -0700 Subject: [PATCH 6/9] links --- .../tracing/ext/opencensus_span/__init__.py | 17 +++++++++++++++++ .../tests/test_tracing_implementations.py | 14 +++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) 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 dd39f588711a..e2ebec6f7733 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 @@ -73,10 +73,27 @@ def __init__(self, span=None, name="span", **kwargs): ) # 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.links.append( + 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): # type: () -> Span 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 aa697ead0bc2..a1efa87bdf44 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 @@ -136,6 +136,18 @@ def test_passing_kind_in_ctor(self): 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 wrapped_class.kind == SpanKind.CLIENT + def test_set_http_attributes(self): with ContextHelper(): trace = tracer_module.Tracer(sampler=AlwaysOnSampler()) From 099ff5a2cfce3e071f103fd1ba5d2aa0bdaa3250 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Mon, 28 Jun 2021 15:19:04 -0700 Subject: [PATCH 7/9] test fixx --- .../tracing/ext/opencensus_span/__init__.py | 2 +- .../tests/test_tracing_implementations.py | 24 ++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) 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 e2ebec6f7733..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 @@ -83,7 +83,7 @@ def __init__(self, span=None, name="span", **kwargs): try: for link in links: ctx = trace_context_http_header_format.TraceContextPropagator().from_headers(link.headers) - self._span_instance.links.append( + self._span_instance.add_link( Link( trace_id=ctx.trace_id, span_id=ctx.span_id, 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 a1efa87bdf44..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 @@ -146,7 +146,29 @@ def test_passing_links_in_ctor(self): ) ] ) - assert wrapped_class.kind == SpanKind.CLIENT + 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(): From 96e692dd49e7982e568e05edf9110883dc9bf837 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Mon, 28 Jun 2021 16:04:19 -0700 Subject: [PATCH 8/9] update azure core --- sdk/core/azure-core-tracing-opencensus/setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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'], From 168d856317b6392c8950a02dc2a44c4ff9fe35fb Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Mon, 28 Jun 2021 16:28:16 -0700 Subject: [PATCH 9/9] shared req --- shared_requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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