Skip to content

Commit f365eea

Browse files
Merge branch 'master' into ESS-3438-3_11to_3_13
2 parents 610f74d + f44081f commit f365eea

6 files changed

Lines changed: 50 additions & 38 deletions

File tree

datareservoirio/_logging.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
import logging
22
import os
3-
from functools import wraps
3+
from functools import cache, wraps
44

5-
from opencensus.ext.azure.log_exporter import AzureLogHandler
5+
from azure.monitor.opentelemetry import configure_azure_monitor
66

77
import datareservoirio as drio
88

99
from ._constants import ENV_VAR_ENABLE_APP_INSIGHTS, ENV_VAR_ENGINE_ROOM_APP_ID
1010
from .globalsettings import environment
1111

12-
exceptions_logger = logging.getLogger(__name__ + "_exception_logger")
13-
exceptions_logger.setLevel(logging.DEBUG)
1412

15-
if os.getenv(ENV_VAR_ENABLE_APP_INSIGHTS) is not None:
16-
enable_app_insights = os.environ[ENV_VAR_ENABLE_APP_INSIGHTS].lower()
17-
if enable_app_insights == "true" or enable_app_insights == "1":
18-
app_insight_handler = AzureLogHandler(
19-
connection_string=environment._application_insight_connectionstring
20-
)
21-
app_insight_handler.setLevel("WARNING")
22-
exceptions_logger.addHandler(app_insight_handler)
13+
@cache
14+
def get_exceptions_logger() -> logging.Logger:
15+
exceptions_logger = logging.getLogger(__name__ + "_exception_logger")
16+
exceptions_logger.setLevel(logging.DEBUG)
17+
18+
if os.getenv(ENV_VAR_ENABLE_APP_INSIGHTS) is not None:
19+
enable_app_insights = os.environ[ENV_VAR_ENABLE_APP_INSIGHTS].lower()
20+
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+
)
25+
exceptions_logger.setLevel("WARNING")
26+
27+
return exceptions_logger
2328

2429

2530
def log_decorator(log_level):
@@ -39,7 +44,7 @@ def wrapper(self, *args, **kwargs):
3944
ENV_VAR_ENGINE_ROOM_APP_ID
4045
)
4146

42-
log_function = getattr(exceptions_logger, log_level)
47+
log_function = getattr(get_exceptions_logger(), log_level)
4348
log_function(e, extra=properties)
4449
raise e
4550

datareservoirio/client.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import logging
2+
import os
23
import time
34
import warnings
45
from collections import defaultdict
56
from concurrent.futures import ThreadPoolExecutor
67
from datetime import datetime
7-
from functools import wraps
8+
from functools import cache, wraps
89
from operator import itemgetter
910
from urllib.parse import urlencode
1011
from uuid import uuid4
1112

1213
import numpy as np
1314
import pandas as pd
1415
import requests
15-
from opencensus.ext.azure.log_exporter import AzureLogHandler
16+
from azure.monitor.opentelemetry import configure_azure_monitor
1617
from tenacity import (
1718
retry,
1819
retry_if_exception_type,
@@ -22,18 +23,29 @@
2223
)
2324
from tqdm.auto import tqdm
2425

26+
from datareservoirio._constants import ENV_VAR_ENABLE_APP_INSIGHTS
27+
2528
from ._logging import log_decorator
2629
from ._utils import function_translation, period_translation
2730
from .globalsettings import environment
2831
from .storage import Storage
2932

3033
log = logging.getLogger(__name__)
3134

32-
metric = logging.getLogger(__name__ + "_metric_appinsight")
33-
metric.setLevel(logging.DEBUG)
34-
metric.addHandler(
35-
AzureLogHandler(connection_string=environment._application_insight_connectionstring)
36-
)
35+
36+
@cache
37+
def metric() -> logging.Logger:
38+
logger = logging.getLogger(__name__ + "_metric_appinsight")
39+
if os.getenv(ENV_VAR_ENABLE_APP_INSIGHTS) is not None:
40+
enable_app_insights = os.environ[ENV_VAR_ENABLE_APP_INSIGHTS].lower()
41+
if enable_app_insights == "true" or enable_app_insights == "1":
42+
logger.setLevel(logging.DEBUG)
43+
configure_azure_monitor(
44+
connection_string=environment._application_insight_connectionstring,
45+
logger_name=__name__ + "_metric_appinsight",
46+
)
47+
return logger
48+
3749

3850
# Default values to push as start/end dates. (Limited by numpy.datetime64)
3951
_END_DEFAULT = 9214646400000000000 # 2262-01-01
@@ -321,15 +333,13 @@ def wrapper(self, series_id, start=None, end=None, **kwargs):
321333
).isoformat()
322334
number_of_samples = len(result)
323335
properties = {
324-
"custom_dimensions": {
325-
"series_id": series_id,
326-
"start": start_date_as_str,
327-
"end": end_date_as_str,
328-
"elapsed": elapsed_time,
329-
"number-of-samples": number_of_samples,
330-
}
336+
"series_id": series_id,
337+
"start": start_date_as_str,
338+
"end": end_date_as_str,
339+
"elapsed": elapsed_time,
340+
"number-of-samples": number_of_samples,
331341
}
332-
metric.info("Timer", extra=properties)
342+
metric().info("Timer", extra=properties)
333343
return result
334344

335345
return wrapper

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ dependencies = [
2626
"requests",
2727
"requests-oauthlib",
2828
"importlib_resources",
29-
"opencensus-ext-azure",
3029
"tenacity<8.5",
3130
"urllib3 > 2",
3231
"tqdm",
32+
"azure-monitor-opentelemetry",
3333
]
3434

3535
[project.urls]

tests/conftest.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import logging
21
from io import BytesIO
32
from pathlib import Path
43
from unittest.mock import Mock
@@ -13,12 +12,6 @@
1312
TEST_PATH = Path(__file__).parent
1413

1514

16-
@pytest.fixture(autouse=True)
17-
def disable_logging(monkeypatch):
18-
"""Disable logging to Application Insight"""
19-
monkeypatch.setattr("datareservoirio.client.AzureLogHandler", logging.NullHandler())
20-
21-
2215
@pytest.fixture
2316
def response_cases():
2417
class ResponseCaseHandler:

tests/test__logging.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
import pytest
55

6-
from datareservoirio._logging import exceptions_logger, log_decorator
6+
from datareservoirio._logging import get_exceptions_logger, log_decorator
7+
8+
exceptions_logger = get_exceptions_logger()
79

810

911
class my_test_class:

tests/test_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
from tenacity import RetryError
1414

1515
import datareservoirio as drio
16-
from datareservoirio._logging import exceptions_logger
16+
from datareservoirio._logging import get_exceptions_logger
1717
from datareservoirio._utils import DataHandler
1818

1919
TEST_PATH = Path(__file__).parent
2020

21+
exceptions_logger = get_exceptions_logger()
22+
2123

2224
def change_logging(self, msg, *args, exc_info=True, **kwargs):
2325
if kwargs["extra"]:

0 commit comments

Comments
 (0)