Skip to content

Commit 6dbbc92

Browse files
authored
Refactor constants (#35066)
* Set up attach function for branching. Add tests * Added todos * Moving non-constants into utils. Renaming * Remove todos * Changelog * Add exporter changelog and lint * rerun tests
1 parent a0d0eb8 commit 6dbbc92

File tree

15 files changed

+188
-119
lines changed

15 files changed

+188
-119
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
- Updated FastAPI sample
2727
([#34738](https://github.com/Azure/azure-sdk-for-python/pull/34738))
28+
- Set up branching logic for attach function
29+
([#35066](https://github.com/Azure/azure-sdk-for-python/pull/35066))
2830

2931
## 1.0.0b23 (2024-02-28)
3032

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ def _is_on_functions():
5050
return environ.get(_FUNCTIONS_WORKER_RUNTIME) is not None
5151

5252
def _is_attach_enabled():
53-
return isdir("/agents/python/")
53+
if _is_on_app_service():
54+
return isdir("/agents/python/")
55+
return False
5456

5557

5658
# AKS

sdk/monitor/azure-monitor-opentelemetry-exporter/tests/test_utils.py

Lines changed: 75 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -104,110 +104,161 @@ def test_create_telemetry_item(self, mock_ns_to_iso_str):
104104
)
105105
self.assertEqual(result, expected)
106106

107-
# Unknown
107+
# Unknown SDK Version Prefix
108108

109-
@patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=False)
109+
@patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=False)
110110
@patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="")
111111
def test_get_sdk_version_prefix(self, mock_system, mock_getenv):
112112
result = _utils._get_sdk_version_prefix()
113113
self.assertEqual(result, "uum_")
114114

115-
@patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=False)
115+
@patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=False)
116116
@patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Linux")
117117
def test_get_sdk_version_prefix_linux(self, mock_system, mock_getenv):
118118
result = _utils._get_sdk_version_prefix()
119119
self.assertEqual(result, "ulm_")
120120

121-
@patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=False)
121+
@patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=False)
122122
@patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Windows")
123123
def test_get_sdk_version_prefix_windows(self, mock_system, mock_getenv):
124124
result = _utils._get_sdk_version_prefix()
125125
self.assertEqual(result, "uwm_")
126126

127-
# App Service
127+
@patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=True)
128+
@patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="")
129+
def test_get_sdk_version_prefix_attach(self, mock_system, mock_getenv):
130+
result = _utils._get_sdk_version_prefix()
131+
self.assertEqual(result, "uui_")
132+
133+
@patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=True)
134+
@patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Linux")
135+
def test_get_sdk_version_prefix_attach_linux(self, mock_system, mock_getenv):
136+
result = _utils._get_sdk_version_prefix()
137+
self.assertEqual(result, "uli_")
138+
139+
@patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=True)
140+
@patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Windows")
141+
def test_get_sdk_version_prefix_attach_windows(self, mock_system, mock_getenv):
142+
result = _utils._get_sdk_version_prefix()
143+
self.assertEqual(result, "uwi_")
144+
145+
# App Service SDK Version Prefix
128146

129-
@patch("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME})
147+
@patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME}, clear=True)
130148
@patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=False)
131149
@patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="")
132150
def test_get_sdk_version_prefix_app_service(self, mock_system, mock_getenv):
133151
result = _utils._get_sdk_version_prefix()
134152
self.assertEqual(result, "aum_")
135153

136-
@patch("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME})
154+
@patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME}, clear=True)
137155
@patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=False)
138156
@patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Linux")
139157
def test_get_sdk_version_prefix_app_service_linux(self, mock_system, mock_getenv):
140158
result = _utils._get_sdk_version_prefix()
141159
self.assertEqual(result, "alm_")
142160

143-
@patch("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME})
161+
@patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME}, clear=True)
144162
@patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=False)
145163
@patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Windows")
146164
def test_get_sdk_version_prefix_app_service_windows(self, mock_system, mock_getenv):
147165
result = _utils._get_sdk_version_prefix()
148166
self.assertEqual(result, "awm_")
149167

150-
@patch("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME})
168+
@patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME}, clear=True)
151169
@patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=True)
152170
@patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="")
153171
def test_get_sdk_version_prefix_app_service_attach(self, mock_system, mock_getenv):
154172
result = _utils._get_sdk_version_prefix()
155173
self.assertEqual(result, "aui_")
156174

157-
@patch("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME})
175+
@patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME}, clear=True)
158176
@patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=True)
159177
@patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Linux")
160178
def test_get_sdk_version_prefix_app_service_linux_attach(self, mock_system, mock_getenv):
161179
result = _utils._get_sdk_version_prefix()
162180
self.assertEqual(result, "ali_")
163181

164-
@patch("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME})
182+
@patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME}, clear=True)
165183
@patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=True)
166184
@patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Windows")
167185
def test_get_sdk_version_prefix_app_service_windows_attach(self, mock_system, mock_getenv):
168186
result = _utils._get_sdk_version_prefix()
169187
self.assertEqual(result, "awi_")
170188

