def extract(self, format, carrier): """Implements the ``extract`` method from the base class.""" # TODO: Finish documentation. # pylint: disable=redefined-builtin # This implementation does not perform the extracing by itself but # uses the configured propagators in opentelemetry.propagators. # TODO: Support Format.BINARY once it is supported in # opentelemetry-python. if format not in self._supported_formats: raise opentracing.UnsupportedFormatException def get_as_list(dict_object, key): value = dict_object.get(key) return [value] if value is not None else [] propagator = propagators.get_global_httptextformat() ctx = propagator.extract(get_as_list, carrier) 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)
def test_distributed_context(self): previous_propagator = propagators.get_global_httptextformat() try: propagators.set_global_httptextformat(MockHTTPTextFormat()) result = requests.get(self.URL) self.assertEqual(result.text, "Hello!") span_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(span_list), 1) span = span_list[0] headers = dict(httpretty.last_request().headers) self.assertIn(MockHTTPTextFormat.TRACE_ID_KEY, headers) self.assertEqual( str(span.get_context().trace_id), headers[MockHTTPTextFormat.TRACE_ID_KEY], ) self.assertIn(MockHTTPTextFormat.SPAN_ID_KEY, headers) self.assertEqual( str(span.get_context().span_id), headers[MockHTTPTextFormat.SPAN_ID_KEY], ) finally: propagators.set_global_httptextformat(previous_propagator)
def inject(self, span_context, format: object, carrier: object): """Injects ``span_context`` into ``carrier``. See base class for more details. Args: span_context: The ``opentracing.SpanContext`` to inject. format: a Python object instance that represents a given carrier format. `format` may be of any type, and `format` equality is defined by Python ``==`` operator. carrier: the format-specific carrier object to inject into """ # pylint: disable=redefined-builtin # This implementation does not perform the injecting by itself but # uses the configured propagators in opentelemetry.propagators. # TODO: Support Format.BINARY once it is supported in # opentelemetry-python. if format not in self._supported_formats: raise UnsupportedFormatException propagator = propagators.get_global_httptextformat() ctx = set_span_in_context(DefaultSpan(span_context.unwrap())) propagator.inject(type(carrier).__setitem__, carrier, context=ctx)
def setUpClass(cls): """Set preferred tracer implementation only once rather than before every test method. """ trace.set_preferred_tracer_implementation(lambda T: Tracer()) # Save current propagator to be restored on teardown. cls._previous_propagator = propagators.get_global_httptextformat() # Set mock propagator for testing. propagators.set_global_httptextformat(MockHTTPTextFormat)
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 inject(self, span_context, format, carrier): """Implements the ``inject`` method from the base class.""" # TODO: Finish documentation. # pylint: disable=redefined-builtin # This implementation does not perform the injecting by itself but # uses the configured propagators in opentelemetry.propagators. # TODO: Support Format.BINARY once it is supported in # opentelemetry-python. if format not in self._supported_formats: raise opentracing.UnsupportedFormatException propagator = propagators.get_global_httptextformat() propagator.inject(span_context.unwrap(), type(carrier).__setitem__, carrier)
def extract(self, format: object, carrier: object): """Returns an ``opentracing.SpanContext`` instance extracted from a ``carrier``. See base class for more details. Args: format: a Python object instance that represents a given carrier format. ``format`` may be of any type, and ``format`` equality is defined by python ``==`` operator. carrier: the format-specific carrier object to extract from Returns: An ``opentracing.SpanContext`` extracted from ``carrier`` or ``None`` if no such ``SpanContext`` could be found. """ # pylint: disable=redefined-builtin # This implementation does not perform the extracing by itself but # uses the configured propagators in opentelemetry.propagators. # TODO: Support Format.BINARY once it is supported in # opentelemetry-python. if format not in self._supported_formats: raise UnsupportedFormatException def get_as_list(dict_object, key): value = dict_object.get(key) return [value] if value is not None else [] propagator = propagators.get_global_httptextformat() ctx = propagator.extract(get_as_list, carrier) span = get_current_span(ctx) if span is not None: otel_context = span.get_context() else: otel_context = INVALID_SPAN_CONTEXT return SpanContextShim(otel_context)
def test_distributed_context(self): previous_propagator = propagators.get_global_httptextformat() try: propagators.set_global_httptextformat(MockHTTPTextFormat()) result = self.perform_request(self.URL) self.assertEqual(result.text, "Hello!") span = self.assert_span() headers = dict(httpretty.last_request().headers) self.assertIn(MockHTTPTextFormat.TRACE_ID_KEY, headers) self.assertEqual( str(span.get_context().trace_id), headers[MockHTTPTextFormat.TRACE_ID_KEY], ) self.assertIn(MockHTTPTextFormat.SPAN_ID_KEY, headers) self.assertEqual( str(span.get_context().span_id), headers[MockHTTPTextFormat.SPAN_ID_KEY], ) finally: propagators.set_global_httptextformat(previous_propagator)
def setUpClass(cls): # Save current propagator to be restored on teardown. cls._previous_propagator = propagators.get_global_httptextformat() # Set mock propagator for testing. propagators.set_global_httptextformat(MockHTTPTextFormat())
DatadogSpanExporter, ) from opentelemetry.ext.datadog.propagator import DatadogFormat from opentelemetry.sdk.trace import TracerProvider app = Flask(__name__) trace.set_tracer_provider(TracerProvider()) trace.get_tracer_provider().add_span_processor( DatadogExportSpanProcessor( DatadogSpanExporter(agent_url="http://*****:*****@app.route("/server_request") def server_request():