def test_should_sample_utilizes_sampled_setting(self): client = make_client("test-service", sample_rate=0) baseplate_observer = TraceBaseplateObserver(client) span_with_sampled_flag = Span("test-id", "test-parent", "test-span-id", True, 0, "test", self.mock_context) self.assertTrue( baseplate_observer.should_sample(span_with_sampled_flag))
def test_should_sample_utilizes_sample_rate(self): client = make_client("test-service", sample_rate=1) baseplate_observer = TraceBaseplateObserver(client) span = Span("test-id", "test-parent", "test-span-id", None, 0, "test", self.mock_context) self.assertTrue(baseplate_observer.should_sample(span)) baseplate_observer.sample_rate = 0 self.assertFalse(baseplate_observer.should_sample(span))
def test_no_tracing_without_sampling(self): client = make_client("test-service", sample_rate=0) baseplate_observer = TraceBaseplateObserver(client) context_mock = mock.Mock() span = ServerSpan("test-id", "test-parent-id", "test-span-id", False, 0, "test", self.mock_context) baseplate_observer.on_server_span_created(context_mock, span) self.assertEqual(len(span.observers), 0)
def test_should_sample_utilizes_force_sampling(self): client = make_client("test-service", sample_rate=0) baseplate_observer = TraceBaseplateObserver(client) span_with_forced = Span("test-id", "test-parent", "test-span-id", False, 1, "test", self.mock_context) span_without_forced = Span("test-id", "test-parent", "test-span-id", False, 0, "test", self.mock_context) self.assertTrue(baseplate_observer.should_sample(span_with_forced)) self.assertFalse(baseplate_observer.should_sample(span_without_forced))
def test_register_server_span_observer(self): client = make_client("test-service") baseplate_observer = TraceBaseplateObserver(client) context_mock = mock.Mock() span = ServerSpan("test-id", "test-parent-id", "test-span-id", True, 0, "test", self.mock_context) baseplate_observer.on_server_span_created(context_mock, span) self.assertEqual(len(span.observers), 1) self.assertEqual(type(span.observers[0]), TraceServerSpanObserver)
def test_force_sampling(self): span_with_debug_flag = Span("test-id", "test-parent", "test-span-id", True, 1, "test", self.mock_context) span_without_debug_flag = Span("test-id", "test-parent", "test-span-id", True, 0, "test", self.mock_context) self.assertTrue( TraceBaseplateObserver.force_sampling(span_with_debug_flag)) self.assertFalse( TraceBaseplateObserver.force_sampling(span_without_debug_flag))
def setUp(self): thread_patch = mock.patch("threading.Thread", autospec=True) thread_patch.start() self.addCleanup(thread_patch.stop) configurator = Configurator() configurator.add_route("example", "/example", request_method="GET") configurator.add_view(example_application, route_name="example", renderer="json") configurator.add_route("local_test", "/local_test", request_method="GET") configurator.add_view(local_parent_trace_within_context, route_name="local_test", renderer="json") self.client = make_client("test-service") self.observer = TraceBaseplateObserver(self.client) self.baseplate = Baseplate() self.baseplate.register(self.observer) self.baseplate_configurator = BaseplateConfigurator( self.baseplate, header_trust_handler=StaticTrustHandler(trust_headers=True), ) configurator.include(self.baseplate_configurator.includeme) app = configurator.make_wsgi_app() self.local_span_ids = [] self.local_span_observers = [] self.test_app = webtest.TestApp(app)
def configure_tracing( self, tracing_client: "baseplate.observers.tracing.TracingClient" ) -> None: """Collect and send span information for request tracing. When configured, this will send tracing information automatically collected by Baseplate to the configured distributed tracing service. .. deprecated:: 1.0 Use :py:meth:`configure_observers` instead. :param tracing_client: Tracing client to send request traces to. """ # pylint: disable=cyclic-import from baseplate.observers.tracing import TraceBaseplateObserver self.register(TraceBaseplateObserver(tracing_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))
def test_remote_recorder_setup(self): client = make_client("test-service", tracing_endpoint=Endpoint("test:1111")) baseplate_observer = TraceBaseplateObserver(client) self.assertTrue(isinstance(baseplate_observer.recorder, RemoteRecorder))
def test_sets_hostname(self): client = make_client("test-service") baseplate_observer = TraceBaseplateObserver(client) self.assertIsNotNone(baseplate_observer.hostname)
def test_logging_recorder_setup(self): client = make_client("test-service") baseplate_observer = TraceBaseplateObserver(client) self.assertEqual(type(baseplate_observer.recorder), LoggingRecorder)
def test_null_recorder_setup(self): client = make_client("test-service", log_if_unconfigured=False) baseplate_observer = TraceBaseplateObserver(client) self.assertEqual(type(baseplate_observer.recorder), NullRecorder)