171-
# Function
189+
# Function SDK Version Prefix
172190

173-
@patch("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME})
174-
@patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=False)
191+
@patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME}, clear=True)
192+
@patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=False)
175193
@patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="")
176194
def test_get_sdk_version_prefix_function(self, mock_system, mock_getenv):
177195
result = _utils._get_sdk_version_prefix()
178196
self.assertEqual(result, "fum_")
179197

180-
@patch("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME})
181-
@patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=False)
198+
@patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME}, clear=True)
199+
@patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=False)
182200
@patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Linux")
183201
def test_get_sdk_version_prefix_function_linux(self, mock_system, mock_getenv):
184202
result = _utils._get_sdk_version_prefix()
185203
self.assertEqual(result, "flm_")
186204

187-
@patch("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME})
188-
@patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=False)
205+
@patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME}, clear=True)
206+
@patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=False)
189207
@patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Windows")
190208
def test_get_sdk_version_prefix_function_windows(self, mock_system, mock_getenv):
191209
result = _utils._get_sdk_version_prefix()
192210
self.assertEqual(result, "fwm_")
193211

194-
@patch("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME})
195-
@patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=True)
212+
@patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME}, clear=True)
213+
@patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=True)
196214
@patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="")
197215
def test_get_sdk_version_prefix_function_attach(self, mock_system, mock_getenv):
198216
result = _utils._get_sdk_version_prefix()
199217
self.assertEqual(result, "fui_")
200218

201-
@patch("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME})
202-
@patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=True)
219+
@patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME}, clear=True)
220+
@patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=True)
203221
@patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Linux")
204222
def test_get_sdk_version_prefix_function_linux_attach(self, mock_system, mock_getenv):
205223
result = _utils._get_sdk_version_prefix()
206224
self.assertEqual(result, "fli_")
207225

