def _instrument(self, **kwargs):
        """Instruments urllib 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 http.client.HTTPResponse
                ``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.
        """

        _instrument(
            tracer_provider=kwargs.get("tracer_provider"),
            span_callback=kwargs.get("span_callback"),
            name_callback=kwargs.get("name_callback"),
        )

        self.init_metrics(
            __name__,
            __version__,
        )

        # pylint: disable=W0201
        self.metric_recorder = HTTPMetricRecorder(self.meter,
                                                  HTTPMetricType.CLIENT)
Esempio n. 2
0
    def _instrument(self, **kwargs):

        # FIXME this is probably a pattern that will show up in the rest of the
        # ext. Find a better way of implementing this.
        # FIXME Probably the evaluation of strings into boolean values can be
        # built inside the Configuration class itself with the magic method
        # __bool__

        if Configuration().DJANGO_INSTRUMENT is False:
            return

        # This can not be solved, but is an inherent problem of this approach:
        # the order of middleware entries matters, and here you have no control
        # on that:
        # https://docs.djangoproject.com/en/3.0/topics/http/middleware/#activating-middleware
        # https://docs.djangoproject.com/en/3.0/ref/middleware/#middleware-ordering

        settings_middleware = getattr(settings, "MIDDLEWARE", [])
        # Django allows to specify middlewares as a tuple, so we convert this tuple to a
        # list, otherwise we wouldn't be able to call append/remove
        if isinstance(settings_middleware, tuple):
            settings_middleware = list(settings_middleware)

        settings_middleware.insert(0, self._opentelemetry_middleware)
        self.init_metrics(
            __name__,
            __version__,
        )
        metric_recorder = HTTPMetricRecorder(self.meter, HTTPMetricType.SERVER)
        setattr(settings, "OTEL_METRIC_RECORDER", metric_recorder)
        setattr(settings, "MIDDLEWARE", settings_middleware)
 def test_ctor_type_server(self):
     meter = metrics_api.get_meter(__name__)
     recorder = HTTPMetricRecorder(meter, HTTPMetricType.SERVER)
     self.assertEqual(recorder._http_type, HTTPMetricType.SERVER)
     self.assertTrue(
         isinstance(recorder._server_duration, metrics.ValueRecorder)
     )
     self.assertIsNone(recorder._client_duration)
 def test_record_server_duration(self):
     meter = metrics_api.get_meter(__name__)
     recorder = HTTPMetricRecorder(meter, HTTPMetricType.SERVER)
     labels = {"test": "asd"}
     with mock.patch("time.time") as time_patch:
         time_patch.return_value = 5.0
         with recorder.record_server_duration(labels):
             labels["test2"] = "asd2"
     match_key = get_dict_as_key({"test": "asd", "test2": "asd2"})
     for key in recorder._server_duration.bound_instruments.keys():
         self.assertEqual(key, match_key)
         # pylint: disable=protected-access
         bound = recorder._server_duration.bound_instruments.get(key)
         for view_data in bound.view_datas:
             self.assertEqual(view_data.labels, key)
             self.assertEqual(view_data.aggregator.current.count, 1)
             self.assertGreaterEqual(view_data.aggregator.current.sum, 0)
Esempio n. 5
0
 def test_ctor(self):
     meter = metrics_api.get_meter(__name__)
     recorder = HTTPMetricRecorder(meter, HTTPMetricType.CLIENT)
     # pylint: disable=protected-access
     self.assertEqual(recorder._http_type, HTTPMetricType.CLIENT)
     self.assertTrue(isinstance(recorder._duration, metrics.ValueRecorder))
     self.assertEqual(recorder._duration.name, "http.client.duration")
     self.assertEqual(
         recorder._duration.description,
         "measures the duration of the outbound HTTP request",
     )
Esempio n. 6
0
    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
        """
        _instrument(
            tracer_provider=kwargs.get("tracer_provider"),
            span_callback=kwargs.get("span_callback"),
        )
        self.init_metrics(
            __name__,
            __version__,
        )
        # pylint: disable=W0201
        self.metric_recorder = HTTPMetricRecorder(self.meter, SpanKind.CLIENT)