From 688a2056851f26f26ac7fe4280961d5834313bf3 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Date: Thu, 3 Aug 2023 15:04:21 -0700 Subject: [PATCH] [AzureMonitorExporter] Refactor Environment Variables (#38012) * refactor Env Vars * cleanup --- .../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 0ca72df157517..4871be2fc1f17 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 0000000000000..3b097b0080eb5 --- /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 + { + /// + /// Available for users to set their Connection String. + /// + /// + /// . + /// + public const string APPLICATIONINSIGHTS_CONNECTION_STRING = "APPLICATIONINSIGHTS_CONNECTION_STRING"; + + /// + /// Available for users to opt-out of Statsbeat. + /// + /// + /// . + /// + public const string APPLICATIONINSIGHTS_STATSBEAT_DISABLED = "APPLICATIONINSIGHTS_STATSBEAT_DISABLED"; + + /// + /// INTERNAL ONLY. Used by Statsbeat to identify if the Exporter is running within Azure Functions. + /// + public const string FUNCTIONS_WORKER_RUNTIME = "FUNCTIONS_WORKER_RUNTIME"; + + /// + /// INTERNAL ONLY. Used by PersistentStorage to identify a Windows temp directory to save telemetry. + /// + public const string LOCALAPPDATA = "LOCALAPPDATA"; + + /// + /// INTERNAL ONLY. Used by PersistentStorage to identify a Windows temp directory to save telemetry. + /// + public const string TEMP = "TEMP"; + + /// + /// INTERNAL ONLY. Used by PersistentStorage to identify a Linux temp directory to save telemetry. + /// + public const string TMPDIR = "TMPDIR"; + + /// + /// INTERNAL ONLY. Used by Statsbeat to get the App Service Host Name. + /// + public const string WEBSITE_HOME_STAMPNAME = "WEBSITE_HOME_STAMPNAME"; + + /// + /// INTERNAL ONLY. Used by Statsbeat to identify Azure Functions. + /// + public const string WEBSITE_HOSTNAME = "WEBSITE_HOSTNAME"; + + /// + /// INTERNAL ONLY. 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 c825961270d03..ca811bbd41dfb 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 94ac48b399bfa..c4e6467dedeb1 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; }