mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-07-28 20:52:57 +08:00
Add instrumentation example to logging (#3314)
* Add instrumentation example to logging * Add example output to logging instrumentation example * Fix logging instrumentation example and output * Fixing rst syntax for logging instrumentation examples --------- Co-authored-by: Emídio Neto <9735060+emdneto@users.noreply.github.com> Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
This commit is contained in:

committed by
GitHub

parent
164259e149
commit
46cf5b5257
@ -14,6 +14,47 @@
|
||||
|
||||
# pylint: disable=empty-docstring,no-value-for-parameter,no-member,no-name-in-module
|
||||
|
||||
"""
|
||||
The OpenTelemetry `logging` integration automatically injects tracing context into
|
||||
log statements, though it is opt-in and must be enabled explicitly by setting the
|
||||
environment variable `OTEL_PYTHON_LOG_CORRELATION` to `true`.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
import logging
|
||||
|
||||
from opentelemetry.instrumentation.logging import LoggingInstrumentor
|
||||
|
||||
LoggingInstrumentor().instrument()
|
||||
|
||||
logging.warning('OTel test')
|
||||
|
||||
When running the above example you will see the following output:
|
||||
|
||||
::
|
||||
|
||||
2025-03-05 09:40:04,398 WARNING [root] [example.py:7] [trace_id=0 span_id=0 resource.service.name= trace_sampled=False] - OTel test
|
||||
|
||||
The environment variable `OTEL_PYTHON_LOG_CORRELATION` must be set to `true`
|
||||
in order to enable trace context injection into logs by calling
|
||||
`logging.basicConfig()` and setting a logging format that makes use of the
|
||||
injected tracing variables.
|
||||
|
||||
Alternatively, `set_logging_format` argument can be set to `True` when
|
||||
initializing the `LoggingInstrumentor` class to achieve the same effect:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
import logging
|
||||
|
||||
from opentelemetry.instrumentation.logging import LoggingInstrumentor
|
||||
|
||||
LoggingInstrumentor().instrument(set_logging_format=True)
|
||||
|
||||
logging.warning('OTel test')
|
||||
|
||||
"""
|
||||
|
||||
import logging # pylint: disable=import-self
|
||||
from os import environ
|
||||
from typing import Collection
|
||||
|
Reference in New Issue
Block a user