Skip to content

Commit

Permalink
Revert import changes to shim
Browse files Browse the repository at this point in the history
  • Loading branch information
c24t committed Jan 23, 2020
1 parent 4197f02 commit 5a224ad
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,8 @@

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 Down Expand Up @@ -121,7 +113,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.
Expand Down Expand Up @@ -159,7 +151,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.
Expand Down Expand Up @@ -300,7 +292,7 @@ def get_baggage_item(self, key): # pylint: disable=W0613
# 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.
Expand Down Expand Up @@ -409,7 +401,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.
Expand Down Expand Up @@ -504,7 +496,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.
Expand All @@ -526,8 +518,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):
Expand Down Expand Up @@ -676,7 +668,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 UnsupportedFormatException
raise opentracing.UnsupportedFormatException

propagator = propagators.get_global_httptextformat()
propagator.inject(
Expand All @@ -693,7 +685,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)
Expand Down
38 changes: 15 additions & 23 deletions ext/opentelemetry-ext-opentracing-shim/tests/test_shim.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,7 @@
import time
import unittest

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
Expand Down Expand Up @@ -63,15 +55,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)
Expand All @@ -98,7 +90,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)
Expand Down Expand Up @@ -368,7 +360,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]
Expand Down Expand Up @@ -455,7 +447,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)

Expand All @@ -482,7 +474,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))

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

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

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

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

0 comments on commit 5a224ad

Please sign in to comment.