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] req/sec perf summary #5163

Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

public class InstrumentationOptions
{
public bool EnableInstrumentation { get; set; }

public bool EnableMiddlewareInstrumentation { get; set; }
}
42 changes: 34 additions & 8 deletions docs/trace/getting-started-aspnetcore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,47 @@
// SPDX-License-Identifier: Apache-2.0

using System.Diagnostics;
using OpenTelemetry.Resources;
using OpenTelemetry.Instrumentation.AspNetCore;
using OpenTelemetry.Instrumentation.AspNetCore.Implementation;
using OpenTelemetry.Trace;

var builder = WebApplication.CreateBuilder(args);

var instrumentationOptions = new InstrumentationOptions();

builder.Configuration.GetSection("InstrumentationOptions").Bind(instrumentationOptions);

Console.WriteLine("EnableOTelInstrumentation: " + instrumentationOptions.EnableInstrumentation);
Console.WriteLine("EnableMiddlewareInstrumentation: " + instrumentationOptions.EnableMiddlewareInstrumentation);

// Configure OpenTelemetry with tracing and auto-start.
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => resource
.AddService(serviceName: builder.Environment.ApplicationName))
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddConsoleExporter());
if (instrumentationOptions.EnableInstrumentation)
{
builder.Services.AddOpenTelemetry()
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation());
}

if (instrumentationOptions.EnableMiddlewareInstrumentation)
{
builder.Services.AddOpenTelemetry()
.WithTracing(tracing => tracing
.AddSource("Microsoft.AspNetCore"));

builder.Services.AddSingleton(new TelemetryMiddleware(new AspNetCoreTraceInstrumentationOptions()));
}

builder.Logging.ClearProviders();
var app = builder.Build();

app.MapGet("/", () => $"Hello World! OpenTelemetry Trace: {Activity.Current?.Id}");
if (instrumentationOptions.EnableMiddlewareInstrumentation)
{
app.UseMiddleware<TelemetryMiddleware>();
}

app.MapGet("/", () =>
{
return $"Hello World! OpenTelemetry Trace: {Activity.Current?.Id}";
});

app.Run();
4 changes: 4 additions & 0 deletions docs/trace/getting-started-aspnetcore/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
"Microsoft.AspNetCore": "Warning"
}
},
"InstrumentationOptions": {
"EnableInstrumentation": false,
"EnableMiddlewareInstrumentation": false
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreTraceInstrumentationOptions.F
OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreTraceInstrumentationOptions.Filter.set -> void
OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreTraceInstrumentationOptions.RecordException.get -> bool
OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreTraceInstrumentationOptions.RecordException.set -> void
OpenTelemetry.Instrumentation.AspNetCore.Implementation.TelemetryMiddleware
OpenTelemetry.Instrumentation.AspNetCore.Implementation.TelemetryMiddleware.InvokeAsync(Microsoft.AspNetCore.Http.HttpContext context, Microsoft.AspNetCore.Http.RequestDelegate next) -> System.Threading.Tasks.Task
OpenTelemetry.Instrumentation.AspNetCore.Implementation.TelemetryMiddleware.TelemetryMiddleware(OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreTraceInstrumentationOptions options) -> void
OpenTelemetry.Metrics.AspNetCoreInstrumentationMeterProviderBuilderExtensions
OpenTelemetry.Trace.AspNetCoreInstrumentationTracerProviderBuilderExtensions
static OpenTelemetry.Metrics.AspNetCoreInstrumentationMeterProviderBuilderExtensions.AddAspNetCoreInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder builder) -> OpenTelemetry.Metrics.MeterProviderBuilder
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using Microsoft.AspNetCore.Http;

namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation;

/// <summary>
/// Telemetry middleware.
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="TelemetryMiddleware"/> class.
/// </remarks>
/// <param name="options">trace options.</param>
public class TelemetryMiddleware(AspNetCoreTraceInstrumentationOptions options) : IMiddleware
{
internal readonly HttpInListener HttpInListener = new HttpInListener(options);

/// <inheritdoc/>
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
try
{
this.HttpInListener.OnEventWritten("Microsoft.AspNetCore.Hosting.HttpRequestIn.Start", context);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if using middleware approach, then HttpInListener should not be used, as that'll incur the cost of DS callbacks, which a middleware can avoid

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it won't, It does not enable DiagnosticSourceSubscriber which we do here

this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(httpInListener, this.isEnabled, AspNetCoreInstrumentationEventSource.Log.UnknownErrorProcessingEvent);

await next(context).ConfigureAwait(false);
this.HttpInListener.OnEventWritten("Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop", context);
}
catch
{
// Exception
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>$(TargetFrameworksForTests)</TargetFrameworks>
<!-- OmniSharp/VS Code requires TargetFrameworks to be in descending order for IntelliSense and analysis. -->
<TargetFrameworks>net7.0;net6.0;net462</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" />
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Condition="'$(TargetFramework)' == '$(NetFrameworkMinimumSupportedVersion)'" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading