Exemple #1
0
 async def test_decorator_has_different_name(self):
     with FakeSpan(name="parent") as parent:
         client = MockClient()
         await client.check_name_is_different()
     assert len(parent.children) == 2
     assert parent.children[0].name == "MockClient.__init__"
     assert parent.children[1].name == "different name"
Exemple #2
0
def test_distributed_tracing_policy_badurl(caplog):
    """Test policy with a bad url that will throw, and be sure policy ignores it"""
    settings.tracing_implementation.set_value(FakeSpan)
    with FakeSpan(name="parent") as root_span:
        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"

        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)
        try:
            raise ValueError("Transport trouble")
        except:
            policy.on_exception(pipeline_request)

    assert len(root_span.children) == 0
 async def test_decorator_has_different_name(self, http_request):
     with FakeSpan(name="parent") as parent:
         client = MockClient(http_request)
         await client.check_name_is_different()
     assert len(parent.children) == 2
     assert parent.children[0].name == "MockClient.__init__"
     assert parent.children[1].name == "different name"
     assert parent.children[1].kind == SpanKind.INTERNAL
    async def test_kind_override(self, http_request):
        with FakeSpan(name="parent") as parent:
            client = MockClient(http_request)
            await client.kind_override()

        assert len(parent.children) == 2
        assert parent.children[0].name == "MockClient.__init__"
        assert parent.children[1].name == "MockClient.kind_override"
        assert parent.children[1].kind == SpanKind.PRODUCER
Exemple #5
0
    async def test_decorator_tracing_attr(self):
        with FakeSpan(name="parent") as parent:
            client = MockClient()
            await client.tracing_attr()

        assert len(parent.children) == 2
        assert parent.children[0].name == "MockClient.__init__"
        assert parent.children[1].name == "MockClient.tracing_attr"
        assert parent.children[1].attributes == {'foo': 'bar'}
Exemple #6
0
def test_distributed_tracing_policy_with_user_agent():
    """Test policy working with user agent."""
    settings.tracing_implementation.set_value(FakeSpan)
    with mock.patch.dict('os.environ', {"AZURE_HTTP_USER_AGENT": "mytools"}):
        with FakeSpan(name="parent") as root_span:
            policy = DistributedTracingPolicy()

            request = HttpRequest("GET", "http://localhost")
            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))

            assert request.headers.get("traceparent") == '123456789'

            policy.on_response(pipeline_request, pipeline_response)

            time.sleep(0.001)
            policy.on_request(pipeline_request)
            try:
                raise ValueError("Transport trouble")
            except:
                policy.on_exception(pipeline_request)

            user_agent.on_response(pipeline_request, pipeline_response)

        network_span = root_span.children[0]
        assert network_span.name == "/"
        assert network_span.attributes.get("http.method") == "GET"
        assert network_span.attributes.get("component") == "http"
        assert network_span.attributes.get("http.url") == "http://localhost"
        assert network_span.attributes.get("http.user_agent").endswith("mytools")
        assert network_span.attributes.get("x-ms-request-id") == "some request id"
        assert network_span.attributes.get("x-ms-client-request-id") == "some client request id"
        assert network_span.attributes.get("http.status_code") == 202

        network_span = root_span.children[1]
        assert network_span.name == "/"
        assert network_span.attributes.get("http.method") == "GET"
        assert network_span.attributes.get("component") == "http"
        assert network_span.attributes.get("http.url") == "http://localhost"
        assert network_span.attributes.get("http.user_agent").endswith("mytools")
        assert network_span.attributes.get("x-ms-client-request-id") == "some client request id"
        assert network_span.attributes.get("x-ms-request-id") is None
        assert network_span.attributes.get("http.status_code") == 504
        # Exception should propagate status for Opencensus
        assert network_span.status == 'Transport trouble'
    async def test_span_complicated(self, http_request):
        with FakeSpan(name="parent") as parent:
            client = MockClient(http_request)
            await client.make_request(2)
            with parent.span("child") as child:
                time.sleep(0.001)
                await client.make_request(2, parent_span=parent)
                assert FakeSpan.get_current_span() == child
                await client.make_request(2)

        assert len(parent.children) == 4
        assert parent.children[0].name == "MockClient.__init__"
        assert not parent.children[0].children
        assert parent.children[1].name == "MockClient.make_request"
        assert not parent.children[1].children
        assert parent.children[2].name == "child"
        assert parent.children[2].children[0].name == "MockClient.make_request"
        assert parent.children[3].name == "MockClient.make_request"
        assert not parent.children[3].children
Exemple #8
0
    def test_decorator_tracing_attr(self, http_request):
        with FakeSpan(name="parent") as parent:
            client = MockClient(http_request)
            client.tracing_attr()

        assert len(parent.children) == 2
        assert parent.children[0].name == "MockClient.__init__"
        assert parent.children[1].name == "MockClient.tracing_attr"
        assert parent.children[1].kind == SpanKind.INTERNAL
        assert parent.children[1].attributes == {'foo': 'bar'}
