diff --git a/OpenTelemetry.sln b/OpenTelemetry.sln
index c9a9a4c3f6f..b941bc96ac5 100644
--- a/OpenTelemetry.sln
+++ b/OpenTelemetry.sln
@@ -262,6 +262,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{A49299
ProjectSection(SolutionItems) = preProject
src\Shared\ActivityHelperExtensions.cs = src\Shared\ActivityHelperExtensions.cs
src\Shared\ActivityInstrumentationHelper.cs = src\Shared\ActivityInstrumentationHelper.cs
+ src\Shared\AssemblyVersionExtensions.cs = src\Shared\AssemblyVersionExtensions.cs
src\Shared\DiagnosticDefinitions.cs = src\Shared\DiagnosticDefinitions.cs
src\Shared\ExceptionExtensions.cs = src\Shared\ExceptionExtensions.cs
src\Shared\Guard.cs = src\Shared\Guard.cs
@@ -305,7 +306,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shims", "Shims", "{A0CB9A10
ProjectSection(SolutionItems) = preProject
src\Shared\Shims\IsExternalInit.cs = src\Shared\Shims\IsExternalInit.cs
src\Shared\Shims\NullableAttributes.cs = src\Shared\Shims\NullableAttributes.cs
- src\Shared\Shims\UnconditionalSuppressMessageAttribute.cs = src\Shared\Shims\UnconditionalSuppressMessageAttribute.cs
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Api.Tests", "test\OpenTelemetry.Api.Tests\OpenTelemetry.Api.Tests.csproj", "{777C04B8-1BD5-43D7-B3CD-D2189DFABCF3}"
diff --git a/src/OpenTelemetry.Instrumentation.GrpcNetClient/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.GrpcNetClient/CHANGELOG.md
index 8eaa0868d8f..4402fa68909 100644
--- a/src/OpenTelemetry.Instrumentation.GrpcNetClient/CHANGELOG.md
+++ b/src/OpenTelemetry.Instrumentation.GrpcNetClient/CHANGELOG.md
@@ -2,6 +2,9 @@
## Unreleased
+* `ActivitySource.Version` is set to NuGet package version.
+ ([#5498](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5498))
+
## 1.8.0-beta.1
Released 2024-Apr-04
diff --git a/src/OpenTelemetry.Instrumentation.GrpcNetClient/Implementation/GrpcClientDiagnosticListener.cs b/src/OpenTelemetry.Instrumentation.GrpcNetClient/Implementation/GrpcClientDiagnosticListener.cs
index 0804f57363b..48d4fa81115 100644
--- a/src/OpenTelemetry.Instrumentation.GrpcNetClient/Implementation/GrpcClientDiagnosticListener.cs
+++ b/src/OpenTelemetry.Instrumentation.GrpcNetClient/Implementation/GrpcClientDiagnosticListener.cs
@@ -14,10 +14,11 @@ namespace OpenTelemetry.Instrumentation.GrpcNetClient.Implementation;
internal sealed class GrpcClientDiagnosticListener : ListenerHandler
{
- internal static readonly AssemblyName AssemblyName = typeof(GrpcClientDiagnosticListener).Assembly.GetName();
+ internal static readonly Assembly Assembly = typeof(GrpcClientDiagnosticListener).Assembly;
+ internal static readonly AssemblyName AssemblyName = Assembly.GetName();
internal static readonly string ActivitySourceName = AssemblyName.Name;
- internal static readonly Version Version = AssemblyName.Version;
- internal static readonly ActivitySource ActivitySource = new(ActivitySourceName, Version.ToString());
+ internal static readonly string Version = Assembly.GetPackageVersion();
+ internal static readonly ActivitySource ActivitySource = new(ActivitySourceName, Version);
private const string OnStartEvent = "Grpc.Net.Client.GrpcOut.Start";
private const string OnStopEvent = "Grpc.Net.Client.GrpcOut.Stop";
diff --git a/src/OpenTelemetry.Instrumentation.GrpcNetClient/OpenTelemetry.Instrumentation.GrpcNetClient.csproj b/src/OpenTelemetry.Instrumentation.GrpcNetClient/OpenTelemetry.Instrumentation.GrpcNetClient.csproj
index 48d9c8e5e65..8aa01b510bf 100644
--- a/src/OpenTelemetry.Instrumentation.GrpcNetClient/OpenTelemetry.Instrumentation.GrpcNetClient.csproj
+++ b/src/OpenTelemetry.Instrumentation.GrpcNetClient/OpenTelemetry.Instrumentation.GrpcNetClient.csproj
@@ -14,6 +14,7 @@
+
diff --git a/src/OpenTelemetry.Instrumentation.SqlClient/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.SqlClient/CHANGELOG.md
index dd2ed6d52dd..18af172d395 100644
--- a/src/OpenTelemetry.Instrumentation.SqlClient/CHANGELOG.md
+++ b/src/OpenTelemetry.Instrumentation.SqlClient/CHANGELOG.md
@@ -2,6 +2,9 @@
## Unreleased
+* `ActivitySource.Version` is set to NuGet package version.
+ ([#5498](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5498))
+
## 1.8.0-beta.1
Released 2024-Apr-04
diff --git a/src/OpenTelemetry.Instrumentation.SqlClient/Implementation/SqlActivitySourceHelper.cs b/src/OpenTelemetry.Instrumentation.SqlClient/Implementation/SqlActivitySourceHelper.cs
index 287ee0d6dc0..3856c2d4e0c 100644
--- a/src/OpenTelemetry.Instrumentation.SqlClient/Implementation/SqlActivitySourceHelper.cs
+++ b/src/OpenTelemetry.Instrumentation.SqlClient/Implementation/SqlActivitySourceHelper.cs
@@ -15,10 +15,10 @@ internal sealed class SqlActivitySourceHelper
{
public const string MicrosoftSqlServerDatabaseSystemName = "mssql";
- public static readonly AssemblyName AssemblyName = typeof(SqlActivitySourceHelper).Assembly.GetName();
+ public static readonly Assembly Assembly = typeof(SqlActivitySourceHelper).Assembly;
+ public static readonly AssemblyName AssemblyName = Assembly.GetName();
public static readonly string ActivitySourceName = AssemblyName.Name;
- public static readonly Version Version = AssemblyName.Version;
- public static readonly ActivitySource ActivitySource = new(ActivitySourceName, Version.ToString());
+ public static readonly ActivitySource ActivitySource = new(ActivitySourceName, Assembly.GetPackageVersion());
public static readonly string ActivityName = ActivitySourceName + ".Execute";
public static readonly IEnumerable> CreationTags = new[]
diff --git a/src/OpenTelemetry.Instrumentation.SqlClient/OpenTelemetry.Instrumentation.SqlClient.csproj b/src/OpenTelemetry.Instrumentation.SqlClient/OpenTelemetry.Instrumentation.SqlClient.csproj
index 09d8a5782f6..c62aa3a0800 100644
--- a/src/OpenTelemetry.Instrumentation.SqlClient/OpenTelemetry.Instrumentation.SqlClient.csproj
+++ b/src/OpenTelemetry.Instrumentation.SqlClient/OpenTelemetry.Instrumentation.SqlClient.csproj
@@ -13,6 +13,7 @@
+
diff --git a/src/OpenTelemetry/OpenTelemetry.csproj b/src/OpenTelemetry/OpenTelemetry.csproj
index a333550e49b..71e3b242cd0 100644
--- a/src/OpenTelemetry/OpenTelemetry.csproj
+++ b/src/OpenTelemetry/OpenTelemetry.csproj
@@ -16,6 +16,7 @@
+
diff --git a/src/OpenTelemetry/Sdk.cs b/src/OpenTelemetry/Sdk.cs
index cda073776ef..fb3bc6403db 100644
--- a/src/OpenTelemetry/Sdk.cs
+++ b/src/OpenTelemetry/Sdk.cs
@@ -5,8 +5,8 @@
#if EXPOSE_EXPERIMENTAL_FEATURES && NET8_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
-using System.Reflection;
using OpenTelemetry.Context.Propagation;
+using OpenTelemetry.Instrumentation;
using OpenTelemetry.Internal;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
@@ -31,8 +31,8 @@ static Sdk()
Activity.ForceDefaultIdFormat = true;
SelfDiagnostics.EnsureInitialized();
- var assemblyInformationalVersion = typeof(Sdk).Assembly.GetCustomAttribute()?.InformationalVersion;
- InformationalVersion = ParseAssemblyInformationalVersion(assemblyInformationalVersion);
+ var sdkAssembly = typeof(Sdk).Assembly;
+ InformationalVersion = sdkAssembly.GetPackageVersion();
}
///
@@ -110,24 +110,4 @@ static LoggerProviderBuilder CreateLoggerProviderBuilder()
{
return new LoggerProviderBuilderBase();
}
-
- internal static string ParseAssemblyInformationalVersion(string? informationalVersion)
- {
- if (string.IsNullOrWhiteSpace(informationalVersion))
- {
- informationalVersion = "1.0.0";
- }
-
- /*
- * InformationalVersion will be in the following format:
- * {majorVersion}.{minorVersion}.{patchVersion}.{pre-release label}.{pre-release version}.{gitHeight}+{Git SHA of current commit}
- * Ex: 1.5.0-alpha.1.40+807f703e1b4d9874a92bd86d9f2d4ebe5b5d52e4
- * The following parts are optional: pre-release label, pre-release version, git height, Git SHA of current commit
- */
-
- var indexOfPlusSign = informationalVersion!.IndexOf('+');
- return indexOfPlusSign > 0
- ? informationalVersion.Substring(0, indexOfPlusSign)
- : informationalVersion;
- }
}
diff --git a/src/Shared/AssemblyVersionExtensions.cs b/src/Shared/AssemblyVersionExtensions.cs
new file mode 100644
index 00000000000..687dabf45f2
--- /dev/null
+++ b/src/Shared/AssemblyVersionExtensions.cs
@@ -0,0 +1,28 @@
+// Copyright The OpenTelemetry Authors
+// SPDX-License-Identifier: Apache-2.0
+
+#nullable enable
+
+using System.Reflection;
+
+namespace OpenTelemetry.Instrumentation;
+
+internal static class AssemblyVersionExtensions
+{
+ public static string GetPackageVersion(this Assembly assembly)
+ {
+ // MinVer https://github.com/adamralph/minver?tab=readme-ov-file#version-numbers
+ // together with Microsoft.SourceLink.GitHub https://github.com/dotnet/sourcelink
+ // fills AssemblyInformationalVersionAttribute by
+ // {majorVersion}.{minorVersion}.{patchVersion}.{pre-release label}.{pre-release version}.{gitHeight}+{Git SHA of current commit}
+ // Ex: 1.5.0-alpha.1.40+807f703e1b4d9874a92bd86d9f2d4ebe5b5d52e4
+ // The following parts are optional: pre-release label, pre-release version, git height, Git SHA of current commit
+ // For package version, value of AssemblyInformationalVersionAttribute without commit hash is returned.
+
+ var informationalVersion = assembly.GetCustomAttribute()!.InformationalVersion;
+ var indexOfPlusSign = informationalVersion!.IndexOf('+');
+ return indexOfPlusSign > 0
+ ? informationalVersion.Substring(0, indexOfPlusSign)
+ : informationalVersion;
+ }
+}
diff --git a/test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.client.cs b/test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.client.cs
index 3799ef73a7f..f06eb5a6fcc 100644
--- a/test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.client.cs
+++ b/test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.client.cs
@@ -435,7 +435,7 @@ public void Grpc_BadArgs()
private static void ValidateGrpcActivity(Activity activityToValidate)
{
Assert.Equal(GrpcClientDiagnosticListener.ActivitySourceName, activityToValidate.Source.Name);
- Assert.Equal(GrpcClientDiagnosticListener.Version.ToString(), activityToValidate.Source.Version);
+ Assert.Equal(GrpcClientDiagnosticListener.Version, activityToValidate.Source.Version);
Assert.Equal(ActivityKind.Client, activityToValidate.Kind);
}
}
diff --git a/test/OpenTelemetry.Tests/SdkTests.cs b/test/OpenTelemetry.Tests/Shared/AssemblyVersionExtensionsTests.cs
similarity index 50%
rename from test/OpenTelemetry.Tests/SdkTests.cs
rename to test/OpenTelemetry.Tests/Shared/AssemblyVersionExtensionsTests.cs
index f4d4908fc4b..3e8c43cee75 100644
--- a/test/OpenTelemetry.Tests/SdkTests.cs
+++ b/test/OpenTelemetry.Tests/Shared/AssemblyVersionExtensionsTests.cs
@@ -3,14 +3,15 @@
#nullable enable
+using System.Reflection;
+using OpenTelemetry.Instrumentation;
using Xunit;
namespace OpenTelemetry.Tests;
-public class SdkTests
+public class AssemblyVersionExtensionsTests
{
[Theory]
- [InlineData(null, "1.0.0")]
[InlineData("1.5.0", "1.5.0")]
[InlineData("1.0.0.0", "1.0.0.0")]
[InlineData("1.0-beta.1", "1.0-beta.1")]
@@ -19,10 +20,19 @@ public class SdkTests
[InlineData("8.0", "8.0")]
[InlineData("8", "8")]
[InlineData("8.0.1.18-alpha1", "8.0.1.18-alpha1")]
- public void ParseAssemblyInformationalVersionTests(string? informationalVersion, string expectedVersion)
+ public void ParseAssemblyInformationalVersionTests(string informationalVersion, string expectedVersion)
{
- var actualVersion = Sdk.ParseAssemblyInformationalVersion(informationalVersion);
+ var assembly = new TestAssembly(informationalVersion);
+ var actualVersion = assembly.GetPackageVersion();
Assert.Equal(expectedVersion, actualVersion);
}
+
+ private class TestAssembly(string informationalVersion) : Assembly
+ {
+ public override object[] GetCustomAttributes(Type attributeType, bool inherit)
+ {
+ return new Attribute[] { new AssemblyInformationalVersionAttribute(informationalVersion) };
+ }
+ }
}