Skip to content

Commit 562f453

Browse files
[Instrumentation.GrpNetClient, Instrumentation.SqlCliet] set ActivitySource.Version to NuGet package version (#5498)
Co-authored-by: Vishwesh Bankwar <[email protected]>
1 parent 9261a67 commit 562f453

File tree

12 files changed

+63
-35
lines changed

12 files changed

+63
-35
lines changed

OpenTelemetry.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{A49299
262262
ProjectSection(SolutionItems) = preProject
263263
src\Shared\ActivityHelperExtensions.cs = src\Shared\ActivityHelperExtensions.cs
264264
src\Shared\ActivityInstrumentationHelper.cs = src\Shared\ActivityInstrumentationHelper.cs
265+
src\Shared\AssemblyVersionExtensions.cs = src\Shared\AssemblyVersionExtensions.cs
265266
src\Shared\DiagnosticDefinitions.cs = src\Shared\DiagnosticDefinitions.cs
266267
src\Shared\ExceptionExtensions.cs = src\Shared\ExceptionExtensions.cs
267268
src\Shared\Guard.cs = src\Shared\Guard.cs
@@ -305,7 +306,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shims", "Shims", "{A0CB9A10
305306
ProjectSection(SolutionItems) = preProject
306307
src\Shared\Shims\IsExternalInit.cs = src\Shared\Shims\IsExternalInit.cs
307308
src\Shared\Shims\NullableAttributes.cs = src\Shared\Shims\NullableAttributes.cs
308-
src\Shared\Shims\UnconditionalSuppressMessageAttribute.cs = src\Shared\Shims\UnconditionalSuppressMessageAttribute.cs
309309
EndProjectSection
310310
EndProject
311311
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Api.Tests", "test\OpenTelemetry.Api.Tests\OpenTelemetry.Api.Tests.csproj", "{777C04B8-1BD5-43D7-B3CD-D2189DFABCF3}"

src/OpenTelemetry.Instrumentation.GrpcNetClient/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
* `ActivitySource.Version` is set to NuGet package version.
6+
([#5498](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5498))
7+
58
## 1.8.0-beta.1
69

710
Released 2024-Apr-04

src/OpenTelemetry.Instrumentation.GrpcNetClient/Implementation/GrpcClientDiagnosticListener.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ namespace OpenTelemetry.Instrumentation.GrpcNetClient.Implementation;
1414

1515
internal sealed class GrpcClientDiagnosticListener : ListenerHandler
1616
{
17-
internal static readonly AssemblyName AssemblyName = typeof(GrpcClientDiagnosticListener).Assembly.GetName();
17+
internal static readonly Assembly Assembly = typeof(GrpcClientDiagnosticListener).Assembly;
18+
internal static readonly AssemblyName AssemblyName = Assembly.GetName();
1819
internal static readonly string ActivitySourceName = AssemblyName.Name;
19-
internal static readonly Version Version = AssemblyName.Version;
20-
internal static readonly ActivitySource ActivitySource = new(ActivitySourceName, Version.ToString());
20+
internal static readonly string Version = Assembly.GetPackageVersion();
21+
internal static readonly ActivitySource ActivitySource = new(ActivitySourceName, Version);
2122

2223
private const string OnStartEvent = "Grpc.Net.Client.GrpcOut.Start";
2324
private const string OnStopEvent = "Grpc.Net.Client.GrpcOut.Stop";

src/OpenTelemetry.Instrumentation.GrpcNetClient/OpenTelemetry.Instrumentation.GrpcNetClient.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<ItemGroup>
1515
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.Http\HttpRequestMessageContextPropagation.cs" Link="Includes\HttpRequestMessageContextPropagation.cs" />
1616
<Compile Include="$(RepoRoot)\src\Shared\Guard.cs" Link="Includes\Guard.cs" />
17+
<Compile Include="$(RepoRoot)\src\Shared\AssemblyVersionExtensions.cs" Link="Includes\AssemblyVersionExtensions.cs" />
1718
<Compile Include="$(RepoRoot)\src\Shared\Options\*.cs" Link="Includes\Options\%(Filename).cs" />
1819
<Compile Include="$(RepoRoot)\src\Shared\Shims\NullableAttributes.cs" Link="Includes\Shims\NullableAttributes.cs" />
1920
</ItemGroup>

src/OpenTelemetry.Instrumentation.SqlClient/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
* `ActivitySource.Version` is set to NuGet package version.
6+
([#5498](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5498))
7+
58
## 1.8.0-beta.1
69

710
Released 2024-Apr-04

src/OpenTelemetry.Instrumentation.SqlClient/Implementation/SqlActivitySourceHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ internal sealed class SqlActivitySourceHelper
1515
{
1616
public const string MicrosoftSqlServerDatabaseSystemName = "mssql";
1717

18-
public static readonly AssemblyName AssemblyName = typeof(SqlActivitySourceHelper).Assembly.GetName();
18+
public static readonly Assembly Assembly = typeof(SqlActivitySourceHelper).Assembly;
19+
public static readonly AssemblyName AssemblyName = Assembly.GetName();
1920
public static readonly string ActivitySourceName = AssemblyName.Name;
20-
public static readonly Version Version = AssemblyName.Version;
21-
public static readonly ActivitySource ActivitySource = new(ActivitySourceName, Version.ToString());
21+
public static readonly ActivitySource ActivitySource = new(ActivitySourceName, Assembly.GetPackageVersion());
2222
public static readonly string ActivityName = ActivitySourceName + ".Execute";
2323

2424
public static readonly IEnumerable<KeyValuePair<string, object>> CreationTags = new[]

src/OpenTelemetry.Instrumentation.SqlClient/OpenTelemetry.Instrumentation.SqlClient.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
<ItemGroup>
1515
<Compile Include="$(RepoRoot)\src\Shared\Guard.cs" Link="Includes\Guard.cs" />
16+
<Compile Include="$(RepoRoot)\src\Shared\AssemblyVersionExtensions.cs" Link="Includes\AssemblyVersionExtensions.cs" />
1617
<Compile Include="$(RepoRoot)\src\Shared\Shims\NullableAttributes.cs" Link="Includes\Shims\NullableAttributes.cs" />
1718
</ItemGroup>
1819

src/OpenTelemetry/OpenTelemetry.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
</ItemGroup>
1717

1818
<ItemGroup>
19+
<Compile Include="$(RepoRoot)\src\Shared\AssemblyVersionExtensions.cs" Link="Includes\AssemblyVersionExtensions.cs" />
1920
<Compile Include="$(RepoRoot)\src\Shared\Configuration\*.cs" Link="Includes\Configuration\%(Filename).cs" />
2021
<Compile Include="$(RepoRoot)\src\Shared\EnvironmentVariables\*.cs" Link="Includes\EnvironmentVariables\%(Filename).cs" />
2122
<Compile Include="$(RepoRoot)\src\Shared\MathHelper.cs" Link="Includes\MathHelper.cs" />

src/OpenTelemetry/Sdk.cs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#if EXPOSE_EXPERIMENTAL_FEATURES && NET8_0_OR_GREATER
66
using System.Diagnostics.CodeAnalysis;
77
#endif
8-
using System.Reflection;
98
using OpenTelemetry.Context.Propagation;
9+
using OpenTelemetry.Instrumentation;
1010
using OpenTelemetry.Internal;
1111
using OpenTelemetry.Logs;
1212
using OpenTelemetry.Metrics;
@@ -31,8 +31,8 @@ static Sdk()
3131
Activity.ForceDefaultIdFormat = true;
3232
SelfDiagnostics.EnsureInitialized();
3333

34-
var assemblyInformationalVersion = typeof(Sdk).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
35-
InformationalVersion = ParseAssemblyInformationalVersion(assemblyInformationalVersion);
34+
var sdkAssembly = typeof(Sdk).Assembly;
35+
InformationalVersion = sdkAssembly.GetPackageVersion();
3636
}
3737

3838
/// <summary>
@@ -110,24 +110,4 @@ static LoggerProviderBuilder CreateLoggerProviderBuilder()
110110
{
111111
return new LoggerProviderBuilderBase();
112112
}
113-
114-
internal static string ParseAssemblyInformationalVersion(string? informationalVersion)
115-
{
116-
if (string.IsNullOrWhiteSpace(informationalVersion))
117-
{
118-
informationalVersion = "1.0.0";
119-
}
120-
121-
/*
122-
* InformationalVersion will be in the following format:
123-
* {majorVersion}.{minorVersion}.{patchVersion}.{pre-release label}.{pre-release version}.{gitHeight}+{Git SHA of current commit}
124-
* Ex: 1.5.0-alpha.1.40+807f703e1b4d9874a92bd86d9f2d4ebe5b5d52e4
125-
* The following parts are optional: pre-release label, pre-release version, git height, Git SHA of current commit
126-
*/
127-
128-
var indexOfPlusSign = informationalVersion!.IndexOf('+');
129-
return indexOfPlusSign > 0
130-
? informationalVersion.Substring(0, indexOfPlusSign)
131-
: informationalVersion;
132-
}
133113
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#nullable enable
5+
6+
using System.Reflection;
7+
8+
namespace OpenTelemetry.Instrumentation;
9+
10+
internal static class AssemblyVersionExtensions
11+
{
12+
public static string GetPackageVersion(this Assembly assembly)
13+
{
14+
// MinVer https://github.com/adamralph/minver?tab=readme-ov-file#version-numbers
15+
// together with Microsoft.SourceLink.GitHub https://github.com/dotnet/sourcelink
16+
// fills AssemblyInformationalVersionAttribute by
17+
// {majorVersion}.{minorVersion}.{patchVersion}.{pre-release label}.{pre-release version}.{gitHeight}+{Git SHA of current commit}
18+
// Ex: 1.5.0-alpha.1.40+807f703e1b4d9874a92bd86d9f2d4ebe5b5d52e4
19+
// The following parts are optional: pre-release label, pre-release version, git height, Git SHA of current commit
20+
// For package version, value of AssemblyInformationalVersionAttribute without commit hash is returned.
21+
22+
var informationalVersion = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()!.InformationalVersion;
23+
var indexOfPlusSign = informationalVersion!.IndexOf('+');
24+
return indexOfPlusSign > 0
25+
? informationalVersion.Substring(0, indexOfPlusSign)
26+
: informationalVersion;
27+
}
28+
}

test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.client.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ public void Grpc_BadArgs()
435435
private static void ValidateGrpcActivity(Activity activityToValidate)
436436
{
437437
Assert.Equal(GrpcClientDiagnosticListener.ActivitySourceName, activityToValidate.Source.Name);
438-
Assert.Equal(GrpcClientDiagnosticListener.Version.ToString(), activityToValidate.Source.Version);
438+
Assert.Equal(GrpcClientDiagnosticListener.Version, activityToValidate.Source.Version);
439439
Assert.Equal(ActivityKind.Client, activityToValidate.Kind);
440440
}
441441
}

test/OpenTelemetry.Tests/SdkTests.cs renamed to test/OpenTelemetry.Tests/Shared/AssemblyVersionExtensionsTests.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33

44
#nullable enable
55

6+
using System.Reflection;
7+
using OpenTelemetry.Instrumentation;
68
using Xunit;
79

810
namespace OpenTelemetry.Tests;
911

10-
public class SdkTests
12+
public class AssemblyVersionExtensionsTests
1113
{
1214
[Theory]
13-
[InlineData(null, "1.0.0")]
1415
[InlineData("1.5.0", "1.5.0")]
1516
[InlineData("1.0.0.0", "1.0.0.0")]
1617
[InlineData("1.0-beta.1", "1.0-beta.1")]
@@ -19,10 +20,19 @@ public class SdkTests
1920
[InlineData("8.0", "8.0")]
2021
[InlineData("8", "8")]
2122
[InlineData("8.0.1.18-alpha1", "8.0.1.18-alpha1")]
22-
public void ParseAssemblyInformationalVersionTests(string? informationalVersion, string expectedVersion)
23+
public void ParseAssemblyInformationalVersionTests(string informationalVersion, string expectedVersion)
2324
{
24-
var actualVersion = Sdk.ParseAssemblyInformationalVersion(informationalVersion);
25+
var assembly = new TestAssembly(informationalVersion);
26+
var actualVersion = assembly.GetPackageVersion();
2527

2628
Assert.Equal(expectedVersion, actualVersion);
2729
}
30+
31+
private class TestAssembly(string informationalVersion) : Assembly
32+
{
33+
public override object[] GetCustomAttributes(Type attributeType, bool inherit)
34+
{
35+
return new Attribute[] { new AssemblyInformationalVersionAttribute(informationalVersion) };
36+
}
37+
}
2838
}

0 commit comments

Comments
 (0)