From f7f419308fa90872e4e1d33983fa2bd3aacae20c Mon Sep 17 00:00:00 2001 From: Branislav Jenco Date: Thu, 16 Oct 2025 12:56:53 +0200 Subject: [PATCH 1/8] Extracted azure monitor config into own function with lock --- datareservoirio/_logging.py | 27 +++++++++++++++++++++------ datareservoirio/client.py | 12 ++++++------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/datareservoirio/_logging.py b/datareservoirio/_logging.py index b9d3b481..cbeb5735 100644 --- a/datareservoirio/_logging.py +++ b/datareservoirio/_logging.py @@ -1,6 +1,7 @@ import logging import os -from functools import cache, wraps +from functools import lru_cache, wraps +import threading from azure.monitor.opentelemetry import configure_azure_monitor @@ -10,18 +11,32 @@ from .globalsettings import environment -@cache +_configure_lock = threading.Lock() +_configured_loggers = {} + +def _ensure_azure_monitor_configured(connection_string, logger_name): + cache_key = (connection_string, logger_name) + + if cache_key not in _configured_loggers: + with _configure_lock: + # Double-check inside lock + if cache_key not in _configured_loggers: + configure_azure_monitor(connection_string=connection_string, logger_name=logger_name) + _configured_loggers[cache_key] = True + + +@lru_cache(maxsize=1) def get_exceptions_logger() -> logging.Logger: + print("bruh", flush=True) + logging.log(logging.INFO, 'foobar') exceptions_logger = logging.getLogger(__name__ + "_exception_logger") exceptions_logger.setLevel(logging.DEBUG) if os.getenv(ENV_VAR_ENABLE_APP_INSIGHTS) is not None: enable_app_insights = os.environ[ENV_VAR_ENABLE_APP_INSIGHTS].lower() if enable_app_insights == "true" or enable_app_insights == "1": - configure_azure_monitor( - connection_string=environment._application_insight_connectionstring, - logger_name=__name__ + "_exceptions_logger", - ) + _ensure_azure_monitor_configured(connection_string=environment._application_insight_connectionstring, + logger_name=__name__ + "_exception_appinsight") exceptions_logger.setLevel("WARNING") return exceptions_logger diff --git a/datareservoirio/client.py b/datareservoirio/client.py index bbb6337a..78134f19 100644 --- a/datareservoirio/client.py +++ b/datareservoirio/client.py @@ -5,7 +5,7 @@ from collections import defaultdict from concurrent.futures import ThreadPoolExecutor from datetime import datetime -from functools import cache, wraps +from functools import lru_cache, wraps from operator import itemgetter from urllib.parse import urlencode from uuid import uuid4 @@ -25,7 +25,7 @@ from datareservoirio._constants import ENV_VAR_ENABLE_APP_INSIGHTS -from ._logging import log_decorator +from ._logging import _ensure_azure_monitor_configured, log_decorator from ._utils import function_translation, period_translation from .globalsettings import environment from .storage import Storage @@ -33,14 +33,14 @@ log = logging.getLogger(__name__) -@cache +@lru_cache(maxsize=1) def metric() -> logging.Logger: logger = logging.getLogger(__name__ + "_metric_appinsight") if os.getenv(ENV_VAR_ENABLE_APP_INSIGHTS) is not None: enable_app_insights = os.environ[ENV_VAR_ENABLE_APP_INSIGHTS].lower() if enable_app_insights == "true" or enable_app_insights == "1": logger.setLevel(logging.DEBUG) - configure_azure_monitor( + _ensure_azure_monitor_configured( connection_string=environment._application_insight_connectionstring, logger_name=__name__ + "_metric_appinsight", ) @@ -344,7 +344,7 @@ def wrapper(self, series_id, start=None, end=None, **kwargs): return wrapper - @log_decorator("exception") + # @log_decorator("exception") @_timer @retry( stop=stop_after_attempt( @@ -361,7 +361,7 @@ def wrapper(self, series_id, start=None, end=None, **kwargs): ), wait=wait_chain(*[wait_fixed(0.1), wait_fixed(0.5), wait_fixed(30)]), ) - @log_decorator("warning") + # @log_decorator("warning") def get( self, series_id, From 84007a934b4c86fc6f2bc87f32d34d5d9f3dc984 Mon Sep 17 00:00:00 2001 From: Branislav Jenco Date: Thu, 16 Oct 2025 13:01:56 +0200 Subject: [PATCH 2/8] Cleanup --- datareservoirio/_logging.py | 4 +--- datareservoirio/client.py | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/datareservoirio/_logging.py b/datareservoirio/_logging.py index cbeb5735..681add55 100644 --- a/datareservoirio/_logging.py +++ b/datareservoirio/_logging.py @@ -19,7 +19,7 @@ def _ensure_azure_monitor_configured(connection_string, logger_name): if cache_key not in _configured_loggers: with _configure_lock: - # Double-check inside lock + # Double-check locking if cache_key not in _configured_loggers: configure_azure_monitor(connection_string=connection_string, logger_name=logger_name) _configured_loggers[cache_key] = True @@ -27,8 +27,6 @@ def _ensure_azure_monitor_configured(connection_string, logger_name): @lru_cache(maxsize=1) def get_exceptions_logger() -> logging.Logger: - print("bruh", flush=True) - logging.log(logging.INFO, 'foobar') exceptions_logger = logging.getLogger(__name__ + "_exception_logger") exceptions_logger.setLevel(logging.DEBUG) diff --git a/datareservoirio/client.py b/datareservoirio/client.py index 78134f19..35a53b42 100644 --- a/datareservoirio/client.py +++ b/datareservoirio/client.py @@ -344,7 +344,7 @@ def wrapper(self, series_id, start=None, end=None, **kwargs): return wrapper - # @log_decorator("exception") + @log_decorator("exception") @_timer @retry( stop=stop_after_attempt( @@ -361,7 +361,7 @@ def wrapper(self, series_id, start=None, end=None, **kwargs): ), wait=wait_chain(*[wait_fixed(0.1), wait_fixed(0.5), wait_fixed(30)]), ) - # @log_decorator("warning") + @log_decorator("warning") def get( self, series_id, From c5a631f63ed5412fcf6f605f3bf3b6b7470c3df3 Mon Sep 17 00:00:00 2001 From: Branislav Jenco Date: Thu, 16 Oct 2025 13:03:05 +0200 Subject: [PATCH 3/8] Formatting --- datareservoirio/_logging.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/datareservoirio/_logging.py b/datareservoirio/_logging.py index 681add55..28615c09 100644 --- a/datareservoirio/_logging.py +++ b/datareservoirio/_logging.py @@ -33,8 +33,9 @@ def get_exceptions_logger() -> logging.Logger: if os.getenv(ENV_VAR_ENABLE_APP_INSIGHTS) is not None: enable_app_insights = os.environ[ENV_VAR_ENABLE_APP_INSIGHTS].lower() if enable_app_insights == "true" or enable_app_insights == "1": - _ensure_azure_monitor_configured(connection_string=environment._application_insight_connectionstring, - logger_name=__name__ + "_exception_appinsight") + _ensure_azure_monitor_configured( + connection_string=environment._application_insight_connectionstring, + logger_name=__name__ + "_exception_appinsight") exceptions_logger.setLevel("WARNING") return exceptions_logger From efab6b01d81bf54dc602e2ccc9edf7e63f9360ce Mon Sep 17 00:00:00 2001 From: Branislav Jenco Date: Thu, 16 Oct 2025 13:04:21 +0200 Subject: [PATCH 4/8] Bug snuck in --- datareservoirio/_logging.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datareservoirio/_logging.py b/datareservoirio/_logging.py index 28615c09..10145ce1 100644 --- a/datareservoirio/_logging.py +++ b/datareservoirio/_logging.py @@ -35,7 +35,7 @@ def get_exceptions_logger() -> logging.Logger: if enable_app_insights == "true" or enable_app_insights == "1": _ensure_azure_monitor_configured( connection_string=environment._application_insight_connectionstring, - logger_name=__name__ + "_exception_appinsight") + logger_name=__name__ + "_exception_logger") exceptions_logger.setLevel("WARNING") return exceptions_logger From 28a8c88c4585ded3d4e210e9a06d9a91f3a0f4a0 Mon Sep 17 00:00:00 2001 From: Branislav Jenco Date: Thu, 16 Oct 2025 13:05:05 +0200 Subject: [PATCH 5/8] And a bit more formatting --- datareservoirio/_logging.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/datareservoirio/_logging.py b/datareservoirio/_logging.py index 10145ce1..fb93e76c 100644 --- a/datareservoirio/_logging.py +++ b/datareservoirio/_logging.py @@ -35,7 +35,8 @@ def get_exceptions_logger() -> logging.Logger: if enable_app_insights == "true" or enable_app_insights == "1": _ensure_azure_monitor_configured( connection_string=environment._application_insight_connectionstring, - logger_name=__name__ + "_exception_logger") + logger_name=__name__ + "_exception_logger" + ) exceptions_logger.setLevel("WARNING") return exceptions_logger From 30060e43fe89251cb018cbfbb252aeac4944c36f Mon Sep 17 00:00:00 2001 From: Branislav Jenco Date: Thu, 16 Oct 2025 13:05:42 +0200 Subject: [PATCH 6/8] Typo --- datareservoirio/_logging.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datareservoirio/_logging.py b/datareservoirio/_logging.py index fb93e76c..1b7ab7f4 100644 --- a/datareservoirio/_logging.py +++ b/datareservoirio/_logging.py @@ -35,7 +35,7 @@ def get_exceptions_logger() -> logging.Logger: if enable_app_insights == "true" or enable_app_insights == "1": _ensure_azure_monitor_configured( connection_string=environment._application_insight_connectionstring, - logger_name=__name__ + "_exception_logger" + logger_name=__name__ + "_exceptions_logger" ) exceptions_logger.setLevel("WARNING") From 26d8aa6231c7b18b488ca267ba38c08cf3ce0b02 Mon Sep 17 00:00:00 2001 From: Branislav Jenco Date: Thu, 16 Oct 2025 13:06:29 +0200 Subject: [PATCH 7/8] One more typo --- datareservoirio/_logging.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datareservoirio/_logging.py b/datareservoirio/_logging.py index 1b7ab7f4..bb7662ff 100644 --- a/datareservoirio/_logging.py +++ b/datareservoirio/_logging.py @@ -35,7 +35,7 @@ def get_exceptions_logger() -> logging.Logger: if enable_app_insights == "true" or enable_app_insights == "1": _ensure_azure_monitor_configured( connection_string=environment._application_insight_connectionstring, - logger_name=__name__ + "_exceptions_logger" + logger_name=__name__ + "_exceptions_logger", ) exceptions_logger.setLevel("WARNING") From b191f913e79b6d7de2764746dc0d33f1c4cf7db3 Mon Sep 17 00:00:00 2001 From: Branislav Jenco Date: Thu, 16 Oct 2025 13:09:11 +0200 Subject: [PATCH 8/8] isort and black --- datareservoirio/_logging.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/datareservoirio/_logging.py b/datareservoirio/_logging.py index bb7662ff..ac5fb023 100644 --- a/datareservoirio/_logging.py +++ b/datareservoirio/_logging.py @@ -1,7 +1,7 @@ import logging import os -from functools import lru_cache, wraps import threading +from functools import lru_cache, wraps from azure.monitor.opentelemetry import configure_azure_monitor @@ -10,18 +10,20 @@ from ._constants import ENV_VAR_ENABLE_APP_INSIGHTS, ENV_VAR_ENGINE_ROOM_APP_ID from .globalsettings import environment - _configure_lock = threading.Lock() _configured_loggers = {} + def _ensure_azure_monitor_configured(connection_string, logger_name): cache_key = (connection_string, logger_name) - + if cache_key not in _configured_loggers: with _configure_lock: # Double-check locking if cache_key not in _configured_loggers: - configure_azure_monitor(connection_string=connection_string, logger_name=logger_name) + configure_azure_monitor( + connection_string=connection_string, logger_name=logger_name + ) _configured_loggers[cache_key] = True