private static TracerProviderBuilder SetMotorSampler(this TracerProviderBuilder builder,
                                                      OpenTelemetryOptions options)
 {
     Sampler sampler = options.SamplingProbability switch
     {
    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);
    }
 private static Func <HttpContext, bool> TelemetryRequestFilter(OpenTelemetryOptions openTelemetryOptions) =>
 req => !openTelemetryOptions.FilterOutTelemetryRequests ||
 !req.Request.Path.StartsWithSegments("/metrics") &&
 !req.Request.Path.StartsWithSegments("/health");