Skip to content

Commit

Permalink
Revert "Revert "Revert import changes to shim""
Browse files Browse the repository at this point in the history
This reverts commit 9d3ef06.
  • Loading branch information
c24t committed Feb 4, 2020
1 parent b348075 commit 0239c08
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import unittest
from unittest import mock

import opentelemetry_example_app.flask_example as flask_example
import requests
from werkzeug.test import Client
from werkzeug.wrappers import BaseResponse

import opentelemetry_example_app.flask_example as flask_example
from opentelemetry.sdk import trace
from opentelemetry.sdk.context.propagation import b3_format

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,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 @@ -125,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.
Expand Down Expand Up @@ -163,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.
Expand Down Expand Up @@ -304,7 +296,7 @@ def get_baggage_item(self, key): # pylint:disable=unused-argument
# 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 @@ -413,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.
Expand Down Expand Up @@ -508,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.
Expand All @@ -530,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):
Expand Down Expand Up @@ -681,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()

Expand All @@ -701,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)
Expand Down
39 changes: 15 additions & 24 deletions ext/opentelemetry-ext-opentracing-shim/tests/test_shim.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@
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
Expand Down Expand Up @@ -66,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)
Expand All @@ -101,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)
Expand Down Expand Up @@ -371,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]
Expand Down Expand Up @@ -458,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)

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

Expand All @@ -497,8 +489,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 @@ -509,8 +500,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 @@ -520,7 +511,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 @@ -532,16 +523,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 0239c08

Please sign in to comment.