def configure_observers(self, app_config: config.RawConfig, module_name: Optional[str] = None) -> 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. :param app_config: The application configuration which should have settings for the error reporter. :param module_name: Name of the root package of the application. If not specified, will be guessed from the package calling this function. """ skipped = [] self.configure_logging() if "metrics.namespace" in app_config: from baseplate.lib.metrics import metrics_client_from_config metrics_client = metrics_client_from_config(app_config) self.configure_metrics(metrics_client) else: skipped.append("metrics") if "tracing.service_name" in app_config: from baseplate.observers.tracing import tracing_client_from_config tracing_client = tracing_client_from_config(app_config) self.configure_tracing(tracing_client) else: skipped.append("tracing") if "sentry.dsn" in app_config: from baseplate.observers.sentry import error_reporter_from_config if module_name is None: module_name = inspect.getmodule( inspect.stack()[1].frame).__name__ error_reporter = error_reporter_from_config( app_config, module_name) self.configure_error_reporting(error_reporter) else: skipped.append("error_reporter") if skipped: logger.debug( "The following observers are unconfigured and won't run: %s", ", ".join(skipped))
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))
def configure_observers(self, app_config: Optional[config.RawConfig] = None, module_name: Optional[str] = None) -> 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. :param app_config: The application configuration which should have settings for the error reporter. If not specified, the config must be passed to the Baseplate() constructor. :param module_name: Name of the root package of the application. If not specified, will be guessed from the package calling this function. """ skipped = [] app_config = app_config or self._app_config if not app_config: raise Exception( "configuration must be passed to Baseplate() or here") self.configure_logging() if gevent.monkey.is_module_patched("socket"): # pylint: disable=cyclic-import from baseplate.observers.timeout import TimeoutBaseplateObserver timeout_observer = TimeoutBaseplateObserver.from_config(app_config) self.register(timeout_observer) else: skipped.append("timeout") if "metrics.namespace" in app_config: from baseplate.lib.metrics import metrics_client_from_config metrics_client = metrics_client_from_config(app_config) self.configure_metrics(metrics_client) else: skipped.append("metrics") if "tracing.service_name" in app_config: from baseplate.observers.tracing import tracing_client_from_config tracing_client = tracing_client_from_config(app_config) self.configure_tracing(tracing_client) else: skipped.append("tracing") if "sentry.dsn" in app_config: from baseplate.observers.sentry import error_reporter_from_config if module_name is None: module = inspect.getmodule(inspect.stack()[1].frame) if not module: raise Exception( "failed to detect module name, pass one explicitly") module_name = module.__name__ error_reporter = error_reporter_from_config( app_config, module_name) self.configure_error_reporting(error_reporter) else: skipped.append("error_reporter") if skipped: logger.debug( "The following observers are unconfigured and won't run: %s", ", ".join(skipped))
def configure_observers( self, app_config: Optional[config.RawConfig] = None, module_name: Optional[str] = None ) -> 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. :param app_config: The application configuration which should have settings for the error reporter. If not specified, the config must be passed to the Baseplate() constructor. :param module_name: Name of the root package of the application. If not specified, will be guessed from the package calling this function. """ skipped = [] if app_config: if self._app_config: raise Exception("pass app_config to the constructor or this method but not both") warn_deprecated( "Passing configuration to configure_observers is deprecated in " "favor of passing it to the Baseplate constructor" ) else: app_config = self._app_config self.configure_logging() if gevent.monkey.is_module_patched("socket"): # pylint: disable=cyclic-import from baseplate.observers.timeout import TimeoutBaseplateObserver timeout_observer = TimeoutBaseplateObserver.from_config(app_config) self.register(timeout_observer) else: skipped.append("timeout") if "metrics.tagging" in app_config: if "metrics.namespace" in app_config: raise ValueError("metrics.namespace not allowed with metrics.tagging") from baseplate.lib.metrics import metrics_client_from_config metrics_client = metrics_client_from_config(app_config) self.configure_tagged_metrics(metrics_client) elif "metrics.namespace" in app_config: from baseplate.lib.metrics import metrics_client_from_config metrics_client = metrics_client_from_config(app_config) self.configure_metrics(metrics_client) else: skipped.append("metrics") if "tracing.service_name" in app_config: from baseplate.observers.tracing import tracing_client_from_config tracing_client = tracing_client_from_config(app_config) self.configure_tracing(tracing_client) else: skipped.append("tracing") if "sentry.dsn" in app_config: from baseplate.observers.sentry import error_reporter_from_config if module_name is None: module_name = get_calling_module_name() error_reporter = error_reporter_from_config(app_config, module_name) self.configure_error_reporting(error_reporter) else: skipped.append("error_reporter") if skipped: logger.debug( "The following observers are unconfigured and won't run: %s", ", ".join(skipped) )