about summary refs log tree commit diff
path: root/.venv/lib/python3.12/site-packages/azure/monitor/opentelemetry/_diagnostics
diff options
context:
space:
mode:
authorS. Solomon Darnell2025-03-28 21:52:21 -0500
committerS. Solomon Darnell2025-03-28 21:52:21 -0500
commit4a52a71956a8d46fcb7294ac71734504bb09bcc2 (patch)
treeee3dc5af3b6313e921cd920906356f5d4febc4ed /.venv/lib/python3.12/site-packages/azure/monitor/opentelemetry/_diagnostics
parentcc961e04ba734dd72309fb548a2f97d67d578813 (diff)
downloadgn-ai-master.tar.gz
two version of R2R are here HEAD master
Diffstat (limited to '.venv/lib/python3.12/site-packages/azure/monitor/opentelemetry/_diagnostics')
-rw-r--r--.venv/lib/python3.12/site-packages/azure/monitor/opentelemetry/_diagnostics/__init__.py0
-rw-r--r--.venv/lib/python3.12/site-packages/azure/monitor/opentelemetry/_diagnostics/diagnostic_logging.py99
-rw-r--r--.venv/lib/python3.12/site-packages/azure/monitor/opentelemetry/_diagnostics/status_logger.py58
3 files changed, 157 insertions, 0 deletions
diff --git a/.venv/lib/python3.12/site-packages/azure/monitor/opentelemetry/_diagnostics/__init__.py b/.venv/lib/python3.12/site-packages/azure/monitor/opentelemetry/_diagnostics/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/.venv/lib/python3.12/site-packages/azure/monitor/opentelemetry/_diagnostics/__init__.py
diff --git a/.venv/lib/python3.12/site-packages/azure/monitor/opentelemetry/_diagnostics/diagnostic_logging.py b/.venv/lib/python3.12/site-packages/azure/monitor/opentelemetry/_diagnostics/diagnostic_logging.py
new file mode 100644
index 00000000..f4e05e39
--- /dev/null
+++ b/.venv/lib/python3.12/site-packages/azure/monitor/opentelemetry/_diagnostics/diagnostic_logging.py
@@ -0,0 +1,99 @@
+# -------------------------------------------------------------------------
+# Copyright (c) Microsoft Corporation. All rights reserved.
+# Licensed under the MIT License. See License in the project root for
+# license information.
+# --------------------------------------------------------------------------
+
+import logging
+import threading
+from os import makedirs
+from os.path import exists, join
+
+from azure.monitor.opentelemetry._utils import (
+    _EXTENSION_VERSION,
+    _IS_DIAGNOSTICS_ENABLED,
+    _env_var_or_default,
+    _get_customer_ikey_from_env_var,
+    _get_log_path,
+)
+from azure.monitor.opentelemetry._version import VERSION
+
+_DIAGNOSTIC_LOGGER_FILE_NAME = "applicationinsights-extension.log"
+_SITE_NAME = _env_var_or_default("WEBSITE_SITE_NAME")
+_SUBSCRIPTION_ID_ENV_VAR = _env_var_or_default("WEBSITE_OWNER_NAME")
+_SUBSCRIPTION_ID = _SUBSCRIPTION_ID_ENV_VAR.split("+")[0] if _SUBSCRIPTION_ID_ENV_VAR else None
+_logger = logging.getLogger(__name__)
+_logger.propagate = False
+_logger.setLevel(logging.DEBUG)
+_DIAGNOSTIC_LOG_PATH = _get_log_path()
+
+_DISTRO_DETECTS_ATTACH = "4100"
+_INFO = "4101"
+_INSTRUMENTATION_SKIPPED = "4101"
+
+_ATTACH_SUCCESS_DISTRO = "4200"
+_ATTACH_SUCCESS_CONFIGURATOR = "4201"
+_INSTRUMENTATION_SUCCEEDED = "4202"
+
+_DEPENDENCY_OVERLAP = "4300"
+
+_ATTACH_FAILURE_DISTRO = "4400"
+_ATTACH_FAILURE_CONFIGURATOR = "4401"
+_ATTACH_DETECTS_SDK = "4402"
+_INSTRUMENTATION_FAILED = "4403"
+
+
+class AzureDiagnosticLogging:
+    _initialized = False
+    _lock = threading.Lock()
+
+    @classmethod
+    def _initialize(cls):
+        with AzureDiagnosticLogging._lock:
+            if not AzureDiagnosticLogging._initialized:
+                if _IS_DIAGNOSTICS_ENABLED and _DIAGNOSTIC_LOG_PATH:
+                    log_format = (
+                        "{"
+                        + '"time":"%(asctime)s.%(msecs)03d", '
+                        + '"level":"%(levelname)s", '
+                        + '"logger":"%(name)s", '
+                        + '"message":"%(message)s", '
+                        + '"properties":{'
+                        + '"operation":"Startup", '
+                        + f'"siteName":"{_SITE_NAME}", '
+                        + f'"ikey":"{_get_customer_ikey_from_env_var()}", '
+                        + f'"extensionVersion":"{_EXTENSION_VERSION}", '
+                        + f'"sdkVersion":"{VERSION}", '
+                        + f'"subscriptionId":"{_SUBSCRIPTION_ID}", '
+                        + '"msgId":"%(msgId)s", '
+                        + '"language":"python"'
+                        + "}"
+                        + "}"
+                    )
+                    if not exists(_DIAGNOSTIC_LOG_PATH):
+                        makedirs(_DIAGNOSTIC_LOG_PATH)
+                    f_handler = logging.FileHandler(join(_DIAGNOSTIC_LOG_PATH, _DIAGNOSTIC_LOGGER_FILE_NAME))
+                    formatter = logging.Formatter(fmt=log_format, datefmt="%Y-%m-%dT%H:%M:%S")
+                    f_handler.setFormatter(formatter)
+                    _logger.addHandler(f_handler)
+                    AzureDiagnosticLogging._initialized = True
+
+    @classmethod
+    def debug(cls, message: str, message_id: str):
+        AzureDiagnosticLogging._initialize()
+        _logger.debug(message, extra={"msgId": message_id})
+
+    @classmethod
+    def info(cls, message: str, message_id: str):
+        AzureDiagnosticLogging._initialize()
+        _logger.info(message, extra={"msgId": message_id})
+
+    @classmethod
+    def warning(cls, message: str, message_id: str):
+        AzureDiagnosticLogging._initialize()
+        _logger.warning(message, extra={"msgId": message_id})
+
+    @classmethod
+    def error(cls, message: str, message_id: str):
+        AzureDiagnosticLogging._initialize()
+        _logger.error(message, extra={"msgId": message_id})
diff --git a/.venv/lib/python3.12/site-packages/azure/monitor/opentelemetry/_diagnostics/status_logger.py b/.venv/lib/python3.12/site-packages/azure/monitor/opentelemetry/_diagnostics/status_logger.py
new file mode 100644
index 00000000..3f1ca29e
--- /dev/null
+++ b/.venv/lib/python3.12/site-packages/azure/monitor/opentelemetry/_diagnostics/status_logger.py
@@ -0,0 +1,58 @@
+# -------------------------------------------------------------------------
+# Copyright (c) Microsoft Corporation. All rights reserved.
+# Licensed under the MIT License. See License in the project root for
+# license information.
+# --------------------------------------------------------------------------
+
+from json import dumps
+from os import getpid, makedirs
+from os.path import exists, join
+from platform import node
+
+from azure.monitor.opentelemetry._utils import (
+    _EXTENSION_VERSION,
+    _IS_DIAGNOSTICS_ENABLED,
+    _get_customer_ikey_from_env_var,
+    _get_log_path,
+)
+from azure.monitor.opentelemetry._version import VERSION
+
+_MACHINE_NAME = node()
+_STATUS_LOG_PATH = _get_log_path(status_log_path=True)
+
+
+def _get_status_logger_file_name(pid):
+    return f"status_{_MACHINE_NAME}_{pid}.json"
+
+
+class AzureStatusLogger:
+    @classmethod
+    def _get_status_json(cls, agent_initialized_successfully, pid, reason=None, sdk_present=None):
+        status_json = {
+            "AgentInitializedSuccessfully": agent_initialized_successfully,
+            "AppType": "python",
+            "MachineName": _MACHINE_NAME,
+            "PID": pid,
+            "SdkVersion": VERSION,
+            "Ikey": _get_customer_ikey_from_env_var(),
+            "ExtensionVersion": _EXTENSION_VERSION,
+        }
+        if reason:
+            status_json["Reason"] = reason
+        if sdk_present:
+            status_json["SDKPresent"] = sdk_present
+        return status_json
+
+    @classmethod
+    def log_status(cls, agent_initialized_successfully, reason=None, sdk_present=None):
+        if _IS_DIAGNOSTICS_ENABLED and _STATUS_LOG_PATH:
+            pid = getpid()
+            status_json = AzureStatusLogger._get_status_json(agent_initialized_successfully, pid, reason, sdk_present)
+            if not exists(_STATUS_LOG_PATH):
+                makedirs(_STATUS_LOG_PATH)
+            # Change to be hostname and pid
+            status_logger_file_name = _get_status_logger_file_name(pid)
+            with open(join(_STATUS_LOG_PATH, status_logger_file_name), "w", encoding="utf8") as f:
+                f.seek(0)
+                f.write(dumps(status_json))
+                f.truncate()