Skip to content

Commit

Permalink
More pylint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Dec 20, 2019
1 parent 2396db1 commit 79912de
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -101,10 +109,10 @@ def create_tracer(otel_tracer_source):
:class:`opentracing.Tracer` using OpenTelemetry under the hood.
Args:
otel_tracer_source: A :class:`opentelemetry.trace.TracerSource` to be used for
constructing the :class:`TracerShim`. A tracer from this source will be used
to perform the actual tracing when user code is instrumented using
the OpenTracing API.
otel_tracer_source: A :class:`opentelemetry.trace.TracerSource` to be
used for constructing the :class:`TracerShim`. A tracer from this
source will be used to perform the actual tracing when user code is
instrumented using the OpenTracing API.
Returns:
The created :class:`TracerShim`.
Expand All @@ -113,7 +121,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.
Expand Down Expand Up @@ -151,7 +159,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.
Expand Down Expand Up @@ -265,7 +273,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=W0613
"""Implements the ``set_baggage_item()`` method from the base class.
Warning:
Expand All @@ -278,7 +286,7 @@ def set_baggage_item(self, key, value):
)
# TODO: Implement.

def get_baggage_item(self, key):
def get_baggage_item(self, key): # pylint: disable=W0613
"""Implements the ``get_baggage_item()`` method from the base class.
Warning:
Expand All @@ -292,7 +300,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.
Expand Down Expand Up @@ -401,7 +409,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.
Expand Down Expand Up @@ -496,7 +504,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.
Expand All @@ -518,8 +526,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):
Expand Down Expand Up @@ -668,7 +676,7 @@ def inject(self, span_context, 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

propagator = propagators.get_global_httptextformat()
propagator.inject(
Expand All @@ -685,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 opentracing.UnsupportedFormatException
raise UnsupportedFormatException

def get_as_list(dict_object, key):
value = dict_object.get(key)
Expand Down
38 changes: 23 additions & 15 deletions ext/opentelemetry-ext-opentracing-shim/tests/test_shim.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@
import time
import unittest

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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)

Expand All @@ -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))

Expand All @@ -486,7 +494,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))

Expand All @@ -497,8 +505,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."""
Expand All @@ -508,7 +516,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)

Expand All @@ -520,16 +528,16 @@ 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)

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):
Expand Down

0 comments on commit 79912de

Please sign in to comment.