Skip to content

Commit

Permalink
[AzureMonitorExporter] Refactor Environment Variables (Azure#38012)
Browse files Browse the repository at this point in the history
* refactor Env Vars

* cleanup
  • Loading branch information
TimothyMothra authored Aug 3, 2023
1 parent 0bb8389 commit 688a205
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Available for users to set their Connection String.
/// </summary>
/// <remarks>
/// <see href="https://learn.microsoft.com/azure/azure-monitor/app/opentelemetry-configuration?#connection-string"/>.
/// </remarks>
public const string APPLICATIONINSIGHTS_CONNECTION_STRING = "APPLICATIONINSIGHTS_CONNECTION_STRING";

/// <summary>
/// Available for users to opt-out of Statsbeat.
/// </summary>
/// <remarks>
/// <see href="https://learn.microsoft.com/azure/azure-monitor/app/statsbeat"/>.
/// </remarks>
public const string APPLICATIONINSIGHTS_STATSBEAT_DISABLED = "APPLICATIONINSIGHTS_STATSBEAT_DISABLED";

/// <summary>
/// INTERNAL ONLY. Used by Statsbeat to identify if the Exporter is running within Azure Functions.
/// </summary>
public const string FUNCTIONS_WORKER_RUNTIME = "FUNCTIONS_WORKER_RUNTIME";

/// <summary>
/// INTERNAL ONLY. Used by PersistentStorage to identify a Windows temp directory to save telemetry.
/// </summary>
public const string LOCALAPPDATA = "LOCALAPPDATA";

/// <summary>
/// INTERNAL ONLY. Used by PersistentStorage to identify a Windows temp directory to save telemetry.
/// </summary>
public const string TEMP = "TEMP";

/// <summary>
/// INTERNAL ONLY. Used by PersistentStorage to identify a Linux temp directory to save telemetry.
/// </summary>
public const string TMPDIR = "TMPDIR";

/// <summary>
/// INTERNAL ONLY. Used by Statsbeat to get the App Service Host Name.
/// </summary>
public const string WEBSITE_HOME_STAMPNAME = "WEBSITE_HOME_STAMPNAME";

/// <summary>
/// INTERNAL ONLY. Used by Statsbeat to identify Azure Functions.
/// </summary>
public const string WEBSITE_HOSTNAME = "WEBSITE_HOSTNAME";

/// <summary>
/// INTERNAL ONLY. Used by Statsbeat to get the App Service Website Name.
/// </summary>
public const string WEBSITE_SITE_NAME = "WEBSITE_SITE_NAME";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ private Measurement<int> 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;
Expand All @@ -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;
}
Expand Down

0 comments on commit 688a205

Please sign in to comment.