def test_distributed_tracing_policy_solo(http_request, http_response):
    """Test policy with no other policy and happy path"""
    settings.tracing_implementation.set_value(FakeSpan)
    with FakeSpan(name="parent") as root_span:
        policy = DistributedTracingPolicy()

        request = http_request("GET", "http://localhost/temp?query=query")
        request.headers["x-ms-client-request-id"] = "some client request id"

        pipeline_request = PipelineRequest(request, PipelineContext(None))
        policy.on_request(pipeline_request)

        response = create_http_response(http_response, request, None)
        response.headers = request.headers
        response.status_code = 202
        response.headers["x-ms-request-id"] = "some request id"

        assert request.headers.get("traceparent") == '123456789'

        policy.on_response(
            pipeline_request,
            PipelineResponse(request, response, PipelineContext(None)))
        time.sleep(0.001)
        policy.on_request(pipeline_request)
        try:
            raise ValueError("Transport trouble")
        except:
            policy.on_exception(pipeline_request)

    # Check on_response
    network_span = root_span.children[0]
    assert network_span.name == "/temp"
    assert network_span.attributes.get("http.method") == "GET"
    assert network_span.attributes.get("component") == "http"
    assert network_span.attributes.get(
        "http.url") == "http://localhost/temp?query=query"
    assert network_span.attributes.get("http.user_agent") is None
    assert network_span.attributes.get("x-ms-request-id") == "some request id"
    assert network_span.attributes.get(
        "x-ms-client-request-id") == "some client request id"
    assert network_span.attributes.get("http.status_code") == 202

    # Check on_exception
    network_span = root_span.children[1]
    assert network_span.name == "/temp"
    assert network_span.attributes.get("http.method") == "GET"
    assert network_span.attributes.get("component") == "http"
    assert network_span.attributes.get(
        "http.url") == "http://localhost/temp?query=query"
    assert network_span.attributes.get(
        "x-ms-client-request-id") == "some client request id"
    assert network_span.attributes.get("http.user_agent") is None
    assert network_span.attributes.get("x-ms-request-id") == None
    assert network_span.attributes.get("http.status_code") == 504
    async def test_span_merge_span(self, http_request):
        with FakeSpan(name="parent") as parent:
            client = MockClient(http_request)
            await client.merge_span_method()
            await client.no_merge_span_method()

        assert len(parent.children) == 3
        assert parent.children[0].name == "MockClient.__init__"
        assert not parent.children[0].children
        assert parent.children[1].name == "MockClient.merge_span_method"
        assert not parent.children[1].children
        assert parent.children[2].name == "MockClient.no_merge_span_method"
        assert parent.children[2].children[0].name == "MockClient.get_foo"
    async def test_used(self, http_request):
        with FakeSpan(name="parent") as parent:
            client = MockClient(http_request, policies=[])
            await client.get_foo(parent_span=parent)
            await client.get_foo()

            assert len(parent.children) == 3
            assert parent.children[0].name == "MockClient.__init__"
            assert not parent.children[0].children
            assert parent.children[1].name == "MockClient.get_foo"
            assert not parent.children[1].children
            assert parent.children[2].name == "MockClient.get_foo"
            assert not parent.children[2].children
Exemple #12
0
    def test_used(self):
        with FakeSpan(name="parent") as parent:
            client = MockClient(policies=[])
            client.get_foo(parent_span=parent)
            client.get_foo()

        assert len(parent.children) == 3
        assert parent.children[0].name == "MockClient.__init__"
        assert not parent.children[0].children
        assert parent.children[1].name == "MockClient.get_foo"
        assert not parent.children[1].children
        assert parent.children[2].name == "MockClient.get_foo"
        assert not parent.children[2].children
def test_span_namer(http_request, http_response):
    settings.tracing_implementation.set_value(FakeSpan)
    with FakeSpan(name="parent") as root_span:

        request = http_request("GET", "http://localhost/temp?query=query")
        pipeline_request = PipelineRequest(request, PipelineContext(None))

        def fixed_namer(http_request):
            assert http_request is request
            return "overridenname"

        policy = DistributedTracingPolicy(network_span_namer=fixed_namer)

        policy.on_request(pipeline_request)

        response = create_http_response(http_response, request, None)
        response.headers = request.headers
        response.status_code = 202

        policy.on_response(
            pipeline_request,
            PipelineResponse(request, response, PipelineContext(None)))

        def operation_namer(http_request):
            assert http_request is request
            return "operation level name"

        pipeline_request.context.options[
            'network_span_namer'] = operation_namer

        policy.on_request(pipeline_request)

        response = create_http_response(http_response, request, None)
        response.headers = request.headers
        response.status_code = 202

        policy.on_response(
            pipeline_request,
            PipelineResponse(request, response, PipelineContext(None)))

    # Check init kwargs
    network_span = root_span.children[0]
    assert network_span.name == "overridenname"

    # Check operation kwargs
    network_span = root_span.children[1]
    assert network_span.name == "operation level name"
    async def test_span_with_exception(self, http_request):
        """Assert that if an exception is raised, the next sibling method is actually a sibling span.
        """
        with FakeSpan(name="parent") as parent:
            client = MockClient(http_request)
            try:
                await client.raising_exception()
            except:
                pass
            await client.get_foo()

        assert len(parent.children) == 3
        assert parent.children[0].name == "MockClient.__init__"
        assert parent.children[1].name == "MockClient.raising_exception"
        # Exception should propagate status for Opencensus
        assert parent.children[
            1].status == 'Something went horribly wrong here'
        assert parent.children[2].name == "MockClient.get_foo"
Exemple #15
0
def test_distributed_tracing_policy_attributes():
    """Test policy with no other policy and happy path"""
    settings.tracing_implementation.set_value(FakeSpan)
    with FakeSpan(name="parent") as root_span:
        policy = DistributedTracingPolicy(tracing_attributes={
            'myattr': 'myvalue'
        })

        request = HttpRequest("GET", "http://localhost/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

        policy.on_response(pipeline_request, PipelineResponse(request, response, PipelineContext(None)))

    # Check on_response
    network_span = root_span.children[0]
    assert network_span.attributes.get("myattr") == "myvalue"