Skip to content

Commit

Permalink
Add support for OTEL_LOG_LEVEL env var
Browse files Browse the repository at this point in the history
  • Loading branch information
ffe4 committed Sep 4, 2020
1 parent c435600 commit b7954b6
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
22 changes: 22 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@
The OpenTelemetry SDK package is an implementation of the OpenTelemetry
API
"""
import logging
import os

from . import metrics, trace, util

__all__ = ["metrics", "trace", "util"]

_LOG_LEVELS = {
"DEBUG": logging.DEBUG,
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"ERROR": logging.ERROR,
"CRITICAL": logging.CRITICAL,
}

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
if os.getenv("OTEL_LOG_LEVEL") is not None:
key = os.getenv("OTEL_LOG_LEVEL").upper()
if key in _LOG_LEVELS:
logger.setLevel(_LOG_LEVELS.get(key))
else:
logger.warning(
"Invalid value for environment variable OTEL_LOG_LEVEL. Defaulting to INFO."
)
58 changes: 58 additions & 0 deletions opentelemetry-sdk/tests/test_sdk_configuration.py
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)

0 comments on commit b7954b6

Please sign in to comment.