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

Update value of the resource attribute telemetry.sdk.version #4375

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

## Unreleased

* The default resource provided by `ResourceBuilder.CreateDefault()` now
adds the `telemetry.sdk.*` attributes defined in the
* The default resource provided by `ResourceBuilder.CreateDefault()` now adds
the `telemetry.sdk.*` attributes defined in the
[specification](https://github.com/open-telemetry/opentelemetry-specification/tree/12fcec1ff255b1535db75708e52a3a21f86f0fae/specification/resource/semantic_conventions#semantic-attributes-with-sdk-provided-default-value).
([#4369](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4369))
* Fixed an issue with `HashCode` computations throwing exceptions on .NET
Standard 2.1 targets.
([#4362](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4362))
* Update value of the resource attribute `telemetry.sdk.version` to show the tag
name which resembles the package version of the SDK.
([#4375](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4375))

## 1.5.0-alpha.2

Expand Down
19 changes: 16 additions & 3 deletions src/OpenTelemetry/Resources/ResourceBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static class ResourceBuilderExtensions
{
[ResourceSemanticConventions.AttributeTelemetrySdkName] = "opentelemetry",
[ResourceSemanticConventions.AttributeTelemetrySdkLanguage] = "dotnet",
[ResourceSemanticConventions.AttributeTelemetrySdkVersion] = GetFileVersion(),
[ResourceSemanticConventions.AttributeTelemetrySdkVersion] = GetAssemblyInformationalVersion(),
});

/// <summary>
Expand Down Expand Up @@ -126,11 +126,24 @@ public static ResourceBuilder AddEnvironmentVariableDetector(this ResourceBuilde
.AddDetectorInternal(sp => new OtelServiceNameEnvVarDetector(sp?.GetService<IConfiguration>() ?? configuration.Value));
}

private static string GetFileVersion()
private static string GetAssemblyInformationalVersion()
{
try
{
return typeof(Resource).Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>()?.Version ?? string.Empty;
var informationalVersion = typeof(Resource).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;

if (informationalVersion == null)
{
return string.Empty;
}

// informationalVersion could be in the following format:
// {majorVersion}.{minorVersion}.{patchVersion}.{pre-release label}.{pre-release version}.{gitHeight}.{Git SHA of current commit}
// The following parts are optional: pre-release label, pre-release version, git height, Git SHA of current commit
// for example: 1.5.0-alpha.1.40+807f703e1b4d9874a92bd86d9f2d4ebe5b5d52e4

var indexOfPlusSign = informationalVersion.IndexOf('+');
return indexOfPlusSign > 0 ? informationalVersion.Substring(0, indexOfPlusSign) : informationalVersion;
}
catch (Exception)
{
Expand Down