diff --git a/sample/Sample.BlazorServer/Program.cs b/sample/Sample.BlazorServer/Program.cs index 7be2c80b2..4b8068c18 100644 --- a/sample/Sample.BlazorServer/Program.cs +++ b/sample/Sample.BlazorServer/Program.cs @@ -5,15 +5,13 @@ using Sample.BlazorServer; using Sample.BlazorServer.Data; -var builder = await WebApplication - .CreateBuilder(args) - .LaunchWith(RocketBooster.For(Imports.Instance), b => b.Set(AssemblyLoadContext.Default)); +var builder = WebApplication.CreateBuilder(args); builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); builder.Services.AddSingleton(); -var app = builder.Build(); +var app = await builder.LaunchWith(RocketBooster.For(Imports.Instance), b => b.Set(AssemblyLoadContext.Default)); if (builder.Environment.IsDevelopment()) { @@ -38,4 +36,4 @@ await app.RunAsync(); -public partial class Program; \ No newline at end of file +public partial class Program; diff --git a/sample/Sample.BlazorWasm/Program.cs b/sample/Sample.BlazorWasm/Program.cs index 2f4d7328f..c5d61dc30 100644 --- a/sample/Sample.BlazorWasm/Program.cs +++ b/sample/Sample.BlazorWasm/Program.cs @@ -4,14 +4,12 @@ using Rocket.Surgery.WebAssembly.Hosting; using Sample.BlazorWasm; -var builder = await WebAssemblyHostBuilder - .CreateDefault(args) - .ConfigureRocketSurgery(Imports.Instance); +var builder = WebAssemblyHostBuilder.CreateDefault(args); builder.RootComponents.Add("app"); builder.Services.AddScoped(_ => new HttpClient { BaseAddress = new(builder.HostEnvironment.BaseAddress), }); -await builder.Build().RunAsync(); +await ( await builder.ConfigureRocketSurgery(Imports.Instance) ).RunAsync(); public static class TestHandler { @@ -58,4 +56,4 @@ public Task Handle(Request request, CancellationToken cancellationToke ); } } -} \ No newline at end of file +} diff --git a/sample/Sample.Classic.Restful/Program.cs b/sample/Sample.Classic.Restful/Program.cs index e3434aedc..d1d3b2941 100644 --- a/sample/Sample.Classic.Restful/Program.cs +++ b/sample/Sample.Classic.Restful/Program.cs @@ -7,9 +7,8 @@ using Sample.Classic.Restful; using Swashbuckle.AspNetCore.SwaggerGen; -var builder = await WebApplication - .CreateBuilder(args) - .LaunchWith(RocketBooster.For(Imports.Instance), b => b.Set(AssemblyLoadContext.Default)); +var builder = WebApplication + .CreateBuilder(args); builder.Services.AddControllers().AddControllersAsServices(); builder.Services .Configure( @@ -23,7 +22,7 @@ } ) ); -var app = builder.Build(); +var app = await builder.LaunchWith(RocketBooster.For(Imports.Instance), b => b.Set(AssemblyLoadContext.Default)); app.UseProblemDetails(); app.UseHttpsRedirection(); @@ -43,4 +42,4 @@ await app.RunAsync(); -public partial class Program; \ No newline at end of file +public partial class Program; diff --git a/sample/Sample.Graphql/Program.cs b/sample/Sample.Graphql/Program.cs index d49d161df..2bab9831c 100644 --- a/sample/Sample.Graphql/Program.cs +++ b/sample/Sample.Graphql/Program.cs @@ -29,8 +29,8 @@ .AddMutationType() .ModifyRequestOptions(options => options.IncludeExceptionDetails = true); -var app = ( await builder - .LaunchWith(RocketBooster.For(Imports.Instance), b => b.Set(AssemblyLoadContext.Default)) ).Build(); +var app = await builder + .LaunchWith(RocketBooster.For(Imports.Instance), b => b.Set(AssemblyLoadContext.Default)); app.UseHttpLogging(); app.UseLaunchPadRequestLogging(); @@ -41,4 +41,4 @@ app.Run(); -public partial class Program; \ No newline at end of file +public partial class Program; diff --git a/sample/Sample.Grpc/Program.cs b/sample/Sample.Grpc/Program.cs index a1a8a92ee..4969f064d 100644 --- a/sample/Sample.Grpc/Program.cs +++ b/sample/Sample.Grpc/Program.cs @@ -5,12 +5,10 @@ using Sample.Grpc; using Sample.Grpc.Services; -var builder = await WebApplication +var app = await WebApplication .CreateBuilder(args) .LaunchWith(RocketBooster.For(Imports.Instance), b => b.Set(AssemblyLoadContext.Default)); -var app = builder.Build(); - app.UseLaunchPadRequestLogging(); app.UseRouting(); @@ -27,4 +25,4 @@ await app.RunAsync(); -public partial class Program; \ No newline at end of file +public partial class Program; diff --git a/sample/Sample.Pages/Program.cs b/sample/Sample.Pages/Program.cs index bc4a8faea..141411ebc 100644 --- a/sample/Sample.Pages/Program.cs +++ b/sample/Sample.Pages/Program.cs @@ -8,13 +8,11 @@ using Rocket.Surgery.LaunchPad.AspNetCore; using Sample.Pages; -var builder = await WebApplication - .CreateBuilder(args) - .LaunchWith(RocketBooster.For(Imports.Instance), b => b.Set(AssemblyLoadContext.Default)); +var builder = WebApplication.CreateBuilder(args); builder.Services.AddRazorPages(); builder.Services.AddControllersWithViews(); -var app = builder.Build(); +var app = await builder.LaunchWith(RocketBooster.For(Imports.Instance), b => b.Set(AssemblyLoadContext.Default)); if (builder.Environment.IsDevelopment()) { @@ -129,4 +127,4 @@ static Task WriteResponse(HttpContext context, HealthReport healthReport) ); } -public partial class Program; \ No newline at end of file +public partial class Program; diff --git a/sample/Sample.Worker/Program.cs b/sample/Sample.Worker/Program.cs index 94f2c2a6e..b334dacb9 100644 --- a/sample/Sample.Worker/Program.cs +++ b/sample/Sample.Worker/Program.cs @@ -2,9 +2,9 @@ using Rocket.Surgery.Hosting; using Sample.Worker; -var builder = await Host - .CreateApplicationBuilder(args) - .LaunchWith(RocketBooster.For(Imports.Instance)); +var builder = Host + .CreateApplicationBuilder(args); builder.Services.AddHostedService(); -await builder.RunAsync(); \ No newline at end of file + +await ( await builder.LaunchWith(RocketBooster.For(Imports.Instance)) ).RunAsync(); diff --git a/src/AspNetCore/Conventions/AspNetCoreConventionInstrumentationConvention.cs b/src/AspNetCore/Conventions/AspNetCoreConventionInstrumentationConvention.cs index d8b642a7c..f930904f2 100644 --- a/src/AspNetCore/Conventions/AspNetCoreConventionInstrumentationConvention.cs +++ b/src/AspNetCore/Conventions/AspNetCoreConventionInstrumentationConvention.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Configuration; +using OpenTelemetry; using OpenTelemetry.Metrics; using OpenTelemetry.Trace; using Rocket.Surgery.Conventions; @@ -8,25 +9,20 @@ namespace Rocket.Surgery.LaunchPad.AspNetCore.Conventions; /// -/// ProblemDetailsConvention. -/// Implements the +/// AspNetCoreConventionInstrumentationConvention. +/// Implements the /// -/// -/// +/// +/// [PublicAPI] [ExportConvention] [AfterConvention(typeof(AspNetCoreConvention))] -public class AspNetCoreConventionInstrumentationConvention : IOpenTelemetryMetricsConvention, IOpenTelemetryTracingConvention +public class AspNetCoreConventionInstrumentationConvention : IOpenTelemetryConvention { /// - public void Register(IConventionContext conventionContext, IConfiguration configuration, MeterProviderBuilder builder) + public void Register(IConventionContext conventionContext, IConfiguration configuration, IOpenTelemetryBuilder builder) { - builder.AddAspNetCoreInstrumentation(); - } - - /// - public void Register(IConventionContext conventionContext, IConfiguration configuration, TracerProviderBuilder builder) - { - builder.AddAspNetCoreInstrumentation(options => options.RecordException = true); + builder.WithTracing(b => b.AddAspNetCoreInstrumentation(options => options.RecordException = true)); + builder.WithMetrics(b => b.AddAspNetCoreInstrumentation()); } } diff --git a/src/Foundation/Conventions/InstrumentationConvention.cs b/src/Foundation/Conventions/InstrumentationConvention.cs index 1145af12d..e30837c2c 100644 --- a/src/Foundation/Conventions/InstrumentationConvention.cs +++ b/src/Foundation/Conventions/InstrumentationConvention.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Configuration; +using OpenTelemetry; using OpenTelemetry.Metrics; using OpenTelemetry.Trace; using Rocket.Surgery.Conventions; @@ -9,22 +10,17 @@ namespace Rocket.Surgery.LaunchPad.Foundation.Conventions; /// /// InstrumentationConvention. -/// Implements and +/// Implements and /// -/// +/// [PublicAPI] [ExportConvention] -public class InstrumentationConvention : IOpenTelemetryMetricsConvention, IOpenTelemetryTracingConvention +public class InstrumentationConvention : IOpenTelemetryConvention { /// - public void Register(IConventionContext conventionContext, IConfiguration configuration, MeterProviderBuilder builder) + public void Register(IConventionContext conventionContext, IConfiguration configuration, IOpenTelemetryBuilder builder) { - builder.AddHttpClientInstrumentation(); - } - - /// - public void Register(IConventionContext conventionContext, IConfiguration configuration, TracerProviderBuilder builder) - { - builder.AddHttpClientInstrumentation(x => x.RecordException = true); + builder.WithTracing(b => b.AddHttpClientInstrumentation(x => x.RecordException = true)); + builder.WithMetrics(b => b.AddHttpClientInstrumentation()); } } diff --git a/src/Hosting/Conventions/OpenTelemetryConvention.cs b/src/Hosting/Conventions/OpenTelemetryConvention.cs index 13e981af1..08ff0f5bc 100644 --- a/src/Hosting/Conventions/OpenTelemetryConvention.cs +++ b/src/Hosting/Conventions/OpenTelemetryConvention.cs @@ -2,26 +2,28 @@ using Microsoft.Extensions.DependencyInjection; using Rocket.Surgery.Conventions; using Rocket.Surgery.Conventions.DependencyInjection; -using Rocket.Surgery.LaunchPad.Hosting.Telemetry; -using Rocket.Surgery.LaunchPad.Serilog; +using Rocket.Surgery.LaunchPad.Telemetry; namespace Rocket.Surgery.LaunchPad.Hosting.Conventions; /// /// EnvironmentLoggingConvention. -/// Implements the +/// Implements the /// -/// +/// [PublicAPI] [ExportConvention] -public class OpenTelemetryConvention : IServiceConvention +public class OpenTelemetryConvention : IServiceAsyncConvention { /// - public void Register(IConventionContext context, IConfiguration configuration, IServiceCollection services) - { - services - .AddOpenTelemetry() - .ApplyConventions(context) - ; - } + public async ValueTask Register( + IConventionContext context, + IConfiguration configuration, + IServiceCollection services, + CancellationToken cancellationToken + ) => + await services + .AddOpenTelemetry() + .ApplyConventionsAsync(context, cancellationToken) + .ConfigureAwait(false); } diff --git a/src/Hosting/Rocket.Surgery.LaunchPad.Hosting.csproj b/src/Hosting/Rocket.Surgery.LaunchPad.Hosting.csproj index 0442cd3fd..bfea87e94 100644 --- a/src/Hosting/Rocket.Surgery.LaunchPad.Hosting.csproj +++ b/src/Hosting/Rocket.Surgery.LaunchPad.Hosting.csproj @@ -17,4 +17,7 @@ + + + diff --git a/src/Hosting/Telemetry/OpenTelemetryHostBuilderExtensions.cs b/src/Hosting/Telemetry/OpenTelemetryHostBuilderExtensions.cs deleted file mode 100644 index 1444eb7f1..000000000 --- a/src/Hosting/Telemetry/OpenTelemetryHostBuilderExtensions.cs +++ /dev/null @@ -1,35 +0,0 @@ -// ReSharper disable once CheckNamespace - -using Rocket.Surgery.LaunchPad.Hosting.Telemetry; - -// ReSharper disable once CheckNamespace -namespace Rocket.Surgery.Conventions; - -/// -/// Helper method for working with -/// -[PublicAPI] -public static class OpenTelemetryHostBuilderExtensions -{ - /// - /// Configure the serilog delegate to the convention scanner - /// - /// The container. - /// The delegate. - /// IConventionHostBuilder. - public static ConventionContextBuilder ConfigureOpenTelemetry(this ConventionContextBuilder container, OpenTelemetryConvention @delegate) - { - if (container == null) - { - throw new ArgumentNullException(nameof(container)); - } - - if (@delegate == null) - { - throw new ArgumentNullException(nameof(@delegate)); - } - - container.AppendDelegate(@delegate); - return container; - } -} diff --git a/src/Hosting/Telemetry/RocketSurgeryOpenTelemetryExtensions.cs b/src/Hosting/Telemetry/RocketSurgeryOpenTelemetryExtensions.cs deleted file mode 100644 index c9a6b8add..000000000 --- a/src/Hosting/Telemetry/RocketSurgeryOpenTelemetryExtensions.cs +++ /dev/null @@ -1,43 +0,0 @@ -using Microsoft.Extensions.Configuration; -using OpenTelemetry; -using Rocket.Surgery.Conventions; -using Rocket.Surgery.LaunchPad.Telemetry; - -namespace Rocket.Surgery.LaunchPad.Hosting.Telemetry; - -/// -/// Extension method to apply configuration conventions -/// -[PublicAPI] -public static class RocketSurgeryOpenTelemetryExtensions -{ - /// - /// Apply configuration conventions - /// - /// - /// - /// - public static OpenTelemetryBuilder ApplyConventions(this OpenTelemetryBuilder builder, IConventionContext conventionContext) - { - var configuration = conventionContext.Get() - ?? throw new ArgumentException("Configuration was not found in context", nameof(conventionContext)); - - builder - .WithMetrics(z => z.ApplyConventions(conventionContext)) - .WithTracing(z => z.ApplyConventions(conventionContext)); - - foreach (var item in conventionContext.Conventions.Get()) - { - if (item is IOpenTelemetryConvention convention) - { - convention.Register(conventionContext, configuration, builder); - } - else if (item is OpenTelemetryConvention @delegate) - { - @delegate(conventionContext, configuration, builder); - } - } - - return builder; - } -} diff --git a/src/HotChocolate/Conventions/InstrumentationConvention.cs b/src/HotChocolate/Conventions/InstrumentationConvention.cs index 0755e9fa2..49a5a4771 100644 --- a/src/HotChocolate/Conventions/InstrumentationConvention.cs +++ b/src/HotChocolate/Conventions/InstrumentationConvention.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Configuration; +using OpenTelemetry; using OpenTelemetry.Trace; using Rocket.Surgery.Conventions; using Rocket.Surgery.Conventions.DependencyInjection; @@ -8,16 +9,16 @@ namespace Rocket.Surgery.LaunchPad.HotChocolate.Conventions; /// /// InstrumentationConvention. -/// Implements and +/// Implements and /// -/// +/// [PublicAPI] [ExportConvention] -public class InstrumentationConvention : IOpenTelemetryTracingConvention +public class InstrumentationConvention : IOpenTelemetryConvention { /// - public void Register(IConventionContext conventionContext, IConfiguration configuration, TracerProviderBuilder builder) + public void Register(IConventionContext conventionContext, IConfiguration configuration, IOpenTelemetryBuilder builder) { - builder.AddHotChocolateInstrumentation(); + builder.WithTracing(b => b.AddHotChocolateInstrumentation()); } } diff --git a/src/Telemetry/IOpenTelemetryAsyncConvention.cs b/src/Telemetry/IOpenTelemetryAsyncConvention.cs new file mode 100644 index 000000000..0c05c3f07 --- /dev/null +++ b/src/Telemetry/IOpenTelemetryAsyncConvention.cs @@ -0,0 +1,22 @@ +using Microsoft.Extensions.Configuration; +using OpenTelemetry; +using Rocket.Surgery.Conventions; + +namespace Rocket.Surgery.LaunchPad.Telemetry; + +/// +/// IMetricsConvention +/// Implements the +/// +/// +public interface IOpenTelemetryAsyncConvention : IConvention +{ + /// + /// Register metrics + /// + /// + /// + /// + /// + ValueTask Register(IConventionContext conventionContext, IConfiguration configuration, IOpenTelemetryBuilder builder, CancellationToken cancellationToken); +} \ No newline at end of file diff --git a/src/Hosting/Telemetry/IOpenTelemetryMetricsConvention.cs b/src/Telemetry/IOpenTelemetryConvention.cs similarity index 84% rename from src/Hosting/Telemetry/IOpenTelemetryMetricsConvention.cs rename to src/Telemetry/IOpenTelemetryConvention.cs index b898f0549..845ce88a8 100644 --- a/src/Hosting/Telemetry/IOpenTelemetryMetricsConvention.cs +++ b/src/Telemetry/IOpenTelemetryConvention.cs @@ -2,7 +2,7 @@ using OpenTelemetry; using Rocket.Surgery.Conventions; -namespace Rocket.Surgery.LaunchPad.Hosting.Telemetry; +namespace Rocket.Surgery.LaunchPad.Telemetry; /// /// IMetricsConvention @@ -17,5 +17,5 @@ public interface IOpenTelemetryConvention : IConvention /// /// /// - void Register(IConventionContext conventionContext, IConfiguration configuration, OpenTelemetryBuilder builder); + void Register(IConventionContext conventionContext, IConfiguration configuration, IOpenTelemetryBuilder builder); } diff --git a/src/Telemetry/IOpenTelemetryMetricsConvention.cs b/src/Telemetry/IOpenTelemetryMetricsConvention.cs index 5a27d90bb..d872c36a3 100644 --- a/src/Telemetry/IOpenTelemetryMetricsConvention.cs +++ b/src/Telemetry/IOpenTelemetryMetricsConvention.cs @@ -4,35 +4,3 @@ using Rocket.Surgery.Conventions; namespace Rocket.Surgery.LaunchPad.Telemetry; - -/// -/// IOpenTelemetryMetricsConvention -/// Implements the -/// -/// -public interface IOpenTelemetryMetricsConvention : IConvention -{ - /// - /// Register metrics - /// - /// - /// - /// - void Register(IConventionContext conventionContext, IConfiguration configuration, MeterProviderBuilder builder); -} - -/// -/// IOpenTelemetryTracingConvention -/// Implements the -/// -/// -public interface IOpenTelemetryTracingConvention : IConvention -{ - /// - /// Register tracing - /// - /// - /// - /// - void Register(IConventionContext conventionContext, IConfiguration configuration, TracerProviderBuilder builder); -} diff --git a/src/Telemetry/OpenTelemetryAsyncConvention.cs b/src/Telemetry/OpenTelemetryAsyncConvention.cs new file mode 100644 index 000000000..fccad12b0 --- /dev/null +++ b/src/Telemetry/OpenTelemetryAsyncConvention.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.Configuration; +using OpenTelemetry; +using Rocket.Surgery.Conventions; + +namespace Rocket.Surgery.LaunchPad.Telemetry; + +/// +/// Delegate OpenTelemetryAsyncConvention +/// +/// +/// +/// +/// +public delegate ValueTask OpenTelemetryAsyncConvention(IConventionContext conventionContext, IConfiguration configuration, IOpenTelemetryBuilder builder, CancellationToken cancellationToken); diff --git a/src/Hosting/Telemetry/OpenTelemetryMetricsConvention.cs b/src/Telemetry/OpenTelemetryConvention.cs similarity index 63% rename from src/Hosting/Telemetry/OpenTelemetryMetricsConvention.cs rename to src/Telemetry/OpenTelemetryConvention.cs index 35dec1a9d..13cb9dfcb 100644 --- a/src/Hosting/Telemetry/OpenTelemetryMetricsConvention.cs +++ b/src/Telemetry/OpenTelemetryConvention.cs @@ -2,12 +2,12 @@ using OpenTelemetry; using Rocket.Surgery.Conventions; -namespace Rocket.Surgery.LaunchPad.Hosting.Telemetry; +namespace Rocket.Surgery.LaunchPad.Telemetry; /// -/// Delegate OpenTelemetryMetricsConvention +/// Delegate OpenTelemetryConvention /// /// /// /// -public delegate void OpenTelemetryConvention(IConventionContext conventionContext, IConfiguration configuration, OpenTelemetryBuilder builder); +public delegate void OpenTelemetryConvention(IConventionContext conventionContext, IConfiguration configuration, IOpenTelemetryBuilder builder); diff --git a/src/Telemetry/OpenTelemetryHostBuilderExtensions.cs b/src/Telemetry/OpenTelemetryHostBuilderExtensions.cs deleted file mode 100644 index cb79a6171..000000000 --- a/src/Telemetry/OpenTelemetryHostBuilderExtensions.cs +++ /dev/null @@ -1,57 +0,0 @@ -// ReSharper disable once CheckNamespace - -using Rocket.Surgery.LaunchPad.Telemetry; - -// ReSharper disable once CheckNamespace -namespace Rocket.Surgery.Conventions; - -/// -/// Helper method for working with -/// -[PublicAPI] -public static class OpenTelemetryHostBuilderExtensions -{ - /// - /// Configure the serilog delegate to the convention scanner - /// - /// The container. - /// The delegate. - /// IConventionHostBuilder. - public static ConventionContextBuilder ConfigureOpenTelemetryMetrics(this ConventionContextBuilder container, OpenTelemetryMetricsConvention @delegate) - { - if (container == null) - { - throw new ArgumentNullException(nameof(container)); - } - - if (@delegate == null) - { - throw new ArgumentNullException(nameof(@delegate)); - } - - container.AppendDelegate(@delegate); - return container; - } - - /// - /// Configure the serilog delegate to the convention scanner - /// - /// The container. - /// The delegate. - /// IConventionHostBuilder. - public static ConventionContextBuilder ConfigureOpenTelemetryTracing(this ConventionContextBuilder container, OpenTelemetryTracingConvention @delegate) - { - if (container == null) - { - throw new ArgumentNullException(nameof(container)); - } - - if (@delegate == null) - { - throw new ArgumentNullException(nameof(@delegate)); - } - - container.AppendDelegate(@delegate); - return container; - } -} diff --git a/src/Telemetry/RocketSurgeryOpenTelemetryExtensions.cs b/src/Telemetry/RocketSurgeryOpenTelemetryExtensions.cs index 7cd74d170..f9a21cd91 100644 --- a/src/Telemetry/RocketSurgeryOpenTelemetryExtensions.cs +++ b/src/Telemetry/RocketSurgeryOpenTelemetryExtensions.cs @@ -1,6 +1,5 @@ using Microsoft.Extensions.Configuration; -using OpenTelemetry.Metrics; -using OpenTelemetry.Trace; +using OpenTelemetry; using Rocket.Surgery.Conventions; namespace Rocket.Surgery.LaunchPad.Telemetry; @@ -16,20 +15,35 @@ public static class RocketSurgeryOpenTelemetryExtensions /// /// /// + /// /// - public static MeterProviderBuilder ApplyConventions(this MeterProviderBuilder builder, IConventionContext conventionContext) + public static async ValueTask ApplyConventionsAsync( + this IOpenTelemetryBuilder builder, + IConventionContext conventionContext, + CancellationToken cancellationToken = default + ) { var configuration = conventionContext.Get() - ?? throw new ArgumentException("Configuration was not found in context", nameof(conventionContext)); - foreach (var item in conventionContext.Conventions.Get()) + ?? throw new ArgumentException("Configuration was not found in context", nameof(conventionContext)); + + foreach (var item in conventionContext.Conventions + .Get()) { - if (item is IOpenTelemetryMetricsConvention convention) - { - convention.Register(conventionContext, configuration, builder); - } - else if (item is OpenTelemetryMetricsConvention @delegate) + switch (item) { - @delegate(conventionContext, configuration, builder); + case IOpenTelemetryConvention convention: + convention.Register(conventionContext, configuration, builder); + break; + case OpenTelemetryConvention @delegate: + @delegate(conventionContext, configuration, builder); + break; + case IOpenTelemetryAsyncConvention convention: + await convention.Register(conventionContext, configuration, builder, cancellationToken); + break; + case OpenTelemetryAsyncConvention @delegate: + await @delegate(conventionContext, configuration, builder, cancellationToken); + break; } } @@ -37,27 +51,113 @@ public static MeterProviderBuilder ApplyConventions(this MeterProviderBuilder bu } /// - /// Apply configuration conventions + /// Configure the serilog delegate to the convention scanner /// - /// - /// - /// - public static TracerProviderBuilder ApplyConventions(this TracerProviderBuilder builder, IConventionContext conventionContext) + /// The container. + /// The delegate. + /// IConventionHostBuilder. + public static ConventionContextBuilder ConfigureOpenTelemetry(this ConventionContextBuilder container, OpenTelemetryConvention @delegate) { - var configuration = conventionContext.Get() - ?? throw new ArgumentException("Configuration was not found in context", nameof(conventionContext)); - foreach (var item in conventionContext.Conventions.Get()) + if (container == null) { - if (item is IOpenTelemetryTracingConvention convention) - { - convention.Register(conventionContext, configuration, builder); - } - else if (item is OpenTelemetryTracingConvention @delegate) - { - @delegate(conventionContext, configuration, builder); - } + throw new ArgumentNullException(nameof(container)); } - return builder; + if (@delegate == null) + { + throw new ArgumentNullException(nameof(@delegate)); + } + + container.AppendDelegate(@delegate); + return container; + } + + + /// + /// Configure the serilog delegate to the convention scanner + /// + /// The container. + /// The delegate. + /// IConventionHostBuilder. + public static ConventionContextBuilder ConfigureOpenTelemetry(this ConventionContextBuilder container, Action @delegate) + { + if (container == null) + { + throw new ArgumentNullException(nameof(container)); + } + + if (@delegate == null) + { + throw new ArgumentNullException(nameof(@delegate)); + } + + container.AppendDelegate(@delegate); + return container; + } + + /// + /// Configure the serilog delegate to the convention scanner + /// + /// The container. + /// The delegate. + /// IConventionHostBuilder. + public static ConventionContextBuilder ConfigureOpenTelemetry(this ConventionContextBuilder container, OpenTelemetryAsyncConvention @delegate) + { + if (container == null) + { + throw new ArgumentNullException(nameof(container)); + } + + if (@delegate == null) + { + throw new ArgumentNullException(nameof(@delegate)); + } + + container.AppendDelegate(@delegate); + return container; + } + + /// + /// Configure the serilog delegate to the convention scanner + /// + /// The container. + /// The delegate. + /// IConventionHostBuilder. + public static ConventionContextBuilder ConfigureOpenTelemetry(this ConventionContextBuilder container, Func @delegate) + { + if (container == null) + { + throw new ArgumentNullException(nameof(container)); + } + + if (@delegate == null) + { + throw new ArgumentNullException(nameof(@delegate)); + } + + container.AppendDelegate(new OpenTelemetryAsyncConvention((_, _, builder, _) => @delegate(builder))); + return container; + } + + /// + /// Configure the serilog delegate to the convention scanner + /// + /// The container. + /// The delegate. + /// IConventionHostBuilder. + public static ConventionContextBuilder ConfigureOpenTelemetry(this ConventionContextBuilder container, Func @delegate) + { + if (container == null) + { + throw new ArgumentNullException(nameof(container)); + } + + if (@delegate == null) + { + throw new ArgumentNullException(nameof(@delegate)); + } + + container.AppendDelegate(new OpenTelemetryAsyncConvention((_, _, builder, token) => @delegate(builder, token))); + return container; } }