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

Azure Monitor - refactor sdkversion #14774

Merged
merged 1 commit into from
Sep 1, 2020
Merged
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 @@ -8,53 +8,36 @@

namespace OpenTelemetry.Exporter.AzureMonitor
{
internal class SdkVersionUtils
internal static class SdkVersionUtils
{
private static string sdkVersion;

internal static string SdkVersion {
get
{
return sdkVersion ??= GetSdkVersion();
}
}
internal static string SdkVersion = GetSdkVersion();

private static string GetSdkVersion()
{
string sdkVer = null;

try
{
Version dotnetSdkVersion = GetVersion(typeof(object));
Version otelSdkVersion = GetVersion(typeof(OpenTelemetry.Sdk));
Version extensionVersion = GetVersion(typeof(OpenTelemetry.Exporter.AzureMonitor.AzureMonitorTraceExporter));

sdkVer = string.Format(CultureInfo.InvariantCulture, $"dotnet{dotnetSdkVersion.ToString(2)}:otel{otelSdkVersion.ToString(3)}:ext{extensionVersion.ToString(3)}");
return string.Format(CultureInfo.InvariantCulture, $"dotnet{dotnetSdkVersion.ToString(2)}:otel{otelSdkVersion.ToString(3)}:ext{extensionVersion.ToString(3)}");
}
catch (Exception ex)
{
AzureMonitorTraceExporterEventSource.Log.SdkVersionCreateFailed(ex);
return null;
}

return sdkVer;
}

private static Version GetVersion(Type type)
{
string versionString = null;

try
{
// TODO: Distinguish preview/stable release and minor versions. e.g: 5.0.0-preview.8.20365.13
versionString = type.Assembly.GetCustomAttributes(false)
.OfType<AssemblyFileVersionAttribute>()
.First()
.Version;
}
catch (Exception ex)
{
AzureMonitorTraceExporterEventSource.Log.SdkVersionCreateFailed(ex);
}
// TODO: Distinguish preview/stable release and minor versions. e.g: 5.0.0-preview.8.20365.13
string versionString = type
.Assembly
.GetCustomAttributes(false)
.OfType<AssemblyFileVersionAttribute>()
.First()
.Version;

// Return zeros rather then failing if the version string fails to parse
return Version.TryParse(versionString, out var version) ? version : new Version();
Expand Down