Skip to content

Commit ecdfe01

Browse files
committed
added readme.rst file log record enrichment behavior of logging instrumentation
Signed-off-by: Wali, Sunil Shidrayi <[email protected]>
1 parent 9cced97 commit ecdfe01

File tree

1 file changed

+134
-0
lines changed
  • instrumentation/opentelemetry-instrumentation-logging

1 file changed

+134
-0
lines changed

instrumentation/opentelemetry-instrumentation-logging/README.rst

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,140 @@ Installation
1313

1414
pip install opentelemetry-instrumentation-logging
1515

16+
Log record enrichment behavior of logging instrumentation
17+
18+
"""
19+
The OpenTelemetry ``logging`` integration automatically injects tracing context into log statements.
20+
It's structured within an `if` statement that checks [`set_logging_format`] is `True`, indicating that logging configuration should be applied.
21+
22+
The integration registers a custom log record factory with the the standard library logging module that automatically inject
23+
tracing context into log record objects. Optionally, the integration can also call ``logging.basicConfig()`` to set a logging
24+
format with placeholders for span ID, trace ID and service name.
25+
26+
1. **Determining the Log Format**: The code first attempts to determine the logging format by looking for a `logging_format` key in the [`kwargs`]( dictionary. If it's not found, it tries to fetch the value from an environment variable named [`OTEL_PYTHON_LOG_FORMAT`]. If neither is available, it defaults to a predefined constant [`DEFAULT_LOGGING_FORMAT`]. This approach provides flexibility, allowing the log format to be specified through function arguments, environment variables, or falling back to a default if neither is provided.
27+
28+
2. **Determining the Log Level**: Similarly, the log level is determined by looking for a [`log_level`] key in [`kwargs`](. If not found, it attempts to fetch a value from an environment variable [`OTEL_PYTHON_LOG_LEVEL`] and maps this value through a [`LEVELS`] dictionary to get the corresponding log level. If this process doesn't yield a result, it defaults to [`logging.INFO`]. This mechanism allows for dynamic adjustment of the log level based on runtime configurations or environment settings.
29+
30+
3. **Applying the Configuration**: Finally, with the determined [`log_format`] and [`log_level`](c, the code calls [`logging.basicConfig(format=log_format, level=log_level)`]. This function is a part of Python's standard logging library and is used to configure the basic settings of the logging system. It sets up the default format for log messages and the log level, which controls the severity of the messages that are processed.
31+
32+
The following keys are injected into log record objects by the factory:
33+
34+
- ``otelSpanID``
35+
- ``otelTraceID``
36+
- ``otelServiceName``
37+
- ``otelTraceSampled``
38+
39+
The integration uses the following logging format by default:
40+
41+
.. code-block::
42+
43+
{default_logging_format}
44+
45+
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"
46+
47+
Enable trace context injection
48+
------------------------------
49+
50+
The integration is opt-in and must be enabled explicitly by setting the environment variable ``OTEL_PYTHON_LOG_CORRELATION`` to ``true``.
51+
52+
The integration always registers the custom factory that injects the tracing context into the log record objects. Setting
53+
``OTEL_PYTHON_LOG_CORRELATION`` to ``true`` calls ``logging.basicConfig()`` to set a logging format that actually makes
54+
use of the injected variables.
55+
56+
57+
Environment variables
58+
---------------------
59+
60+
.. envvar:: OTEL_PYTHON_LOG_CORRELATION
61+
62+
This env var must be set to ``true`` in order to enable trace context injection into logs by calling ``logging.basicConfig()`` and
63+
setting a logging format that makes use of the injected tracing variables.
64+
65+
Alternatively, ``set_logging_format`` argument can be set to ``True`` when initializing the ``LoggingInstrumentor`` class to achieve the
66+
same effect.
67+
68+
.. code-block::
69+
70+
LoggingInstrumentor(set_logging_format=True)
71+
72+
The default value is ``false``.
73+
74+
.. envvar:: OTEL_PYTHON_LOG_FORMAT
75+
76+
This env var can be used to instruct the instrumentation to use a custom logging format.
77+
78+
Alternatively, a custom logging format can be passed to the ``LoggingInstrumentor`` as the ``logging_format`` argument. For example:
79+
80+
.. code-block::
81+
82+
LoggingInstrumentor(logging_format='%(msg)s [span_id=%(span_id)s]')
83+
84+
85+
The default value is:
86+
87+
.. code-block::
88+
89+
{default_logging_format}
90+
91+
.. envvar:: OTEL_PYTHON_LOG_LEVEL
92+
93+
This env var can be used to set a custom logging level.
94+
95+
Alternatively, log level can be passed to the ``LoggingInstrumentor`` during initialization. For example:
96+
97+
.. code-block::
98+
99+
LoggingInstrumentor(log_level=logging.DEBUG)
100+
101+
102+
The default value is ``info``.
103+
104+
Options are:
105+
106+
- ``info``
107+
- ``error``
108+
- ``debug``
109+
- ``warning``
110+
111+
Manually calling logging.basicConfig
112+
------------------------------------
113+
114+
``logging.basicConfig()`` can be called to set a global logging level and format. Only the first ever call has any effect on the global logger.
115+
Any subsequent calls have no effect and do not override a previously configured global logger. This integration calls ``logging.basicConfig()`` for you
116+
when ``OTEL_PYTHON_LOG_CORRELATION`` is set to ``true``. It uses the format and level specified by ``OTEL_PYTHON_LOG_FORMAT`` and ``OTEL_PYTHON_LOG_LEVEL``
117+
environment variables respectively.
118+
119+
If you code or some other library/framework you are using calls logging.basicConfig before this integration is enabled, then this integration's logging
120+
format will not be used and log statements will not contain tracing context. For this reason, you'll need to make sure this integration is enabled as early
121+
as possible in the service lifecycle or your framework is configured to use a logging format with placeholders for tracing context. This can be achieved by
122+
adding the following placeholders to your logging format:
123+
124+
.. code-block::
125+
126+
%(otelSpanID)s %(otelTraceID)s %(otelServiceName)s %(otelTraceSampled)s
127+
128+
129+
130+
API
131+
-----
132+
133+
.. code-block:: python
134+
135+
from opentelemetry.instrumentation.logging import LoggingInstrumentor
136+
137+
LoggingInstrumentor().instrument(set_logging_format=True)
138+
139+
140+
Note
141+
-----
142+
143+
If you do not set ``OTEL_PYTHON_LOG_CORRELATION`` to ``true`` but instead set the logging format manually or through your framework, you must ensure that this
144+
integration is enabled before you set the logging format. This is important because unless the integration is enabled, the tracing context variables
145+
are not injected into the log record objects. This means any attempted log statements made after setting the logging format and before enabling this integration
146+
will result in KeyError exceptions. Such exceptions are automatically swallowed by the logging module and do not result in crashes but you may still lose out
147+
on important log messages.
148+
"""
149+
16150

17151
References
18152
----------

0 commit comments

Comments
 (0)