def test_add_to_context(self): mock_client = mock.Mock(spec=Client) mock_batch = mock_client.batch.return_value mock_context = mock.Mock() mock_server_span = mock.Mock(spec=ServerSpan) mock_server_span.name = "name" observer = MetricsBaseplateObserver(mock_client) observer.on_server_span_created(mock_context, mock_server_span) self.assertEqual(mock_context.metrics, mock_batch) self.assertEqual(mock_server_span.register.call_count, 1)
def configure_metrics(self, metrics_client: metrics.Client) -> None: """Send timing metrics to the given client. This also adds a :py:class:`baseplate.lib.metrics.Batch` object to the ``metrics`` attribute on the :py:class:`~baseplate.RequestContext` where you can add your own application-specific metrics. The batch is automatically flushed at the end of the request. .. deprecated:: 1.0 Use :py:meth:`configure_observers` instead. :param metrics_client: Metrics client to send request metrics to. """ # pylint: disable=cyclic-import from baseplate.observers.metrics import MetricsBaseplateObserver self._metrics_client = metrics_client self.register(MetricsBaseplateObserver(metrics_client))
def configure_observers(self) -> None: """Configure diagnostics observers based on application configuration. This installs all the currently supported observers that have settings in the configuration file. See :py:mod:`baseplate.observers` for the configuration settings available for each observer. """ skipped = [] from baseplate.observers.logging import LoggingBaseplateObserver self.register(LoggingBaseplateObserver()) if gevent.monkey.is_module_patched("socket"): from baseplate.observers.timeout import TimeoutBaseplateObserver timeout_observer = TimeoutBaseplateObserver.from_config( self._app_config) self.register(timeout_observer) else: skipped.append("timeout") if "metrics.tagging" in self._app_config: if "metrics.namespace" in self._app_config: raise ValueError( "metrics.namespace not allowed with metrics.tagging") from baseplate.lib.metrics import metrics_client_from_config from baseplate.observers.metrics_tagged import TaggedMetricsBaseplateObserver self._metrics_client = metrics_client_from_config(self._app_config) self.register( TaggedMetricsBaseplateObserver.from_config_and_client( self._app_config, self._metrics_client)) elif "metrics.namespace" in self._app_config: from baseplate.lib.metrics import metrics_client_from_config from baseplate.observers.metrics import MetricsBaseplateObserver self._metrics_client = metrics_client_from_config(self._app_config) self.register( MetricsBaseplateObserver.from_config_and_client( self._app_config, self._metrics_client)) else: skipped.append("metrics") if "tracing.service_name" in self._app_config: from baseplate.observers.tracing import tracing_client_from_config from baseplate.observers.tracing import TraceBaseplateObserver tracing_client = tracing_client_from_config(self._app_config) self.register(TraceBaseplateObserver(tracing_client)) else: skipped.append("tracing") if "sentry.dsn" in self._app_config or "SENTRY_DSN" in os.environ: from baseplate.observers.sentry import init_sentry_client_from_config from baseplate.observers.sentry import SentryBaseplateObserver from baseplate.observers.sentry import _SentryUnhandledErrorReporter init_sentry_client_from_config(self._app_config) _SentryUnhandledErrorReporter.install() self.register(SentryBaseplateObserver()) else: skipped.append("sentry") if skipped: logger.debug( "The following observers are unconfigured and won't run: %s", ", ".join(skipped))