-
Notifications
You must be signed in to change notification settings - Fork 659
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for OTEL_LOG_LEVEL env var
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import logging | ||
import unittest | ||
from importlib import reload | ||
from logging import CRITICAL, DEBUG, ERROR, INFO, WARNING | ||
from unittest.mock import patch | ||
|
||
from opentelemetry import sdk | ||
|
||
sdk_logger = logging.getLogger("opentelemetry.sdk") | ||
|
||
|
||
def reload_sdk_with_env(env_dict): | ||
with patch.dict("os.environ", env_dict, clear=True): | ||
reload(sdk) | ||
|
||
|
||
class TestLogLevelEnv(unittest.TestCase): | ||
def tearDown(self): | ||
"""reload sdk to reset logging level""" | ||
reload(sdk) | ||
|
||
def test_loglevel_is_info_by_default(self): | ||
reload_sdk_with_env({}) | ||
self.assertEqual(sdk_logger.getEffectiveLevel(), INFO) | ||
|
||
def test_setting_log_level_env_to_valid_value(self): | ||
valid_value_log_level_pairs = [ | ||
("DEBUG", DEBUG), | ||
("INFO", INFO), | ||
("WARNING", WARNING), | ||
("ERROR", ERROR), | ||
("CRITICAL", CRITICAL), | ||
] | ||
for value, log_level in valid_value_log_level_pairs: | ||
reload_sdk_with_env({"OTEL_LOG_LEVEL": value}) | ||
self.assertEqual(sdk_logger.getEffectiveLevel(), log_level, value) | ||
|
||
def test_log_level_is_propagated(self): | ||
reload_sdk_with_env({"OTEL_LOG_LEVEL": "CRITICAL"}) | ||
self.assertEqual(sdk_logger.getEffectiveLevel(), CRITICAL) | ||
self.assertEqual( | ||
sdk_logger.getChild("trace").getEffectiveLevel(), CRITICAL | ||
) | ||
self.assertEqual( | ||
sdk_logger.getChild("metrics").getEffectiveLevel(), CRITICAL | ||
) | ||
self.assertEqual( | ||
sdk_logger.getChild("resources").getEffectiveLevel(), CRITICAL | ||
) | ||
|
||
def test_invalid_log_level_value_warns_and_defaults_to_info(self): | ||
with self.assertLogs(level=WARNING): | ||
reload_sdk_with_env({"OTEL_LOG_LEVEL": "NOT_A_VALID_LOG_LEVEL"}) | ||
self.assertEqual(sdk_logger.getEffectiveLevel(), INFO) | ||
|
||
def test_log_level_is_case_insensitive(self): | ||
reload_sdk_with_env({"OTEL_LOG_LEVEL": "cRiTiCaL"}) | ||
self.assertEqual(sdk_logger.getEffectiveLevel(), CRITICAL) |