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 24 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 @@ -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 @@ -76,3 +76,4 @@ internal AspNetCoreMetricsInstrumentationOptions(IConfiguration configuration)
/// </summary>
public AspNetCoreMetricEnrichmentFunc Enrich { get; set; }
}

51 changes: 51 additions & 0 deletions src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,57 @@
and
[metrics](https://github.com/open-telemetry/semantic-conventions/blob/2bad9afad58fbd6b33cc683d1ad1f006e35e4a5d/docs/http/http-metrics.md).

* Following metrics will now be enabled by default when targeting
`.NET8.0` or newer framework.

* **Meter** : `Microsoft.AspNetCore.Hosting`
* `http.server.request.duration`
* `http.server.active_requests`

* **Meter** : `Microsoft.AspNetCore.Server.Kestrel`
* `kestrel.active_connections`
* `kestrel.connection.duration`
* `kestrel.rejected_connections`
* `kestrel.queued_connections`
* `kestrel.queued_requests`
* `kestrel.upgraded_connections`
* `kestrel.tls_handshake.duration`
* `kestrel.active_tls_handshakes`

* **Meter** : `Microsoft.AspNetCore.Http.Connections`
* `signalr.server.connection.duration`
* `signalr.server.active_connections`

* **Meter** : `Microsoft.AspNetCore.Routing`
* `aspnetcore.routing.match_attempts`

* **Meter** : `Microsoft.AspNetCore.Diagnostics`
* `aspnetcore.diagnostics.exceptions`

* **Meter** : `Microsoft.AspNetCore.RateLimiting`
* `aspnetcore.rate_limiting.active_request_leases`
* `aspnetcore.rate_limiting.request_lease.duration`
* `aspnetcore.rate_limiting.queued_requests`
* `aspnetcore.rate_limiting.request.time_in_queue`
* `aspnetcore.rate_limiting.requests`
cijothomas marked this conversation as resolved.
Show resolved Hide resolved

**NOTES**:
* When targeting `.NET8.0` framework, `http.server.request.duration` metric
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
will only follow
[v1.22.0](https://github.com/open-telemetry/semantic-conventions/blob/v1.22.0/docs/http/http-metrics.md#metric-httpclientrequestduration)
semantic conventions specification. Ability to switch behavior to older
conventions using `OTEL_SEMCONV_STABILITY_OPT_IN` environment variable is
not available.
* Users can opt-out of metrics that are not required using
[views](https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/docs/metrics/customizing-the-sdk#drop-an-instrument).

([#4934](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4934))

* Added `network.protocol.name` dimension to `http.server.request.duration`
metric. This change only affects users setting `OTEL_SEMCONV_STABILITY_OPT_IN`
to `http` or `http/dup`.
([#4934](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4934))

## 1.5.1-beta.1

Released 2023-Jul-20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,10 @@ public void UnknownErrorProcessingEvent(string handlerName, string eventName, st
{
this.WriteEvent(5, handlerName, eventName, ex);
}

[Event(6, Message = "'{0}' is not supported for .NET8.0 and above targets", Level = EventLevel.Warning)]
public void UnsupportedOption(string optionName)
{
this.WriteEvent(6, optionName);
}
}
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,8 +14,10 @@
// limitations under the License.
// </copyright>

#if !NET8_0_OR_GREATER
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
#endif
using OpenTelemetry.Instrumentation.AspNetCore;
using OpenTelemetry.Instrumentation.AspNetCore.Implementation;
using OpenTelemetry.Internal;
Expand All @@ -34,7 +36,15 @@ 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 builder.ConfigureMeters();
#else
return AddAspNetCoreInstrumentation(builder, name: null, configureAspNetCoreInstrumentationOptions: null);
#endif
}

/// <summary>
/// Enables the incoming requests automatic data collection for ASP.NET Core.
Expand All @@ -61,6 +71,11 @@ public static MeterProviderBuilder AddAspNetCoreInstrumentation(
{
Guard.ThrowIfNull(builder);

#if NET8_0_OR_GREATER
AspNetCoreInstrumentationEventSource.Log.UnsupportedOption(nameof(AspNetCoreMetricsInstrumentationOptions));
return builder.ConfigureMeters();
#else

// Note: Warm-up the status code mapping.
_ = TelemetryHelper.BoxedStatusCodes;

Expand Down Expand Up @@ -90,5 +105,17 @@ public static MeterProviderBuilder AddAspNetCoreInstrumentation(
});

return builder;
#endif
}

internal static MeterProviderBuilder ConfigureMeters(this MeterProviderBuilder builder)
{
return builder
.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");
}
}
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
}
Loading
Loading