AspNetCore Metrics instrumentation for .NET8 (#3130)
This commit is contained in:
parent
e24efe00e9
commit
5067bc6bd5
|
@ -96,6 +96,18 @@ internal static class EnvironmentConfigurationMetricHelper
|
|||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public static MeterProviderBuilder AddAspNetCoreInstrumentation(MeterProviderBuilder builder, LazyInstrumentationLoader lazyInstrumentationLoader)
|
||||
{
|
||||
if (DotNetVersionHelper.DotNetMajorVersion >= 8)
|
||||
{
|
||||
// AspNetCore has build in support for metrics in .NET8. Executing OpenTelemetry.Instrumentation.AspNetCore in this case leads to duplicated metrics.
|
||||
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");
|
||||
}
|
||||
|
||||
DelayedInitialization.Metrics.AddAspNetCore(lazyInstrumentationLoader);
|
||||
return builder.AddMeter("OpenTelemetry.Instrumentation.AspNetCore");
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
<PackageVersion Include="Microsoft.AspNetCore" Version="2.2.0" />
|
||||
<PackageVersion Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
|
||||
<PackageVersion Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
|
||||
<PackageVersion Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.0" />
|
||||
<PackageVersion Include="Microsoft.Build" Version="15.9.20" />
|
||||
<PackageVersion Include="Microsoft.Build.Utilities.Core" Version="15.9.20" />
|
||||
<PackageVersion Include="Microsoft.Data.SqlClient" Version="5.1.2" />
|
||||
|
|
|
@ -68,7 +68,9 @@ public class GraphQLTests : TestHelper
|
|||
|
||||
SetEnvironmentVariable("OTEL_DOTNET_AUTO_GRAPHQL_SET_DOCUMENT", setDocument.ToString());
|
||||
SetEnvironmentVariable("OTEL_DOTNET_AUTO_TRACES_INSTRUMENTATION_ENABLED", "false");
|
||||
SetEnvironmentVariable("OTEL_DOTNET_AUTO_METRICS_INSTRUMENTATION_ENABLED", "false"); // disable metrics to disable side effect of AspNetCore - working propagation on .NET6 and .NET7
|
||||
SetEnvironmentVariable("OTEL_DOTNET_AUTO_TRACES_GRAPHQL_INSTRUMENTATION_ENABLED", "true");
|
||||
SetEnvironmentVariable("OTEL_DOTNET_AUTO_TRACES_ASPNETCORE_INSTRUMENTATION_ENABLED", "true"); // AspNetCore Instrumentation enables propagation used in this test
|
||||
SetEnvironmentVariable("OTEL_TRACES_SAMPLER", "always_on");
|
||||
SetEnvironmentVariable("OTEL_DOTNET_AUTO_NETFX_REDIRECT_ENABLED", "false");
|
||||
|
||||
|
@ -110,7 +112,7 @@ public class GraphQLTests : TestHelper
|
|||
private static byte[] GetTraceIdBytes(byte id)
|
||||
{
|
||||
var traceId = new byte[16];
|
||||
traceId[traceId.Length - 1] = id;
|
||||
traceId[^1] = id;
|
||||
|
||||
return traceId;
|
||||
}
|
||||
|
@ -120,27 +122,11 @@ public class GraphQLTests : TestHelper
|
|||
return id.ToString("x32");
|
||||
}
|
||||
|
||||
private static bool VerifyNotImplementedException(Span span)
|
||||
{
|
||||
var exceptionEvent = span.Events.SingleOrDefault();
|
||||
|
||||
if (exceptionEvent == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return
|
||||
exceptionEvent.Attributes.Any(x => x.Key == "exception.type" && x.Value?.StringValue == "System.NotImplementedException") &&
|
||||
exceptionEvent.Attributes.Any(x => x.Key == "exception.message") &&
|
||||
exceptionEvent.Attributes.Any(x => x.Key == "exception.stacktrace");
|
||||
}
|
||||
|
||||
private void Expect(
|
||||
MockSpansCollector collector,
|
||||
string spanName,
|
||||
byte id,
|
||||
bool setDocument,
|
||||
Predicate<Span>? verifyFailure = null)
|
||||
bool setDocument)
|
||||
{
|
||||
var traceIdBytes = GetTraceIdBytes(id);
|
||||
|
||||
|
@ -152,11 +138,6 @@ public class GraphQLTests : TestHelper
|
|||
return false;
|
||||
}
|
||||
|
||||
if (verifyFailure != null)
|
||||
{
|
||||
return verifyFailure(span);
|
||||
}
|
||||
|
||||
if (setDocument && !span.Attributes.Any(attr => attr.Key == "graphql.document" && !string.IsNullOrWhiteSpace(attr.Value?.StringValue)))
|
||||
{
|
||||
return false;
|
||||
|
|
|
@ -82,12 +82,18 @@ public class HttpTests : TestHelper
|
|||
{
|
||||
using var collector = new MockMetricsCollector(Output);
|
||||
SetExporter(collector);
|
||||
collector.Expect("OpenTelemetry.Instrumentation.AspNetCore");
|
||||
#if NET8_0_OR_GREATER
|
||||
collector.Expect("System.Net.Http");
|
||||
collector.Expect("System.Net.NameResolution");
|
||||
collector.ExpectAdditionalEntries(x => x.All(m => m.InstrumentationScopeName != "OpenTelemetry.Instrumentation.Http"));
|
||||
collector.Expect("Microsoft.AspNetCore.Hosting");
|
||||
collector.Expect("Microsoft.AspNetCore.Server.Kestrel");
|
||||
collector.Expect("Microsoft.AspNetCore.Http.Connections");
|
||||
collector.Expect("Microsoft.AspNetCore.Routing");
|
||||
collector.Expect("Microsoft.AspNetCore.Diagnostics");
|
||||
collector.Expect("Microsoft.AspNetCore.RateLimiting");
|
||||
collector.ExpectAdditionalEntries(x => x.All(m => m.InstrumentationScopeName != "OpenTelemetry.Instrumentation.AspNetCore" && m.InstrumentationScopeName != "OpenTelemetry.Instrumentation.Http"));
|
||||
#else
|
||||
collector.Expect("OpenTelemetry.Instrumentation.AspNetCore");
|
||||
collector.Expect("OpenTelemetry.Instrumentation.Http");
|
||||
#endif
|
||||
|
||||
|
|
|
@ -17,10 +17,8 @@
|
|||
#if NET6_0_OR_GREATER
|
||||
|
||||
using IntegrationTests.Helpers;
|
||||
using OpenTelemetry.AutoInstrumentation;
|
||||
using OpenTelemetry.Proto.Logs.V1;
|
||||
using Xunit.Abstractions;
|
||||
using Xunit.Sdk;
|
||||
|
||||
namespace IntegrationTests;
|
||||
|
||||
|
|
|
@ -16,8 +16,14 @@
|
|||
|
||||
using System.Diagnostics;
|
||||
using System.Net.Http;
|
||||
#if NET8_0_OR_GREATER
|
||||
using System.Threading.RateLimiting;
|
||||
#endif
|
||||
using Microsoft.AspNetCore.Hosting.Server;
|
||||
using Microsoft.AspNetCore.Hosting.Server.Features;
|
||||
#if NET8_0_OR_GREATER
|
||||
using Microsoft.AspNetCore.SignalR.Client;
|
||||
#endif
|
||||
|
||||
namespace TestApplication.Http;
|
||||
|
||||
|
@ -40,10 +46,23 @@ public class Program
|
|||
var dnsAddress = address?.Replace("127.0.0.1", "localhost"); // needed to force DNS resolution to test metrics
|
||||
using var httpClient = new HttpClient();
|
||||
httpClient.GetAsync($"{dnsAddress}/test").Wait();
|
||||
httpClient.GetAsync($"{dnsAddress}/exception").Wait();
|
||||
#if NET8_0_OR_GREATER
|
||||
var hubConnection = new HubConnectionBuilder().WithUrl($"{dnsAddress}/signalr").Build();
|
||||
hubConnection.StartAsync().Wait();
|
||||
hubConnection.StopAsync().Wait();
|
||||
#endif
|
||||
}
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
#if NET8_0_OR_GREATER
|
||||
.ConfigureServices(
|
||||
services => services
|
||||
.AddRateLimiter(rateLimiterOptions => rateLimiterOptions.GlobalLimiter = PartitionedRateLimiter.Create<HttpContext, string>(httpContext => RateLimitPartition.GetNoLimiter("1")))
|
||||
.AddConnections()
|
||||
.AddSignalR())
|
||||
#endif
|
||||
.ConfigureWebHostDefaults(webBuilder =>
|
||||
{
|
||||
webBuilder.UseStartup<Startup>();
|
||||
|
|
|
@ -15,11 +15,6 @@
|
|||
// </copyright>
|
||||
|
||||
using System.Diagnostics;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace TestApplication.Http;
|
||||
|
||||
|
@ -42,16 +37,26 @@ public class Startup
|
|||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
app.Map(
|
||||
"/test",
|
||||
configuration => configuration.Run(async context =>
|
||||
{
|
||||
using (var activity = MyActivitySource.StartActivity("manual span"))
|
||||
app
|
||||
#if NET8_0_OR_GREATER
|
||||
.UseRouting() // enables metrics for Microsoft.AspNetCore.Routing in .NET8+
|
||||
.UseExceptionHandler(new ExceptionHandlerOptions { ExceptionHandler = _ => Task.CompletedTask }) // together with call to /exception enables metrics for Microsoft.AspNetCore.Diagnostics for .NET8+
|
||||
.UseRateLimiter() // enables metrics for Microsoft.AspNetCore.RateLimiting in .NET8+
|
||||
.UseEndpoints(x => x.MapHub<TestHub>("/signalr")) // together with connection to SignalR Hub enables metrics for Microsoft.AspNetCore.Http.Connections for .NET8
|
||||
#endif
|
||||
.Map(
|
||||
"/test",
|
||||
configuration => configuration.Run(async context =>
|
||||
{
|
||||
activity?.SetTag("test_tag", "test_value");
|
||||
}
|
||||
using (var activity = MyActivitySource.StartActivity("manual span"))
|
||||
{
|
||||
activity?.SetTag("test_tag", "test_value");
|
||||
}
|
||||
|
||||
await context.Response.WriteAsync("Pong");
|
||||
}));
|
||||
await context.Response.WriteAsync("Pong");
|
||||
}))
|
||||
.Map(
|
||||
"/exception",
|
||||
configuration => configuration.Run(_ => throw new InvalidOperationException("Just to throw something")));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,5 +4,9 @@
|
|||
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
|
||||
<TargetFrameworks Condition=" '$(IsCentos)' == '' ">net8.0;$(TargetFrameworks)</TargetFrameworks>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Condition=" '$(TargetFramework)' == 'net8.0' " />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
// <copyright file="TestHub.cs" company="OpenTelemetry Authors">
|
||||
// Copyright The OpenTelemetry Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// </copyright>
|
||||
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace TestApplication.Http;
|
||||
|
||||
public class TestHub : Hub
|
||||
{
|
||||
}
|
Loading…
Reference in New Issue