Skip to content

Commit 2d44f8d

Browse files
authored
Refactor statsbeat constants (#34742)
1 parent 97b34d7 commit 2d44f8d

File tree

5 files changed

+51
-32
lines changed

5 files changed

+51
-32
lines changed

sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
### Breaking Changes
1515

16+
- Rename Statbeat environments variables to use `APPLICATIONINSIGHTS_*`
17+
([#34742](https://github.com/Azure/azure-sdk-for-python/pull/34742))
18+
1619
### Bugs Fixed
1720

1821
### Other Changes

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/_constants.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,28 @@
8282
_REQ_THROTTLE_NAME,
8383
]
8484
)
85+
_APPLICATIONINSIGHTS_STATS_CONNECTION_STRING_ENV_NAME = "APPLICATIONINSIGHTS_STATS_CONNECTION_STRING"
86+
_APPLICATIONINSIGHTS_STATS_SHORT_EXPORT_INTERVAL_ENV_NAME = "APPLICATIONINSIGHTS_STATS_SHORT_EXPORT_INTERVAL"
87+
_APPLICATIONINSIGHTS_STATS_LONG_EXPORT_INTERVAL_ENV_NAME = "APPLICATIONINSIGHTS_STATS_LONG_EXPORT_INTERVAL"
88+
# pylint: disable=line-too-long
89+
_DEFAULT_NON_EU_STATS_CONNECTION_STRING = "InstrumentationKey=c4a29126-a7cb-47e5-b348-11414998b11e;IngestionEndpoint=https://westus-0.in.applicationinsights.azure.com/"
90+
_DEFAULT_EU_STATS_CONNECTION_STRING = "InstrumentationKey=7dc56bab-3c0c-4e9f-9ebb-d1acadee8d0f;IngestionEndpoint=https://westeurope-5.in.applicationinsights.azure.com/"
91+
_DEFAULT_STATS_SHORT_EXPORT_INTERVAL = 900 # 15 minutes
92+
_DEFAULT_STATS_LONG_EXPORT_INTERVAL = 86400 # 24 hours
93+
_EU_ENDPOINTS = [
94+
"westeurope",
95+
"northeurope",
96+
"francecentral",
97+
"francesouth",
98+
"germanywestcentral",
99+
"norwayeast",
100+
"norwaywest",
101+
"swedencentral",
102+
"switzerlandnorth",
103+
"switzerlandwest",
104+
"uksouth",
105+
"ukwest",
106+
]
85107

86108
# Instrumentations
87109

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/_quickpulse/_live_metrics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555

5656
PROCESS = psutil.Process()
5757

58-
def enable_live_metrics(**kwargs: Any) -> None:
58+
def enable_live_metrics(**kwargs: Any) -> None: # pylint: disable=C4758
5959
"""Live metrics entry point.
6060
6161
:keyword str connection_string: The connection string used for your Application Insights resource.

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/statsbeat/_statsbeat.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,23 @@
77
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
88
from opentelemetry.sdk.resources import Resource
99

10+
from azure.monitor.opentelemetry.exporter._constants import (
11+
_APPLICATIONINSIGHTS_STATS_CONNECTION_STRING_ENV_NAME,
12+
_APPLICATIONINSIGHTS_STATS_LONG_EXPORT_INTERVAL_ENV_NAME,
13+
_APPLICATIONINSIGHTS_STATS_SHORT_EXPORT_INTERVAL_ENV_NAME,
14+
_DEFAULT_NON_EU_STATS_CONNECTION_STRING,
15+
_DEFAULT_EU_STATS_CONNECTION_STRING,
16+
_DEFAULT_STATS_SHORT_EXPORT_INTERVAL,
17+
_DEFAULT_STATS_LONG_EXPORT_INTERVAL,
18+
_EU_ENDPOINTS,
19+
)
1020
from azure.monitor.opentelemetry.exporter.statsbeat._exporter import _StatsBeatExporter
1121
from azure.monitor.opentelemetry.exporter.statsbeat._statsbeat_metrics import _StatsbeatMetrics
1222
from azure.monitor.opentelemetry.exporter.statsbeat._state import (
1323
_STATSBEAT_STATE,
1424
_STATSBEAT_STATE_LOCK,
1525
)
1626

17-
# pylint: disable=line-too-long
18-
_DEFAULT_NON_EU_STATS_CONNECTION_STRING = "InstrumentationKey=c4a29126-a7cb-47e5-b348-11414998b11e;IngestionEndpoint=https://westus-0.in.applicationinsights.azure.com/"
19-
_DEFAULT_EU_STATS_CONNECTION_STRING = "InstrumentationKey=7dc56bab-3c0c-4e9f-9ebb-d1acadee8d0f;IngestionEndpoint=https://westeurope-5.in.applicationinsights.azure.com/"
20-
_DEFAULT_STATS_SHORT_EXPORT_INTERVAL = 900 # 15 minutes
21-
_DEFAULT_STATS_LONG_EXPORT_INTERVAL = 86400 # 24 hours
22-
_EU_ENDPOINTS = [
23-
"westeurope",
24-
"northeurope",
25-
"francecentral",
26-
"francesouth",
27-
"germanywestcentral",
28-
"norwayeast",
29-
"norwaywest",
30-
"swedencentral",
31-
"switzerlandnorth",
32-
"switzerlandwest",
33-
"uksouth",
34-
"ukwest",
35-
]
3627

3728
_STATSBEAT_METRICS = None
3829
_STATSBEAT_LOCK = threading.Lock()
@@ -92,7 +83,7 @@ def shutdown_statsbeat_metrics() -> None:
9283

9384

9485
def _get_stats_connection_string(endpoint: str) -> str:
95-
cs_env = os.environ.get("APPLICATION_INSIGHTS_STATS_CONNECTION_STRING")
86+
cs_env = os.environ.get(_APPLICATIONINSIGHTS_STATS_CONNECTION_STRING_ENV_NAME)
9687
if cs_env:
9788
return cs_env
9889
for endpoint_location in _EU_ENDPOINTS:
@@ -104,7 +95,7 @@ def _get_stats_connection_string(endpoint: str) -> str:
10495

10596
# seconds
10697
def _get_stats_short_export_interval() -> int:
107-
ei_env = os.environ.get("APPLICATION_INSIGHTS_STATS_SHORT_EXPORT_INTERVAL")
98+
ei_env = os.environ.get(_APPLICATIONINSIGHTS_STATS_SHORT_EXPORT_INTERVAL_ENV_NAME)
10899
if ei_env:
109100
try:
110101
return int(ei_env)
@@ -115,7 +106,7 @@ def _get_stats_short_export_interval() -> int:
115106

116107
# seconds
117108
def _get_stats_long_export_interval() -> int:
118-
ei_env = os.environ.get("APPLICATION_INSIGHTS_STATS_LONG_EXPORT_INTERVAL")
109+
ei_env = os.environ.get(_APPLICATIONINSIGHTS_STATS_LONG_EXPORT_INTERVAL_ENV_NAME)
119110
if ei_env:
120111
try:
121112
return int(ei_env)

sdk/monitor/azure-monitor-opentelemetry-exporter/tests/statsbeat/test_statsbeat.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@
2828
_REQUESTS_MAP,
2929
_STATSBEAT_STATE,
3030
)
31-
from azure.monitor.opentelemetry.exporter.statsbeat._statsbeat import (
31+
from azure.monitor.opentelemetry.exporter._constants import (
32+
_APPLICATIONINSIGHTS_STATS_CONNECTION_STRING_ENV_NAME,
3233
_DEFAULT_STATS_LONG_EXPORT_INTERVAL,
3334
_DEFAULT_STATS_SHORT_EXPORT_INTERVAL,
35+
_APPLICATIONINSIGHTS_STATS_SHORT_EXPORT_INTERVAL_ENV_NAME,
36+
_APPLICATIONINSIGHTS_STATS_LONG_EXPORT_INTERVAL_ENV_NAME,
3437
)
3538
from azure.monitor.opentelemetry.exporter.statsbeat._statsbeat_metrics import (
3639
_shorten_host,
@@ -110,7 +113,7 @@ def test_collect_statsbeat_metrics_non_eu(
110113
self.assertIsNone(_statsbeat._STATSBEAT_METRICS)
111114
with mock.patch.dict(
112115
os.environ, {
113-
"APPLICATION_INSIGHTS_STATS_CONNECTION_STRING": "",
116+
_APPLICATIONINSIGHTS_STATS_CONNECTION_STRING_ENV_NAME: "",
114117
}):
115118
_statsbeat.collect_statsbeat_metrics(exporter)
116119
self.assertIsNotNone(_statsbeat._STATSBEAT_METRICS)
@@ -141,7 +144,7 @@ def test_collect_statsbeat_metrics_eu(
141144
self.assertIsNone(_statsbeat._STATSBEAT_METRICS)
142145
with mock.patch.dict(
143146
os.environ, {
144-
"APPLICATION_INSIGHTS_STATS_CONNECTION_STRING": "",
147+
_APPLICATIONINSIGHTS_STATS_CONNECTION_STRING_ENV_NAME: "",
145148
}):
146149
_statsbeat.collect_statsbeat_metrics(exporter)
147150
self.assertIsNotNone(_statsbeat._STATSBEAT_METRICS)
@@ -162,8 +165,8 @@ def test_collect_statsbeat_metrics_eu(
162165
@mock.patch.dict(
163166
"os.environ",
164167
{
165-
"APPLICATION_INSIGHTS_STATS_SHORT_EXPORT_INTERVAL": "",
166-
"APPLICATION_INSIGHTS_STATS_LONG_EXPORT_INTERVAL": "",
168+
_APPLICATIONINSIGHTS_STATS_SHORT_EXPORT_INTERVAL_ENV_NAME: "",
169+
_APPLICATIONINSIGHTS_STATS_LONG_EXPORT_INTERVAL_ENV_NAME: "",
167170
},
168171
)
169172
def test_collect_statsbeat_metrics_aad(
@@ -195,8 +198,8 @@ def test_collect_statsbeat_metrics_aad(
195198
@mock.patch.dict(
196199
"os.environ",
197200
{
198-
"APPLICATION_INSIGHTS_STATS_SHORT_EXPORT_INTERVAL": "",
199-
"APPLICATION_INSIGHTS_STATS_LONG_EXPORT_INTERVAL": "",
201+
_APPLICATIONINSIGHTS_STATS_SHORT_EXPORT_INTERVAL_ENV_NAME: "",
202+
_APPLICATIONINSIGHTS_STATS_LONG_EXPORT_INTERVAL_ENV_NAME: "",
200203
},
201204
)
202205
def test_collect_statsbeat_metrics_no_aad(
@@ -227,8 +230,8 @@ def test_collect_statsbeat_metrics_no_aad(
227230
@mock.patch.dict(
228231
"os.environ",
229232
{
230-
"APPLICATION_INSIGHTS_STATS_SHORT_EXPORT_INTERVAL": "",
231-
"APPLICATION_INSIGHTS_STATS_LONG_EXPORT_INTERVAL": "",
233+
_APPLICATIONINSIGHTS_STATS_SHORT_EXPORT_INTERVAL_ENV_NAME: "",
234+
_APPLICATIONINSIGHTS_STATS_LONG_EXPORT_INTERVAL_ENV_NAME: "",
232235
},
233236
)
234237
def test_collect_statsbeat_metrics_distro_version(

0 commit comments

Comments
 (0)