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

[ASP.NET Core] Add support for .NET8.0 specific metrics #4934

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
48a425f
Add support for .NET8.0 specific metrics
Oct 7, 2023
2c5afab
fix build
Oct 9, 2023
e9d2292
fix test
Oct 9, 2023
85bb4cf
fix build
Oct 9, 2023
10b9a75
revert api changes
Oct 9, 2023
d48b798
add simple test
Oct 9, 2023
067b84a
Add more tests
Oct 10, 2023
244b43f
changelog
Oct 10, 2023
be07341
fix space
Oct 10, 2023
98e323d
pr feedback
Oct 11, 2023
5c206dd
Merge branch 'main' into vibankwa/add-net8-aspnetcore-metrics
vishweshbankwar Oct 12, 2023
892f5da
Merge branch 'vibankwa/add-net8-aspnetcore-metrics' of https://github…
Oct 12, 2023
43dfa49
Merge branch 'main' into vibankwa/add-net8-aspnetcore-metrics
vishweshbankwar Oct 18, 2023
2c8928c
address feedback
Oct 18, 2023
81be314
Merge branch 'main' into vibankwa/add-net8-aspnetcore-metrics
vishweshbankwar Oct 18, 2023
09546ba
Merge branch 'main' into vibankwa/add-net8-aspnetcore-metrics
vishweshbankwar Oct 19, 2023
f85de21
Update src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/As…
vishweshbankwar Oct 19, 2023
2794531
Update src/OpenTelemetry.Instrumentation.AspNetCore/MeterProviderBuil…
vishweshbankwar Oct 19, 2023
21705e4
Merge branch 'main' into vibankwa/add-net8-aspnetcore-metrics
vishweshbankwar Oct 19, 2023
5da0741
Update src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/As…
vishweshbankwar Oct 19, 2023
7459fff
Merge branch 'main' into vibankwa/add-net8-aspnetcore-metrics
vishweshbankwar Oct 19, 2023
d60250b
test
Oct 19, 2023
4f82317
Merge branch 'vibankwa/add-net8-aspnetcore-metrics' of https://github…
Oct 19, 2023
1649dbb
Merge branch 'main' into vibankwa/add-net8-aspnetcore-metrics
utpilla Oct 20, 2023
8066515
Update src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md
vishweshbankwar Oct 20, 2023
3b0e27e
add link to docs for metric details
Oct 20, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,9 @@ OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreInstrumentationOptions.Filter
OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreInstrumentationOptions.Filter.set -> void
OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreInstrumentationOptions.RecordException.get -> bool
OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreInstrumentationOptions.RecordException.set -> void
OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions
OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.AspNetCoreMetricEnrichmentFunc
OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.AspNetCoreMetricsInstrumentationOptions() -> void
OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Enrich.get -> OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.AspNetCoreMetricEnrichmentFunc
OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Enrich.set -> void
OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Filter.get -> System.Func<string, Microsoft.AspNetCore.Http.HttpContext, bool>
OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.Filter.set -> void
OpenTelemetry.Metrics.MeterProviderBuilderExtensions
OpenTelemetry.Trace.TracerProviderBuilderExtensions
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddAspNetCoreInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddAspNetCoreInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder, string name, System.Action<OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions> configureAspNetCoreInstrumentationOptions) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddAspNetCoreInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions> configureAspNetCoreInstrumentationOptions) -> OpenTelemetry.Metrics.MeterProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddAspNetCoreInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddAspNetCoreInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, string name, System.Action<OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreInstrumentationOptions> configureAspNetCoreInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddAspNetCoreInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreInstrumentationOptions> configureAspNetCoreInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder
virtual OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetricsInstrumentationOptions.AspNetCoreMetricEnrichmentFunc.Invoke(string name, Microsoft.AspNetCore.Http.HttpContext context, ref System.Diagnostics.TagList tags) -> void
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
// </copyright>

#if !NET8_0_OR_GREATER
using System.Diagnostics.Metrics;
using System.Reflection;
using OpenTelemetry.Instrumentation.AspNetCore.Implementation;
Expand Down Expand Up @@ -59,3 +60,4 @@ public void Dispose()
this.meter?.Dispose();
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
// </copyright>

