Skip to content

Commit 8d2a46a

Browse files
authored
Add diagnostics for AKS (#40988)
* Enable diagnostic logging for AKS. Needs tests * stash * Tests pass * changelog * spelling * test_diagnostics_aks_attach not passing * Default return * test_diagnostics_aks_attach failing. Adding asserts * change tests * add more test variety * mock exporter * add mock args * lint * clarify comment * cover functions appsvc overlap * typo
1 parent dcac933 commit 8d2a46a

File tree

7 files changed

+97
-16
lines changed

7 files changed

+97
-16
lines changed

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

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

55
### Features Added
66

7+
- Enabled diagnostics for AKS Attach
8+
([#40988](https://github.com/Azure/azure-sdk-for-python/pull/40988))
9+
710
### Breaking Changes
811

912
### Bugs Fixed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
from azure.monitor.opentelemetry._utils import (
1313
_EXTENSION_VERSION,
14-
_IS_DIAGNOSTICS_ENABLED,
1514
_env_var_or_default,
1615
_get_customer_ikey_from_env_var,
1716
_get_log_path,
17+
_is_diagnostics_enabled,
1818
)
1919
from azure.monitor.opentelemetry._version import VERSION
2020

@@ -51,7 +51,7 @@ class AzureDiagnosticLogging:
5151
def _initialize(cls):
5252
with AzureDiagnosticLogging._lock:
5353
if not AzureDiagnosticLogging._initialized:
54-
if _IS_DIAGNOSTICS_ENABLED and _DIAGNOSTIC_LOG_PATH:
54+
if _is_diagnostics_enabled() and _DIAGNOSTIC_LOG_PATH:
5555
log_format = (
5656
"{"
5757
+ '"time":"%(asctime)s.%(msecs)03d", '

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
from azure.monitor.opentelemetry._utils import (
1313
_EXTENSION_VERSION,
14-
_IS_DIAGNOSTICS_ENABLED,
1514
_get_customer_ikey_from_env_var,
1615
_get_log_path,
16+
_is_diagnostics_enabled,
1717
)
1818
from azure.monitor.opentelemetry._version import VERSION
1919

@@ -45,7 +45,7 @@ def _get_status_json(cls, agent_initialized_successfully, pid, reason=None, sdk_
4545

4646
@classmethod
4747
def log_status(cls, agent_initialized_successfully, reason=None, sdk_present=None):
48-
if _IS_DIAGNOSTICS_ENABLED and _STATUS_LOG_PATH:
48+
if _is_diagnostics_enabled() and _STATUS_LOG_PATH:
4949
pid = getpid()
5050
status_json = AzureStatusLogger._get_status_json(agent_initialized_successfully, pid, reason, sdk_present)
5151
if not exists(_STATUS_LOG_PATH):

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
)
1515
from azure.monitor.opentelemetry.exporter._utils import ( # pylint: disable=import-error,no-name-in-module
1616
_is_on_app_service,
17+
_is_on_aks,
18+
_is_on_functions,
19+
_is_attach_enabled,
1720
)
1821
from azure.monitor.opentelemetry._constants import (
1922
_LOG_PATH_LINUX,
@@ -26,8 +29,6 @@
2629

2730
# --------------------Diagnostic/status logging------------------------------
2831

29-
# TODO: Add environment variable to enabled diagnostics off of App Service
30-
_IS_DIAGNOSTICS_ENABLED = _is_on_app_service()
3132
_CUSTOMER_IKEY_ENV_VAR = None
3233

3334

@@ -43,6 +44,15 @@ def _get_customer_ikey_from_env_var():
4344
return _CUSTOMER_IKEY_ENV_VAR
4445

4546

47+
# TODO: Add environment variable to enable/disable diagnostics
48+
def _is_diagnostics_enabled():
49+
if _is_on_functions():
50+
return False
51+
if _is_on_app_service() or _is_on_aks():
52+
return _is_attach_enabled()
53+
return False
54+
55+
4656
def _get_log_path(status_log_path=False):
4757
system = platform.system()
4858
if system == "Linux":

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ def set_up(
9292
TEST_VERSION,
9393
).start()
9494
patch(
95-
"azure.monitor.opentelemetry._diagnostics.diagnostic_logging._IS_DIAGNOSTICS_ENABLED",
96-
is_diagnostics_enabled,
95+
"azure.monitor.opentelemetry._diagnostics.diagnostic_logging._is_diagnostics_enabled",
96+
return_value=is_diagnostics_enabled,
9797
).start()
9898

9999

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def set_up(file_path, is_diagnostics_enabled=True):
5050
TEST_VERSION,
5151
).start()
5252
patch(
53-
"azure.monitor.opentelemetry._diagnostics.status_logger._IS_DIAGNOSTICS_ENABLED",
54-
is_diagnostics_enabled,
53+
"azure.monitor.opentelemetry._diagnostics.status_logger._is_diagnostics_enabled",
54+
return_value=is_diagnostics_enabled,
5555
).start()
5656
patch(
5757
"azure.monitor.opentelemetry._diagnostics.status_logger.getpid",

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

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from unittest.mock import patch
1111

1212
from azure.monitor.opentelemetry import _utils
13+
from azure.monitor.opentelemetry.exporter._constants import _AKS_ARM_NAMESPACE_ID
1314

1415
TEST_VALUE = "TEST_VALUE"
1516
TEST_IKEY = "1234abcd-ab12-34cd-ab12-a23456abcdef"
@@ -45,15 +46,82 @@ def test_ikey_defaults(self):
4546
reload(_utils)
4647
self.assertEqual(_utils._get_customer_ikey_from_env_var(), "unknown")
4748

48-
@patch.dict("os.environ", {"WEBSITE_SITE_NAME": TEST_VALUE})
49-
def test_diagnostics_enabled(self):
49+
@patch(
50+
"azure.monitor.opentelemetry.exporter._utils._is_attach_enabled",
51+
return_value=True,
52+
)
53+
@patch(
54+
"azure.monitor.opentelemetry.exporter._utils._is_on_app_service",
55+
return_value=True,
56+
)
57+
@patch(
58+
"azure.monitor.opentelemetry.exporter._utils._is_on_aks",
59+
return_value=False,
60+
)
61+
@patch(
62+
"azure.monitor.opentelemetry.exporter._utils._is_on_functions",
63+
return_value=False,
64+
)
65+
def test_diagnostics_app_service_attach(
66+
self, attach_mock, app_service_mock, aks_mock, functions_mock
67+
):
68+
reload(_utils)
69+
self.assertTrue(_utils._is_diagnostics_enabled())
70+
71+
@patch(
72+
"azure.monitor.opentelemetry.exporter._utils._is_attach_enabled",
73+
return_value=True,
74+
)
75+
@patch(
76+
"azure.monitor.opentelemetry.exporter._utils._is_on_app_service",
77+
return_value=False,
78+
)
79+
@patch(
80+
"azure.monitor.opentelemetry.exporter._utils._is_on_aks",
81+
return_value=True,
82+
)
83+
@patch(
84+
"azure.monitor.opentelemetry.exporter._utils._is_on_functions",
85+
return_value=False,
86+
)
87+
def test_diagnostics_aks_attach(
88+
self, attach_mock, app_service_mock, aks_mock, functions_mock
89+
):
5090
reload(_utils)
51-
self.assertTrue(_utils._IS_DIAGNOSTICS_ENABLED)
91+
self.assertTrue(_utils._is_diagnostics_enabled())
5292

53-
def test_diagnostics_disabled(self):
54-
clear_env_var("WEBSITE_SITE_NAME")
93+
@patch(
94+
"azure.monitor.opentelemetry.exporter._utils._is_attach_enabled",
95+
return_value=True,
96+
)
97+
# Functions have the WEBSITE_SITE_NAME environment variable.
98+
# This causes them to appear as App Service resources
99+
@patch(
100+
"azure.monitor.opentelemetry.exporter._utils._is_on_app_service",
101+
return_value=True,
102+
)
103+
@patch(
104+
"azure.monitor.opentelemetry.exporter._utils._is_on_aks",
105+
return_value=False,
106+
)
107+
@patch(
108+
"azure.monitor.opentelemetry.exporter._utils._is_on_functions",
109+
return_value=True,
110+
)
111+
def test_diagnostics_functions_attach(
112+
self, attach_mock, app_service_mock, aks_mock, functions_mock
113+
):
114+
reload(_utils)
115+
# Functions attach does not currently enable diagnostics
116+
self.assertFalse(_utils._is_diagnostics_enabled())
117+
118+
@patch(
119+
"azure.monitor.opentelemetry.exporter._utils._is_attach_enabled",
120+
return_value=False,
121+
)
122+
def test_diagnostics_disabled(self, attach_mock):
55123
reload(_utils)
56-
self.assertFalse(_utils._IS_DIAGNOSTICS_ENABLED)
124+
self.assertFalse(_utils._is_diagnostics_enabled())
57125

58126
@patch(
59127
"azure.monitor.opentelemetry._utils.platform.system",

0 commit comments

Comments
 (0)