From 3a28c516ccc01eb22587a3af1f12e84460772436 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Thu, 3 Aug 2023 14:28:23 -0700 Subject: [PATCH 1/2] refactor Env Vars --- .../src/Internals/AzureMonitorTransmitter.cs | 4 +- .../Internals/EnvironmentVariableConstants.cs | 59 +++++++++++++++++++ .../PersistentStorage/StorageHelper.cs | 6 +- .../Statsbeat/AzureMonitorStatsbeat.cs | 8 +-- 4 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/EnvironmentVariableConstants.cs diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/AzureMonitorTransmitter.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/AzureMonitorTransmitter.cs index 0ca72df15751..4871be2fc1f1 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/AzureMonitorTransmitter.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/AzureMonitorTransmitter.cs @@ -62,7 +62,7 @@ internal static ConnectionVars InitializeConnectionVars(AzureMonitorExporterOpti { if (options.ConnectionString == null) { - var connectionString = platform.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING"); + var connectionString = platform.GetEnvironmentVariable(EnvironmentVariableConstants.APPLICATIONINSIGHTS_CONNECTION_STRING); if (!string.IsNullOrWhiteSpace(connectionString)) { @@ -137,7 +137,7 @@ private static ApplicationInsightsRestClient InitializeRestClient(AzureMonitorEx { try { - var disableStatsbeat = platform.GetEnvironmentVariable("APPLICATIONINSIGHTS_STATSBEAT_DISABLED"); + var disableStatsbeat = platform.GetEnvironmentVariable(EnvironmentVariableConstants.APPLICATIONINSIGHTS_STATSBEAT_DISABLED); if (string.Equals(disableStatsbeat, "true", StringComparison.OrdinalIgnoreCase)) { AzureMonitorExporterEventSource.Log.StatsbeatDisabled(); diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/EnvironmentVariableConstants.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/EnvironmentVariableConstants.cs new file mode 100644 index 000000000000..12b4c985ac5c --- /dev/null +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/EnvironmentVariableConstants.cs @@ -0,0 +1,59 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +namespace Azure.Monitor.OpenTelemetry.Exporter.Internals +{ + internal static class EnvironmentVariableConstants + { + /// + /// Used to set the Connection String. + /// + /// + /// . + /// + public const string APPLICATIONINSIGHTS_CONNECTION_STRING = "APPLICATIONINSIGHTS_CONNECTION_STRING"; + + /// + /// Used to turn off Statsbeat. + /// + /// + /// . + /// + public const string APPLICATIONINSIGHTS_STATSBEAT_DISABLED = "APPLICATIONINSIGHTS_STATSBEAT_DISABLED"; + + /// + /// Used by Statsbeat to identify if the Exporter is running within Azure Functions. + /// + public const string FUNCTIONS_WORKER_RUNTIME = "FUNCTIONS_WORKER_RUNTIME"; + + /// + /// Used by PersistentStorage to identify a Windows temp directory to save telemetry. + /// + public const string LOCALAPPDATA = "LOCALAPPDATA"; + + /// + /// Used by PersistentStorage to identify a Windows temp directory to save telemetry. + /// + public const string TEMP = "TEMP"; + + /// + /// Used by PersistentStorage to identify a Linux temp directory to save telemetry. + /// + public const string TMPDIR = "TMPDIR"; + + /// + /// Used by Statsbeat to get the App Service Host Name. + /// + public const string WEBSITE_HOME_STAMPNAME = "WEBSITE_HOME_STAMPNAME"; + + /// + /// Used by Statsbeat to identify Azure Functions. + /// + public const string WEBSITE_HOSTNAME = "WEBSITE_HOSTNAME"; + + /// + /// Used by Statsbeat to get the App Service Website Name. + /// + public const string WEBSITE_SITE_NAME = "WEBSITE_SITE_NAME"; + } +} diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/PersistentStorage/StorageHelper.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/PersistentStorage/StorageHelper.cs index c825961270d0..ca811bbd41df 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/PersistentStorage/StorageHelper.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/PersistentStorage/StorageHelper.cs @@ -34,15 +34,15 @@ internal static string GetStorageDirectory(IPlatform platform, string? configure if (platform.IsOSPlatform(OSPlatform.Windows)) { - if (TryCreateTelemetryDirectory(platform: platform, path: environmentVars["LOCALAPPDATA"]?.ToString(), createdDirectoryPath: out dirPath) - || TryCreateTelemetryDirectory(platform: platform, path: environmentVars["TEMP"]?.ToString(), createdDirectoryPath: out dirPath)) + if (TryCreateTelemetryDirectory(platform: platform, path: environmentVars[EnvironmentVariableConstants.LOCALAPPDATA]?.ToString(), createdDirectoryPath: out dirPath) + || TryCreateTelemetryDirectory(platform: platform, path: environmentVars[EnvironmentVariableConstants.TEMP]?.ToString(), createdDirectoryPath: out dirPath)) { return dirPath; } } else { - if (TryCreateTelemetryDirectory(platform: platform, path: environmentVars["TMPDIR"]?.ToString(), createdDirectoryPath: out dirPath) + if (TryCreateTelemetryDirectory(platform: platform, path: environmentVars[EnvironmentVariableConstants.TMPDIR]?.ToString(), createdDirectoryPath: out dirPath) || TryCreateTelemetryDirectory(platform: platform, path: "/var/tmp/", createdDirectoryPath: out dirPath) || TryCreateTelemetryDirectory(platform: platform, path: "/tmp/", createdDirectoryPath: out dirPath)) { diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Statsbeat/AzureMonitorStatsbeat.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Statsbeat/AzureMonitorStatsbeat.cs index 94ac48b399bf..c4e6467dedeb 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Statsbeat/AzureMonitorStatsbeat.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Statsbeat/AzureMonitorStatsbeat.cs @@ -144,12 +144,12 @@ private Measurement GetAttachStatsbeat() private void SetResourceProviderDetails(IPlatform platform) { - var appSvcWebsiteName = platform.GetEnvironmentVariable("WEBSITE_SITE_NAME"); + var appSvcWebsiteName = platform.GetEnvironmentVariable(EnvironmentVariableConstants.WEBSITE_SITE_NAME); if (appSvcWebsiteName != null) { _resourceProvider = "appsvc"; _resourceProviderId = appSvcWebsiteName; - var appSvcWebsiteHostName = platform.GetEnvironmentVariable("WEBSITE_HOME_STAMPNAME"); + var appSvcWebsiteHostName = platform.GetEnvironmentVariable(EnvironmentVariableConstants.WEBSITE_HOME_STAMPNAME); if (!string.IsNullOrEmpty(appSvcWebsiteHostName)) { _resourceProviderId += "/" + appSvcWebsiteHostName; @@ -158,11 +158,11 @@ private void SetResourceProviderDetails(IPlatform platform) return; } - var functionsWorkerRuntime = platform.GetEnvironmentVariable("FUNCTIONS_WORKER_RUNTIME"); + var functionsWorkerRuntime = platform.GetEnvironmentVariable(EnvironmentVariableConstants.FUNCTIONS_WORKER_RUNTIME); if (functionsWorkerRuntime != null) { _resourceProvider = "functions"; - _resourceProviderId = platform.GetEnvironmentVariable("WEBSITE_HOSTNAME"); + _resourceProviderId = platform.GetEnvironmentVariable(EnvironmentVariableConstants.WEBSITE_HOSTNAME); return; } From e6eeeedbf893a398afe0f3fdf4047c32b92ff702 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Thu, 3 Aug 2023 14:29:51 -0700 Subject: [PATCH 2/2] cleanup --- .../Internals/EnvironmentVariableConstants.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/EnvironmentVariableConstants.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/EnvironmentVariableConstants.cs index 12b4c985ac5c..3b097b0080eb 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/EnvironmentVariableConstants.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/EnvironmentVariableConstants.cs @@ -6,7 +6,7 @@ namespace Azure.Monitor.OpenTelemetry.Exporter.Internals internal static class EnvironmentVariableConstants { /// - /// Used to set the Connection String. + /// Available for users to set their Connection String. /// /// /// . @@ -14,7 +14,7 @@ internal static class EnvironmentVariableConstants public const string APPLICATIONINSIGHTS_CONNECTION_STRING = "APPLICATIONINSIGHTS_CONNECTION_STRING"; /// - /// Used to turn off Statsbeat. + /// Available for users to opt-out of Statsbeat. /// /// /// . @@ -22,37 +22,37 @@ internal static class EnvironmentVariableConstants public const string APPLICATIONINSIGHTS_STATSBEAT_DISABLED = "APPLICATIONINSIGHTS_STATSBEAT_DISABLED"; /// - /// Used by Statsbeat to identify if the Exporter is running within Azure Functions. + /// INTERNAL ONLY. Used by Statsbeat to identify if the Exporter is running within Azure Functions. /// public const string FUNCTIONS_WORKER_RUNTIME = "FUNCTIONS_WORKER_RUNTIME"; /// - /// Used by PersistentStorage to identify a Windows temp directory to save telemetry. + /// INTERNAL ONLY. Used by PersistentStorage to identify a Windows temp directory to save telemetry. /// public const string LOCALAPPDATA = "LOCALAPPDATA"; /// - /// Used by PersistentStorage to identify a Windows temp directory to save telemetry. + /// INTERNAL ONLY. Used by PersistentStorage to identify a Windows temp directory to save telemetry. /// public const string TEMP = "TEMP"; /// - /// Used by PersistentStorage to identify a Linux temp directory to save telemetry. + /// INTERNAL ONLY. Used by PersistentStorage to identify a Linux temp directory to save telemetry. /// public const string TMPDIR = "TMPDIR"; /// - /// Used by Statsbeat to get the App Service Host Name. + /// INTERNAL ONLY. Used by Statsbeat to get the App Service Host Name. /// public const string WEBSITE_HOME_STAMPNAME = "WEBSITE_HOME_STAMPNAME"; /// - /// Used by Statsbeat to identify Azure Functions. + /// INTERNAL ONLY. Used by Statsbeat to identify Azure Functions. /// public const string WEBSITE_HOSTNAME = "WEBSITE_HOSTNAME"; /// - /// Used by Statsbeat to get the App Service Website Name. + /// INTERNAL ONLY. Used by Statsbeat to get the App Service Website Name. /// public const string WEBSITE_SITE_NAME = "WEBSITE_SITE_NAME"; }