Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## Unreleased
- [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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ internal static IOpenTelemetryBuilder UseApplicationInsightsTelemetry(this IOpen
})
.AddAzureAppServiceDetector()
.AddAzureVMDetector()
.AddDetector(sp => new ApplicationVersionResourceDetector(sp.GetRequiredService<IOptions<ApplicationInsightsServiceOptions>>().Value.ApplicationVersion))
.AddDetector(sp => new AspNetCoreEnvironmentResourceDetector(sp.GetService<IConfiguration>()));

builder.ConfigureResource(configureResource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ internal static IOpenTelemetryBuilder UseApplicationInsightsTelemetry(this IOpen
new KeyValuePair<string, object>("telemetry.distro.version", VersionUtils.GetVersion(typeof(ApplicationInsightsExtensions))),
})
.AddAzureAppServiceDetector()
.AddAzureVMDetector();
.AddAzureVMDetector()
.AddDetector(sp => new ApplicationVersionResourceDetector(sp.GetRequiredService<IOptions<ApplicationInsightsServiceOptions>>().Value.ApplicationVersion));

builder.ConfigureResource(configureResource);

Expand Down
30 changes: 30 additions & 0 deletions NETCORE/src/Shared/ApplicationVersionResourceDetector.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Resource detector that adds the application version as a service.version resource attribute.
/// </summary>
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<string, object>(SemanticConventions.AttributeServiceVersion, this.applicationVersion)]);
}
}
}
1 change: 1 addition & 0 deletions NETCORE/src/Shared/Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)ActivityFilterProcessor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ApplicationVersionResourceDetector.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ApplicationInsightsExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ApplicationInsightsServiceOptions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Internals\ApplicationNameProvider.cs" />
Expand Down
26 changes: 26 additions & 0 deletions NETCORE/test/Shared/ConfigurationTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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()
{
Expand Down
Loading