#if !NET8_0_OR_GREATER
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
using System.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -76,3 +77,4 @@ internal AspNetCoreMetricsInstrumentationOptions(IConfiguration configuration)
/// </summary>
public AspNetCoreMetricEnrichmentFunc Enrich { get; set; }
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
// </copyright>

#if !NET8_0_OR_GREATER
using System.Diagnostics;
using System.Diagnostics.Metrics;
using Microsoft.AspNetCore.Http;
Expand All @@ -32,6 +33,7 @@ internal sealed class HttpInMetricsListener : ListenerHandler

private const string OnStopEvent = "Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop";
private const string EventName = "OnStopActivity";
private const string NetworkProtocolName = "http";

private readonly Meter meter;
private readonly AspNetCoreMetricsInstrumentationOptions options;
Expand Down Expand Up @@ -184,6 +186,7 @@ public void OnEventWritten_New(string name, object payload)
TagList tags = default;

// see the spec https://github.com/open-telemetry/semantic-conventions/blob/v1.21.0/docs/http/http-spans.md
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeNetworkProtocolName, NetworkProtocolName));
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeNetworkProtocolVersion, HttpTagHelper.GetFlavorTagValueFromProtocol(context.Request.Protocol)));
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeUrlScheme, context.Request.Scheme));
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpRequestMethod, context.Request.Method));
Expand Down Expand Up @@ -214,3 +217,4 @@ public void OnEventWritten_New(string name, object payload)
this.httpServerRequestDuration.Record(Activity.Current.Duration.TotalSeconds, tags);
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
// limitations under the License.
// </copyright>

#if !NET8_0_OR_GREATER
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenTelemetry.Instrumentation.AspNetCore;
using OpenTelemetry.Instrumentation.AspNetCore.Implementation;
using OpenTelemetry.Internal;
#else
using OpenTelemetry.Internal;
#endif

namespace OpenTelemetry.Metrics;

Expand All @@ -34,8 +38,23 @@ public static class MeterProviderBuilderExtensions
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
public static MeterProviderBuilder AddAspNetCoreInstrumentation(
this MeterProviderBuilder builder)
=> AddAspNetCoreInstrumentation(builder, name: null, configureAspNetCoreInstrumentationOptions: null);
{
Guard.ThrowIfNull(builder);

#if !NET8_0_OR_GREATER
return AddAspNetCoreInstrumentation(builder, name: null, configureAspNetCoreInstrumentationOptions: null);
#else
return builder
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
.AddMeter("Microsoft.AspNetCore.Hosting")
.AddMeter("Microsoft.AspNetCore.Server.Kestrel")
.AddMeter("Microsoft.AspNetCore.Http.Connections")
.AddMeter("Microsoft.AspNetCore.Routing")
.AddMeter("Microsoft.AspNetCore.Diagnostics")
.AddMeter("Microsoft.AspNetCore.RateLimiting");
#endif
}

#if !NET8_0_OR_GREATER
/// <summary>
/// Enables the incoming requests automatic data collection for ASP.NET Core.
/// </summary>
Expand Down Expand Up @@ -91,4 +110,5 @@ public static MeterProviderBuilder AddAspNetCoreInstrumentation(

return builder;
}
#endif
}
1 change: 1 addition & 0 deletions src/Shared/SemanticConventions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ internal static class SemanticConventions
public const string AttributeHttpRequestMethod = "http.request.method"; // replaces: "http.method" (AttributeHttpMethod)
public const string AttributeHttpResponseStatusCode = "http.response.status_code"; // replaces: "http.status_code" (AttributeHttpStatusCode)
public const string AttributeNetworkProtocolVersion = "network.protocol.version"; // replaces: "http.flavor" (AttributeHttpFlavor)
public const string AttributeNetworkProtocolName = "network.protocol.name";
public const string AttributeServerAddress = "server.address"; // replaces: "net.host.name" (AttributeNetHostName) and "net.peer.name" (AttributeNetPeerName)
public const string AttributeServerPort = "server.port"; // replaces: "net.host.port" (AttributeNetHostPort) and "net.peer.port" (AttributeNetPeerPort)
public const string AttributeServerSocketAddress = "server.socket.address"; // replaces: "net.peer.ip" (AttributeNetPeerIp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void ConfigureTestServices(IServiceCollection services)
Assert.True(optionsPickedFromDI);
}

