Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Added Status Logger
([#217](https://github.com/microsoft/ApplicationInsights-Python/pull/217))
- Added Diagnostic Logging for App Service
([#212](https://github.com/microsoft/ApplicationInsights-Python/pull/212))
- Updated main and distro READMEs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
if _SUBSCRIPTION_ID_ENV_VAR
else None
)
_opentelemetry_logger = logging.getLogger(
_OPENTELEMETRY_DIAGNOSTIC_LOGGER_NAME
)
_logger = logging.getLogger(__name__)
_DIAGNOSTIC_LOG_PATH = _get_log_path()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# -------------------------------------------------------------------------
# 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.distro._constants import (
_CUSTOMER_IKEY,
_EXTENSION_VERSION,
_IS_DIAGNOSTICS_ENABLED,
_get_log_path,
)
from azure.monitor.opentelemetry.distro._version import VERSION

_MACHINE_NAME = node()
_STATUS_LOG_PATH = _get_log_path(status_log_path=True)


class AzureStatusLogger:
def _get_status_json(agent_initialized_successfully, pid, reason=None):
status_json = {
"AgentInitializedSuccessfully": agent_initialized_successfully,
"AppType": "python",
"MachineName": _MACHINE_NAME,
"PID": pid,
"SdkVersion": VERSION,
"Ikey": _CUSTOMER_IKEY,
"ExtensionVersion": _EXTENSION_VERSION,
}
if reason:
status_json["Reason"] = reason
return status_json

def log_status(agent_initialized_successfully, reason=None):
if _IS_DIAGNOSTICS_ENABLED and _STATUS_LOG_PATH:
pid = getpid()
status_json = AzureStatusLogger._get_status_json(
agent_initialized_successfully, pid, reason
)
if not exists(_STATUS_LOG_PATH):
makedirs(_STATUS_LOG_PATH)
# Change to be hostname and pid
status_logger_file_name = f"status_{_MACHINE_NAME}_{pid}.json"
with open(
join(_STATUS_LOG_PATH, status_logger_file_name), "w"
) as f:
f.seek(0)
f.write(dumps(status_json))
f.truncate()
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# --------------------------------------------------------------------------


from azure.monitor.opentelemetry.distro._diagnostic_logging import (
from azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging import (
AzureDiagnosticLogging,
)
from opentelemetry.sdk._configuration import _OTelSDKConfigurator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import logging
from os import environ

from azure.monitor.opentelemetry.distro._diagnostic_logging import (
from azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging import (
AzureDiagnosticLogging,
)
from azure.monitor.opentelemetry.distro._diagnostics._status_logger import (
AzureStatusLogger,
)
from opentelemetry.environment_variables import (
OTEL_METRICS_EXPORTER,
OTEL_TRACES_EXPORTER,
Expand All @@ -34,6 +37,7 @@ def _configure(self, **kwargs) -> None:

def _configure_auto_instrumentation() -> None:
try:
AzureStatusLogger.log_status(False, "Distro being configured.")
AzureDiagnosticLogging.enable(_logger)
AzureDiagnosticLogging.enable(_opentelemetry_logger)
# TODO: Enabled when duplicate logging issue is solved
Expand All @@ -51,10 +55,12 @@ def _configure_auto_instrumentation() -> None:
environ.setdefault(
OTEL_TRACES_EXPORTER, "azure_monitor_opentelemetry_exporter"
)
AzureStatusLogger.log_status(True)
_logger.info(
"Azure Monitor OpenTelemetry Distro configured successfully."
)
except Exception as exc:
AzureStatusLogger.log_status(False, reason=exc)
_logger.error(
"Azure Monitor OpenTelemetry Distro failed during "
+ f"configuration: {exc}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from unittest import TestCase
from unittest.mock import patch

import azure.monitor.opentelemetry.distro._diagnostic_logging as diagnostic_logger
import azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging as diagnostic_logger

TEST_LOGGER_PATH = str(Path.home())
TEST_DIAGNOSTIC_LOGGER_FILE_NAME = "test-applicationinsights-extension.log"
Expand Down Expand Up @@ -88,27 +88,27 @@ def set_up(
reload(diagnostic_logger)
assert not diagnostic_logger.AzureDiagnosticLogging._initialized
patch(
"azure.monitor.opentelemetry.distro._diagnostic_logging._DIAGNOSTIC_LOG_PATH",
"azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging._DIAGNOSTIC_LOG_PATH",
TEST_LOGGER_PATH,
).start()
patch(
"azure.monitor.opentelemetry.distro._diagnostic_logging._DIAGNOSTIC_LOGGER_FILE_NAME",
"azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging._DIAGNOSTIC_LOGGER_FILE_NAME",
TEST_DIAGNOSTIC_LOGGER_FILE_NAME,
).start()
patch(
"azure.monitor.opentelemetry.distro._diagnostic_logging._CUSTOMER_IKEY",
"azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging._CUSTOMER_IKEY",
TEST_CUSTOMER_IKEY,
).start()
patch(
"azure.monitor.opentelemetry.distro._diagnostic_logging._EXTENSION_VERSION",
"azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging._EXTENSION_VERSION",
TEST_EXTENSION_VERSION,
).start()
patch(
"azure.monitor.opentelemetry.distro._diagnostic_logging.VERSION",
"azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging.VERSION",
TEST_VERSION,
).start()
patch(
"azure.monitor.opentelemetry.distro._diagnostic_logging._IS_DIAGNOSTICS_ENABLED",
"azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging._IS_DIAGNOSTICS_ENABLED",
is_diagnostics_enabled,
).start()
diagnostic_logger.AzureDiagnosticLogging.enable(logger)
Expand Down
Loading