Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor statsbeat constants #34742

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

### Breaking Changes

- Rename Statbeat environments variables to use `APPLICATIONINSIGHTS_*`
([#34742](https://github.com/Azure/azure-sdk-for-python/pull/34742))

### Bugs Fixed

### Other Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,28 @@
_REQ_THROTTLE_NAME,
]
)
_APPLICATIONINSIGHTS_STATS_CONNECTION_STRING_ENV_NAME = "APPLICATIONINSIGHTS_STATS_CONNECTION_STRING"
_APPLICATIONINSIGHTS_STATS_SHORT_EXPORT_INTERVAL_ENV_NAME = "APPLICATIONINSIGHTS_STATS_SHORT_EXPORT_INTERVAL"
_APPLICATIONINSIGHTS_STATS_LONG_EXPORT_INTERVAL_ENV_NAME = "APPLICATIONINSIGHTS_STATS_LONG_EXPORT_INTERVAL"
# pylint: disable=line-too-long
_DEFAULT_NON_EU_STATS_CONNECTION_STRING = "InstrumentationKey=c4a29126-a7cb-47e5-b348-11414998b11e;IngestionEndpoint=https://westus-0.in.applicationinsights.azure.com/"
_DEFAULT_EU_STATS_CONNECTION_STRING = "InstrumentationKey=7dc56bab-3c0c-4e9f-9ebb-d1acadee8d0f;IngestionEndpoint=https://westeurope-5.in.applicationinsights.azure.com/"
_DEFAULT_STATS_SHORT_EXPORT_INTERVAL = 900 # 15 minutes
_DEFAULT_STATS_LONG_EXPORT_INTERVAL = 86400 # 24 hours
_EU_ENDPOINTS = [
"westeurope",
"northeurope",
"francecentral",
"francesouth",
"germanywestcentral",
"norwayeast",
"norwaywest",
"swedencentral",
"switzerlandnorth",
"switzerlandwest",
"uksouth",
"ukwest",
]

# Instrumentations

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

PROCESS = psutil.Process()

