Skip to content

Commit f44081f

Browse files
ESS-3444 Moving to Open Telemetry from Open Census (#139)
* Moving to Open Telemetry from Open Census * Only enable logger when environment variable is set
1 parent 2be5523 commit f44081f

6 files changed

Lines changed: 57 additions & 51 deletions

File tree

datareservoirio/_logging.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import logging
22
import os
3-
from functools import wraps, cache
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+
1213
@cache
1314
def get_exceptions_logger() -> logging.Logger:
1415
exceptions_logger = logging.getLogger(__name__ + "_exception_logger")
@@ -17,13 +18,15 @@ def get_exceptions_logger() -> logging.Logger:
1718
if os.getenv(ENV_VAR_ENABLE_APP_INSIGHTS) is not None:
1819
enable_app_insights = os.environ[ENV_VAR_ENABLE_APP_INSIGHTS].lower()
1920
if enable_app_insights == "true" or enable_app_insights == "1":
20-
app_insight_handler = AzureLogHandler(
21-
connection_string=environment._application_insight_connectionstring
21+
configure_azure_monitor(
22+
connection_string=environment._application_insight_connectionstring,
23+
logger_name=__name__ + "_exceptions_logger",
2224
)
23-
app_insight_handler.setLevel("WARNING")
24-
exceptions_logger.addHandler(app_insight_handler)
25+
exceptions_logger.setLevel("WARNING")
26+
2527
return exceptions_logger
2628

29+
2730
def log_decorator(log_level):
2831
def decorator(func):
2932
@wraps(func)

datareservoirio/client.py

Lines changed: 20 additions & 13 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, cache
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,22 +23,30 @@
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

35+
3236
@cache
3337
def metric() -> logging.Logger:
3438
logger = logging.getLogger(__name__ + "_metric_appinsight")
35-
logger.setLevel(logging.DEBUG)
36-
logger.addHandler(
37-
AzureLogHandler(connection_string=environment._application_insight_connectionstring)
38-
)
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+
)
3947
return logger
4048

49+
4150
# Default values to push as start/end dates. (Limited by numpy.datetime64)
4251
_END_DEFAULT = 9214646400000000000 # 2262-01-01
4352
_START_DEFAULT = -9214560000000000000 # 1678-01-01
@@ -324,13 +333,11 @@ def wrapper(self, series_id, start=None, end=None, **kwargs):
324333
).isoformat()
325334
number_of_samples = len(result)
326335
properties = {
327-
"custom_dimensions": {
328-
"series_id": series_id,
329-
"start": start_date_as_str,
330-
"end": end_date_as_str,
331-
"elapsed": elapsed_time,
332-
"number-of-samples": number_of_samples,
333-
}
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,
334341
}
335342
metric().info("Timer", extra=properties)
336343
return result

pyproject.toml

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,32 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "datareservoirio"
7-
authors = [
8-
{ name="4Subsea", email="support@4subsea.com" }
9-
]
7+
authors = [{ name = "4Subsea", email = "support@4subsea.com" }]
108
dynamic = ["version"]
119
description = "DataReservoir.io Python API"
1210
readme = "README.rst"
13-
license = { file="LICENSE" }
11+
license = { file = "LICENSE" }
1412
requires-python = ">3.10"
1513
classifiers = [
16-
"Development Status :: 5 - Production/Stable",
17-
"License :: OSI Approved :: MIT License",
18-
"Operating System :: OS Independent",
19-
"Programming Language :: Python :: 3.10",
20-
"Programming Language :: Python :: 3.11",
21-
"Programming Language :: Python :: 3.12",
14+
"Development Status :: 5 - Production/Stable",
15+
"License :: OSI Approved :: MIT License",
16+
"Operating System :: OS Independent",
17+
"Programming Language :: Python :: 3.10",
18+
"Programming Language :: Python :: 3.11",
19+
"Programming Language :: Python :: 3.12",
2220
]
2321
dependencies = [
24-
"numpy",
25-
"oauthlib",
26-
"pandas",
27-
"pyarrow",
28-
"requests",
29-
"requests-oauthlib",
30-
"importlib_resources",
31-
"opencensus-ext-azure",
32-
"tenacity<8.5",
33-
"urllib3 > 2",
34-
"tqdm"
22+
"numpy",
23+
"oauthlib",
24+
"pandas",
25+
"pyarrow",
26+
"requests",
27+
"requests-oauthlib",
28+
"importlib_resources",
29+
"tenacity<8.5",
30+
"urllib3 > 2",
31+
"tqdm",
32+
"azure-monitor-opentelemetry",
3533
]
3634

3735
[project.urls]
@@ -45,7 +43,7 @@ include = ["datareservoirio*"]
4543
namespaces = false
4644

4745
[tool.setuptools.dynamic]
48-
version = {attr = "datareservoirio.__version__"}
46+
version = { attr = "datareservoirio.__version__" }
4947

5048
[tool.pytest.ini_options]
5149
pythonpath = [".", "src"]
@@ -74,4 +72,5 @@ deps =
7472
sphinx==5.3.0
7573
pydata-sphinx-theme==0.11.0
7674
myst_parser<2.0
77-
"""
75+
"""
76+

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)