Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AzureMonitorExporter] refactor how EnvironmentVariables are read #38017

Merged
merged 7 commits into from
Nov 2, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -419,5 +419,17 @@ public void FailedToAddLogAttribute(string key, Exception ex)

[Event(41, Message = "An exception {1} occurred while adding the log attribute associated with the key {0}", Level = EventLevel.Warning)]
public void FailedToAddLogAttribute(string key, string exceptionMessage) => WriteEvent(41, key, exceptionMessage);

[NonEvent]
public void FailedToReadEnvironmentVariables(Exception ex)
{
if (IsEnabled(EventLevel.Warning))
{
FailedToReadEnvironmentVariables(ex.FlattenException().ToInvariantString());
}
}

[Event(42, Message = "Failed to read environment variables due to an exception. This may prevent the Exporter from initializing. {0}", Level = EventLevel.Warning)]
public void FailedToReadEnvironmentVariables(string errorMessage) => WriteEvent(42, errorMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using System;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
using Azure.Monitor.OpenTelemetry.Exporter.Internals.Platform;
Expand Down Expand Up @@ -30,19 +29,18 @@ internal static string GetStorageDirectory(IPlatform platform, string? configure
internal static string? GetDefaultStorageDirectory(IPlatform platform)
{
string? dirPath;
IDictionary environmentVars = platform.GetEnvironmentVariables();

if (platform.IsOSPlatform(OSPlatform.Windows))
{
if (TryCreateTelemetryDirectory(platform: platform, path: environmentVars[EnvironmentVariableConstants.LOCALAPPDATA]?.ToString(), createdDirectoryPath: out dirPath)
|| TryCreateTelemetryDirectory(platform: platform, path: environmentVars[EnvironmentVariableConstants.TEMP]?.ToString(), createdDirectoryPath: out dirPath))
if (TryCreateTelemetryDirectory(platform: platform, path: platform.GetEnvironmentVariable(EnvironmentVariableConstants.LOCALAPPDATA), createdDirectoryPath: out dirPath)
|| TryCreateTelemetryDirectory(platform: platform, path: platform.GetEnvironmentVariable(EnvironmentVariableConstants.TEMP), createdDirectoryPath: out dirPath))
{
return dirPath;
}
}
else
{
if (TryCreateTelemetryDirectory(platform: platform, path: environmentVars[EnvironmentVariableConstants.TMPDIR]?.ToString(), createdDirectoryPath: out dirPath)
if (TryCreateTelemetryDirectory(platform: platform, path: platform.GetEnvironmentVariable(EnvironmentVariableConstants.TMPDIR), 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 @@ -3,6 +3,7 @@

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
Expand All @@ -12,9 +13,22 @@ namespace Azure.Monitor.OpenTelemetry.Exporter.Internals.Platform
{
internal class DefaultPlatform : IPlatform
{
public string? GetEnvironmentVariable(string name) => Environment.GetEnvironmentVariable(name);
private readonly IDictionary _environmentVariables;

public IDictionary GetEnvironmentVariables() => Environment.GetEnvironmentVariables();
public DefaultPlatform()
{
try
{
_environmentVariables = Environment.GetEnvironmentVariables();
}
catch (Exception ex)
{
AzureMonitorExporterEventSource.Log.FailedToReadEnvironmentVariables(ex);
_environmentVariables = new Dictionary<string, object>();
}
}

public string? GetEnvironmentVariable(string name) => _environmentVariables[name]?.ToString();

public string GetOSPlatformName()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

namespace Azure.Monitor.OpenTelemetry.Exporter.Internals
namespace Azure.Monitor.OpenTelemetry.Exporter.Internals.Platform
{
internal static class EnvironmentVariableConstants
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections;
using System.Runtime.InteropServices;

namespace Azure.Monitor.OpenTelemetry.Exporter.Internals.Platform
Expand All @@ -15,8 +14,6 @@ internal interface IPlatform
/// <returns></returns>
public string? GetEnvironmentVariable(string name);

public IDictionary GetEnvironmentVariables();

public bool IsOSPlatform(OSPlatform osPlatform);

public string GetOSPlatformName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Net;
using Azure.Monitor.OpenTelemetry.Exporter.Internals.Diagnostics;
using Azure.Monitor.OpenTelemetry.Exporter.Internals.Platform;
using Azure.Monitor.OpenTelemetry.Exporter.Models;
using OpenTelemetry.Resources;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace Azure.Monitor.OpenTelemetry.Exporter.Internals
internal sealed class TransmitterFactory
{
public static readonly TransmitterFactory Instance = new();
public static readonly IPlatform platform = new DefaultPlatform();

internal readonly Dictionary<string, AzureMonitorTransmitter> _transmitters = new();
private readonly object _lockObj = new();
Expand All @@ -30,7 +29,7 @@ public AzureMonitorTransmitter Get(AzureMonitorExporterOptions azureMonitorExpor
{
if (!_transmitters.TryGetValue(key, out transmitter))
{
transmitter = new AzureMonitorTransmitter(azureMonitorExporterOptions, platform);
transmitter = new AzureMonitorTransmitter(azureMonitorExporterOptions, new DefaultPlatform());

_transmitters.Add(key, transmitter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Azure.Monitor.OpenTelemetry.Exporter.Internals.Platform;
Expand All @@ -24,8 +23,6 @@ internal class MockPlatform : IPlatform

public string? GetEnvironmentVariable(string name) => environmentVariables.TryGetValue(name, out var value) ? value : null;

public IDictionary GetEnvironmentVariables() => environmentVariables;

public string GetOSPlatformName() => OSPlatformName;

public bool IsOSPlatform(OSPlatform osPlatform) => IsOsPlatformFunc(osPlatform);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Net;
using Azure.Monitor.OpenTelemetry.Exporter.Internals;
using Azure.Monitor.OpenTelemetry.Exporter.Internals.Platform;
using Azure.Monitor.OpenTelemetry.Exporter.Models;
using OpenTelemetry.Resources;
using Xunit;
Expand Down