コード例 #1
0
    async def call(self, module, method, wrapped, instance, args, kwargs):
        method = kwargs["method"] if "method" in kwargs else args[0]
        url = kwargs["url"] if "url" in kwargs else args[1]
        url = str(url)

        signature = " ".join([method.upper(), get_host_from_url(url)])
        url = sanitize_url(url)
        transaction = execution_context.get_transaction()

        async with async_capture_span(
                signature,
                span_type="external",
                span_subtype="http",
                extra={"http": {
                    "url": url
                }},
                leaf=True,
        ) as span:
            leaf_span = span
            while isinstance(leaf_span, DroppedSpan):
                leaf_span = leaf_span.parent

            parent_id = leaf_span.id if leaf_span else transaction.id
            trace_parent = transaction.trace_parent.copy_from(
                span_id=parent_id, trace_options=TracingOptions(recorded=True))
            headers = kwargs.get("headers") or {}
            self._set_disttracing_headers(headers, trace_parent, transaction)
            kwargs["headers"] = headers
            response = await wrapped(*args, **kwargs)
            if response:
                if span.context:
                    span.context["http"]["status_code"] = response.status
                span.set_success(
                ) if response.status < 400 else span.set_failure()
            return response
コード例 #2
0
    def call(self, module, method, wrapped, instance, args, kwargs):
        request_object = args[1] if len(args) > 1 else kwargs["req"]

        method = request_object.get_method()
        host = request_host(request_object)

        url = sanitize_url(request_object.get_full_url())
        signature = method.upper() + " " + host

        transaction = execution_context.get_transaction()

        with capture_span(
            signature, span_type="external", span_subtype="http", extra={"http": {"url": url}}, leaf=True
        ) as span:
            # if urllib has been called in a leaf span, this span might be a DroppedSpan.
            leaf_span = span
            while isinstance(leaf_span, DroppedSpan):
                leaf_span = leaf_span.parent

            parent_id = leaf_span.id if leaf_span else transaction.id
            trace_parent = transaction.trace_parent.copy_from(
                span_id=parent_id, trace_options=TracingOptions(recorded=True)
            )
            self._set_disttracing_headers(request_object, trace_parent, transaction)
            return wrapped(*args, **kwargs)
コード例 #3
0
    def call(self, module, method, wrapped, instance, args, kwargs):
        request = kwargs.get("request") or args[0]

        request_method = request.method.upper()
        url = str(request.url)
        name = "{request_method} {host}".format(request_method=request_method,
                                                host=get_host_from_url(url))
        url = sanitize_url(url)

        with capture_span(
                name,
                span_type="external",
                span_subtype="http",
                extra={"http": {
                    "url": url
                }},
                leaf=True,
        ) as span:
            response = wrapped(*args, **kwargs)
            if response is not None:
                if span.context:
                    span.context["http"]["status_code"] = response.status_code
                span.set_success(
                ) if response.status_code < 400 else span.set_failure()
            return response
コード例 #4
0
    def call(self, module, method, wrapped, instance, args, kwargs):
        args, kwargs, params = self._ensure_headers_in_kwargs(args, kwargs)

        signature = params.get("method", "GET").upper()
        signature += " " + get_host_from_url(params["url"])
        url = sanitize_url(params["url"])
        transaction = execution_context.get_transaction()
        with capture_span(
            signature,
            span_type="external",
            span_subtype="http",
            extra={"http": {"url": url}},
            leaf=True,
        ) as span:
            # if httplib2 has been called in a leaf span, this span might be a DroppedSpan.
            leaf_span = span
            while isinstance(leaf_span, DroppedSpan):
                leaf_span = leaf_span.parent

            # It's possible that there are only dropped spans, e.g. if we started dropping spans.
            # In this case, the transaction.id is used
            parent_id = leaf_span.id if leaf_span else transaction.id
            trace_parent = transaction.trace_parent.copy_from(
                span_id=parent_id, trace_options=TracingOptions(recorded=True)
            )
            self._set_disttracing_headers(params["headers"], trace_parent, transaction)
            if leaf_span:
                leaf_span.dist_tracing_propagated = True

            response, content = wrapped(*args, **kwargs)
            if span.context:
                span.context["http"]["status_code"] = response.status
            span.set_success() if response.status < 400 else span.set_failure()
            return response, content
コード例 #5
0
    def call(self, module, method, wrapped, instance, args, kwargs):
        if "request" in kwargs:
            request = kwargs["request"]
        else:
            request = args[0]

        signature = request.method.upper()
        signature += " " + get_host_from_url(request.url)
        url = sanitize_url(request.url)

        with capture_span(signature, "ext.http.requests", {"http": {"url": url}}, leaf=True):
            return wrapped(*args, **kwargs)
コード例 #6
0
    def call(self, module, method, wrapped, instance, args, kwargs):
        request_object = args[1] if len(args) > 1 else kwargs["req"]

        method = request_object.get_method()
        host = request_host(request_object)

        url = sanitize_url(request_object.get_full_url())
        destination = url_to_destination(url)
        signature = method.upper() + " " + host

        transaction = execution_context.get_transaction()

        with capture_span(
                signature,
                span_type="external",
                span_subtype="http",
                extra={
                    "http": {
                        "url": url
                    },
                    "destination": destination
                },
                leaf=True,
        ) as span:
            # if urllib has been called in a leaf span, this span might be a DroppedSpan.
            leaf_span = span
            while isinstance(leaf_span, DroppedSpan):
                leaf_span = leaf_span.parent

            parent_id = leaf_span.id if leaf_span else transaction.id
            trace_parent = transaction.trace_parent.copy_from(
                span_id=parent_id, trace_options=TracingOptions(recorded=True))
            self._set_disttracing_headers(request_object, trace_parent,
                                          transaction)
            response = wrapped(*args, **kwargs)
            if response:
                status = getattr(response, "status",
                                 None) or response.getcode()  # Python 2 compat
                if span.context:
                    span.context["http"]["status_code"] = status
                span.set_success() if status < 400 else span.set_failure()
            return response
コード例 #7
0
ファイル: requests.py プロジェクト: elastic/apm-agent-python
    def call(self, module, method, wrapped, instance, args, kwargs):
        if "request" in kwargs:
            request = kwargs["request"]
        else:
            request = args[0]

        signature = request.method.upper()
        signature += " " + get_host_from_url(request.url)
        url = sanitize_url(request.url)

        with capture_span(
            signature,
            span_type="external",
            span_subtype="http",
            extra={"http": {"url": url}},
            leaf=True,
        ) as span:
            response = wrapped(*args, **kwargs)
            # requests.Response objects are falsy if status code > 400, so we have to check for None instead
            if response is not None:
                if span.context:
                    span.context["http"]["status_code"] = response.status_code
                span.set_success() if response.status_code < 400 else span.set_failure()
            return response
コード例 #8
0
def test_url_sanitization_urlencoded_password():
    sanitized = sanitize_url(
        "http://*****:*****@localhost:123/foo?bar=baz#bazzinga")
    assert sanitized == "http://*****:*****@localhost:123/foo?bar=baz#bazzinga" % constants.MASK
コード例 #9
0
def test_url_sanitization():
    sanitized = sanitize_url(
        "http://*****:*****@localhost:123/foo?bar=baz#bazzinga")
    assert sanitized == "http://*****:*****@localhost:123/foo?bar=baz#bazzinga" % constants.MASK