Skip to content

Commit

Permalink
[ASP.NET Core] Add support for .NET8.0 specific metrics (open-telemet…
Browse files Browse the repository at this point in the history
  • Loading branch information
vishweshbankwar authored Oct 20, 2023
1 parent 50d3af0 commit 38e21a9
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 14 deletions.
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; }
}

55 changes: 55 additions & 0 deletions src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,61 @@
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`

For details about each individual metric check [ASP.NET Core
docs
page](https://learn.microsoft.com/dotnet/core/diagnostics/built-in-metrics-aspnetcore).

**NOTES**:
* When targeting `.NET8.0` framework or newer, `http.server.request.duration` metric
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));
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

0 comments on commit 38e21a9

Please sign in to comment.