Skip to content

Commit

Permalink
Revert "Revert import changes to shim"
Browse files Browse the repository at this point in the history
This reverts commit 5a224ad.
  • Loading branch information
c24t committed Jan 23, 2020
1 parent ccb97e5 commit fec0fbe
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 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 Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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):
Expand Down Expand Up @@ -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()

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

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

Expand All @@ -521,16 +529,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 fec0fbe

Please sign in to comment.