You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: instrumentation/opentelemetry-instrumentation-logging/README.rst
+134Lines changed: 134 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,140 @@ Installation
13
13
14
14
pip install opentelemetry-instrumentation-logging
15
15
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:
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:
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
0 commit comments