def _instrument(self, **kwargs):
        """Instruments requests module

        Args:
            **kwargs: Optional arguments
                ``tracer_provider``: a TracerProvider, defaults to global
                ``span_callback``: An optional callback invoked before returning the http response. Invoked with Span and requests.Response
                ``name_callback``: Callback which calculates a generic span name for an
                    outgoing HTTP request based on the method and url.
                    Optional: Defaults to get_default_span_name.
                ``excluded_urls``: A string containing a comma-delimited
                    list of regexes used to exclude URLs from tracking
        """
        tracer_provider = kwargs.get("tracer_provider")
        tracer = get_tracer(__name__, __version__, tracer_provider)
        excluded_urls = kwargs.get("excluded_urls")
        meter_provider = kwargs.get("meter_provider")
        meter = get_meter(
            __name__,
            __version__,
            meter_provider,
        )
        duration_histogram = meter.create_histogram(
            name="http.client.duration",
            unit="ms",
            description="measures the duration of the outbound HTTP request",
        )
        _instrument(
            tracer,
            duration_histogram,
            span_callback=kwargs.get("span_callback"),
            name_callback=kwargs.get("name_callback"),
            excluded_urls=_excluded_urls_from_env
            if excluded_urls is None else parse_excluded_urls(excluded_urls),
        )
    def instrument_app(
        app: fastapi.FastAPI,
        server_request_hook: _ServerRequestHookT = None,
        client_request_hook: _ClientRequestHookT = None,
        client_response_hook: _ClientResponseHookT = None,
        tracer_provider=None,
        excluded_urls=None,
    ):
        """Instrument an uninstrumented FastAPI application."""
        if not hasattr(app, "_is_instrumented_by_opentelemetry"):
            app._is_instrumented_by_opentelemetry = False

        if not getattr(app, "_is_instrumented_by_opentelemetry", False):
            if excluded_urls is None:
                excluded_urls = _excluded_urls_from_env
            else:
                excluded_urls = parse_excluded_urls(excluded_urls)

            app.add_middleware(
                OpenTelemetryMiddleware,
                excluded_urls=excluded_urls,
                default_span_details=_get_route_details,
                server_request_hook=server_request_hook,
                client_request_hook=client_request_hook,
                client_response_hook=client_response_hook,
                tracer_provider=tracer_provider,
            )
            app._is_instrumented_by_opentelemetry = True
        else:
            _logger.warning(
                "Attempting to instrument FastAPI app while already instrumented"
            )
 def _instrument(self, **kwargs):
     self._original_fastapi = fastapi.FastAPI
     _InstrumentedFastAPI._tracer_provider = kwargs.get("tracer_provider")
     _excluded_urls = kwargs.get("excluded_urls")
     _InstrumentedFastAPI._excluded_urls = (
         _excluded_urls_from_env
         if _excluded_urls is None else parse_excluded_urls(_excluded_urls))
     fastapi.FastAPI = _InstrumentedFastAPI
Beispiel #4
0
    def instrument_app(
        app,
        request_hook=None,
        response_hook=None,
        tracer_provider=None,
        excluded_urls=None,
        meter_provider=None,
    ):
        if not hasattr(app, "_is_instrumented_by_opentelemetry"):
            app._is_instrumented_by_opentelemetry = False

        if not app._is_instrumented_by_opentelemetry:
            excluded_urls = (parse_excluded_urls(excluded_urls)
                             if excluded_urls is not None else
                             _excluded_urls_from_env)
            meter = get_meter(__name__, __version__, meter_provider)
            duration_histogram = meter.create_histogram(
                name="http.server.duration",
                unit="ms",
                description="measures the duration of the inbound HTTP request",
            )
            active_requests_counter = meter.create_up_down_counter(
                name="http.server.active_requests",
                unit="requests",
                description=
                "measures the number of concurrent HTTP requests that are currently in-flight",
            )

            app._original_wsgi_app = app.wsgi_app
            app.wsgi_app = _rewrapped_app(
                app.wsgi_app,
                active_requests_counter,
                duration_histogram,
                response_hook,
                excluded_urls=excluded_urls,
            )

            tracer = trace.get_tracer(__name__, __version__, tracer_provider)

            _before_request = _wrapped_before_request(
                request_hook,
                tracer,
                excluded_urls=excluded_urls,
            )
            app._before_request = _before_request
            app.before_request(_before_request)

            _teardown_request = _wrapped_teardown_request(
                excluded_urls=excluded_urls, )
            app._teardown_request = _teardown_request
            app.teardown_request(_teardown_request)
            app._is_instrumented_by_opentelemetry = True
        else:
            _logger.warning(
                "Attempting to instrument Flask app while already instrumented"
            )
Beispiel #5
0
 def _instrument(self, **kwargs):
     self._original_flask = flask.Flask
     request_hook = kwargs.get("request_hook")
     response_hook = kwargs.get("response_hook")
     if callable(request_hook):
         _InstrumentedFlask._request_hook = request_hook
     if callable(response_hook):
         _InstrumentedFlask._response_hook = response_hook
     tracer_provider = kwargs.get("tracer_provider")
     _InstrumentedFlask._tracer_provider = tracer_provider
     excluded_urls = kwargs.get("excluded_urls")
     _InstrumentedFlask._excluded_urls = (
         _excluded_urls_from_env
         if excluded_urls is None else parse_excluded_urls(excluded_urls))
     flask.Flask = _InstrumentedFlask
    def instrument_app(
        app: fastapi.FastAPI,
        tracer_provider=None,
        excluded_urls=None,
    ):
        """Instrument an uninstrumented FastAPI application."""
        if not getattr(app, "is_instrumented_by_opentelemetry", False):
            if excluded_urls is None:
                excluded_urls = _excluded_urls_from_env
            else:
                excluded_urls = parse_excluded_urls(excluded_urls)

            app.add_middleware(
                OpenTelemetryMiddleware,
                excluded_urls=excluded_urls,
                span_details_callback=_get_route_details,
                tracer_provider=tracer_provider,
            )
            app.is_instrumented_by_opentelemetry = True
Beispiel #7
0
    def instrument_app(
        app,
        request_hook=None,
        response_hook=None,
        tracer_provider=None,
        excluded_urls=None,
    ):
        if not hasattr(app, "_is_instrumented_by_opentelemetry"):
            app._is_instrumented_by_opentelemetry = False

        if not app._is_instrumented_by_opentelemetry:
            excluded_urls = (parse_excluded_urls(excluded_urls)
                             if excluded_urls is not None else
                             _excluded_urls_from_env)
            app._original_wsgi_app = app.wsgi_app
            app.wsgi_app = _rewrapped_app(app.wsgi_app,
                                          response_hook,
                                          excluded_urls=excluded_urls)

            tracer = trace.get_tracer(__name__, __version__, tracer_provider)

            _before_request = _wrapped_before_request(
                request_hook,
                tracer,
                excluded_urls=excluded_urls,
            )
            app._before_request = _before_request
            app.before_request(_before_request)

            _teardown_request = _wrapped_teardown_request(
                excluded_urls=excluded_urls, )
            app._teardown_request = _teardown_request
            app.teardown_request(_teardown_request)
            app._is_instrumented_by_opentelemetry = True
        else:
            _logger.warning(
                "Attempting to instrument Flask app while already instrumented"
            )