Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for OTEL_LOG_LEVEL env var #1069

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)