def test_custom_format_and_level_api(self, basic_config_mock): # pylint: disable=no-self-use LoggingInstrumentor().uninstrument() LoggingInstrumentor().instrument( set_logging_format=True, logging_format="%(message)s span_id=%(otelSpanID)s", log_level=logging.WARNING, ) basic_config_mock.assert_called_with( format="%(message)s span_id=%(otelSpanID)s", level=logging.WARNING)
def test_basic_config_called(self, basic_config_mock): LoggingInstrumentor().uninstrument() LoggingInstrumentor().instrument() self.assertFalse(basic_config_mock.called) LoggingInstrumentor().uninstrument() env_patch = mock.patch.dict("os.environ", {"OTEL_PYTHON_LOG_CORRELATION": "true"}) env_patch.start() LoggingInstrumentor().instrument() basic_config_mock.assert_called_with(format=DEFAULT_LOGGING_FORMAT, level=logging.INFO) env_patch.stop()
def test_custom_format_and_level_env(self, basic_config_mock): LoggingInstrumentor().uninstrument() LoggingInstrumentor().instrument() self.assertFalse(basic_config_mock.called) LoggingInstrumentor().uninstrument() env_patch = mock.patch.dict( "os.environ", { "OTEL_PYTHON_LOG_CORRELATION": "true", "OTEL_PYTHON_LOG_FORMAT": "%(message)s %(otelSpanID)s", "OTEL_PYTHON_LOG_LEVEL": "error", }, ) env_patch.start() LoggingInstrumentor().instrument() basic_config_mock.assert_called_with( format="%(message)s %(otelSpanID)s", level=logging.ERROR) env_patch.stop()
def test_log_hook(self): LoggingInstrumentor().uninstrument() LoggingInstrumentor().instrument( set_logging_format=True, log_hook=log_hook, ) with self.tracer.start_as_current_span("s1") as span: span_id = format(span.get_span_context().span_id, "016x") trace_id = format(span.get_span_context().trace_id, "032x") with self.caplog.at_level(level=logging.INFO): logger = logging.getLogger("test logger") logger.info("hello") self.assertEqual(len(self.caplog.records), 1) record = self.caplog.records[0] self.assertEqual(record.otelSpanID, span_id) self.assertEqual(record.otelTraceID, trace_id) self.assertEqual(record.otelServiceName, "unknown_service") self.assertEqual( record.custom_user_attribute_from_log_hook, "some-value" )
def test_uninstrumented(self): with self.tracer.start_as_current_span("s1") as span: span_id = format(span.get_span_context().span_id, "016x") trace_id = format(span.get_span_context().trace_id, "032x") self.assert_trace_context_injected(span_id, trace_id) LoggingInstrumentor().uninstrument() self.caplog.clear() with self.tracer.start_as_current_span("s1") as span: span_id = format(span.get_span_context().span_id, "016x") trace_id = format(span.get_span_context().trace_id, "032x") with self.caplog.at_level(level=logging.INFO): logger = logging.getLogger("test logger") logger.info("hello") self.assertEqual(len(self.caplog.records), 1) record = self.caplog.records[0] self.assertFalse(hasattr(record, "otelSpanID")) self.assertFalse(hasattr(record, "otelTraceID")) self.assertFalse(hasattr(record, "otelServiceName"))
def tearDown(self): super().tearDown() LoggingInstrumentor().uninstrument()
def setUp(self): super().setUp() LoggingInstrumentor().instrument() self.tracer = get_tracer(__name__)
def init_celery_beat_tracing(*args, **kwargs): CeleryInstrumentor().instrument() LoggingInstrumentor().instrument()
def setUp(self): super().setUp() LoggingInstrumentor().instrument(tracer_provider=FakeTracerProvider())