def test_span_with_opencensus_complicated(self, value): with ContextHelper(tracer_to_use=value) as ctx: exporter = MockExporter() trace = tracer_module.Tracer(sampler=AlwaysOnSampler(), exporter=exporter) with trace.start_span(name="OverAll") as parent: client = MockClient() client.make_request(2) with trace.span("child") as child: time.sleep(0.001) client.make_request(2, parent_span=parent) assert OpenCensusSpan.get_current_span() == child client.make_request(2) trace.finish() exporter.build_tree() parent = exporter.root assert len(parent.children) == 4 assert parent.children[0].span_data.name == "MockClient.__init__" assert parent.children[1].span_data.name == "MockClient.make_request" assert parent.children[1].children[0].span_data.name == "MockClient.get_foo" assert parent.children[1].children[1].span_data.name == "MockClient.make_request" assert parent.children[2].span_data.name == "child" assert parent.children[2].children[0].span_data.name == "MockClient.make_request" assert parent.children[3].span_data.name == "MockClient.make_request" assert parent.children[3].children[0].span_data.name == "MockClient.get_foo" assert parent.children[3].children[1].span_data.name == "MockClient.make_request" children = parent.children[1].children assert len(children) == 2
async def test_span_with_exception(value): """Assert that if an exception is raised, the next sibling method is actually a sibling span. """ with ContextHelper(tracer_to_use=value): exporter = MockExporter() trace = tracer_module.Tracer(sampler=AlwaysOnSampler(), exporter=exporter) with trace.span("overall"): client = MockClient() try: await client.raising_exception() except: pass await client.get_foo() trace.finish() exporter.build_tree() parent = exporter.root assert len(parent.children) == 3 assert parent.children[0].span_data.name == "MockClient.__init__" assert parent.children[ 1].span_data.name == "MockClient.raising_exception" # Exception should propagate status for Opencensus assert parent.children[ 1].span_data.status.message == 'Something went horribly wrong here' assert parent.children[2].span_data.name == "MockClient.get_foo"
def test_distributed_tracing_policy_badurl(caplog): """Test policy with a bad url that will throw, and be sure policy ignores it""" with ContextHelper(): exporter = MockExporter() trace = tracer_module.Tracer(sampler=AlwaysOnSampler(), exporter=exporter) with trace.span("parent"): policy = DistributedTracingPolicy() request = HttpRequest("GET", "http://[[[") request.headers["x-ms-client-request-id"] = "some client request id" pipeline_request = PipelineRequest(request, PipelineContext(None)) with caplog.at_level(logging.WARNING, logger="azure.core.pipeline.policies.distributed_tracing"): policy.on_request(pipeline_request) assert "Unable to start network span" in caplog.text response = HttpResponse(request, None) response.headers = request.headers response.status_code = 202 response.headers["x-ms-request-id"] = "some request id" ctx = trace.span_context header = trace.propagator.to_headers(ctx) assert request.headers.get("traceparent") is None # Got not network trace policy.on_response(pipeline_request, PipelineResponse(request, response, PipelineContext(None))) time.sleep(0.001) policy.on_request(pipeline_request) policy.on_exception(pipeline_request) trace.finish() exporter.build_tree() parent = exporter.root assert len(parent.children) == 0
def test_distributed_tracing_policy_with_user_agent(): """Test policy working with user agent.""" with ContextHelper(environ={"AZURE_HTTP_USER_AGENT": "mytools"}): exporter = MockExporter() trace = tracer_module.Tracer(sampler=AlwaysOnSampler(), exporter=exporter) with trace.span("parent"): tracing_context.current_span.set(OpenCensusSpan(trace.current_span())) policy = DistributedTracingPolicy() request = HttpRequest("GET", "http://127.0.0.1") request.headers["x-ms-client-request-id"] = "some client request id" pipeline_request = PipelineRequest(request, PipelineContext(None)) user_agent = UserAgentPolicy() user_agent.on_request(pipeline_request) policy.on_request(pipeline_request) response = HttpResponse(request, None) response.headers = request.headers response.status_code = 202 response.headers["x-ms-request-id"] = "some request id" pipeline_response = PipelineResponse(request, response, PipelineContext(None)) ctx = trace.span_context header = trace.propagator.to_headers(ctx) assert request.headers.get("traceparent") == header.get("traceparent") policy.on_response(pipeline_request, pipeline_response) time.sleep(0.001) policy.on_request(pipeline_request) policy.on_exception(pipeline_request) user_agent.on_response(pipeline_request, pipeline_response) trace.finish() exporter.build_tree() parent = exporter.root network_span = parent.children[0] assert network_span.span_data.name == "/" assert network_span.span_data.attributes.get("http.method") == "GET" assert network_span.span_data.attributes.get("component") == "http" assert network_span.span_data.attributes.get("http.url") == "http://127.0.0.1" assert network_span.span_data.attributes.get("http.user_agent").endswith("mytools") assert network_span.span_data.attributes.get("x-ms-request-id") == "some request id" assert network_span.span_data.attributes.get("x-ms-client-request-id") == "some client request id" assert network_span.span_data.attributes.get("http.status_code") == 202 network_span = parent.children[1] assert network_span.span_data.name == "/" assert network_span.span_data.attributes.get("http.method") == "GET" assert network_span.span_data.attributes.get("component") == "http" assert network_span.span_data.attributes.get("http.url") == "http://127.0.0.1" assert network_span.span_data.attributes.get("http.user_agent").endswith("mytools") assert network_span.span_data.attributes.get("x-ms-client-request-id") == "some client request id" assert network_span.span_data.attributes.get("x-ms-request-id") is None assert network_span.span_data.attributes.get("http.status_code") == 504
def test_distributed_tracing_policy_solo(): """Test policy with no other policy and happy path""" with ContextHelper(): exporter = MockExporter() trace = tracer_module.Tracer(sampler=AlwaysOnSampler(), exporter=exporter) with trace.span("parent"): tracing_context.current_span.set( OpenCensusSpan(trace.current_span())) policy = DistributedTracingPolicy() request = HttpRequest("GET", "http://127.0.0.1/temp?query=query") pipeline_request = PipelineRequest(request, PipelineContext(None)) policy.on_request(pipeline_request) response = HttpResponse(request, None) response.headers = request.headers response.status_code = 202 response.headers["x-ms-request-id"] = "some request id" ctx = trace.span_context header = trace.propagator.to_headers(ctx) assert request.headers.get("traceparent") == header.get( "traceparent") policy.on_response( pipeline_request, PipelineResponse(request, response, PipelineContext(None))) time.sleep(0.001) policy.on_request(pipeline_request) policy.on_exception(pipeline_request) trace.finish() exporter.build_tree() parent = exporter.root network_span = parent.children[0] assert network_span.span_data.name == "/temp" assert network_span.span_data.attributes.get("http.method") == "GET" assert network_span.span_data.attributes.get("component") == "http" assert network_span.span_data.attributes.get( "http.url") == "http://127.0.0.1/temp?query=query" assert network_span.span_data.attributes.get("http.user_agent") is None assert network_span.span_data.attributes.get( "x-ms-request-id") == "some request id" assert network_span.span_data.attributes.get("http.status_code") == 202 network_span = parent.children[1] assert network_span.span_data.name == "/temp" assert network_span.span_data.attributes.get("http.method") == "GET" assert network_span.span_data.attributes.get("component") == "http" assert network_span.span_data.attributes.get( "http.url") == "http://127.0.0.1/temp?query=query" assert network_span.span_data.attributes.get("http.user_agent") is None assert network_span.span_data.attributes.get("x-ms-request-id") == None assert network_span.span_data.attributes.get("http.status_code") == 504
def test_decorator_has_different_name(self): with ContextHelper(): exporter = MockExporter() trace = tracer_module.Tracer(sampler=AlwaysOnSampler(), exporter=exporter) with trace.span("overall"): client = MockClient() client.check_name_is_different() trace.finish() exporter.build_tree() parent = exporter.root assert len(parent.children) == 2 assert parent.children[0].span_data.name == "MockClient.__init__" assert parent.children[1].span_data.name == "different name"
def test_span(self): exporter = MockExporter() with ContextHelper() as ctx: tracer = tracer_module.Tracer(sampler=AlwaysOnSampler(), exporter=exporter) assert OpenCensusSpan.get_current_tracer() is tracer wrapped_class = OpenCensusSpan() assert tracer.current_span() == wrapped_class.span_instance child = wrapped_class.span() assert tracer.current_span() == child.span_instance assert child.span_instance.name == "span" assert child.span_instance.context_tracer.trace_id == tracer.span_context.trace_id assert child.span_instance.parent_span is wrapped_class.span_instance tracer.finish() exporter.build_tree() parent = exporter.root assert len(parent.children) == 1 assert parent.children[0].span_data.span_id == child.span_instance.span_id
def test_with_opencencus_used(self): with ContextHelper(): exporter = MockExporter() trace = tracer_module.Tracer(sampler=AlwaysOnSampler(), exporter=exporter) parent = trace.start_span(name="OverAll") client = MockClient(policies=[]) client.get_foo(parent_span=parent) client.get_foo() parent.finish() trace.finish() exporter.build_tree() parent = exporter.root assert len(parent.children) == 3 assert parent.children[0].span_data.name == "MockClient.__init__" assert not parent.children[0].children assert parent.children[1].span_data.name == "MockClient.get_foo" assert not parent.children[1].children
def test_should_only_propagate(self): with ContextHelper(should_only_propagate=True): exporter = MockExporter() trace = tracer_module.Tracer(sampler=AlwaysOnSampler(), exporter=exporter) with trace.start_span(name="OverAll") as parent: client = MockClient() client.make_request(2) with trace.span("child") as child: client.make_request(2, parent_span=parent) assert OpenCensusSpan.get_current_span() == child client.make_request(2) trace.finish() exporter.build_tree() parent = exporter.root assert len(parent.children) == 1 assert parent.children[0].span_data.name == "child" assert not parent.children[0].children
async def test_span_with_opencensus_merge_span(value): with ContextHelper(tracer_to_use=value) as ctx: exporter = MockExporter() trace = tracer_module.Tracer(sampler=AlwaysOnSampler(), exporter=exporter) with trace.start_span(name="OverAll") as parent: client = MockClient() await client.merge_span_method() await client.no_merge_span_method() trace.finish() exporter.build_tree() parent = exporter.root assert len(parent.children) == 3 assert parent.children[0].span_data.name == "MockClient.__init__" assert not parent.children[0].children assert parent.children[ 1].span_data.name == "MockClient.merge_span_method" assert not parent.children[1].children assert parent.children[ 2].span_data.name == "MockClient.no_merge_span_method" assert parent.children[2].children[ 0].span_data.name == "MockClient.get_foo"
def test_span_with_exception(self): """Assert that if an exception is raised, the next sibling method is actually a sibling span. """ with ContextHelper(): exporter = MockExporter() trace = tracer_module.Tracer(sampler=AlwaysOnSampler(), exporter=exporter) with trace.span("overall"): client = MockClient() try: client.raising_exception() except: pass client.get_foo() trace.finish() exporter.build_tree() parent = exporter.root assert len(parent.children) == 3 assert parent.children[0].span_data.name == "MockClient.__init__" assert parent.children[ 1].span_data.name == "MockClient.raising_exception" assert parent.children[2].span_data.name == "MockClient.get_foo"