Пример #1
0
 public static IMotorHostBuilder ConfigurePrometheus(this IMotorHostBuilder hostBuilder)
 {
     hostBuilder.ConfigureServices((_, services) =>
     {
         services.AddSingleton(typeof(IMetricsFactory <>), typeof(MetricsFactory <>));
         services.AddSingleton <IMotorMetricsFactory, MotorMetricsFactory>();
     });
     return(hostBuilder);
 }
 public static IMotorHostBuilder ConfigurePublisher <TOutput>(this IMotorHostBuilder hostBuilder,
                                                              Action <HostBuilderContext, IPublisherBuilder <TOutput> > action)
     where TOutput : class
 {
     hostBuilder.ConfigureServices((context, collection) =>
     {
         var consumerBuilder = new PublisherBuilder <TOutput>(collection, context);
         consumerBuilder.Build(action);
     });
     return(hostBuilder);
 }
Пример #3
0
 public static IMotorHostBuilder ConfigureSentry(this IMotorHostBuilder hostBuilder)
 {
     return(hostBuilder
            .ConfigureServices((context, services) =>
     {
         var sentryOptions = new SentrySerilogOptions();
         context.Configuration.GetSection("Sentry").Bind(sentryOptions);
         SentrySdk.Init(sentryOptions);
         services.AddTransient <IOptions <SentrySerilogOptions> >(_ => MSOptions.Create(sentryOptions));
     }));
 }
 public static IMotorHostBuilder ConfigureConsumer <TInput>(this IMotorHostBuilder hostBuilder,
                                                            Action <HostBuilderContext, IConsumerBuilder <TInput> > action)
     where TInput : class
 {
     hostBuilder.ConfigureServices((context, collection) =>
     {
         var consumerBuilder = new ConsumerBuilder <TInput>(collection, context);
         collection.AddHostedService <TypedConsumerService <TInput> >();
         action.Invoke(context, consumerBuilder);
     });
     return(hostBuilder);
 }
Пример #5
0
 public static IMotorHostBuilder ConfigurePublisher <TOutput>(this IMotorHostBuilder hostBuilder,
                                                              Action <HostBuilderContext, IPublisherBuilder <TOutput> > action)
     where TOutput : class
 {
     hostBuilder.ConfigureServices((context, collection) =>
     {
         var consumerBuilder = new PublisherBuilder <TOutput>(collection, context);
         action.Invoke(context, consumerBuilder);
         if (consumerBuilder.PublisherImplType == null)
         {
             throw new ArgumentNullException(nameof(consumerBuilder.PublisherImplType));
         }
         collection.AddTransient(typeof(ITypedMessagePublisher <TOutput>), consumerBuilder.PublisherImplType);
     });
     return(hostBuilder);
 }
Пример #6
0
 public static IMotorHostBuilder ConfigureServices(this IMotorHostBuilder hostBuilder,
                                                   Action <IServiceCollection> configureDelegate)
 {
     hostBuilder.ConfigureServices((context, collection) => configureDelegate(collection));
     return(hostBuilder);
 }
    public static IMotorHostBuilder ConfigureOpenTelemetry(this IMotorHostBuilder hostBuilder)
    {
        hostBuilder
        .ConfigureServices((hostContext, services) =>
        {
            var usedExport = UsedExporter.Jaeger;
            if (hostContext.Configuration.GetSection(OtlpExporter).Exists())
            {
                services.Configure <OtlpExporterOptions>(hostContext.Configuration.GetSection(OtlpExporter));
                usedExport = UsedExporter.Oltp;
            }
            else
            {
                services.Configure <JaegerExporterOptions>(hostContext.Configuration.GetSection(JaegerExporter));
            }

            var telemetryOptions = new OpenTelemetryOptions();
            hostContext.Configuration.GetSection(OpenTelemetry).Bind(telemetryOptions);
            services.AddSingleton(telemetryOptions);
            services.AddOpenTelemetryTracing(builder =>
            {
                builder
                .AddAspNetCoreInstrumentation(options =>
                {
                    options.Filter = TelemetryRequestFilter(telemetryOptions);
                })
                .Configure((provider, providerBuilder) =>
                {
                    var httpClientInstrumentationOptions =
                        provider.GetService <IOptions <HttpClientInstrumentationOptions> >()?.Value ??
                        new HttpClientInstrumentationOptions();
                    var applicationNameService = provider.GetRequiredService <IApplicationNameService>();
                    var logger      = provider.GetRequiredService <ILogger <OpenTelemetryOptions> >();
                    providerBuilder = providerBuilder
                                      .AddHttpClientInstrumentation(options =>
                    {
                        options.Enrich        = httpClientInstrumentationOptions.Enrich;
                        options.Filter        = httpClientInstrumentationOptions.Filter;
                        options.SetHttpFlavor = httpClientInstrumentationOptions.SetHttpFlavor;
                    })
                                      .AddSource(OpenTelemetryOptions.DefaultActivitySourceName)
                                      .AddSource(telemetryOptions.Sources.ToArray())
                                      .SetResourceBuilder(ResourceBuilder.CreateDefault()
                                                          .AddService(applicationNameService.GetFullName(),
                                                                      serviceVersion: applicationNameService.GetVersion())
                                                          .AddAttributes(new Dictionary <string, object>
                    {
                        {
                            AttributeMotorNetEnvironment,
                            hostContext.HostingEnvironment.EnvironmentName.ToLower()
                        },
                        {
                            AttributeMotorNetLibraryVersion,
                            applicationNameService.GetLibVersion()
                        }
                    }))
                                      .SetMotorSampler(telemetryOptions);

                    switch (usedExport)
                    {
                    case UsedExporter.Oltp:
                        providerBuilder.AddOtlpExporter(logger, provider);
                        break;

                    default:
                        providerBuilder.AddJaegerExporter(logger, provider);
                        break;
                    }
                });
            });
        });
        return(hostBuilder);
    }