diff --git a/CHANGELOG.md b/CHANGELOG.md index b592d266c..b688ef8c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - [Fix bug where Debug/Trace level logs from TrackTrace API were not emitted to Application Insights](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3121/changes) - [Fix Track API calls to not mutate the passed in dictionary if it is readonly](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3119) - [Fix `NullReferenceException` in `TelemetryClient.Flush()` and `FlushAsync()` when called from DI scenarios](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3125) +- [Fix `ApplicationVersion` ignored: `ApplicationInsightsServiceOptions.ApplicationVersion` is now applied as `service.version` on the OpenTelemetry Resource for both AspNetCore and WorkerService packages](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3124) ## Version 3.0.0 - [Replaced `netstandard2.0` with `net8.0` target framework in `Microsoft.ApplicationInsights.NLogTarget` package.](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3102) diff --git a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs index cf68a1026..def7a8e63 100644 --- a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs +++ b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs @@ -133,6 +133,7 @@ internal static IOpenTelemetryBuilder UseApplicationInsightsTelemetry(this IOpen }) .AddAzureAppServiceDetector() .AddAzureVMDetector() + .AddDetector(sp => new ApplicationVersionResourceDetector(sp.GetRequiredService>().Value.ApplicationVersion)) .AddDetector(sp => new AspNetCoreEnvironmentResourceDetector(sp.GetService())); builder.ConfigureResource(configureResource); diff --git a/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs b/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs index 106002ee3..805c95b14 100644 --- a/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs +++ b/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs @@ -129,7 +129,8 @@ internal static IOpenTelemetryBuilder UseApplicationInsightsTelemetry(this IOpen new KeyValuePair("telemetry.distro.version", VersionUtils.GetVersion(typeof(ApplicationInsightsExtensions))), }) .AddAzureAppServiceDetector() - .AddAzureVMDetector(); + .AddAzureVMDetector() + .AddDetector(sp => new ApplicationVersionResourceDetector(sp.GetRequiredService>().Value.ApplicationVersion)); builder.ConfigureResource(configureResource); diff --git a/NETCORE/src/Shared/ApplicationVersionResourceDetector.cs b/NETCORE/src/Shared/ApplicationVersionResourceDetector.cs new file mode 100644 index 000000000..221fd092f --- /dev/null +++ b/NETCORE/src/Shared/ApplicationVersionResourceDetector.cs @@ -0,0 +1,30 @@ +#if AI_ASPNETCORE_WEB +namespace Microsoft.ApplicationInsights.AspNetCore +#else +namespace Microsoft.ApplicationInsights.WorkerService +#endif +{ + using System.Collections.Generic; + using Microsoft.ApplicationInsights.Internal; + using OpenTelemetry.Resources; + + /// + /// Resource detector that adds the application version as a service.version resource attribute. + /// + internal class ApplicationVersionResourceDetector : IResourceDetector + { + private readonly string applicationVersion; + + public ApplicationVersionResourceDetector(string applicationVersion) + { + this.applicationVersion = applicationVersion; + } + + public Resource Detect() + { + return string.IsNullOrWhiteSpace(this.applicationVersion) + ? Resource.Empty + : new Resource([new KeyValuePair(SemanticConventions.AttributeServiceVersion, this.applicationVersion)]); + } + } +} diff --git a/NETCORE/src/Shared/Shared.projitems b/NETCORE/src/Shared/Shared.projitems index 789859897..71ae50527 100644 --- a/NETCORE/src/Shared/Shared.projitems +++ b/NETCORE/src/Shared/Shared.projitems @@ -10,6 +10,7 @@ + diff --git a/NETCORE/test/Shared/ConfigurationTests.cs b/NETCORE/test/Shared/ConfigurationTests.cs index 7e44ced7e..99f675a54 100644 --- a/NETCORE/test/Shared/ConfigurationTests.cs +++ b/NETCORE/test/Shared/ConfigurationTests.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Linq; using Azure.Monitor.OpenTelemetry.Exporter; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -100,6 +101,31 @@ public void ReadsApplicationVersionFromApplicationInsightsSectionInConfig() Assert.Equal("1.0.0", options.ApplicationVersion); } + [Theory] + [InlineData("2.5.0", "2.5.0")] + [InlineData(null, null)] + [InlineData("", null)] + [InlineData(" ", null)] + public void ApplicationVersionResourceDetectorReturnsExpectedResource(string inputVersion, string expectedVersion) + { + // ARRANGE & ACT + var detector = new ApplicationVersionResourceDetector(inputVersion); + var resource = detector.Detect(); + + // VALIDATE + if (expectedVersion == null) + { + Assert.Equal(OpenTelemetry.Resources.Resource.Empty, resource); + } + else + { + Assert.NotEqual(OpenTelemetry.Resources.Resource.Empty, resource); + var attributes = resource.Attributes.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); + Assert.True(attributes.ContainsKey("service.version")); + Assert.Equal(expectedVersion, attributes["service.version"]); + } + } + [Fact] public void ConfigurationFlowsFromApplicationInsightsSectionToAzureMonitorExporter() {