Skip to content

Commit

Permalink
Add otelTraceSampled to instrumetation-logging
Browse files Browse the repository at this point in the history
  • Loading branch information
bitnahian committed Apr 25, 2023
1 parent 19fe771 commit 4fe0f2b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def record_factory(*args, **kwargs):

record.otelSpanID = "0"
record.otelTraceID = "0"
record.otelTraceSampled = False

nonlocal service_name
if service_name is None:
Expand All @@ -113,6 +114,7 @@ def record_factory(*args, **kwargs):
if ctx != INVALID_SPAN_CONTEXT:
record.otelSpanID = format(ctx.span_id, "016x")
record.otelTraceID = format(ctx.trace_id, "032x")
record.otelTraceSampled = ctx.trace_flags.sampled
if callable(LoggingInstrumentor._log_hook):
try:
LoggingInstrumentor._log_hook( # pylint: disable=E1102
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

DEFAULT_LOGGING_FORMAT = "%(asctime)s %(levelname)s [%(name)s] [%(filename)s:%(lineno)d] [trace_id=%(otelTraceID)s span_id=%(otelSpanID)s resource.service.name=%(otelServiceName)s] - %(message)s"
DEFAULT_LOGGING_FORMAT = "%(asctime)s %(levelname)s [%(name)s] [%(filename)s:%(lineno)d] [trace_id=%(otelTraceID)s span_id=%(otelSpanID)s resource.service.name=%(otelServiceName)s trace_sampled=%(otelTraceSampled)s] - %(message)s"


_MODULE_DOC = """
Expand All @@ -27,6 +27,7 @@
- ``otelSpanID``
- ``otelTraceID``
- ``otelServiceName``
- ``otelTraceSampled``
The integration uses the following logging format by default:
Expand Down Expand Up @@ -113,7 +114,7 @@
.. code-block::
%(otelSpanID)s %(otelTraceID)s %(otelServiceName)s
%(otelSpanID)s %(otelTraceID)s %(otelServiceName)s %(otelTraceSampled)s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def test_trace_context_injection(self):
self.assertEqual(record.otelSpanID, "0")
self.assertEqual(record.otelTraceID, "0")
self.assertEqual(record.otelServiceName, "")
self.assertEqual(record.otelTraceSampled, False)


def log_hook(span, record):
Expand All @@ -82,24 +83,26 @@ def tearDown(self):
super().tearDown()
LoggingInstrumentor().uninstrument()

def assert_trace_context_injected(self, span_id, trace_id):
def assert_trace_context_injected(self, span_id, trace_id, trace_sampled):
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.otelTraceSampled, trace_sampled)
self.assertEqual(record.otelServiceName, "unknown_service")

def test_trace_context_injection(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)
trace_sampled = span.get_span_context().trace_flags.sampled
self.assert_trace_context_injected(span_id, trace_id, trace_sampled)

def test_trace_context_injection_without_span(self):
self.assert_trace_context_injected("0", "0")
self.assert_trace_context_injected("0", "0", False)

@mock.patch("logging.basicConfig")
def test_basic_config_called(self, basic_config_mock):
Expand Down Expand Up @@ -163,6 +166,7 @@ def test_log_hook(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")
trace_sampled = span.get_span_context().trace_flags.sampled
with self.caplog.at_level(level=logging.INFO):
logger = logging.getLogger("test logger")
logger.info("hello")
Expand All @@ -171,6 +175,7 @@ def test_log_hook(self):
self.assertEqual(record.otelSpanID, span_id)
self.assertEqual(record.otelTraceID, trace_id)
self.assertEqual(record.otelServiceName, "unknown_service")
self.assertEqual(record.otelTraceSampled, trace_sampled)
self.assertEqual(
record.custom_user_attribute_from_log_hook, "some-value"
)
Expand All @@ -179,14 +184,16 @@ 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)
trace_sampled = span.get_span_context().trace_flags.sampled
self.assert_trace_context_injected(span_id, trace_id, trace_sampled)

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")
trace_sampled = span.get_span_context().trace_flags.sampled
with self.caplog.at_level(level=logging.INFO):
logger = logging.getLogger("test logger")
logger.info("hello")
Expand All @@ -195,3 +202,4 @@ def test_uninstrumented(self):
self.assertFalse(hasattr(record, "otelSpanID"))
self.assertFalse(hasattr(record, "otelTraceID"))
self.assertFalse(hasattr(record, "otelServiceName"))
self.assertFalse(hasattr(record, "otelTraceSampled"))

0 comments on commit 4fe0f2b

Please sign in to comment.