#if !NET8_0_OR_GREATER
[Theory]
[InlineData(null)]
[InlineData("CustomName")]
Expand Down Expand Up @@ -95,4 +96,5 @@ void ConfigureTestServices(IServiceCollection services)

Assert.True(optionsPickedFromDI);
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@
// limitations under the License.
// </copyright>

#if !NET8_0_OR_GREATER
using System.Diagnostics;
#endif
using Microsoft.AspNetCore.Hosting;
#if !NET8_0_OR_GREATER
using Microsoft.AspNetCore.Http;
#endif
using Microsoft.AspNetCore.Mvc.Testing;
#if !NET8_0_OR_GREATER
using Microsoft.AspNetCore.TestHost;
#endif
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -51,10 +57,10 @@ public void AddAspNetCoreInstrumentation_BadArgs()
}

[Fact]
public async Task RequestMetricIsCaptured_Old()
public async Task RequestMetricIsCaptured_New()
{
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string> { [SemanticConventionOptInKeyName] = null })
.AddInMemoryCollection(new Dictionary<string, string> { [SemanticConventionOptInKeyName] = "http" })
.Build();

var metricItems = new List<Metric>();
Expand Down Expand Up @@ -87,25 +93,27 @@ public async Task RequestMetricIsCaptured_Old()
this.meterProvider.Dispose();

var requestMetrics = metricItems
.Where(item => item.Name == "http.server.duration")
.Where(item => item.Name == "http.server.request.duration")
.ToArray();

var metric = Assert.Single(requestMetrics);
Assert.Equal("ms", metric.Unit);

Assert.Equal("s", metric.Unit);
var metricPoints = GetMetricPoints(metric);
Assert.Equal(2, metricPoints.Count);

AssertMetricPoints_Old(
AssertMetricPoints_New(
metricPoints: metricPoints,
expectedRoutes: new List<string> { "api/Values", "api/Values/{id}" },
expectedTagsCount: 6);
}

#if !NET8_0_OR_GREATER
[Fact]
public async Task RequestMetricIsCaptured_New()
public async Task RequestMetricIsCaptured_Old()
{
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string> { [SemanticConventionOptInKeyName] = "http" })
.AddInMemoryCollection(new Dictionary<string, string> { [SemanticConventionOptInKeyName] = null })
.Build();

var metricItems = new List<Metric>();
Expand Down Expand Up @@ -138,19 +146,18 @@ public async Task RequestMetricIsCaptured_New()
this.meterProvider.Dispose();

var requestMetrics = metricItems
.Where(item => item.Name == "http.server.request.duration")
.Where(item => item.Name == "http.server.duration")
.ToArray();

var metric = Assert.Single(requestMetrics);

Assert.Equal("s", metric.Unit);
Assert.Equal("ms", metric.Unit);
var metricPoints = GetMetricPoints(metric);
Assert.Equal(2, metricPoints.Count);

AssertMetricPoints_New(
AssertMetricPoints_Old(
metricPoints: metricPoints,
expectedRoutes: new List<string> { "api/Values", "api/Values/{id}" },
expectedTagsCount: 5);
expectedTagsCount: 6);
}

[Fact]
Expand Down Expand Up @@ -218,7 +225,7 @@ public async Task RequestMetricIsCaptured_Dup()
AssertMetricPoints_New(
metricPoints: metricPoints,
expectedRoutes: new List<string> { "api/Values", "api/Values/{id}" },
expectedTagsCount: 5);
expectedTagsCount: 6);
}

[Fact]
Expand Down Expand Up @@ -323,6 +330,7 @@ void ConfigureTestServices(IServiceCollection services)
Assert.Contains(tagsToAdd[0], tags);
Assert.Contains(tagsToAdd[1], tags);
}
#endif

public void Dispose()
{
Expand Down
Loading