Skip to content

Commit 4d1c254

Browse files
committed
Constants tests pass for conn str revamp
1 parent 28355d2 commit 4d1c254

File tree

7 files changed

+145
-77
lines changed

7 files changed

+145
-77
lines changed

azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from logging import NOTSET, getLogger
77

88
from azure.monitor.opentelemetry.distro.util import get_configurations
9+
from azure.monitor.opentelemetry.distro._constants import ConnectionStringConstants
910
from azure.monitor.opentelemetry.exporter import (
1011
ApplicationInsightsSampler,
1112
AzureMonitorLogExporter,
@@ -33,6 +34,12 @@ def configure_azure_monitor(**kwargs):
3334
"""
3435

3536
configurations = get_configurations(**kwargs)
37+
conn_str = configurations.get("connection_string", "")
38+
if conn_str is None:
39+
# TODO: JEREVOSS: We could levae this for the exporter to determine.
40+
configurations["connection_string"] = ConnectionStringConstants.get_conn_str()
41+
else:
42+
ConnectionStringConstants.set_conn_str(conn_str)
3643
service_name = configurations.get("service_name", "")
3744
service_namespace = configurations.get("service_namespace", "")
3845
service_instance_id = configurations.get("service_instance_id", "")

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

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
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+
17
import logging
28
import platform
39
from os import environ
@@ -7,12 +13,6 @@
713
ConnectionStringParser,
814
)
915

10-
# -------------------------------------------------------------------------
11-
# Copyright (c) Microsoft Corporation. All rights reserved.
12-
# Licensed under the MIT License. See License in the project root for
13-
# license information.
14-
# --------------------------------------------------------------------------
15-
1616
_LOG_PATH_LINUX = "/var/log/applicationinsights"
1717
_LOG_PATH_WINDOWS = "\\LogFiles\\ApplicationInsights"
1818
_IS_ON_APP_SERVICE = "WEBSITE_SITE_NAME" in environ
@@ -22,12 +22,6 @@
2222
# _EXPORTER_DIAGNOSTICS_ENABLED_ENV_VAR = (
2323
# "AZURE_MONITOR_OPENTELEMETRY_DISTRO_ENABLE_EXPORTER_DIAGNOSTICS"
2424
# )
25-
logger = logging.getLogger(__name__)
26-
_CUSTOMER_IKEY = "unknown"
27-
try:
28-
_CUSTOMER_IKEY = ConnectionStringParser().instrumentation_key
29-
except ValueError as e:
30-
logger.error("Failed to parse Instrumentation Key: %s" % e)
3125

3226

3327
def _get_log_path(status_log_path=False):
@@ -64,3 +58,27 @@ def _env_var_or_default(var_name, default_val=""):
6458
)
6559
# TODO: Enabled when duplciate logging issue is solved
6660
# _EXPORTER_DIAGNOSTICS_ENABLED = _is_exporter_diagnostics_enabled()
61+
62+
63+
class ConnectionStringConstants:
64+
_conn_str_parser = None
65+
66+
67+
def set_conn_str_from_env_var():
68+
ConnectionStringConstants._conn_str_parser = ConnectionStringParser()
69+
70+
71+
def set_conn_str(conn_str):
72+
ConnectionStringConstants._conn_str_parser = ConnectionStringParser(conn_str)
73+
74+
75+
def get_conn_str():
76+
if ConnectionStringConstants._conn_str_parser is None:
77+
return None
78+
return ConnectionStringConstants._conn_str_parser._conn_str
79+
80+
81+
def get_customer_ikey():
82+
if ConnectionStringConstants._conn_str_parser is None:
83+
return None
84+
return ConnectionStringConstants._conn_str_parser.instrumentation_key

azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/_diagnostics/_diagnostic_logging.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from os.path import exists, join
1111

1212
from azure.monitor.opentelemetry.distro._constants import (
13-
_CUSTOMER_IKEY,
13+
ConnectionStringConstants,
1414
_EXTENSION_VERSION,
1515
_IS_DIAGNOSTICS_ENABLED,
1616
_env_var_or_default,
@@ -39,6 +39,14 @@ def _initialize():
3939
with AzureDiagnosticLogging._lock:
4040
if not AzureDiagnosticLogging._initialized:
4141
if _IS_DIAGNOSTICS_ENABLED and _DIAGNOSTIC_LOG_PATH:
42+
customer_ikey = ConnectionStringConstants.get_customer_ikey()
43+
if customer_ikey is None:
44+
try:
45+
ConnectionStringConstants.set_conn_str_from_env_var()
46+
customer_ikey = ConnectionStringConstants.get_customer_ikey()
47+
except ValueError as e:
48+
_logger.error("Failed to parse Instrumentation Key: %s" % e)
49+
customer_ikey = "unknown"
4250
format = (
4351
"{"
4452
+ '"time":"%(asctime)s.%(msecs)03d", '
@@ -48,7 +56,7 @@ def _initialize():
4856
+ '"properties":{'
4957
+ '"operation":"Startup", '
5058
+ f'"sitename":"{_SITE_NAME}", '
51-
+ f'"ikey":"{_CUSTOMER_IKEY}", '
59+
+ f'"ikey":"{customer_ikey}", '
5260
+ f'"extensionVersion":"{_EXTENSION_VERSION}", '
5361
+ f'"sdkVersion":"{VERSION}", '
5462
+ f'"subscriptionId":"{_SUBSCRIPTION_ID}", '

azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/_diagnostics/_status_logger.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,36 @@
1010
from platform import node
1111

1212
from azure.monitor.opentelemetry.distro._constants import (
13-
_CUSTOMER_IKEY,
13+
ConnectionStringConstants,
1414
_EXTENSION_VERSION,
1515
_IS_DIAGNOSTICS_ENABLED,
1616
_get_log_path,
1717
)
1818
from azure.monitor.opentelemetry.distro._version import VERSION
19+
import logging
1920

2021
_MACHINE_NAME = node()
2122
_STATUS_LOG_PATH = _get_log_path(status_log_path=True)
23+
_logger = logging.getLogger(__name__)
2224

2325

2426
class AzureStatusLogger:
2527
def _get_status_json(agent_initialized_successfully, pid, reason=None):
28+
customer_ikey = ConnectionStringConstants.get_customer_ikey()
29+
if customer_ikey is None:
30+
try:
31+
ConnectionStringConstants.set_conn_str_from_env_var()
32+
customer_ikey = ConnectionStringConstants.get_customer_ikey()
33+
except ValueError as e:
34+
_logger.error("Failed to parse Instrumentation Key: %s" % e)
35+
customer_ikey = "unknown"
2636
status_json = {
2737
"AgentInitializedSuccessfully": agent_initialized_successfully,
2838
"AppType": "python",
2939
"MachineName": _MACHINE_NAME,
3040
"PID": pid,
3141
"SdkVersion": VERSION,
32-
"Ikey": _CUSTOMER_IKEY,
42+
"Ikey": customer_ikey,
3343
"ExtensionVersion": _EXTENSION_VERSION,
3444
}
3545
if reason:

azure-monitor-opentelemetry-distro/tests/diagnostics/test_diagnostic_logging.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ def set_up(
9696
TEST_DIAGNOSTIC_LOGGER_FILE_NAME,
9797
).start()
9898
patch(
99-
"azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging._CUSTOMER_IKEY",
100-
TEST_CUSTOMER_IKEY,
99+
"azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging.get_customer_ikey",
100+
return_value=TEST_CUSTOMER_IKEY,
101101
).start()
102102
patch(
103103
"azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging._EXTENSION_VERSION",

azure-monitor-opentelemetry-distro/tests/diagnostics/test_status_logger.py

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@ def setUp(self) -> None:
7272
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._STATUS_LOG_PATH",
7373
TEST_LOGGER_PATH,
7474
)
75-
@patch(
76-
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._CUSTOMER_IKEY",
77-
TEST_CUSTOMER_IKEY,
78-
)
7975
@patch(
8076
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._EXTENSION_VERSION",
8177
TEST_EXTENSION_VERSION,
@@ -88,15 +84,19 @@ def setUp(self) -> None:
8884
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._IS_DIAGNOSTICS_ENABLED",
8985
True,
9086
)
91-
@patch(
92-
"azure.monitor.opentelemetry.distro._diagnostics._status_logger.getpid",
93-
return_value=TEST_PID,
94-
)
9587
@patch(
9688
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._MACHINE_NAME",
9789
TEST_MACHINE_NAME,
9890
)
99-
def test_log_status_success(self, mock_getpid):
91+
@patch(
92+
"azure.monitor.opentelemetry.distro._diagnostics._status_logger.get_customer_ikey",
93+
return_value=TEST_CUSTOMER_IKEY,
94+
)
95+
@patch(
96+
"azure.monitor.opentelemetry.distro._diagnostics._status_logger.getpid",
97+
return_value=TEST_PID,
98+
)
99+
def test_log_status_success(self, mock_get_customer_ikey, mock_getpid):
100100
AzureStatusLogger.log_status(False, MESSAGE1)
101101
AzureStatusLogger.log_status(True, MESSAGE2)
102102
check_file_for_messages(True, MESSAGE2)
@@ -105,10 +105,6 @@ def test_log_status_success(self, mock_getpid):
105105
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._STATUS_LOG_PATH",
106106
TEST_LOGGER_PATH,
107107
)
108-
@patch(
109-
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._CUSTOMER_IKEY",
110-
TEST_CUSTOMER_IKEY,
111-
)
112108
@patch(
113109
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._EXTENSION_VERSION",
114110
TEST_EXTENSION_VERSION,
@@ -121,15 +117,19 @@ def test_log_status_success(self, mock_getpid):
121117
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._IS_DIAGNOSTICS_ENABLED",
122118
True,
123119
)
124-
@patch(
125-
"azure.monitor.opentelemetry.distro._diagnostics._status_logger.getpid",
126-
return_value=TEST_PID,
127-
)
128120
@patch(
129121
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._MACHINE_NAME",
130122
TEST_MACHINE_NAME,
131123
)
132-
def test_log_status_failed_initialization(self, mock_getpid):
124+
@patch(
125+
"azure.monitor.opentelemetry.distro._diagnostics._status_logger.get_customer_ikey",
126+
return_value=TEST_CUSTOMER_IKEY,
127+
)
128+
@patch(
129+
"azure.monitor.opentelemetry.distro._diagnostics._status_logger.getpid",
130+
return_value=TEST_PID,
131+
)
132+
def test_log_status_failed_initialization(self, mock_get_customer_ikey, mock_getpid):
133133
AzureStatusLogger.log_status(True, MESSAGE1)
134134
AzureStatusLogger.log_status(False, MESSAGE2)
135135
check_file_for_messages(False, MESSAGE2)
@@ -138,10 +138,6 @@ def test_log_status_failed_initialization(self, mock_getpid):
138138
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._STATUS_LOG_PATH",
139139
TEST_LOGGER_PATH,
140140
)
141-
@patch(
142-
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._CUSTOMER_IKEY",
143-
TEST_CUSTOMER_IKEY,
144-
)
145141
@patch(
146142
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._EXTENSION_VERSION",
147143
TEST_EXTENSION_VERSION,
@@ -154,15 +150,19 @@ def test_log_status_failed_initialization(self, mock_getpid):
154150
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._IS_DIAGNOSTICS_ENABLED",
155151
True,
156152
)
157-
@patch(
158-
"azure.monitor.opentelemetry.distro._diagnostics._status_logger.getpid",
159-
return_value=TEST_PID,
160-
)
161153
@patch(
162154
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._MACHINE_NAME",
163155
TEST_MACHINE_NAME,
164156
)
165-
def test_log_status_no_reason(self, mock_getpid):
157+
@patch(
158+
"azure.monitor.opentelemetry.distro._diagnostics._status_logger.get_customer_ikey",
159+
return_value=TEST_CUSTOMER_IKEY,
160+
)
161+
@patch(
162+
"azure.monitor.opentelemetry.distro._diagnostics._status_logger.getpid",
163+
return_value=TEST_PID,
164+
)
165+
def test_log_status_no_reason(self, mock_get_customer_ikey, mock_getpid):
166166
AzureStatusLogger.log_status(False, MESSAGE1)
167167
AzureStatusLogger.log_status(True)
168168
check_file_for_messages(True)
@@ -171,10 +171,6 @@ def test_log_status_no_reason(self, mock_getpid):
171171
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._STATUS_LOG_PATH",
172172
TEST_LOGGER_PATH,
173173
)
174-
@patch(
175-
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._CUSTOMER_IKEY",
176-
TEST_CUSTOMER_IKEY,
177-
)
178174
@patch(
179175
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._EXTENSION_VERSION",
180176
TEST_EXTENSION_VERSION,
@@ -187,15 +183,19 @@ def test_log_status_no_reason(self, mock_getpid):
187183
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._IS_DIAGNOSTICS_ENABLED",
188184
False,
189185
)
190-
@patch(
191-
"azure.monitor.opentelemetry.distro._diagnostics._status_logger.getpid",
192-
return_value=TEST_PID,
193-
)
194186
@patch(
195187
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._MACHINE_NAME",
196188
TEST_MACHINE_NAME,
197189
)
198-
def test_disabled_log_status_success(self, mock_getpid):
190+
@patch(
191+
"azure.monitor.opentelemetry.distro._diagnostics._status_logger.get_customer_ikey",
192+
return_value=TEST_CUSTOMER_IKEY,
193+
)
194+
@patch(
195+
"azure.monitor.opentelemetry.distro._diagnostics._status_logger.getpid",
196+
return_value=TEST_PID,
197+
)
198+
def test_disabled_log_status_success(self, mock_get_customer_ikey, mock_getpid):
199199
AzureStatusLogger.log_status(False, MESSAGE1)
200200
AzureStatusLogger.log_status(True, MESSAGE2)
201201
check_file_is_empty()
@@ -204,10 +204,6 @@ def test_disabled_log_status_success(self, mock_getpid):
204204
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._STATUS_LOG_PATH",
205205
TEST_LOGGER_PATH,
206206
)
207-
@patch(
208-
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._CUSTOMER_IKEY",
209-
TEST_CUSTOMER_IKEY,
210-
)
211207
@patch(
212208
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._EXTENSION_VERSION",
213209
TEST_EXTENSION_VERSION,
@@ -220,15 +216,19 @@ def test_disabled_log_status_success(self, mock_getpid):
220216
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._IS_DIAGNOSTICS_ENABLED",
221217
False,
222218
)
223-
@patch(
224-
"azure.monitor.opentelemetry.distro._diagnostics._status_logger.getpid",
225-
return_value=TEST_PID,
226-
)
227219
@patch(
228220
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._MACHINE_NAME",
229221
TEST_MACHINE_NAME,
230222
)
231-
def test_disabled_log_status_failed_initialization(self, mock_getpid):
223+
@patch(
224+
"azure.monitor.opentelemetry.distro._diagnostics._status_logger.get_customer_ikey",
225+
return_value=TEST_CUSTOMER_IKEY,
226+
)
227+
@patch(
228+
"azure.monitor.opentelemetry.distro._diagnostics._status_logger.getpid",
229+
return_value=TEST_PID,
230+
)
231+
def test_disabled_log_status_failed_initialization(self, mock_get_customer_ikey, mock_getpid):
232232
AzureStatusLogger.log_status(True, MESSAGE1)
233233
AzureStatusLogger.log_status(False, MESSAGE2)
234234
check_file_is_empty()
@@ -237,10 +237,6 @@ def test_disabled_log_status_failed_initialization(self, mock_getpid):
237237
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._STATUS_LOG_PATH",
238238
TEST_LOGGER_PATH,
239239
)
240-
@patch(
241-
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._CUSTOMER_IKEY",
242-
TEST_CUSTOMER_IKEY,
243-
)
244240
@patch(
245241
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._EXTENSION_VERSION",
246242
TEST_EXTENSION_VERSION,
@@ -253,15 +249,19 @@ def test_disabled_log_status_failed_initialization(self, mock_getpid):
253249
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._IS_DIAGNOSTICS_ENABLED",
254250
False,
255251
)
256-
@patch(
257-
"azure.monitor.opentelemetry.distro._diagnostics._status_logger.getpid",
258-
return_value=TEST_PID,
259-
)
260252
@patch(
261253
"azure.monitor.opentelemetry.distro._diagnostics._status_logger._MACHINE_NAME",
262254
TEST_MACHINE_NAME,
263255
)
264-
def test_disabled_log_status_no_reason(self, mock_getpid):
256+
@patch(
257+
"azure.monitor.opentelemetry.distro._diagnostics._status_logger.get_customer_ikey",
258+
return_value=TEST_CUSTOMER_IKEY,
259+
)
260+
@patch(
261+
"azure.monitor.opentelemetry.distro._diagnostics._status_logger.getpid",
262+
return_value=TEST_PID,
263+
)
264+
def test_disabled_log_status_no_reason(self, mock_get_customer_ikey, mock_getpid):
265265
AzureStatusLogger.log_status(False, MESSAGE1)
266266
AzureStatusLogger.log_status(True)
267267
check_file_is_empty()

0 commit comments

Comments
 (0)