def test_distributed_context(self): previous_propagator = get_global_textmap() try: set_global_textmap(MockTextMapPropagator()) result = self.perform_request(self.URL) self.assertEqual(result.text, "Hello!") span = self.assert_span() headers = dict(httpretty.last_request().headers) self.assertIn(MockTextMapPropagator.TRACE_ID_KEY, headers) self.assertEqual( str(span.get_span_context().trace_id), headers[MockTextMapPropagator.TRACE_ID_KEY], ) self.assertIn(MockTextMapPropagator.SPAN_ID_KEY, headers) self.assertEqual( str(span.get_span_context().span_id), headers[MockTextMapPropagator.SPAN_ID_KEY], ) finally: set_global_textmap(previous_propagator)
def test_uninstrument_does_not_inject_headers(self): headers = {} previous_propagator = get_global_textmap() try: set_global_textmap(MockTextMapPropagator()) def intercept_headers(**kwargs): headers.update(kwargs["request"].headers) ec2 = self._make_client("ec2") BotocoreInstrumentor().uninstrument() ec2.meta.events.register_first( "before-send.ec2.DescribeInstances", intercept_headers ) with self.tracer_provider.get_tracer("test").start_span("parent"): ec2.describe_instances() self.assertNotIn(MockTextMapPropagator.TRACE_ID_KEY, headers) self.assertNotIn(MockTextMapPropagator.SPAN_ID_KEY, headers) finally: set_global_textmap(previous_propagator)
def test_client_interceptor_trace_context_propagation( self, ): # pylint: disable=no-self-use """ensure that client interceptor correctly inject trace context into all outgoing requests.""" previous_propagator = get_global_textmap() try: set_global_textmap(MockTextMapPropagator()) interceptor = OpenTelemetryClientInterceptor( trace._DefaultTracer() ) carrier = tuple() def invoker(request, metadata): nonlocal carrier carrier = metadata return {} request = Request(client_id=1, request_data="data") interceptor.intercept_unary( request, {}, _UnaryClientInfo( full_method="/GRPCTestServer/SimpleMethod", timeout=None ), invoker=invoker, ) assert len(carrier) == 2 assert carrier[0][0] == "mock-traceid" assert carrier[0][1] == "0" assert carrier[1][0] == "mock-spanid" assert carrier[1][1] == "0" finally: set_global_textmap(previous_propagator)
def setUpClass(cls): # Save current propagator to be restored on teardown. cls._previous_propagator = get_global_textmap() # Set mock propagator for testing. set_global_textmap(MockTextMapPropagator())