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
示例#2
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
示例#3
0
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
示例#4
0
    def _create_pipeline(self, credential, **kwargs):
        # type: (Any, **Any) -> Tuple[Configuration, Pipeline]
        self._credential_policy = None
        if hasattr(credential, "get_token"):
            self._credential_policy = BearerTokenCredentialPolicy(credential, STORAGE_OAUTH_SCOPE)
        elif isinstance(credential, SharedKeyCredentialPolicy):
            self._credential_policy = credential
        elif credential is not None:
            raise TypeError("Unsupported credential: {}".format(credential))

        config = kwargs.get("_configuration") or create_configuration(**kwargs)
        if kwargs.get("_pipeline"):
            return config, kwargs["_pipeline"]
        config.transport = kwargs.get("transport")  # type: ignore
        if "connection_timeout" not in kwargs:
            kwargs["connection_timeout"] = DEFAULT_SOCKET_TIMEOUT
        if not config.transport:
            config.transport = RequestsTransport(**kwargs)
        policies = [
            QueueMessagePolicy(),
            config.headers_policy,
            config.user_agent_policy,
            StorageContentValidation(),
            StorageRequestHook(**kwargs),
            self._credential_policy,
            ContentDecodePolicy(),
            RedirectPolicy(**kwargs),
            StorageHosts(hosts=self._hosts, **kwargs),
            config.retry_policy,
            config.logging_policy,
            StorageResponseHook(**kwargs),
            DistributedTracingPolicy(),
        ]
        return config, Pipeline(config.transport, policies=policies)
    def _build_pipeline(config: Configuration, transport: HttpTransport,
                        **kwargs: Any) -> AsyncPipeline:
        policies = [
            config.headers_policy, config.user_agent_policy,
            config.proxy_policy, config.redirect_policy, config.retry_policy,
            config.authentication_policy, config.logging_policy,
            DistributedTracingPolicy()
        ]

        if transport is None:
            transport = AsyncioRequestsTransport(**kwargs)

        return AsyncPipeline(transport, policies=policies)
示例#6
0
 def __init__(self, auth_url, config=None, policies=None, transport=None, **kwargs):
     # type: (str, Optional[Configuration], Optional[Iterable[HTTPPolicy]], Optional[HttpTransport], Mapping[str, Any]) -> None
     config = config or self._create_config(**kwargs)
     policies = policies or [
         ContentDecodePolicy(),
         config.retry_policy,
         config.logging_policy,
         DistributedTracingPolicy(),
     ]
     if not transport:
         transport = RequestsTransport(**kwargs)
     self._pipeline = Pipeline(transport=transport, policies=policies)
     super(AuthnClient, self).__init__(auth_url, **kwargs)
    def _create_appconfig_pipeline(self):
        policies = [
            self.config.headers_policy,
            self.config.user_agent_policy,
            self.config.logging_policy,  # HTTP request/response log
            AppConfigRequestsCredentialsPolicy(self.config.credentials),
            DistributedTracingPolicy()
        ]

        return AsyncPipeline(
            AsyncioRequestsTransport(configuration=self.config),  # Send HTTP request using requests
            policies,
        )
 def __init__(self,
              config: "Optional[Configuration]" = None,
              policies: "Optional[Iterable[HTTPPolicy]]" = None,
              transport: "Optional[AsyncHttpTransport]" = None,
              **kwargs: "Any") -> None:
     config = config or self._create_config(**kwargs)
     policies = policies or [
         ContentDecodePolicy(),
         config.retry_policy,
         config.logging_policy,
         DistributedTracingPolicy(),
     ]
     if not transport:
         transport = AsyncioRequestsTransport(**kwargs)
     self._pipeline = AsyncPipeline(transport=transport, policies=policies)
     super().__init__(**kwargs)
    def _build_pipeline(self, config, transport, **kwargs):
        # type: (Configuration, HttpTransport) -> Pipeline
        policies = [
            config.headers_policy,
            config.user_agent_policy,
            config.proxy_policy,
            config.redirect_policy,
            config.retry_policy,
            config.authentication_policy,
            config.logging_policy,
            DistributedTracingPolicy(),
        ]

        if transport is None:
            transport = RequestsTransport(**kwargs)

        return Pipeline(transport, policies=policies)
