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 @@ -6,6 +6,8 @@
([#295](https://github.com/microsoft/ApplicationInsights-Python/pull/295))
- Enable Azure Core Tracing OpenTelemetry plugin
([#269](https://github.com/microsoft/ApplicationInsights-Python/pull/269))
- Fix connection string environment variable bug for manual instrumentation
([#302](https://github.com/microsoft/ApplicationInsights-Python/pull/302))

## [1.0.0b13](https://github.com/microsoft/ApplicationInsights-Python/releases/tag/v1.0.0b13) - 2023-06-14

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,21 @@
# _EXPORTER_DIAGNOSTICS_ENABLED_ENV_VAR = (
# "AZURE_MONITOR_OPENTELEMETRY_DISTRO_ENABLE_EXPORTER_DIAGNOSTICS"
# )
_CUSTOMER_IKEY_ENV_VAR = None
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_customer_ikey_from_env_var():
global _CUSTOMER_IKEY_ENV_VAR
if not _CUSTOMER_IKEY_ENV_VAR:
_CUSTOMER_IKEY_ENV_VAR = "unknown"
try:
_CUSTOMER_IKEY_ENV_VAR = (
ConnectionStringParser().instrumentation_key
)
except ValueError as e:
logger.error("Failed to parse Instrumentation Key: %s", e)
return _CUSTOMER_IKEY_ENV_VAR


def _get_log_path(status_log_path=False):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
from os.path import exists, join

from azure.monitor.opentelemetry._constants import (
_CUSTOMER_IKEY,
_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
Expand Down Expand Up @@ -49,7 +49,7 @@ def _initialize(cls):
+ '"properties":{'
+ '"operation":"Startup", '
+ f'"sitename":"{_SITE_NAME}", '
+ f'"ikey":"{_CUSTOMER_IKEY}", '
+ f'"ikey":"{_get_customer_ikey_from_env_var()}", '
+ f'"extensionVersion":"{_EXTENSION_VERSION}", '
+ f'"sdkVersion":"{VERSION}", '
+ f'"subscriptionId":"{_SUBSCRIPTION_ID}", '
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
from platform import node

from azure.monitor.opentelemetry._constants import (
_CUSTOMER_IKEY,
_EXTENSION_VERSION,
_IS_DIAGNOSTICS_ENABLED,
_get_customer_ikey_from_env_var,
_get_log_path,
)
from azure.monitor.opentelemetry._version import VERSION
Expand All @@ -32,7 +32,7 @@ def _get_status_json(
"MachineName": _MACHINE_NAME,
"PID": pid,
"SdkVersion": VERSION,
"Ikey": _CUSTOMER_IKEY,
"Ikey": _get_customer_ikey_from_env_var(),
"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.diagnostics._diagnostic_logging._CUSTOMER_IKEY",
TEST_CUSTOMER_IKEY,
"azure.monitor.opentelemetry.diagnostics._diagnostic_logging._get_customer_ikey_from_env_var",
return_value=TEST_CUSTOMER_IKEY,
).start()
patch(
"azure.monitor.opentelemetry.diagnostics._diagnostic_logging._EXTENSION_VERSION",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ def setUp(self) -> None:
TEST_LOGGER_PATH,
)
@patch(
"azure.monitor.opentelemetry.diagnostics._status_logger._CUSTOMER_IKEY",
TEST_CUSTOMER_IKEY,
"azure.monitor.opentelemetry.diagnostics._status_logger._get_customer_ikey_from_env_var",
return_value=TEST_CUSTOMER_IKEY,
)
@patch(
"azure.monitor.opentelemetry.diagnostics._status_logger._EXTENSION_VERSION",
Expand All @@ -96,7 +96,7 @@ def setUp(self) -> None:
"azure.monitor.opentelemetry.diagnostics._status_logger._MACHINE_NAME",
TEST_MACHINE_NAME,
)
def test_log_status_success(self, mock_getpid):
def test_log_status_success(self, mock_getpid, mock_get_ikey):
AzureStatusLogger.log_status(False, MESSAGE1)
AzureStatusLogger.log_status(True, MESSAGE2)
check_file_for_messages(True, MESSAGE2)
Expand All @@ -106,8 +106,8 @@ def test_log_status_success(self, mock_getpid):
TEST_LOGGER_PATH,
)
@patch(
"azure.monitor.opentelemetry.diagnostics._status_logger._CUSTOMER_IKEY",
TEST_CUSTOMER_IKEY,
"azure.monitor.opentelemetry.diagnostics._status_logger._get_customer_ikey_from_env_var",
return_value=TEST_CUSTOMER_IKEY,
)
@patch(
"azure.monitor.opentelemetry.diagnostics._status_logger._EXTENSION_VERSION",
Expand All @@ -129,7 +129,9 @@ def test_log_status_success(self, mock_getpid):
"azure.monitor.opentelemetry.diagnostics._status_logger._MACHINE_NAME",
TEST_MACHINE_NAME,
)
def test_log_status_failed_initialization(self, mock_getpid):
def test_log_status_failed_initialization(
self, mock_getpid, mock_get_ikey
):
AzureStatusLogger.log_status(True, MESSAGE1)
AzureStatusLogger.log_status(False, MESSAGE2)
check_file_for_messages(False, MESSAGE2)
Expand All @@ -139,8 +141,8 @@ def test_log_status_failed_initialization(self, mock_getpid):
TEST_LOGGER_PATH,
)
@patch(
"azure.monitor.opentelemetry.diagnostics._status_logger._CUSTOMER_IKEY",
TEST_CUSTOMER_IKEY,
"azure.monitor.opentelemetry.diagnostics._status_logger._get_customer_ikey_from_env_var",
return_value=TEST_CUSTOMER_IKEY,
)
@patch(
"azure.monitor.opentelemetry.diagnostics._status_logger._EXTENSION_VERSION",
Expand All @@ -162,7 +164,7 @@ def test_log_status_failed_initialization(self, mock_getpid):
"azure.monitor.opentelemetry.diagnostics._status_logger._MACHINE_NAME",
TEST_MACHINE_NAME,
)
def test_log_status_no_reason(self, mock_getpid):
def test_log_status_no_reason(self, mock_getpid, mock_get_ikey):
AzureStatusLogger.log_status(False, MESSAGE1)
AzureStatusLogger.log_status(True)
check_file_for_messages(True)
Expand All @@ -172,8 +174,8 @@ def test_log_status_no_reason(self, mock_getpid):
TEST_LOGGER_PATH,
)
@patch(
"azure.monitor.opentelemetry.diagnostics._status_logger._CUSTOMER_IKEY",
TEST_CUSTOMER_IKEY,
"azure.monitor.opentelemetry.diagnostics._status_logger._get_customer_ikey_from_env_var",
return_value=TEST_CUSTOMER_IKEY,
)
@patch(
"azure.monitor.opentelemetry.diagnostics._status_logger._EXTENSION_VERSION",
Expand All @@ -195,7 +197,7 @@ def test_log_status_no_reason(self, mock_getpid):
"azure.monitor.opentelemetry.diagnostics._status_logger._MACHINE_NAME",
TEST_MACHINE_NAME,
)
def test_disabled_log_status_success(self, mock_getpid):
def test_disabled_log_status_success(self, mock_getpid, mock_get_ikey):
AzureStatusLogger.log_status(False, MESSAGE1)
AzureStatusLogger.log_status(True, MESSAGE2)
check_file_is_empty()
Expand All @@ -205,8 +207,8 @@ def test_disabled_log_status_success(self, mock_getpid):
TEST_LOGGER_PATH,
)
@patch(
"azure.monitor.opentelemetry.diagnostics._status_logger._CUSTOMER_IKEY",
TEST_CUSTOMER_IKEY,
"azure.monitor.opentelemetry.diagnostics._status_logger._get_customer_ikey_from_env_var",
return_value=TEST_CUSTOMER_IKEY,
)
@patch(
"azure.monitor.opentelemetry.diagnostics._status_logger._EXTENSION_VERSION",
Expand All @@ -228,7 +230,9 @@ def test_disabled_log_status_success(self, mock_getpid):
"azure.monitor.opentelemetry.diagnostics._status_logger._MACHINE_NAME",
TEST_MACHINE_NAME,
)
def test_disabled_log_status_failed_initialization(self, mock_getpid):
def test_disabled_log_status_failed_initialization(
self, mock_getpid, mock_get_ikey
):
AzureStatusLogger.log_status(True, MESSAGE1)
AzureStatusLogger.log_status(False, MESSAGE2)
check_file_is_empty()
Expand All @@ -238,8 +242,8 @@ def test_disabled_log_status_failed_initialization(self, mock_getpid):
TEST_LOGGER_PATH,
)
@patch(
"azure.monitor.opentelemetry.diagnostics._status_logger._CUSTOMER_IKEY",
TEST_CUSTOMER_IKEY,
"azure.monitor.opentelemetry.diagnostics._status_logger._get_customer_ikey_from_env_var",
return_value=TEST_CUSTOMER_IKEY,
)
@patch(
"azure.monitor.opentelemetry.diagnostics._status_logger._EXTENSION_VERSION",
Expand All @@ -261,7 +265,7 @@ def test_disabled_log_status_failed_initialization(self, mock_getpid):
"azure.monitor.opentelemetry.diagnostics._status_logger._MACHINE_NAME",
TEST_MACHINE_NAME,
)
def test_disabled_log_status_no_reason(self, mock_getpid):
def test_disabled_log_status_no_reason(self, mock_getpid, mock_get_ikey):
AzureStatusLogger.log_status(False, MESSAGE1)
AzureStatusLogger.log_status(True)
check_file_is_empty()
8 changes: 6 additions & 2 deletions azure-monitor-opentelemetry/tests/test_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ def test_extension_version_default(self):
)
def test_ikey(self):
reload(_constants)
self.assertEqual(_constants._CUSTOMER_IKEY, TEST_IKEY)
self.assertEqual(
_constants._get_customer_ikey_from_env_var(), TEST_IKEY
)

def test_ikey_defaults(self):
clear_env_var("APPLICATIONINSIGHTS_CONNECTION_STRING")
reload(_constants)
self.assertEqual(_constants._CUSTOMER_IKEY, "unknown")
self.assertEqual(
_constants._get_customer_ikey_from_env_var(), "unknown"
)

# TODO: Enabled when duplicate logging issue is solved
# @patch.dict(
Expand Down