Exemple #1
0
 def test_response_attributes(self):
     otel_wsgi.add_response_attributes(self.span, "404 Not Found", {})
     expected = (
         mock.call("http.status_code", 404),
         mock.call("http.status_text", "Not Found"),
     )
     self.assertEqual(self.span.set_attribute.call_count, len(expected))
     self.span.set_attribute.assert_has_calls(expected, any_order=True)
Exemple #2
0
 def _start_response(status, response_headers, *args, **kwargs):
     span = flask_request.environ.get(_ENVIRON_SPAN_KEY)
     if span:
         otel_wsgi.add_response_attributes(span, status,
                                           response_headers)
     else:
         logger.warning(
             "Flask environ's OpenTelemetry span missing at _start_response(%s)",
             status,
         )
     return start_response(status, response_headers, *args, **kwargs)
    def trace_tween(request):
        if _excluded_urls.url_disabled(request.url):
            request.environ[_ENVIRON_ENABLED_KEY] = False
            # short-circuit when we don't want to trace anything
            return handler(request)

        request.environ[_ENVIRON_ENABLED_KEY] = True
        request.environ[_ENVIRON_STARTTIME_KEY] = time_ns()

        try:
            response = handler(request)
            response_or_exception = response
        except HTTPException as exc:
            # If the exception is a pyramid HTTPException,
            # that's still valuable information that isn't necessarily
            # a 500. For instance, HTTPFound is a 302.
            # As described in docs, Pyramid exceptions are all valid
            # response types
            response_or_exception = exc
            raise
        finally:
            span = request.environ.get(_ENVIRON_SPAN_KEY)
            enabled = request.environ.get(_ENVIRON_ENABLED_KEY)
            if not span and enabled:
                _logger.warning(
                    "Pyramid environ's OpenTelemetry span missing."
                    "If the OpenTelemetry tween was added manually, make sure"
                    "PyramidInstrumentor().instrument_config(config) is called"
                )
            elif enabled:
                otel_wsgi.add_response_attributes(
                    span,
                    response_or_exception.status,
                    response_or_exception.headers,
                )

                activation = request.environ.get(_ENVIRON_ACTIVATION_KEY)

                if isinstance(response_or_exception, HTTPException):
                    activation.__exit__(
                        type(response_or_exception),
                        response_or_exception,
                        getattr(response_or_exception, "__traceback__", None),
                    )
                else:
                    activation.__exit__(None, None, None)

                context.detach(request.environ.get(_ENVIRON_TOKEN))

        return response
        def _start_response(status, response_headers, *args, **kwargs):
            if not _excluded_urls.url_disabled(flask.request.url):
                span = flask.request.environ.get(_ENVIRON_SPAN_KEY)

                if span:
                    otel_wsgi.add_response_attributes(span, status,
                                                      response_headers)
                else:
                    _logger.warning(
                        "Flask environ's OpenTelemetry span "
                        "missing at _start_response(%s)",
                        status,
                    )

            return start_response(status, response_headers, *args, **kwargs)
    def process_response(self, request, response):
        if (self._environ_activation_key in request.META.keys()
                and self._environ_span_key in request.META.keys()):
            add_response_attributes(
                request.META[self._environ_span_key],
                "{} {}".format(response.status_code, response.reason_phrase),
                response,
            )
            request.META.pop(self._environ_span_key)

            request.META[self._environ_activation_key].__exit__(
                None, None, None)
            request.META.pop(self._environ_activation_key)

        if self._environ_token in request.META.keys():
            detach(request.environ.get(self._environ_token))
            request.META.pop(self._environ_token)

        return response
Exemple #6
0
 def test_response_attributes_invalid_status_code(self):
     otel_wsgi.add_response_attributes(self.span, "Invalid Status Code", {})
     self.assertEqual(self.span.set_attribute.call_count, 1)
     self.span.set_attribute.assert_called_with("http.status_text",
                                                "Status Code")