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

Add default storage location #26494

Merged
merged 10 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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 @@ -8,6 +8,7 @@ public partial class AzureMonitorExporterOptions : Azure.Core.ClientOptions
{
public AzureMonitorExporterOptions(Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions.ServiceVersion version = Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions.ServiceVersion.V2020_09_15_Preview) { }
public string ConnectionString { get { throw null; } set { } }
public string StorageDirectory { get { throw null; } set { } }
public Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions.ServiceVersion Version { get { throw null; } set { } }
public enum ServiceVersion
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public static TracerProviderBuilder AddAzureMonitorTraceExporter(this TracerProv
var options = new AzureMonitorExporterOptions();
configure?.Invoke(options);

if (options.StorageDirectory == null)
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
{
options.StorageDirectory = StorageHelper.GetDefaultStorageDirectory();
}

// TODO: Pick Simple vs Batching based on AzureMonitorExporterOptions
return builder.AddProcessor(new BatchActivityExportProcessor(new AzureMonitorTraceExporter(options)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@ public enum ServiceVersion
/// </summary>
V2020_09_15_Preview = 1,
}

public string StorageDirectory { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

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

namespace Azure.Monitor.OpenTelemetry.Exporter
{
internal static class StorageHelper
{
private static string defaultStorageDirectory;
private const string nonWindowsVarTmp = "/var/tmp/";
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
private const string nonWindowsTmp = "/tmp/";
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved

internal static string GetDefaultStorageDirectory()
{
if (defaultStorageDirectory != null)
{
return defaultStorageDirectory;
}
else
{
string dirPath;
IDictionary environmentVars = Environment.GetEnvironmentVariables();

if (IsWindowsOS())
{
string localAppData = environmentVars["LOCALAPPDATA"].ToString();
if (localAppData != null)
{
dirPath = CreateTelemetryDirectory(localAppData);
if (dirPath != null)
{
defaultStorageDirectory = dirPath;
return defaultStorageDirectory;
}

string temp = environmentVars["TEMP"].ToString();
if (temp != null)
{
dirPath = CreateTelemetryDirectory(temp);
if (dirPath != null)
{
defaultStorageDirectory = dirPath;
return defaultStorageDirectory;
}
}
}
}
else
{
string tmpdir = environmentVars["TMPDIR"].ToString();
if (tmpdir != null)
{
dirPath = CreateTelemetryDirectory(tmpdir);
if (dirPath != null)
{
defaultStorageDirectory = dirPath;
return defaultStorageDirectory;
}
}

dirPath = CreateTelemetryDirectory(nonWindowsVarTmp);
if (dirPath != null)
{
defaultStorageDirectory = dirPath;
return defaultStorageDirectory;
}

dirPath = CreateTelemetryDirectory(nonWindowsTmp);
if (dirPath != null)
{
defaultStorageDirectory = dirPath;
return defaultStorageDirectory;
}
}

return defaultStorageDirectory;
}
}

/// <summary>
/// Creates directory for storing telemetry.
/// </summary>
/// <param name="path">Base directory.</param>
/// <returns>Directory path if it is created else null.</returns>
private static string CreateTelemetryDirectory(string path)
{
try
{
string telemetryDirPath = Path.Combine(path, "Microsoft", "ApplicationInsights");
Directory.CreateDirectory(telemetryDirPath);
return telemetryDirPath;
}
catch (Exception ex)
{
AzureMonitorExporterEventSource.Log.Write($"ErrorCreatingDefaultStorageFolder{EventLevelSuffix.Error}", $"{ex.ToInvariantString()}");
return null;
}
}

internal static bool IsWindowsOS()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
{
return true;
}

return false;
}
}
}