Skip to content

Commit ed8f2c8

Browse files
authored
Merge pull request #217 from jeremydvoss/status-logger
Added status logger
2 parents 62cb576 + e86f6c6 commit ed8f2c8

8 files changed

Lines changed: 338 additions & 12 deletions

File tree

CHANGELOG.md

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

33
## Unreleased
44

5+
- Added Status Logger
6+
([#217](https://github.com/microsoft/ApplicationInsights-Python/pull/217))
57
- Added Diagnostic Logging for App Service
68
([#212](https://github.com/microsoft/ApplicationInsights-Python/pull/212))
79
- Updated main and distro READMEs

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

Whitespace-only changes.

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
if _SUBSCRIPTION_ID_ENV_VAR
2828
else None
2929
)
30-
_opentelemetry_logger = logging.getLogger(
31-
_OPENTELEMETRY_DIAGNOSTIC_LOGGER_NAME
32-
)
3330
_logger = logging.getLogger(__name__)
3431
_DIAGNOSTIC_LOG_PATH = _get_log_path()
3532

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
from json import dumps
8+
from os import getpid, makedirs
9+
from os.path import exists, join
10+
from platform import node
11+
12+
from azure.monitor.opentelemetry.distro._constants import (
13+
_CUSTOMER_IKEY,
14+
_EXTENSION_VERSION,
15+
_IS_DIAGNOSTICS_ENABLED,
16+
_get_log_path,
17+
)
18+
from azure.monitor.opentelemetry.distro._version import VERSION
19+
20+
_MACHINE_NAME = node()
21+
_STATUS_LOG_PATH = _get_log_path(status_log_path=True)
22+
23+
24+
class AzureStatusLogger:
25+
def _get_status_json(agent_initialized_successfully, pid, reason=None):
26+
status_json = {
27+
"AgentInitializedSuccessfully": agent_initialized_successfully,
28+
"AppType": "python",
29+
"MachineName": _MACHINE_NAME,
30+
"PID": pid,
31+
"SdkVersion": VERSION,
32+
"Ikey": _CUSTOMER_IKEY,
33+
"ExtensionVersion": _EXTENSION_VERSION,
34+
}
35+
if reason:
36+
status_json["Reason"] = reason
37+
return status_json
38+
39+
def log_status(agent_initialized_successfully, reason=None):
40+
if _IS_DIAGNOSTICS_ENABLED and _STATUS_LOG_PATH:
41+
pid = getpid()
42+
status_json = AzureStatusLogger._get_status_json(
43+
agent_initialized_successfully, pid, reason
44+
)
45+
if not exists(_STATUS_LOG_PATH):
46+
makedirs(_STATUS_LOG_PATH)
47+
# Change to be hostname and pid
48+
status_logger_file_name = f"status_{_MACHINE_NAME}_{pid}.json"
49+
with open(
50+
join(_STATUS_LOG_PATH, status_logger_file_name), "w"
51+
) as f:
52+
f.seek(0)
53+
f.write(dumps(status_json))
54+
f.truncate()

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

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

77

8-
from azure.monitor.opentelemetry.distro._diagnostic_logging import (
8+
from azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging import (
99
AzureDiagnosticLogging,
1010
)
1111
from opentelemetry.sdk._configuration import _OTelSDKConfigurator

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
import logging
77
from os import environ
88

9-
from azure.monitor.opentelemetry.distro._diagnostic_logging import (
9+
from azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging import (
1010
AzureDiagnosticLogging,
1111
)
12+
from azure.monitor.opentelemetry.distro._diagnostics._status_logger import (
13+
AzureStatusLogger,
14+
)
1215
from opentelemetry.environment_variables import (
1316
OTEL_METRICS_EXPORTER,
1417
OTEL_TRACES_EXPORTER,
@@ -34,6 +37,7 @@ def _configure(self, **kwargs) -> None:
3437

3538
def _configure_auto_instrumentation() -> None:
3639
try:
40+
AzureStatusLogger.log_status(False, "Distro being configured.")
3741
AzureDiagnosticLogging.enable(_logger)
3842
AzureDiagnosticLogging.enable(_opentelemetry_logger)
3943
# TODO: Enabled when duplicate logging issue is solved
@@ -51,10 +55,12 @@ def _configure_auto_instrumentation() -> None:
5155
environ.setdefault(
5256
OTEL_TRACES_EXPORTER, "azure_monitor_opentelemetry_exporter"
5357
)
58+
AzureStatusLogger.log_status(True)
5459
_logger.info(
5560
"Azure Monitor OpenTelemetry Distro configured successfully."
5661
)
5762
except Exception as exc:
63+
AzureStatusLogger.log_status(False, reason=exc)
5864
_logger.error(
5965
"Azure Monitor OpenTelemetry Distro failed during "
6066
+ f"configuration: {exc}"

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from unittest import TestCase
1313
from unittest.mock import patch
1414

15-
import azure.monitor.opentelemetry.distro._diagnostic_logging as diagnostic_logger
15+
import azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging as diagnostic_logger
1616

1717
TEST_LOGGER_PATH = str(Path.home())
1818
TEST_DIAGNOSTIC_LOGGER_FILE_NAME = "test-applicationinsights-extension.log"
@@ -88,27 +88,27 @@ def set_up(
8888
reload(diagnostic_logger)
8989
assert not diagnostic_logger.AzureDiagnosticLogging._initialized
9090
patch(
91-
"azure.monitor.opentelemetry.distro._diagnostic_logging._DIAGNOSTIC_LOG_PATH",
91+
"azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging._DIAGNOSTIC_LOG_PATH",
9292
TEST_LOGGER_PATH,
9393
).start()
9494
patch(
95-
"azure.monitor.opentelemetry.distro._diagnostic_logging._DIAGNOSTIC_LOGGER_FILE_NAME",
95+
"azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging._DIAGNOSTIC_LOGGER_FILE_NAME",
9696
TEST_DIAGNOSTIC_LOGGER_FILE_NAME,
9797
).start()
9898
patch(
99-
"azure.monitor.opentelemetry.distro._diagnostic_logging._CUSTOMER_IKEY",
99+
"azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging._CUSTOMER_IKEY",
100100
TEST_CUSTOMER_IKEY,
101101
).start()
102102
patch(
103-
"azure.monitor.opentelemetry.distro._diagnostic_logging._EXTENSION_VERSION",
103+
"azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging._EXTENSION_VERSION",
104104
TEST_EXTENSION_VERSION,
105105
).start()
106106
patch(
107-
"azure.monitor.opentelemetry.distro._diagnostic_logging.VERSION",
107+
"azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging.VERSION",
108108
TEST_VERSION,
109109
).start()
110110
patch(
111-
"azure.monitor.opentelemetry.distro._diagnostic_logging._IS_DIAGNOSTICS_ENABLED",
111+
"azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging._IS_DIAGNOSTICS_ENABLED",
112112
is_diagnostics_enabled,
113113
).start()
114114
diagnostic_logger.AzureDiagnosticLogging.enable(logger)

0 commit comments

Comments
 (0)