Skip to content

Commit 688a205

Browse files
author
Timothy Mothra
authored
[AzureMonitorExporter] Refactor Environment Variables (#38012)
* refactor Env Vars * cleanup
1 parent 0bb8389 commit 688a205

File tree

4 files changed

+68
-9
lines changed

4 files changed

+68
-9
lines changed

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/AzureMonitorTransmitter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ internal static ConnectionVars InitializeConnectionVars(AzureMonitorExporterOpti
6262
{
6363
if (options.ConnectionString == null)
6464
{
65-
var connectionString = platform.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING");
65+
var connectionString = platform.GetEnvironmentVariable(EnvironmentVariableConstants.APPLICATIONINSIGHTS_CONNECTION_STRING);
6666

6767
if (!string.IsNullOrWhiteSpace(connectionString))
6868
{
@@ -137,7 +137,7 @@ private static ApplicationInsightsRestClient InitializeRestClient(AzureMonitorEx
137137
{
138138
try
139139
{
140-
var disableStatsbeat = platform.GetEnvironmentVariable("APPLICATIONINSIGHTS_STATSBEAT_DISABLED");
140+
var disableStatsbeat = platform.GetEnvironmentVariable(EnvironmentVariableConstants.APPLICATIONINSIGHTS_STATSBEAT_DISABLED);
141141
if (string.Equals(disableStatsbeat, "true", StringComparison.OrdinalIgnoreCase))
142142
{
143143
AzureMonitorExporterEventSource.Log.StatsbeatDisabled();
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
namespace Azure.Monitor.OpenTelemetry.Exporter.Internals
5+
{
6+
internal static class EnvironmentVariableConstants
7+
{
8+
/// <summary>
9+
/// Available for users to set their Connection String.
10+
/// </summary>
11+
/// <remarks>
12+
/// <see href="https://learn.microsoft.com/azure/azure-monitor/app/opentelemetry-configuration?#connection-string"/>.
13+
/// </remarks>
14+
public const string APPLICATIONINSIGHTS_CONNECTION_STRING = "APPLICATIONINSIGHTS_CONNECTION_STRING";
15+
16+
/// <summary>
17+
/// Available for users to opt-out of Statsbeat.
18+
/// </summary>
19+
/// <remarks>
20+
/// <see href="https://learn.microsoft.com/azure/azure-monitor/app/statsbeat"/>.
21+
/// </remarks>
22+
public const string APPLICATIONINSIGHTS_STATSBEAT_DISABLED = "APPLICATIONINSIGHTS_STATSBEAT_DISABLED";
23+
24+
/// <summary>
25+
/// INTERNAL ONLY. Used by Statsbeat to identify if the Exporter is running within Azure Functions.
26+
/// </summary>
27+
public const string FUNCTIONS_WORKER_RUNTIME = "FUNCTIONS_WORKER_RUNTIME";
28+
29+
/// <summary>
30+
/// INTERNAL ONLY. Used by PersistentStorage to identify a Windows temp directory to save telemetry.
31+
/// </summary>
32+
public const string LOCALAPPDATA = "LOCALAPPDATA";
33+
34+
/// <summary>
35+
/// INTERNAL ONLY. Used by PersistentStorage to identify a Windows temp directory to save telemetry.
36+
/// </summary>
37+
public const string TEMP = "TEMP";
38+
39+
/// <summary>
40+
/// INTERNAL ONLY. Used by PersistentStorage to identify a Linux temp directory to save telemetry.
41+
/// </summary>
42+
public const string TMPDIR = "TMPDIR";
43+
44+
/// <summary>
45+
/// INTERNAL ONLY. Used by Statsbeat to get the App Service Host Name.
46+
/// </summary>
47+
public const string WEBSITE_HOME_STAMPNAME = "WEBSITE_HOME_STAMPNAME";
48+
49+
/// <summary>
50+
/// INTERNAL ONLY. Used by Statsbeat to identify Azure Functions.
51+
/// </summary>
52+
public const string WEBSITE_HOSTNAME = "WEBSITE_HOSTNAME";
53+
54+
/// <summary>
55+
/// INTERNAL ONLY. Used by Statsbeat to get the App Service Website Name.
56+
/// </summary>
57+
public const string WEBSITE_SITE_NAME = "WEBSITE_SITE_NAME";
58+
}
59+
}

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/PersistentStorage/StorageHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ internal static string GetStorageDirectory(IPlatform platform, string? configure
3434

3535
if (platform.IsOSPlatform(OSPlatform.Windows))
3636
{
37-
if (TryCreateTelemetryDirectory(platform: platform, path: environmentVars["LOCALAPPDATA"]?.ToString(), createdDirectoryPath: out dirPath)
38-
|| TryCreateTelemetryDirectory(platform: platform, path: environmentVars["TEMP"]?.ToString(), createdDirectoryPath: out dirPath))
37+
if (TryCreateTelemetryDirectory(platform: platform, path: environmentVars[EnvironmentVariableConstants.LOCALAPPDATA]?.ToString(), createdDirectoryPath: out dirPath)
38+
|| TryCreateTelemetryDirectory(platform: platform, path: environmentVars[EnvironmentVariableConstants.TEMP]?.ToString(), createdDirectoryPath: out dirPath))
3939
{
4040
return dirPath;
4141
}
4242
}
4343
else
4444
{
45-
if (TryCreateTelemetryDirectory(platform: platform, path: environmentVars["TMPDIR"]?.ToString(), createdDirectoryPath: out dirPath)
45+
if (TryCreateTelemetryDirectory(platform: platform, path: environmentVars[EnvironmentVariableConstants.TMPDIR]?.ToString(), createdDirectoryPath: out dirPath)
4646
|| TryCreateTelemetryDirectory(platform: platform, path: "/var/tmp/", createdDirectoryPath: out dirPath)
4747
|| TryCreateTelemetryDirectory(platform: platform, path: "/tmp/", createdDirectoryPath: out dirPath))
4848
{

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Statsbeat/AzureMonitorStatsbeat.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ private Measurement<int> GetAttachStatsbeat()
144144

145145
private void SetResourceProviderDetails(IPlatform platform)
146146
{
147-
var appSvcWebsiteName = platform.GetEnvironmentVariable("WEBSITE_SITE_NAME");
147+
var appSvcWebsiteName = platform.GetEnvironmentVariable(EnvironmentVariableConstants.WEBSITE_SITE_NAME);
148148
if (appSvcWebsiteName != null)
149149
{
150150
_resourceProvider = "appsvc";
151151
_resourceProviderId = appSvcWebsiteName;
152-
var appSvcWebsiteHostName = platform.GetEnvironmentVariable("WEBSITE_HOME_STAMPNAME");
152+
var appSvcWebsiteHostName = platform.GetEnvironmentVariable(EnvironmentVariableConstants.WEBSITE_HOME_STAMPNAME);
153153
if (!string.IsNullOrEmpty(appSvcWebsiteHostName))
154154
{
155155
_resourceProviderId += "/" + appSvcWebsiteHostName;
@@ -158,11 +158,11 @@ private void SetResourceProviderDetails(IPlatform platform)
158158
return;
159159
}
160160

161-
var functionsWorkerRuntime = platform.GetEnvironmentVariable("FUNCTIONS_WORKER_RUNTIME");
161+
var functionsWorkerRuntime = platform.GetEnvironmentVariable(EnvironmentVariableConstants.FUNCTIONS_WORKER_RUNTIME);
162162
if (functionsWorkerRuntime != null)
163163
{
164164
_resourceProvider = "functions";
165-
_resourceProviderId = platform.GetEnvironmentVariable("WEBSITE_HOSTNAME");
165+
_resourceProviderId = platform.GetEnvironmentVariable(EnvironmentVariableConstants.WEBSITE_HOSTNAME);
166166

167167
return;
168168
}

0 commit comments

Comments
 (0)