Skip to content

Commit f7f4193

Browse files
Extracted azure monitor config into own function with lock
1 parent b474e39 commit f7f4193

2 files changed

Lines changed: 27 additions & 12 deletions

File tree

datareservoirio/_logging.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
import os
3-
from functools import cache, wraps
3+
from functools import lru_cache, wraps
4+
import threading
45

56
from azure.monitor.opentelemetry import configure_azure_monitor
67

@@ -10,18 +11,32 @@
1011
from .globalsettings import environment
1112

1213

13-
@cache
14+
_configure_lock = threading.Lock()
15+
_configured_loggers = {}
16+
17+
def _ensure_azure_monitor_configured(connection_string, logger_name):
18+
cache_key = (connection_string, logger_name)
19+
20+
if cache_key not in _configured_loggers:
21+
with _configure_lock:
22+
# Double-check inside lock
23+
if cache_key not in _configured_loggers:
24+
configure_azure_monitor(connection_string=connection_string, logger_name=logger_name)
25+
_configured_loggers[cache_key] = True
26+
27+
28+
@lru_cache(maxsize=1)
1429
def get_exceptions_logger() -> logging.Logger:
30+
print("bruh", flush=True)
31+
logging.log(logging.INFO, 'foobar')
1532
exceptions_logger = logging.getLogger(__name__ + "_exception_logger")
1633
exceptions_logger.setLevel(logging.DEBUG)
1734

1835
if os.getenv(ENV_VAR_ENABLE_APP_INSIGHTS) is not None:
1936
enable_app_insights = os.environ[ENV_VAR_ENABLE_APP_INSIGHTS].lower()
2037
if enable_app_insights == "true" or enable_app_insights == "1":
21-
configure_azure_monitor(
22-
connection_string=environment._application_insight_connectionstring,
23-
logger_name=__name__ + "_exceptions_logger",
24-
)
38+
_ensure_azure_monitor_configured(connection_string=environment._application_insight_connectionstring,
39+
logger_name=__name__ + "_exception_appinsight")
2540
exceptions_logger.setLevel("WARNING")
2641

2742
return exceptions_logger

datareservoirio/client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from collections import defaultdict
66
from concurrent.futures import ThreadPoolExecutor
77
from datetime import datetime
8-
from functools import cache, wraps
8+
from functools import lru_cache, wraps
99
from operator import itemgetter
1010
from urllib.parse import urlencode
1111
from uuid import uuid4
@@ -25,22 +25,22 @@
2525

2626
from datareservoirio._constants import ENV_VAR_ENABLE_APP_INSIGHTS
2727

28-
from ._logging import log_decorator
28+
from ._logging import _ensure_azure_monitor_configured, log_decorator
2929
from ._utils import function_translation, period_translation
3030
from .globalsettings import environment
3131
from .storage import Storage
3232

3333
log = logging.getLogger(__name__)
3434

3535

36-
@cache
36+
@lru_cache(maxsize=1)
3737
def metric() -> logging.Logger:
3838
logger = logging.getLogger(__name__ + "_metric_appinsight")
3939
if os.getenv(ENV_VAR_ENABLE_APP_INSIGHTS) is not None:
4040
enable_app_insights = os.environ[ENV_VAR_ENABLE_APP_INSIGHTS].lower()
4141
if enable_app_insights == "true" or enable_app_insights == "1":
4242
logger.setLevel(logging.DEBUG)
43-
configure_azure_monitor(
43+
_ensure_azure_monitor_configured(
4444
connection_string=environment._application_insight_connectionstring,
4545
logger_name=__name__ + "_metric_appinsight",
4646
)
@@ -344,7 +344,7 @@ def wrapper(self, series_id, start=None, end=None, **kwargs):
344344

345345
return wrapper
346346

347-
@log_decorator("exception")
347+
# @log_decorator("exception")
348348
@_timer
349349
@retry(
350350
stop=stop_after_attempt(
@@ -361,7 +361,7 @@ def wrapper(self, series_id, start=None, end=None, **kwargs):
361361
),
362362
wait=wait_chain(*[wait_fixed(0.1), wait_fixed(0.5), wait_fixed(30)]),
363363
)
364-
@log_decorator("warning")
364+
# @log_decorator("warning")
365365
def get(
366366
self,
367367
series_id,

0 commit comments

Comments
 (0)