208-
@patch("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME})
209-
@patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=True)
226+
@patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME}, clear=True)
227+
@patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=True)
210228
@patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Windows")
211229
def test_get_sdk_version_prefix_function_windows_attach(self, mock_system, mock_getenv):
212230
result = _utils._get_sdk_version_prefix()
213231
self.assertEqual(result, "fwi_")
232+
233+
# Attach
234+
235+
@patch(
236+
"azure.monitor.opentelemetry.exporter._utils.isdir",
237+
return_value=True,
238+
)
239+
@patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME})
240+
def test_attach_enabled(self, mock_isdir):
241+
self.assertEqual(
242+
_utils._is_attach_enabled(), True
243+
)
244+
245+
@patch(
246+
"azure.monitor.opentelemetry.exporter._utils.isdir",
247+
return_value=False,
248+
)
249+
@patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME})
250+
def test_attach_app_service_disabled(self, mock_isdir):
251+
self.assertEqual(
252+
_utils._is_attach_enabled(), False
253+
)
254+
255+
@patch(
256+
"azure.monitor.opentelemetry.exporter._utils.isdir",
257+
return_value=True,
258+
)
259+
@patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {}, clear=True)
260+
def test_attach_off_app_service_with_agent(self, mock_isdir):
261+
# This is not an expected scenario and just tests the default
262+
self.assertEqual(
263+
_utils._is_attach_enabled(), False
264+
)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
- Updated FastAPI sample
1717
([#34738](https://github.com/Azure/azure-sdk-for-python/pull/34738))
18+
- Refactored constants and utils
19+
([#35066](https://github.com/Azure/azure-sdk-for-python/pull/35066))
1820

1921
## 1.3.0 (2024-02-29)
2022

sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_autoinstrumentation/distro.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from azure.monitor.opentelemetry._diagnostics.status_logger import (
3636
AzureStatusLogger,
3737
)
38-
from azure.monitor.opentelemetry._util.configurations import (
38+
from azure.monitor.opentelemetry._utils.configurations import (
3939
_get_otel_disabled_instrumentations,
4040
)
4141

sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
_DISTRO_DETECTS_ATTACH,
5050
AzureDiagnosticLogging,
5151
)
52-
from azure.monitor.opentelemetry._util.configurations import (
52+
from azure.monitor.opentelemetry._utils.configurations import (
5353
_get_configurations,
5454
_is_instrumentation_enabled,
5555
)

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

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@
44
# license information.
55
# --------------------------------------------------------------------------
66

7-
import logging
8-
import platform
9-
from os import environ
10-
from pathlib import Path
11-
12-
from azure.monitor.opentelemetry.exporter._connection_string_parser import ( # pylint: disable=import-error,no-name-in-module
13-
ConnectionStringParser,
14-
)
157
from azure.monitor.opentelemetry.exporter._constants import ( # pylint: disable=import-error,no-name-in-module
168
_AZURE_MONITOR_DISTRO_VERSION_ARG,
179
)
@@ -35,52 +27,10 @@
3527

3628
_LOG_PATH_LINUX = "/var/log/applicationinsights"
3729
_LOG_PATH_WINDOWS = "\\LogFiles\\ApplicationInsights"
38-
_IS_ON_APP_SERVICE = "WEBSITE_SITE_NAME" in environ
39-
# TODO: Add environment variable to enabled diagnostics off of App Service
40-
_IS_DIAGNOSTICS_ENABLED = _IS_ON_APP_SERVICE
41-
_CUSTOMER_IKEY_ENV_VAR = None
4230
_PREVIEW_ENTRY_POINT_WARNING = "Autoinstrumentation for the Azure Monitor OpenTelemetry Distro is in preview."
43-
logger = logging.getLogger(__name__)
44-
45-
46-
# pylint: disable=global-statement
47-
def _get_customer_ikey_from_env_var():
48-
global _CUSTOMER_IKEY_ENV_VAR
49-
if not _CUSTOMER_IKEY_ENV_VAR:
50-
_CUSTOMER_IKEY_ENV_VAR = "unknown"
51-
try:
52-
_CUSTOMER_IKEY_ENV_VAR = (
53-
ConnectionStringParser().instrumentation_key
54-
)
55-
except ValueError as e:
56-
logger.error("Failed to parse Instrumentation Key: %s", e)
57-
return _CUSTOMER_IKEY_ENV_VAR
58-
5931

60-
def _get_log_path(status_log_path=False):
61-
system = platform.system()
62-
if system == "Linux":
63-
return _LOG_PATH_LINUX
64-
if system == "Windows":
65-
log_path = str(Path.home()) + _LOG_PATH_WINDOWS
66-
if status_log_path:
67-
return log_path + "\\status"
68-
return log_path
69-
return None
70-
71-
72-
def _env_var_or_default(var_name, default_val=""):
73-
try:
74-
return environ[var_name]
75-
except KeyError:
76-
return default_val
77-
78-
79-
_EXTENSION_VERSION = _env_var_or_default(
80-
"ApplicationInsightsAgent_EXTENSION_VERSION", "disabled"
81-
)
8232

83-
# Instrumentations
33+
# --------------------Instrumentations------------------------------
8434

8535
# Opt-out
8636
_AZURE_SDK_INSTRUMENTATION_NAME = "azure_sdk"

sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_diagnostics/diagnostic_logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from os import makedirs
1010
from os.path import exists, join
1111

12-
from azure.monitor.opentelemetry._constants import (
12+
from azure.monitor.opentelemetry._utils import (
1313
_EXTENSION_VERSION,
1414
_IS_DIAGNOSTICS_ENABLED,
1515
_env_var_or_default,

sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_diagnostics/status_logger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from os.path import exists, join
1010
from platform import node
1111

12-
from azure.monitor.opentelemetry._constants import (
12+
from azure.monitor.opentelemetry._utils import (
1313
_EXTENSION_VERSION,
1414
_IS_DIAGNOSTICS_ENABLED,
1515
_get_customer_ikey_from_env_var,

sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_util/__init__.py

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# -------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
7+
import logging
8+
import platform
9+
from os import environ
10+
from pathlib import Path
11+
12+
from azure.monitor.opentelemetry.exporter._connection_string_parser import ( # pylint: disable=import-error,no-name-in-module
13+
ConnectionStringParser,
14+
)
15+
from azure.monitor.opentelemetry.exporter._utils import _is_on_app_service # pylint: disable=import-error,no-name-in-module
16+
from azure.monitor.opentelemetry._constants import (
17+
_LOG_PATH_LINUX,
18+
_LOG_PATH_WINDOWS,
19+
)
20+
21+
22+
logger = logging.getLogger(__name__)
23+
24+
25+
# --------------------Diagnostic/status logging------------------------------
26+
27+
# TODO: Add environment variable to enabled diagnostics off of App Service
28+
_IS_DIAGNOSTICS_ENABLED = _is_on_app_service()
29+
_CUSTOMER_IKEY_ENV_VAR = None
30+
31+
32+
# pylint: disable=global-statement
33+
def _get_customer_ikey_from_env_var():
34+
global _CUSTOMER_IKEY_ENV_VAR
35+
if not _CUSTOMER_IKEY_ENV_VAR:
36+
_CUSTOMER_IKEY_ENV_VAR = "unknown"
37+
try:
38+
_CUSTOMER_IKEY_ENV_VAR = (
39+
ConnectionStringParser().instrumentation_key
40+
)
41+
except ValueError as e:
42+
logger.error("Failed to parse Instrumentation Key: %s", e)
43+
return _CUSTOMER_IKEY_ENV_VAR
44+
45+
46+
def _get_log_path(status_log_path=False):
47+
system = platform.system()
48+
if system == "Linux":
49+
return _LOG_PATH_LINUX
50+
if system == "Windows":
51+
log_path = str(Path.home()) + _LOG_PATH_WINDOWS
52+
if status_log_path:
53+
return log_path + "\\status"
54+
return log_path
55+
return None
56+
57+
58+
def _env_var_or_default(var_name, default_val=""):
59+
try:
60+
return environ[var_name]
61+
except KeyError:
62+
return default_val
63+
64+
65+
_EXTENSION_VERSION = _env_var_or_default(
66+
"ApplicationInsightsAgent_EXTENSION_VERSION", "disabled"
67+
)

0 commit comments

Comments
 (0)