def enable_live_metrics(**kwargs: Any) -> None:
def enable_live_metrics(**kwargs: Any) -> None: # pylint: disable=C4758
"""Live metrics entry point.

:keyword str connection_string: The connection string used for your Application Insights resource.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,23 @@
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
from opentelemetry.sdk.resources import Resource

from azure.monitor.opentelemetry.exporter._constants import (
_APPLICATIONINSIGHTS_STATS_CONNECTION_STRING_ENV_NAME,
_APPLICATIONINSIGHTS_STATS_LONG_EXPORT_INTERVAL_ENV_NAME,
_APPLICATIONINSIGHTS_STATS_SHORT_EXPORT_INTERVAL_ENV_NAME,
_DEFAULT_NON_EU_STATS_CONNECTION_STRING,
_DEFAULT_EU_STATS_CONNECTION_STRING,
_DEFAULT_STATS_SHORT_EXPORT_INTERVAL,
_DEFAULT_STATS_LONG_EXPORT_INTERVAL,
_EU_ENDPOINTS,
)
from azure.monitor.opentelemetry.exporter.statsbeat._exporter import _StatsBeatExporter
from azure.monitor.opentelemetry.exporter.statsbeat._statsbeat_metrics import _StatsbeatMetrics
from azure.monitor.opentelemetry.exporter.statsbeat._state import (
_STATSBEAT_STATE,
_STATSBEAT_STATE_LOCK,
)

# pylint: disable=line-too-long
_DEFAULT_NON_EU_STATS_CONNECTION_STRING = "InstrumentationKey=c4a29126-a7cb-47e5-b348-11414998b11e;IngestionEndpoint=https://westus-0.in.applicationinsights.azure.com/"
_DEFAULT_EU_STATS_CONNECTION_STRING = "InstrumentationKey=7dc56bab-3c0c-4e9f-9ebb-d1acadee8d0f;IngestionEndpoint=https://westeurope-5.in.applicationinsights.azure.com/"
_DEFAULT_STATS_SHORT_EXPORT_INTERVAL = 900 # 15 minutes
_DEFAULT_STATS_LONG_EXPORT_INTERVAL = 86400 # 24 hours
_EU_ENDPOINTS = [
"westeurope",
"northeurope",
"francecentral",
"francesouth",
"germanywestcentral",
"norwayeast",
"norwaywest",
"swedencentral",
"switzerlandnorth",
"switzerlandwest",
"uksouth",
"ukwest",
]

_STATSBEAT_METRICS = None
_STATSBEAT_LOCK = threading.Lock()
Expand Down Expand Up @@ -92,7 +83,7 @@ def shutdown_statsbeat_metrics() -> None:


def _get_stats_connection_string(endpoint: str) -> str:
cs_env = os.environ.get("APPLICATION_INSIGHTS_STATS_CONNECTION_STRING")
cs_env = os.environ.get(_APPLICATIONINSIGHTS_STATS_CONNECTION_STRING_ENV_NAME)
if cs_env:
return cs_env
for endpoint_location in _EU_ENDPOINTS:
Expand All @@ -104,7 +95,7 @@ def _get_stats_connection_string(endpoint: str) -> str:

# seconds
def _get_stats_short_export_interval() -> int:
ei_env = os.environ.get("APPLICATION_INSIGHTS_STATS_SHORT_EXPORT_INTERVAL")
ei_env = os.environ.get(_APPLICATIONINSIGHTS_STATS_SHORT_EXPORT_INTERVAL_ENV_NAME)
if ei_env:
try:
return int(ei_env)
Expand All @@ -115,7 +106,7 @@ def _get_stats_short_export_interval() -> int:

# seconds
def _get_stats_long_export_interval() -> int:
ei_env = os.environ.get("APPLICATION_INSIGHTS_STATS_LONG_EXPORT_INTERVAL")
ei_env = os.environ.get(_APPLICATIONINSIGHTS_STATS_LONG_EXPORT_INTERVAL_ENV_NAME)
if ei_env:
try:
return int(ei_env)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
_REQUESTS_MAP,
_STATSBEAT_STATE,
)
from azure.monitor.opentelemetry.exporter.statsbeat._statsbeat import (
from azure.monitor.opentelemetry.exporter._constants import (
_APPLICATIONINSIGHTS_STATS_CONNECTION_STRING_ENV_NAME,
_DEFAULT_STATS_LONG_EXPORT_INTERVAL,
_DEFAULT_STATS_SHORT_EXPORT_INTERVAL,
_APPLICATIONINSIGHTS_STATS_SHORT_EXPORT_INTERVAL_ENV_NAME,
_APPLICATIONINSIGHTS_STATS_LONG_EXPORT_INTERVAL_ENV_NAME,
)
from azure.monitor.opentelemetry.exporter.statsbeat._statsbeat_metrics import (
_shorten_host,
Expand Down Expand Up @@ -110,7 +113,7 @@ def test_collect_statsbeat_metrics_non_eu(
self.assertIsNone(_statsbeat._STATSBEAT_METRICS)
with mock.patch.dict(
os.environ, {
"APPLICATION_INSIGHTS_STATS_CONNECTION_STRING": "",
_APPLICATIONINSIGHTS_STATS_CONNECTION_STRING_ENV_NAME: "",
}):
_statsbeat.collect_statsbeat_metrics(exporter)
self.assertIsNotNone(_statsbeat._STATSBEAT_METRICS)
Expand Down Expand Up @@ -141,7 +144,7 @@ def test_collect_statsbeat_metrics_eu(
self.assertIsNone(_statsbeat._STATSBEAT_METRICS)
with mock.patch.dict(
os.environ, {
"APPLICATION_INSIGHTS_STATS_CONNECTION_STRING": "",
_APPLICATIONINSIGHTS_STATS_CONNECTION_STRING_ENV_NAME: "",
}):
_statsbeat.collect_statsbeat_metrics(exporter)
self.assertIsNotNone(_statsbeat._STATSBEAT_METRICS)
Expand All @@ -162,8 +165,8 @@ def test_collect_statsbeat_metrics_eu(
@mock.patch.dict(
"os.environ",
{
"APPLICATION_INSIGHTS_STATS_SHORT_EXPORT_INTERVAL": "",
"APPLICATION_INSIGHTS_STATS_LONG_EXPORT_INTERVAL": "",
_APPLICATIONINSIGHTS_STATS_SHORT_EXPORT_INTERVAL_ENV_NAME: "",
_APPLICATIONINSIGHTS_STATS_LONG_EXPORT_INTERVAL_ENV_NAME: "",
},
)
def test_collect_statsbeat_metrics_aad(
Expand Down Expand Up @@ -195,8 +198,8 @@ def test_collect_statsbeat_metrics_aad(
@mock.patch.dict(
"os.environ",
{
"APPLICATION_INSIGHTS_STATS_SHORT_EXPORT_INTERVAL": "",
"APPLICATION_INSIGHTS_STATS_LONG_EXPORT_INTERVAL": "",
_APPLICATIONINSIGHTS_STATS_SHORT_EXPORT_INTERVAL_ENV_NAME: "",
_APPLICATIONINSIGHTS_STATS_LONG_EXPORT_INTERVAL_ENV_NAME: "",
},
)
def test_collect_statsbeat_metrics_no_aad(
Expand Down Expand Up @@ -227,8 +230,8 @@ def test_collect_statsbeat_metrics_no_aad(
@mock.patch.dict(
"os.environ",
{
"APPLICATION_INSIGHTS_STATS_SHORT_EXPORT_INTERVAL": "",
"APPLICATION_INSIGHTS_STATS_LONG_EXPORT_INTERVAL": "",
_APPLICATIONINSIGHTS_STATS_SHORT_EXPORT_INTERVAL_ENV_NAME: "",
_APPLICATIONINSIGHTS_STATS_LONG_EXPORT_INTERVAL_ENV_NAME: "",
},
)
def test_collect_statsbeat_metrics_distro_version(
Expand Down