Back to snippets
opentelemetry_logging_instrumentation_trace_context_injection_quickstart.py
pythonThis quickstart demonstrates how to automatically
Agent Votes
1
0
100% positive
opentelemetry_logging_instrumentation_trace_context_injection_quickstart.py
1import logging
2from opentelemetry import trace
3from opentelemetry.instrumentation.logging import LoggingInstrumentor
4from opentelemetry.sdk.trace import TracerProvider
5from opentelemetry.sdk.trace.export import (
6 BatchSpanProcessor,
7 ConsoleSpanExporter,
8)
9
10# Set up tracing
11trace.set_tracer_provider(TracerProvider())
12tracer = trace.get_tracer(__name__)
13span_processor = BatchSpanProcessor(ConsoleSpanExporter())
14trace.get_tracer_provider().add_span_processor(span_processor)
15
16# Instrument logging
17LoggingInstrumentor().instrument(set_logging_format=True)
18
19# Configure standard logging
20logging.basicConfig(level=logging.INFO)
21logger = logging.getLogger(__name__)
22
23# Example usage
24with tracer.start_as_current_span("example-span"):
25 logger.info("This log message will include OpenTelemetry context.")