Skip to content

Commit b4ba407

Browse files
committed
Move to our own env var for controlling autoinstrumentation
1 parent 267f85b commit b4ba407

3 files changed

Lines changed: 32 additions & 7 deletions

File tree

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
DEFAULT_LOGGING_FORMAT,
6767
)
6868
from opentelemetry.instrumentation.logging.environment_variables import (
69+
OTEL_PYTHON_LOG_AUTO_INSTRUMENTATION,
6970
OTEL_PYTHON_LOG_CODE_ATTRIBUTES,
7071
OTEL_PYTHON_LOG_CORRELATION,
7172
OTEL_PYTHON_LOG_FORMAT,
@@ -211,20 +212,23 @@ def record_factory(*args, **kwargs):
211212
# - the sdk logging handler is enabled and we should do no nothing
212213
# - the sdk logging handler is not enabled and we should setup the handler by default
213214
# - the sdk logging handler is not enabled and the user do not want we setup the handler
214-
# FIXME: we are reusing the very same env var of the sdk but probably we should be use a new one
215-
logging_autoinstrumentation_env_var = (
215+
sdk_autoinstrumentation_env_var = (
216216
environ.get(
217217
"OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED", "notset"
218218
)
219219
.strip()
220220
.lower()
221221
)
222-
if logging_autoinstrumentation_env_var == "true":
222+
if sdk_autoinstrumentation_env_var == "true":
223223
_logger.warning(
224224
"Disabling logging auto-instrumentation. If you have opentelemetry-instrumentation-logging "
225225
"you don't need to set `OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true`"
226226
)
227-
elif logging_autoinstrumentation_env_var == "notset":
227+
elif (
228+
environ.get(OTEL_PYTHON_LOG_AUTO_INSTRUMENTATION, "true")
229+
.strip()
230+
.lower()
231+
) == "true":
228232
log_code_attributes = (
229233
environ.get(OTEL_PYTHON_LOG_CODE_ATTRIBUTES, "false")
230234
.strip()

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
OTEL_PYTHON_LOG_AUTO_INSTRUMENTATION = "OTEL_PYTHON_LOG_AUTO_INSTRUMENTATION"
1516
OTEL_PYTHON_LOG_CORRELATION = "OTEL_PYTHON_LOG_CORRELATION"
1617
OTEL_PYTHON_LOG_FORMAT = "OTEL_PYTHON_LOG_FORMAT"
1718
OTEL_PYTHON_LOG_LEVEL = "OTEL_PYTHON_LOG_LEVEL"

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def test_no_op_tracer_provider(self):
264264
"os.environ",
265265
{"OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED": "true"},
266266
)
267-
def test_handler_setup_is_disabled_if_autoinstrumentation_env_var_is_set_to_true(
267+
def test_handler_setup_is_disabled_if_sdk_autoinstrumentation_env_var_is_set_to_true(
268268
self,
269269
):
270270
LoggingInstrumentor().uninstrument()
@@ -291,7 +291,27 @@ def test_handler_setup_is_disabled_if_autoinstrumentation_env_var_is_set_to_true
291291
"os.environ",
292292
{"OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED": "false"},
293293
)
294-
def test_handler_setup_is_disabled_if_autoinstrumentation_env_var_is_set_to_false(
294+
def test_handler_setup_is_enabled_if_sdk_autoinstrumentation_env_var_is_set_to_false(
295+
self,
296+
):
297+
LoggingInstrumentor().uninstrument()
298+
with self.caplog.at_level(level=logging.WARNING):
299+
LoggingInstrumentor().instrument()
300+
301+
self.assertEqual(len(self.caplog.records), 0)
302+
root_logger = logging.getLogger()
303+
logging_handler_instances = [
304+
handler
305+
for handler in root_logger.handlers
306+
if isinstance(handler, LoggingHandler)
307+
]
308+
self.assertEqual(len(logging_handler_instances), 1)
309+
310+
@mock.patch.dict(
311+
"os.environ",
312+
{"OTEL_PYTHON_LOG_AUTO_INSTRUMENTATION": "false"},
313+
)
314+
def test_handler_setup_is_enabled_if_autoinstrumentation_env_var_is_set_to_false(
295315
self,
296316
):
297317
LoggingInstrumentor().uninstrument()
@@ -307,7 +327,7 @@ def test_handler_setup_is_disabled_if_autoinstrumentation_env_var_is_set_to_fals
307327
]
308328
self.assertEqual(logging_handler_instances, [])
309329

310-
def test_handler_setup_is_called_if_autoinstrumentation_env_var_is_not_set(
330+
def test_handler_setup_is_called_if_autoinstrumentation_env_vars_are_not_set(
311331
self,
312332
):
313333
LoggingInstrumentor().uninstrument()

0 commit comments

Comments
 (0)