diff --git a/docs/trace/getting-started-aspnetcore/InstrumentationOptions.cs b/docs/trace/getting-started-aspnetcore/InstrumentationOptions.cs new file mode 100644 index 00000000000..84b88f1284c --- /dev/null +++ b/docs/trace/getting-started-aspnetcore/InstrumentationOptions.cs @@ -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; } +} diff --git a/docs/trace/getting-started-aspnetcore/Program.cs b/docs/trace/getting-started-aspnetcore/Program.cs index 5c2affb0c43..4a9eb42e2f2 100644 --- a/docs/trace/getting-started-aspnetcore/Program.cs +++ b/docs/trace/getting-started-aspnetcore/Program.cs @@ -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(); +} + +app.MapGet("/", () => +{ + return $"Hello World! OpenTelemetry Trace: {Activity.Current?.Id}"; +}); app.Run(); diff --git a/docs/trace/getting-started-aspnetcore/appsettings.json b/docs/trace/getting-started-aspnetcore/appsettings.json index 10f68b8c8b4..ec574bb5523 100644 --- a/docs/trace/getting-started-aspnetcore/appsettings.json +++ b/docs/trace/getting-started-aspnetcore/appsettings.json @@ -5,5 +5,9 @@ "Microsoft.AspNetCore": "Warning" } }, + "InstrumentationOptions": { + "EnableInstrumentation": false, + "EnableMiddlewareInstrumentation": false + }, "AllowedHosts": "*" } diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/PublicAPI.Unshipped.txt index fc47928891a..0c395bacb7e 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/.publicApi/PublicAPI.Unshipped.txt @@ -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 diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/TelemetryMiddleware.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/TelemetryMiddleware.cs new file mode 100644 index 00000000000..2edd35a86bb --- /dev/null +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/TelemetryMiddleware.cs @@ -0,0 +1,33 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +using Microsoft.AspNetCore.Http; + +namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation; + +/// +/// Telemetry middleware. +/// +/// +/// Initializes a new instance of the class. +/// +/// trace options. +public class TelemetryMiddleware(AspNetCoreTraceInstrumentationOptions options) : IMiddleware +{ + internal readonly HttpInListener HttpInListener = new HttpInListener(options); + + /// + public async Task InvokeAsync(HttpContext context, RequestDelegate next) + { + try + { + this.HttpInListener.OnEventWritten("Microsoft.AspNetCore.Hosting.HttpRequestIn.Start", context); + await next(context).ConfigureAwait(false); + this.HttpInListener.OnEventWritten("Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop", context); + } + catch + { + // Exception + } + } +} diff --git a/test/OpenTelemetry.Tests.Stress/OpenTelemetry.Tests.Stress.csproj b/test/OpenTelemetry.Tests.Stress/OpenTelemetry.Tests.Stress.csproj index 60e3c917910..0aa5cd799e4 100644 --- a/test/OpenTelemetry.Tests.Stress/OpenTelemetry.Tests.Stress.csproj +++ b/test/OpenTelemetry.Tests.Stress/OpenTelemetry.Tests.Stress.csproj @@ -1,12 +1,12 @@ Exe - $(TargetFrameworksForTests) + + net7.0;net6.0;net462 -