Skip to content

Commit de627dc

Browse files
committed
Update doc and cleanup tests
1 parent 4de465e commit de627dc

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
class LoggingInstrumentor(BaseInstrumentor): # pylint: disable=empty-docstring
9191
__doc__ = f"""An instrumentor for stdlib logging module.
9292
93-
This instrumentor injects tracing context into logging records and optionally sets the global logging format to the following:
93+
This instrumentor optionally injects tracing context into logging records and sets the global logging format to the following:
9494
9595
.. code-block::
9696
@@ -99,6 +99,8 @@ class LoggingInstrumentor(BaseInstrumentor): # pylint: disable=empty-docstring
9999
def log_hook(span: Span, record: LogRecord):
100100
if span and span.is_recording():
101101
record.custom_user_attribute_from_log_hook = "some-value"
102+
span_ctx = span.get_span_context()
103+
record.from_sampled_span = span_ctx.trace_flags.sampled
102104
103105
Args:
104106
tracer_provider: Tracer provider instance that can be used to fetch a tracer.

instrumentation/opentelemetry-instrumentation-logging/tests/test_logging.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ def test_trace_context_injection_with_log_correlation_from_env_var(self):
101101
LoggingInstrumentor().uninstrument()
102102
LoggingInstrumentor().instrument()
103103
with self.tracer.start_as_current_span("s1") as span:
104-
span_id = format(span.get_span_context().span_id, "016x")
105-
trace_id = format(span.get_span_context().trace_id, "032x")
106-
trace_sampled = span.get_span_context().trace_flags.sampled
104+
span_ctx = span.get_span_context()
105+
span_id = format(span_ctx.span_id, "016x")
106+
trace_id = format(span_ctx.trace_id, "032x")
107+
trace_sampled = span_ctx.trace_flags.sampled
107108
self.assert_trace_context_injected(
108109
span_id, trace_id, trace_sampled
109110
)
@@ -112,9 +113,10 @@ def test_trace_context_injection_with_log_correlation_instrument_arg(self):
112113
LoggingInstrumentor().uninstrument()
113114
LoggingInstrumentor().instrument(set_logging_format=True)
114115
with self.tracer.start_as_current_span("s1") as span:
115-
span_id = format(span.get_span_context().span_id, "016x")
116-
trace_id = format(span.get_span_context().trace_id, "032x")
117-
trace_sampled = span.get_span_context().trace_flags.sampled
116+
span_ctx = span.get_span_context()
117+
span_id = format(span_ctx.span_id, "016x")
118+
trace_id = format(span_ctx.trace_id, "032x")
119+
trace_sampled = span_ctx.trace_flags.sampled
118120
self.assert_trace_context_injected(
119121
span_id, trace_id, trace_sampled
120122
)
@@ -197,9 +199,10 @@ def test_log_hook_with_set_logging_format(self):
197199
log_hook=log_hook,
198200
)
199201
with self.tracer.start_as_current_span("s1") as span:
200-
span_id = format(span.get_span_context().span_id, "016x")
201-
trace_id = format(span.get_span_context().trace_id, "032x")
202-
trace_sampled = span.get_span_context().trace_flags.sampled
202+
span_ctx = span.get_span_context()
203+
span_id = format(span_ctx.span_id, "016x")
204+
trace_id = format(span_ctx.trace_id, "032x")
205+
trace_sampled = span_ctx.trace_flags.sampled
203206
with self.caplog.at_level(level=logging.INFO):
204207
logger = logging.getLogger("test logger")
205208
logger.info("hello")

0 commit comments

Comments
 (0)