Fix OTLP user agent (#4167)

This commit is contained in:
Alan West 2023-02-10 16:18:08 -08:00 committed by GitHub
parent 9892311378
commit 89cc8dcb9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 6 deletions

View File

@ -42,7 +42,7 @@ namespace OpenTelemetry.Exporter
internal static readonly KeyValuePair<string, string>[] StandardHeaders = new KeyValuePair<string, string>[]
{
new KeyValuePair<string, string>("User-Agent", UserAgentProductVersion != null ? $"{UserAgentProduct}/{UserAgentProductVersion}" : UserAgentProduct),
new KeyValuePair<string, string>("User-Agent", GetUserAgentString()),
};
internal readonly Func<HttpClient> DefaultHttpClientFactory;
@ -52,8 +52,6 @@ namespace OpenTelemetry.Exporter
private const OtlpExportProtocol DefaultOtlpExportProtocol = OtlpExportProtocol.Grpc;
private const string UserAgentProduct = "OTel-OTLP-Exporter-Dotnet";
private static readonly string UserAgentProductVersion = GetAssemblyVersion();
private Uri endpoint;
/// <summary>
@ -205,16 +203,17 @@ namespace OpenTelemetry.Exporter
sp.GetRequiredService<IOptionsMonitor<BatchExportActivityProcessorOptions>>().Get(name)));
}
private static string GetAssemblyVersion()
private static string GetUserAgentString()
{
try
{
var assemblyVersion = typeof(OtlpExporterOptions).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
return assemblyVersion.InformationalVersion;
var informationalVersion = assemblyVersion.InformationalVersion;
return string.IsNullOrEmpty(informationalVersion) ? UserAgentProduct : $"{UserAgentProduct}/{informationalVersion}";
}
catch (Exception)
{
return null;
return UserAgentProduct;
}
}
}