|
1 | 1 | import logging |
| 2 | +import os |
2 | 3 | import time |
3 | 4 | import warnings |
4 | 5 | from collections import defaultdict |
5 | 6 | from concurrent.futures import ThreadPoolExecutor |
6 | 7 | from datetime import datetime |
7 | | -from functools import wraps, cache |
| 8 | +from functools import cache, wraps |
8 | 9 | from operator import itemgetter |
9 | 10 | from urllib.parse import urlencode |
10 | 11 | from uuid import uuid4 |
11 | 12 |
|
12 | 13 | import numpy as np |
13 | 14 | import pandas as pd |
14 | 15 | import requests |
15 | | -from opencensus.ext.azure.log_exporter import AzureLogHandler |
| 16 | +from azure.monitor.opentelemetry import configure_azure_monitor |
16 | 17 | from tenacity import ( |
17 | 18 | retry, |
18 | 19 | retry_if_exception_type, |
|
22 | 23 | ) |
23 | 24 | from tqdm.auto import tqdm |
24 | 25 |
|
| 26 | +from datareservoirio._constants import ENV_VAR_ENABLE_APP_INSIGHTS |
| 27 | + |
25 | 28 | from ._logging import log_decorator |
26 | 29 | from ._utils import function_translation, period_translation |
27 | 30 | from .globalsettings import environment |
28 | 31 | from .storage import Storage |
29 | 32 |
|
30 | 33 | log = logging.getLogger(__name__) |
31 | 34 |
|
| 35 | + |
32 | 36 | @cache |
33 | 37 | def metric() -> logging.Logger: |
34 | 38 | 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 | + ) |
39 | 47 | return logger |
40 | 48 |
|
| 49 | + |
41 | 50 | # Default values to push as start/end dates. (Limited by numpy.datetime64) |
42 | 51 | _END_DEFAULT = 9214646400000000000 # 2262-01-01 |
43 | 52 | _START_DEFAULT = -9214560000000000000 # 1678-01-01 |
@@ -324,13 +333,11 @@ def wrapper(self, series_id, start=None, end=None, **kwargs): |
324 | 333 | ).isoformat() |
325 | 334 | number_of_samples = len(result) |
326 | 335 | 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, |
334 | 341 | } |
335 | 342 | metric().info("Timer", extra=properties) |
336 | 343 | return result |
|
0 commit comments