From fec0fbef1cf35434c61bc729d0a9b5a4d62c761a Mon Sep 17 00:00:00 2001 From: Chris Kleinknecht Date: Wed, 22 Jan 2020 17:01:43 -0800 Subject: [PATCH 1/2] Revert "Revert import changes to shim" This reverts commit 5a224ad843b552beb484f6b1c01306685634e46e. --- .../ext/opentracing_shim/__init__.py | 28 +++++++++----- .../tests/test_shim.py | 38 +++++++++++-------- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/ext/opentelemetry-ext-opentracing-shim/src/opentelemetry/ext/opentracing_shim/__init__.py b/ext/opentelemetry-ext-opentracing-shim/src/opentelemetry/ext/opentracing_shim/__init__.py index ce35681d3f5..7bef8f57299 100644 --- a/ext/opentelemetry-ext-opentracing-shim/src/opentelemetry/ext/opentracing_shim/__init__.py +++ b/ext/opentelemetry-ext-opentracing-shim/src/opentelemetry/ext/opentracing_shim/__init__.py @@ -82,8 +82,16 @@ import logging -import opentracing from deprecated import deprecated +from opentracing import ( # pylint: disable=no-name-in-module + Format, + Scope, + ScopeManager, + Span, + SpanContext, + Tracer, + UnsupportedFormatException, +) import opentelemetry.trace as trace_api from opentelemetry import propagators @@ -114,7 +122,7 @@ def create_tracer(otel_tracer_source): return TracerShim(otel_tracer_source.get_tracer(__name__, __version__)) -class SpanContextShim(opentracing.SpanContext): +class SpanContextShim(SpanContext): """Implements :class:`opentracing.SpanContext` by wrapping a :class:`opentelemetry.trace.SpanContext` object. @@ -152,7 +160,7 @@ def baggage(self): # TODO: Implement. -class SpanShim(opentracing.Span): +class SpanShim(Span): """Implements :class:`opentracing.Span` by wrapping a :class:`opentelemetry.trace.Span` object. @@ -293,7 +301,7 @@ def get_baggage_item(self, key): # TODO: Implement. -class ScopeShim(opentracing.Scope): +class ScopeShim(Scope): """A `ScopeShim` wraps the OpenTelemetry functionality related to span activation/deactivation while using OpenTracing :class:`opentracing.Scope` objects for presentation. @@ -402,7 +410,7 @@ def close(self): self._span.unwrap().end() -class ScopeManagerShim(opentracing.ScopeManager): +class ScopeManagerShim(ScopeManager): """Implements :class:`opentracing.ScopeManager` by setting and getting the active `opentelemetry.trace.Span` in the OpenTelemetry tracer. @@ -497,7 +505,7 @@ def tracer(self): return self._tracer -class TracerShim(opentracing.Tracer): +class TracerShim(Tracer): """Implements :class:`opentracing.Tracer` by wrapping a :class:`opentelemetry.trace.Tracer` object. @@ -519,8 +527,8 @@ def __init__(self, tracer): super().__init__(scope_manager=ScopeManagerShim(self)) self._otel_tracer = tracer self._supported_formats = ( - opentracing.Format.TEXT_MAP, - opentracing.Format.HTTP_HEADERS, + Format.TEXT_MAP, + Format.HTTP_HEADERS, ) def unwrap(self): @@ -670,7 +678,7 @@ def inject(self, span_context, format, carrier): # opentelemetry-python. if format not in self._supported_formats: - raise opentracing.UnsupportedFormatException + raise UnsupportedFormatException propagator = propagators.get_global_httptextformat() @@ -690,7 +698,7 @@ def extract(self, format, carrier): # TODO: Support Format.BINARY once it is supported in # opentelemetry-python. if format not in self._supported_formats: - raise opentracing.UnsupportedFormatException + raise UnsupportedFormatException def get_as_list(dict_object, key): value = dict_object.get(key) diff --git a/ext/opentelemetry-ext-opentracing-shim/tests/test_shim.py b/ext/opentelemetry-ext-opentracing-shim/tests/test_shim.py index 997f97195eb..c3bfc35f3bf 100644 --- a/ext/opentelemetry-ext-opentracing-shim/tests/test_shim.py +++ b/ext/opentelemetry-ext-opentracing-shim/tests/test_shim.py @@ -15,7 +15,15 @@ import time from unittest import TestCase -import opentracing +from opentracing import ( # pylint: disable=no-name-in-module + Format, + Scope, + Span, + SpanContext, + Tracer, + UnsupportedFormatException, + child_of, +) import opentelemetry.ext.opentracing_shim as opentracingshim from opentelemetry import propagators, trace @@ -55,15 +63,15 @@ def tearDownClass(cls): def test_shim_type(self): # Verify shim is an OpenTracing tracer. - self.assertIsInstance(self.shim, opentracing.Tracer) + self.assertIsInstance(self.shim, Tracer) def test_start_active_span(self): """Test span creation and activation using `start_active_span()`.""" with self.shim.start_active_span("TestSpan") as scope: # Verify correct type of Scope and Span objects. - self.assertIsInstance(scope, opentracing.Scope) - self.assertIsInstance(scope.span, opentracing.Span) + self.assertIsInstance(scope, Scope) + self.assertIsInstance(scope.span, Span) # Verify span is started. self.assertIsNotNone(scope.span.unwrap().start_time) @@ -90,7 +98,7 @@ def test_start_span(self): with self.shim.start_span("TestSpan") as span: # Verify correct type of Span object. - self.assertIsInstance(span, opentracing.Span) + self.assertIsInstance(span, Span) # Verify span is started. self.assertIsNotNone(span.unwrap().start_time) @@ -360,7 +368,7 @@ def test_references(self): """Test span creation using the `references` argument.""" with self.shim.start_span("ParentSpan") as parent: - ref = opentracing.child_of(parent.context) + ref = child_of(parent.context) with self.shim.start_active_span( "ChildSpan", references=[ref] @@ -447,7 +455,7 @@ def test_span_context(self): otel_context = trace.SpanContext(1234, 5678) context = opentracingshim.SpanContextShim(otel_context) - self.assertIsInstance(context, opentracing.SpanContext) + self.assertIsInstance(context, SpanContext) self.assertEqual(context.unwrap().trace_id, 1234) self.assertEqual(context.unwrap().span_id, 5678) @@ -474,7 +482,7 @@ def test_inject_http_headers(self): context = opentracingshim.SpanContextShim(otel_context) headers = {} - self.shim.inject(context, opentracing.Format.HTTP_HEADERS, headers) + self.shim.inject(context, Format.HTTP_HEADERS, headers) self.assertEqual(headers[MockHTTPTextFormat.TRACE_ID_KEY], str(1220)) self.assertEqual(headers[MockHTTPTextFormat.SPAN_ID_KEY], str(7478)) @@ -487,7 +495,7 @@ def test_inject_text_map(self): # Verify Format.TEXT_MAP text_map = {} - self.shim.inject(context, opentracing.Format.TEXT_MAP, text_map) + self.shim.inject(context, Format.TEXT_MAP, text_map) self.assertEqual(text_map[MockHTTPTextFormat.TRACE_ID_KEY], str(1220)) self.assertEqual(text_map[MockHTTPTextFormat.SPAN_ID_KEY], str(7478)) @@ -498,8 +506,8 @@ def test_inject_binary(self): context = opentracingshim.SpanContextShim(otel_context) # Verify exception for non supported binary format. - with self.assertRaises(opentracing.UnsupportedFormatException): - self.shim.inject(context, opentracing.Format.BINARY, bytearray()) + with self.assertRaises(UnsupportedFormatException): + self.shim.inject(context, Format.BINARY, bytearray()) def test_extract_http_headers(self): """Test `extract()` method for Format.HTTP_HEADERS.""" @@ -509,7 +517,7 @@ def test_extract_http_headers(self): MockHTTPTextFormat.SPAN_ID_KEY: 7478, } - ctx = self.shim.extract(opentracing.Format.HTTP_HEADERS, carrier) + ctx = self.shim.extract(Format.HTTP_HEADERS, carrier) self.assertEqual(ctx.unwrap().trace_id, 1220) self.assertEqual(ctx.unwrap().span_id, 7478) @@ -521,7 +529,7 @@ def test_extract_text_map(self): MockHTTPTextFormat.SPAN_ID_KEY: 7478, } - ctx = self.shim.extract(opentracing.Format.TEXT_MAP, carrier) + ctx = self.shim.extract(Format.TEXT_MAP, carrier) self.assertEqual(ctx.unwrap().trace_id, 1220) self.assertEqual(ctx.unwrap().span_id, 7478) @@ -529,8 +537,8 @@ def test_extract_binary(self): """Test `extract()` method for Format.BINARY.""" # Verify exception for non supported binary format. - with self.assertRaises(opentracing.UnsupportedFormatException): - self.shim.extract(opentracing.Format.BINARY, bytearray()) + with self.assertRaises(UnsupportedFormatException): + self.shim.extract(Format.BINARY, bytearray()) class MockHTTPTextFormat(HTTPTextFormat): From 01619bc41631d6f7fff9726c4fd4b85f00740473 Mon Sep 17 00:00:00 2001 From: Chris Kleinknecht Date: Thu, 23 Jan 2020 13:55:36 -0800 Subject: [PATCH 2/2] Use pylint ignore directives instead --- .../ext/opentracing_shim/__init__.py | 35 +++++++--------- .../tests/test_shim.py | 41 ++++++++----------- 2 files changed, 33 insertions(+), 43 deletions(-) diff --git a/ext/opentelemetry-ext-opentracing-shim/src/opentelemetry/ext/opentracing_shim/__init__.py b/ext/opentelemetry-ext-opentracing-shim/src/opentelemetry/ext/opentracing_shim/__init__.py index 7bef8f57299..b7753754db0 100644 --- a/ext/opentelemetry-ext-opentracing-shim/src/opentelemetry/ext/opentracing_shim/__init__.py +++ b/ext/opentelemetry-ext-opentracing-shim/src/opentelemetry/ext/opentracing_shim/__init__.py @@ -80,18 +80,13 @@ https://docs.python.org/3/tutorial/floatingpoint.html """ +# TODO: make pylint use 3p opentracing module for type inference +# pylint:disable=no-member + import logging +import opentracing from deprecated import deprecated -from opentracing import ( # pylint: disable=no-name-in-module - Format, - Scope, - ScopeManager, - Span, - SpanContext, - Tracer, - UnsupportedFormatException, -) import opentelemetry.trace as trace_api from opentelemetry import propagators @@ -122,7 +117,7 @@ def create_tracer(otel_tracer_source): return TracerShim(otel_tracer_source.get_tracer(__name__, __version__)) -class SpanContextShim(SpanContext): +class SpanContextShim(opentracing.SpanContext): """Implements :class:`opentracing.SpanContext` by wrapping a :class:`opentelemetry.trace.SpanContext` object. @@ -160,7 +155,7 @@ def baggage(self): # TODO: Implement. -class SpanShim(Span): +class SpanShim(opentracing.Span): """Implements :class:`opentracing.Span` by wrapping a :class:`opentelemetry.trace.Span` object. @@ -274,7 +269,7 @@ def log(self, **kwargs): def log_event(self, event, payload=None): super().log_event(event, payload=payload) - def set_baggage_item(self, key, value): + def set_baggage_item(self, key, value): # pylint:disable=unused-argument """Implements the ``set_baggage_item()`` method from the base class. Warning: @@ -287,7 +282,7 @@ def set_baggage_item(self, key, value): ) # TODO: Implement. - def get_baggage_item(self, key): + def get_baggage_item(self, key): # pylint:disable=unused-argument """Implements the ``get_baggage_item()`` method from the base class. Warning: @@ -301,7 +296,7 @@ def get_baggage_item(self, key): # TODO: Implement. -class ScopeShim(Scope): +class ScopeShim(opentracing.Scope): """A `ScopeShim` wraps the OpenTelemetry functionality related to span activation/deactivation while using OpenTracing :class:`opentracing.Scope` objects for presentation. @@ -410,7 +405,7 @@ def close(self): self._span.unwrap().end() -class ScopeManagerShim(ScopeManager): +class ScopeManagerShim(opentracing.ScopeManager): """Implements :class:`opentracing.ScopeManager` by setting and getting the active `opentelemetry.trace.Span` in the OpenTelemetry tracer. @@ -505,7 +500,7 @@ def tracer(self): return self._tracer -class TracerShim(Tracer): +class TracerShim(opentracing.Tracer): """Implements :class:`opentracing.Tracer` by wrapping a :class:`opentelemetry.trace.Tracer` object. @@ -527,8 +522,8 @@ def __init__(self, tracer): super().__init__(scope_manager=ScopeManagerShim(self)) self._otel_tracer = tracer self._supported_formats = ( - Format.TEXT_MAP, - Format.HTTP_HEADERS, + opentracing.Format.TEXT_MAP, + opentracing.Format.HTTP_HEADERS, ) def unwrap(self): @@ -678,7 +673,7 @@ def inject(self, span_context, format, carrier): # opentelemetry-python. if format not in self._supported_formats: - raise UnsupportedFormatException + raise opentracing.UnsupportedFormatException propagator = propagators.get_global_httptextformat() @@ -698,7 +693,7 @@ def extract(self, format, carrier): # TODO: Support Format.BINARY once it is supported in # opentelemetry-python. if format not in self._supported_formats: - raise UnsupportedFormatException + raise opentracing.UnsupportedFormatException def get_as_list(dict_object, key): value = dict_object.get(key) diff --git a/ext/opentelemetry-ext-opentracing-shim/tests/test_shim.py b/ext/opentelemetry-ext-opentracing-shim/tests/test_shim.py index c3bfc35f3bf..fb82cb44027 100644 --- a/ext/opentelemetry-ext-opentracing-shim/tests/test_shim.py +++ b/ext/opentelemetry-ext-opentracing-shim/tests/test_shim.py @@ -12,18 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. +# TODO: make pylint use 3p opentracing module for type inference +# pylint:disable=no-member + import time from unittest import TestCase -from opentracing import ( # pylint: disable=no-name-in-module - Format, - Scope, - Span, - SpanContext, - Tracer, - UnsupportedFormatException, - child_of, -) +import opentracing import opentelemetry.ext.opentracing_shim as opentracingshim from opentelemetry import propagators, trace @@ -63,15 +58,15 @@ def tearDownClass(cls): def test_shim_type(self): # Verify shim is an OpenTracing tracer. - self.assertIsInstance(self.shim, Tracer) + self.assertIsInstance(self.shim, opentracing.Tracer) def test_start_active_span(self): """Test span creation and activation using `start_active_span()`.""" with self.shim.start_active_span("TestSpan") as scope: # Verify correct type of Scope and Span objects. - self.assertIsInstance(scope, Scope) - self.assertIsInstance(scope.span, Span) + self.assertIsInstance(scope, opentracing.Scope) + self.assertIsInstance(scope.span, opentracing.Span) # Verify span is started. self.assertIsNotNone(scope.span.unwrap().start_time) @@ -98,7 +93,7 @@ def test_start_span(self): with self.shim.start_span("TestSpan") as span: # Verify correct type of Span object. - self.assertIsInstance(span, Span) + self.assertIsInstance(span, opentracing.Span) # Verify span is started. self.assertIsNotNone(span.unwrap().start_time) @@ -368,7 +363,7 @@ def test_references(self): """Test span creation using the `references` argument.""" with self.shim.start_span("ParentSpan") as parent: - ref = child_of(parent.context) + ref = opentracing.child_of(parent.context) with self.shim.start_active_span( "ChildSpan", references=[ref] @@ -455,7 +450,7 @@ def test_span_context(self): otel_context = trace.SpanContext(1234, 5678) context = opentracingshim.SpanContextShim(otel_context) - self.assertIsInstance(context, SpanContext) + self.assertIsInstance(context, opentracing.SpanContext) self.assertEqual(context.unwrap().trace_id, 1234) self.assertEqual(context.unwrap().span_id, 5678) @@ -482,7 +477,7 @@ def test_inject_http_headers(self): context = opentracingshim.SpanContextShim(otel_context) headers = {} - self.shim.inject(context, Format.HTTP_HEADERS, headers) + self.shim.inject(context, opentracing.Format.HTTP_HEADERS, headers) self.assertEqual(headers[MockHTTPTextFormat.TRACE_ID_KEY], str(1220)) self.assertEqual(headers[MockHTTPTextFormat.SPAN_ID_KEY], str(7478)) @@ -495,7 +490,7 @@ def test_inject_text_map(self): # Verify Format.TEXT_MAP text_map = {} - self.shim.inject(context, Format.TEXT_MAP, text_map) + self.shim.inject(context, opentracing.Format.TEXT_MAP, text_map) self.assertEqual(text_map[MockHTTPTextFormat.TRACE_ID_KEY], str(1220)) self.assertEqual(text_map[MockHTTPTextFormat.SPAN_ID_KEY], str(7478)) @@ -506,8 +501,8 @@ def test_inject_binary(self): context = opentracingshim.SpanContextShim(otel_context) # Verify exception for non supported binary format. - with self.assertRaises(UnsupportedFormatException): - self.shim.inject(context, Format.BINARY, bytearray()) + with self.assertRaises(opentracing.UnsupportedFormatException): + self.shim.inject(context, opentracing.Format.BINARY, bytearray()) def test_extract_http_headers(self): """Test `extract()` method for Format.HTTP_HEADERS.""" @@ -517,7 +512,7 @@ def test_extract_http_headers(self): MockHTTPTextFormat.SPAN_ID_KEY: 7478, } - ctx = self.shim.extract(Format.HTTP_HEADERS, carrier) + ctx = self.shim.extract(opentracing.Format.HTTP_HEADERS, carrier) self.assertEqual(ctx.unwrap().trace_id, 1220) self.assertEqual(ctx.unwrap().span_id, 7478) @@ -529,7 +524,7 @@ def test_extract_text_map(self): MockHTTPTextFormat.SPAN_ID_KEY: 7478, } - ctx = self.shim.extract(Format.TEXT_MAP, carrier) + ctx = self.shim.extract(opentracing.Format.TEXT_MAP, carrier) self.assertEqual(ctx.unwrap().trace_id, 1220) self.assertEqual(ctx.unwrap().span_id, 7478) @@ -537,8 +532,8 @@ def test_extract_binary(self): """Test `extract()` method for Format.BINARY.""" # Verify exception for non supported binary format. - with self.assertRaises(UnsupportedFormatException): - self.shim.extract(Format.BINARY, bytearray()) + with self.assertRaises(opentracing.UnsupportedFormatException): + self.shim.extract(opentracing.Format.BINARY, bytearray()) class MockHTTPTextFormat(HTTPTextFormat):