From aca9fa09d7b247c245666d1ad344a1ce73ab6bfe Mon Sep 17 00:00:00 2001 From: Siim Kallas Date: Mon, 30 Oct 2023 12:19:45 +0200 Subject: [PATCH] add debug logs --- splunk_otel/distro.py | 8 ++------ splunk_otel/metrics.py | 8 ++------ splunk_otel/profiling/__init__.py | 9 +++++++++ splunk_otel/tracing.py | 8 ++------ splunk_otel/util.py | 24 ++++++++++++++++++++++++ 5 files changed, 39 insertions(+), 18 deletions(-) diff --git a/splunk_otel/distro.py b/splunk_otel/distro.py index 48dc5ab8..4bd24f99 100644 --- a/splunk_otel/distro.py +++ b/splunk_otel/distro.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import logging import os from typing import Any, Dict @@ -24,12 +23,9 @@ from splunk_otel.profiling import _start_profiling from splunk_otel.profiling.options import _Options as ProfilingOptions from splunk_otel.tracing import _configure_tracing -from splunk_otel.util import _is_truthy +from splunk_otel.util import _get_logger, _is_truthy -otel_log_level = os.environ.get("OTEL_LOG_LEVEL", logging.INFO) - -logger = logging.getLogger(__file__) -logger.setLevel(otel_log_level) +logger = _get_logger(__name__) class _SplunkDistro(BaseDistro): diff --git a/splunk_otel/metrics.py b/splunk_otel/metrics.py index 4b19b3db..8db04ce4 100644 --- a/splunk_otel/metrics.py +++ b/splunk_otel/metrics.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import logging import os from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter @@ -21,12 +20,9 @@ from opentelemetry.sdk.metrics import MeterProvider from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader -from splunk_otel.util import _is_truthy +from splunk_otel.util import _get_logger, _is_truthy -otel_log_level = os.environ.get("OTEL_LOG_LEVEL", logging.INFO) - -logger = logging.getLogger(__file__) -logger.setLevel(otel_log_level) +logger = _get_logger(__name__) def start_metrics() -> MeterProvider: diff --git a/splunk_otel/profiling/__init__.py b/splunk_otel/profiling/__init__.py index 8296710f..dec5e56d 100644 --- a/splunk_otel/profiling/__init__.py +++ b/splunk_otel/profiling/__init__.py @@ -14,6 +14,7 @@ import base64 import gzip +import os import sys import threading import time @@ -35,8 +36,11 @@ import splunk_otel from splunk_otel.profiling import profile_pb2 from splunk_otel.profiling.options import _Options +from splunk_otel.util import _get_logger from splunk_otel.version import __version__ +logger = _get_logger(__name__) + thread_states = {} batch_processor = None @@ -247,6 +251,11 @@ def _force_flush(): def _start_profiling(options): + logger.debug( + "starting profiling call_stack_interval=%s endpoint=%s", + options.call_stack_interval, + options.endpoint, + ) wrapt.wrap_function_wrapper( "opentelemetry.context", "attach", _wrapped_context_attach ) diff --git a/splunk_otel/tracing.py b/splunk_otel/tracing.py index 392063e3..371e1631 100644 --- a/splunk_otel/tracing.py +++ b/splunk_otel/tracing.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import logging import os import sys from typing import Collection, Dict, Optional, Union @@ -27,12 +26,9 @@ from pkg_resources import iter_entry_points from splunk_otel.options import _Options, _SpanExporterFactory -from splunk_otel.util import _is_truthy +from splunk_otel.util import _get_logger, _is_truthy -otel_log_level = os.environ.get("OTEL_LOG_LEVEL", logging.INFO) - -logger = logging.getLogger(__file__) -logger.setLevel(otel_log_level) +logger = _get_logger(__name__) def start_tracing( diff --git a/splunk_otel/util.py b/splunk_otel/util.py index 4ebc6ec2..add919da 100644 --- a/splunk_otel/util.py +++ b/splunk_otel/util.py @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +import logging +import os from typing import Any @@ -19,3 +21,25 @@ def _is_truthy(value: Any) -> bool: if isinstance(value, str): value = value.lower().strip() return value in [True, 1, "true", "yes"] + + +def _get_log_level(level): + levels = { + "none": logging.NOTSET, + "debug": logging.DEBUG, + "info": logging.INFO, + "warn": logging.WARNING, + "error": logging.ERROR, + "fatal": logging.CRITICAL, + } + + return levels[level.lower()] + + +def _get_logger(name): + level = _get_log_level(os.environ.get("OTEL_LOG_LEVEL", "info")) + logger = logging.getLogger(name) + logger.setLevel(level) + logger.addHandler(logging.StreamHandler()) + + return logger