Skip to content

Conn str revamp #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
([#217](https://github.com/microsoft/ApplicationInsights-Python/pull/217))
- Add Logging configuration to Distro API
([#218](https://github.com/microsoft/ApplicationInsights-Python/pull/218))
- Removing diagnostic logging from its module's logger. Preventing duplicate handlers
([#225](https://github.com/microsoft/ApplicationInsights-Python/pull/225))
- Redesdigning connection string constants to work for configuration and diagnostics
([#226](https://github.com/microsoft/ApplicationInsights-Python/pull/226))

## [1.0.0b8](https://github.com/microsoft/ApplicationInsights-Python/releases/tag/v1.0.0b8) - 2022-09-26

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
# --------------------------------------------------------------------------
from logging import NOTSET, getLogger

from azure.monitor.opentelemetry.distro._constants import (
ConnectionStringConstants,
)
from azure.monitor.opentelemetry.distro.util import get_configurations
from azure.monitor.opentelemetry.exporter import (
ApplicationInsightsSampler,
Expand Down Expand Up @@ -33,6 +36,14 @@ def configure_azure_monitor(**kwargs):
"""

configurations = get_configurations(**kwargs)
conn_str = configurations.get("connection_string", "")
if conn_str is None:
# TODO: JEREVOSS: We could levae this for the exporter to determine.
configurations[
"connection_string"
] = ConnectionStringConstants.get_conn_str()
else:
ConnectionStringConstants.set_conn_str(conn_str)
service_name = configurations.get("service_name", "")
service_namespace = configurations.get("service_namespace", "")
service_instance_id = configurations.get("service_instance_id", "")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import logging
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License in the project root for
# license information.
# --------------------------------------------------------------------------

import platform
from os import environ
from pathlib import Path
Expand All @@ -7,12 +12,6 @@
ConnectionStringParser,
)

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License in the project root for
# license information.
# --------------------------------------------------------------------------

_LOG_PATH_LINUX = "/var/log/applicationinsights"
_LOG_PATH_WINDOWS = "\\LogFiles\\ApplicationInsights"
_IS_ON_APP_SERVICE = "WEBSITE_SITE_NAME" in environ
Expand All @@ -22,12 +21,6 @@
# _EXPORTER_DIAGNOSTICS_ENABLED_ENV_VAR = (
# "AZURE_MONITOR_OPENTELEMETRY_DISTRO_ENABLE_EXPORTER_DIAGNOSTICS"
# )
logger = logging.getLogger(__name__)
_CUSTOMER_IKEY = "unknown"
try:
_CUSTOMER_IKEY = ConnectionStringParser().instrumentation_key
except ValueError as e:
logger.error("Failed to parse Instrumentation Key: %s" % e)


def _get_log_path(status_log_path=False):
Expand Down Expand Up @@ -64,3 +57,25 @@ def _env_var_or_default(var_name, default_val=""):
)
# TODO: Enabled when duplciate logging issue is solved
# _EXPORTER_DIAGNOSTICS_ENABLED = _is_exporter_diagnostics_enabled()


class ConnectionStringConstants:
_conn_str_parser = None

def set_conn_str_from_env_var():
ConnectionStringConstants._conn_str_parser = ConnectionStringParser()

def set_conn_str(conn_str):
ConnectionStringConstants._conn_str_parser = ConnectionStringParser(
conn_str
)

def get_conn_str():
if ConnectionStringConstants._conn_str_parser is None:
return None
return ConnectionStringConstants._conn_str_parser._conn_str

def get_customer_ikey():
if ConnectionStringConstants._conn_str_parser is None:
return None
return ConnectionStringConstants._conn_str_parser.instrumentation_key
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
from os.path import exists, join

from azure.monitor.opentelemetry.distro._constants import (
_CUSTOMER_IKEY,
_EXTENSION_VERSION,
_IS_DIAGNOSTICS_ENABLED,
ConnectionStringConstants,
_env_var_or_default,
_get_log_path,
)
from azure.monitor.opentelemetry.distro._version import VERSION

_OPENTELEMETRY_DIAGNOSTIC_LOGGER_NAME = "opentelemetry"
_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")
Expand All @@ -40,6 +39,20 @@ def _initialize():
with AzureDiagnosticLogging._lock:
if not AzureDiagnosticLogging._initialized:
if _IS_DIAGNOSTICS_ENABLED and _DIAGNOSTIC_LOG_PATH:
customer_ikey = (
ConnectionStringConstants.get_customer_ikey()
)
if customer_ikey is None:
try:
ConnectionStringConstants.set_conn_str_from_env_var()
customer_ikey = (
ConnectionStringConstants.get_customer_ikey()
)
except ValueError as e:
_logger.error(
"Failed to parse Instrumentation Key: %s" % e
)
customer_ikey = "unknown"
format = (
"{"
+ '"time":"%(asctime)s.%(msecs)03d", '
Expand All @@ -49,7 +62,7 @@ def _initialize():
+ '"properties":{'
+ '"operation":"Startup", '
+ f'"sitename":"{_SITE_NAME}", '
+ f'"ikey":"{_CUSTOMER_IKEY}", '
+ f'"ikey":"{customer_ikey}", '
+ f'"extensionVersion":"{_EXTENSION_VERSION}", '
+ f'"sdkVersion":"{VERSION}", '
+ f'"subscriptionId":"{_SUBSCRIPTION_ID}", '
Expand All @@ -68,14 +81,19 @@ def _initialize():
fmt=format, datefmt="%Y-%m-%dT%H:%M:%S"
)
AzureDiagnosticLogging._f_handler.setFormatter(formatter)
_logger.addHandler(AzureDiagnosticLogging._f_handler)
AzureDiagnosticLogging._initialized = True
_logger.info("Initialized Azure Diagnostic Logger.")

def enable(logger: logging.Logger):
AzureDiagnosticLogging._initialize()
if AzureDiagnosticLogging._initialized:
logger.addHandler(AzureDiagnosticLogging._f_handler)
_logger.info(
"Added Azure diagnostics logging to %s." % logger.name
)
if AzureDiagnosticLogging._f_handler in logger.handlers:
_logger.info(
"Azure diagnostics already enabled for %s logger."
% logger.name
)
else:
logger.addHandler(AzureDiagnosticLogging._f_handler)
_logger.info(
"Added Azure diagnostics logging to %s." % logger.name
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,42 @@
# license information.
# --------------------------------------------------------------------------

import logging
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,
ConnectionStringConstants,
_get_log_path,
)
from azure.monitor.opentelemetry.distro._version import VERSION

_MACHINE_NAME = node()
_STATUS_LOG_PATH = _get_log_path(status_log_path=True)
_logger = logging.getLogger(__name__)


class AzureStatusLogger:
def _get_status_json(agent_initialized_successfully, pid, reason=None):
customer_ikey = ConnectionStringConstants.get_customer_ikey()
if customer_ikey is None:
try:
ConnectionStringConstants.set_conn_str_from_env_var()
customer_ikey = ConnectionStringConstants.get_customer_ikey()
except ValueError as e:
_logger.error("Failed to parse Instrumentation Key: %s" % e)
customer_ikey = "unknown"
status_json = {
"AgentInitializedSuccessfully": agent_initialized_successfully,
"AppType": "python",
"MachineName": _MACHINE_NAME,
"PID": pid,
"SdkVersion": VERSION,
"Ikey": _CUSTOMER_IKEY,
"Ikey": customer_ikey,
"ExtensionVersion": _EXTENSION_VERSION,
}
if reason:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ def set_up(
TEST_DIAGNOSTIC_LOGGER_FILE_NAME,
).start()
patch(
"azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging._CUSTOMER_IKEY",
TEST_CUSTOMER_IKEY,
"azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging.ConnectionStringConstants.get_customer_ikey",
return_value=TEST_CUSTOMER_IKEY,
).start()
patch(
"azure.monitor.opentelemetry.distro._diagnostics._diagnostic_logging._EXTENSION_VERSION",
Expand Down Expand Up @@ -151,6 +151,14 @@ def test_warning(self):
TEST_LOGGER_SUB_MODULE.warning(MESSAGE2)
check_file_for_messages("WARNING", (MESSAGE1, MESSAGE2))

def test_warning_multiple_enable(self):
set_up(is_diagnostics_enabled=True)
diagnostic_logger.AzureDiagnosticLogging.enable(TEST_LOGGER)
diagnostic_logger.AzureDiagnosticLogging.enable(TEST_LOGGER)
TEST_LOGGER_SUB_MODULE.warning(MESSAGE1)
TEST_LOGGER_SUB_MODULE.warning(MESSAGE2)
check_file_for_messages("WARNING", (MESSAGE1, MESSAGE2))

def test_error(self):
set_up(is_diagnostics_enabled=True)
TEST_LOGGER_SUB_MODULE.error(MESSAGE1)
Expand Down
Loading