示例#10
0
 def __init__(self,
              auth_url: str,
              config: Optional[Configuration] = None,
              policies: Optional[Iterable[HTTPPolicy]] = None,
              transport: Optional[AsyncHttpTransport] = None,
              **kwargs: Mapping[str, Any]) -> None:
     config = config or self._create_config(**kwargs)
     policies = policies or [
         ContentDecodePolicy(),
         config.retry_policy,
         config.logging_policy,
         DistributedTracingPolicy(),
     ]
     if not transport:
         transport = AsyncioRequestsTransport(**kwargs)
     self._pipeline = AsyncPipeline(transport=transport, policies=policies)
     super(AsyncAuthnClient, self).__init__(auth_url, **kwargs)
示例#11
0
    def _build_pipeline(config: Configuration, transport: AsyncHttpTransport, **kwargs: "**Any") -> AsyncPipeline:
        policies = [
            config.headers_policy,
            config.user_agent_policy,
            config.proxy_policy,
            config.redirect_policy,
            config.retry_policy,
            config.authentication_policy,
            config.logging_policy,
            DistributedTracingPolicy(),
        ]

        if transport is None:
            from azure.core.pipeline.transport import AioHttpTransport

            transport = AioHttpTransport(**kwargs)

        return AsyncPipeline(transport, policies=policies)
    def _create_appconfig_pipeline(self, **kwargs):
        transport = kwargs.get('transport')
        policies = kwargs.get('policies')

        if policies is None:  # [] is a valid policy list
            policies = [
                self.config.headers_policy,
                self.config.user_agent_policy,
                AppConfigRequestsCredentialsPolicy(self.config.credentials),
                self.config.retry_policy,
                self.config.logging_policy,  # HTTP request/response log
                DistributedTracingPolicy(),
            ]

        if not transport:
            transport = RequestsTransport(**kwargs)

        return Pipeline(transport, policies)
 def __init__(
         self,
         config=None,  # type: Optional[Configuration]
         policies=None,  # type: Optional[Iterable[HTTPPolicy]]
         transport=None,  # type: Optional[HttpTransport]
         **kwargs  # type: Any
 ):
     # type: (...) -> None
     config = config or self._create_config(**kwargs)
     policies = policies or [
         ContentDecodePolicy(),
         config.retry_policy,
         config.logging_policy,
         DistributedTracingPolicy(),
     ]
     if not transport:
         transport = RequestsTransport(**kwargs)
     self._pipeline = Pipeline(transport=transport, policies=policies)
     super(AuthnClient, self).__init__(**kwargs)
示例#14
0
 def _create_pipeline(self, credential, **kwargs):
     # type: (Any, **Any) -> Tuple[Configuration, Pipeline]
     self._credential_policy = None
     if hasattr(credential, 'get_token'):
         self._credential_policy = AsyncBearerTokenCredentialPolicy(
             credential, STORAGE_OAUTH_SCOPE)
     elif isinstance(credential, SharedKeyCredentialPolicy):
         self._credential_policy = credential
     elif credential is not None:
         raise TypeError("Unsupported credential: {}".format(credential))
     config = kwargs.get('_configuration') or create_configuration(**kwargs)
     if kwargs.get('_pipeline'):
         return config, kwargs['_pipeline']
     config.transport = kwargs.get('transport')  # type: ignore
     if 'connection_timeout' not in kwargs:
         kwargs['connection_timeout'] = DEFAULT_SOCKET_TIMEOUT[
             0]  # type: ignore
     if not config.transport:
         try:
             from azure.core.pipeline.transport import AioHttpTransport
         except ImportError:
             raise ImportError(
                 "Unable to create async transport. Please check aiohttp is installed."
             )
         config.transport = AioHttpTransport(**kwargs)
     policies = [
         QueueMessagePolicy(),
         config.headers_policy,
         config.user_agent_policy,
         StorageContentValidation(),
         StorageRequestHook(**kwargs),
         self._credential_policy,
         ContentDecodePolicy(),
         AsyncRedirectPolicy(**kwargs),
         StorageHosts(hosts=self._hosts, **kwargs),  # type: ignore
         config.retry_policy,
         config.logging_policy,
         AsyncStorageResponseHook(**kwargs),
         DistributedTracingPolicy(),
     ]
     return config, AsyncPipeline(config.transport, policies=policies)