Skip to content

Commit

Permalink
otel-shim: Adding test for empty context from extract
Browse files Browse the repository at this point in the history
  • Loading branch information
toumorokoshi committed Jun 5, 2020
1 parent 9e5ddbb commit c5b960a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,10 @@ def get_as_list(dict_object, key):

propagator = propagators.get_global_httptextformat()
ctx = propagator.extract(get_as_list, carrier)
otel_context = trace_api.get_current_span(ctx).get_context()
span = trace_api.get_current_span(ctx)
if span is not None:
otel_context = span.get_context()
else:
otel_context = trace_api.INVALID_SPAN_CONTEXT

return SpanContextShim(otel_context)
19 changes: 18 additions & 1 deletion ext/opentelemetry-ext-opentracing-shim/tests/test_shim.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
from opentelemetry import propagators, trace
from opentelemetry.ext.opentracing_shim import util
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.test.mock_httptextformat import MockHTTPTextFormat
from opentelemetry.test.mock_httptextformat import (
MockHTTPTextFormat,
NOOPHTTPTextFormat,
)


class TestShim(TestCase):
Expand Down Expand Up @@ -515,6 +518,20 @@ def test_extract_http_headers(self):
self.assertEqual(ctx.unwrap().trace_id, 1220)
self.assertEqual(ctx.unwrap().span_id, 7478)

def test_extract_empty_context_returns_invalid_context(self):
"""In the case where the propagator cannot extract a
SpanContext, extract should return and invalid span context.
"""
_old_propagator = propagators.get_global_httptextformat()
propagators.set_global_httptextformat(NOOPHTTPTextFormat())
try:
carrier = {}

ctx = self.shim.extract(opentracing.Format.HTTP_HEADERS, carrier)
self.assertEqual(ctx.unwrap(), trace.INVALID_SPAN_CONTEXT)
finally:
propagators.set_global_httptextformat(_old_propagator)

def test_extract_text_map(self):
"""Test `extract()` method for Format.TEXT_MAP."""

Expand Down
4 changes: 0 additions & 4 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,10 +747,6 @@ def get_tracer(
),
)

@staticmethod
def get_current_span() -> Span:
return trace_api.get_current_span()

def add_span_processor(self, span_processor: SpanProcessor) -> None:
"""Registers a new :class:`SpanProcessor` for this `TracerProvider`.
Expand Down
26 changes: 25 additions & 1 deletion tests/util/src/opentelemetry/test/mock_httptextformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import typing

from opentelemetry import trace
from opentelemetry.context import Context
from opentelemetry.context import Context, get_current
from opentelemetry.trace.propagation import (
get_current_span,
set_span_in_context,
Expand All @@ -28,6 +28,30 @@
)


class NOOPHTTPTextFormat(HTTPTextFormat):
"""A propagator that does not extract nor inject.
This class is useful for catching edge cases assuming
a SpanContext will always be present.
"""

def extract(
self,
get_from_carrier: Getter[HTTPTextFormatT],
carrier: HTTPTextFormatT,
context: typing.Optional[Context] = None,
) -> Context:
return get_current()

def inject(
self,
set_in_carrier: Setter[HTTPTextFormatT],
carrier: HTTPTextFormatT,
context: typing.Optional[Context] = None,
) -> None:
return None


class MockHTTPTextFormat(HTTPTextFormat):
"""Mock propagator for testing purposes."""

Expand Down

0 comments on commit c5b960a

Please sign in to comment.