Example #1
0
        /// <summary>
        /// Enables gRPClient Instrumentation.
        /// </summary>
        /// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
        /// <param name="configure">GrpcClient configuration options.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
        public static TracerProviderBuilder AddGrpcClientInstrumentation(
            this TracerProviderBuilder builder,
            Action <GrpcClientInstrumentationOptions> configure = null)
        {
            Guard.ThrowIfNull(builder, nameof(builder));

            var grpcOptions = new GrpcClientInstrumentationOptions();

            configure?.Invoke(grpcOptions);

            builder.AddInstrumentation(() => new GrpcClientInstrumentation(grpcOptions));
            builder.AddSource(GrpcClientDiagnosticListener.ActivitySourceName);
            builder.AddLegacySource("Grpc.Net.Client.GrpcOut");

            return(builder);
        }
Example #2
0
        /// <summary>
        /// Enables the incoming requests automatic data collection for ASP.NET Core.
        /// </summary>
        /// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
        /// <param name="configureAspNetCoreInstrumentationOptions">ASP.NET Core Request configuration options.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
        public static TracerProviderBuilder AddAspNetCoreInstrumentation(
            this TracerProviderBuilder builder,
            Action <AspNetCoreInstrumentationOptions> configureAspNetCoreInstrumentationOptions = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var aspnetCoreOptions = new AspNetCoreInstrumentationOptions();

            configureAspNetCoreInstrumentationOptions?.Invoke(aspnetCoreOptions);
            builder.AddDiagnosticSourceInstrumentation((activitySource) => new AspNetCoreInstrumentation(activitySource, aspnetCoreOptions));

            return(builder);
        }
Example #3
0
        /// <summary>
        /// Enables the incoming requests automatic data collection for OWIN.
        /// </summary>
        /// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
        /// <param name="configureOwinInstrumentationOptions">OWIN Request configuration options.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
        public static TracerProviderBuilder AddOwinInstrumentation(
            this TracerProviderBuilder builder,
            Action <OwinInstrumentationOptions> configureOwinInstrumentationOptions = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var owinOptions = new OwinInstrumentationOptions();

            configureOwinInstrumentationOptions?.Invoke(owinOptions);

            OwinInstrumentationActivitySource.Options = owinOptions;

            return(builder.AddSource(OwinInstrumentationActivitySource.ActivitySourceName));
        }
Example #4
0
        /// <summary>
        /// Registers a Zipkin exporter that will receive <see cref="System.Diagnostics.Activity"/> instances.
        /// </summary>
        /// <param name="builder"><see cref="TracerProviderBuilder"/> builder to use.</param>
        /// <param name="configure">Exporter configuration options.</param>
        /// <param name="processorConfigure">Activity processor configuration.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
        public static TracerProviderBuilder UseZipkinExporter(this TracerProviderBuilder builder, Action <ZipkinExporterOptions> configure = null, Action <ActivityProcessorPipelineBuilder> processorConfigure = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder.AddProcessorPipeline(pipeline =>
            {
                var options = new ZipkinExporterOptions();
                configure?.Invoke(options);

                var exporter = new ZipkinExporter(options);
                processorConfigure?.Invoke(pipeline);
                pipeline.SetExporter(exporter);
            }));
        }
        /// <summary>
        /// Registers a Zipkin exporter that will receive <see cref="System.Diagnostics.Activity"/> instances.
        /// </summary>
        /// <param name="builder"><see cref="TracerProviderBuilder"/> builder to use.</param>
        /// <param name="configure">Exporter configuration options.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
        public static TracerProviderBuilder AddZPagesExporter(
            this TracerProviderBuilder builder,
            Action <ZPagesExporterOptions> configure = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var exporterOptions = new ZPagesExporterOptions();

            configure?.Invoke(exporterOptions);
            var zpagesExporter = new ZPagesExporter(exporterOptions);

            // TODO: Pick Simple vs Batching based on ZipkinExporterOptions
            return(builder.AddProcessor(new ZPagesProcessor(zpagesExporter)));
        }
Example #6
0
        /// <summary>
        /// Adds a DiagnosticSource based instrumentation.
        /// This is required for libraries which is already instrumented with
        /// DiagnosticSource and Activity, without using ActivitySource.
        /// </summary>
        /// <typeparam name="TInstrumentation">Type of instrumentation class.</typeparam>
        /// <param name="tracerProviderBuilder">TracerProviderBuilder instance.</param>
        /// <param name="instrumentationFactory">Function that builds instrumentation.</param>
        /// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
        internal static TracerProviderBuilder AddDiagnosticSourceInstrumentation <TInstrumentation>(
            this TracerProviderBuilder tracerProviderBuilder,
            Func <ActivitySourceAdapter, TInstrumentation> instrumentationFactory)
            where TInstrumentation : class
        {
            if (instrumentationFactory == null)
            {
                throw new ArgumentNullException(nameof(instrumentationFactory));
            }

            if (tracerProviderBuilder is TracerProviderBuilderSdk tracerProviderBuilderSdk)
            {
                tracerProviderBuilderSdk.AddDiagnosticSourceInstrumentation(instrumentationFactory);
            }

            return(tracerProviderBuilder);
        }
        public static TracerProviderBuilder AddConsoleExporter(this TracerProviderBuilder builder, Action <ConsoleExporterOptions> configure = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (builder is IDeferredTracerProviderBuilder deferredTracerProviderBuilder)
            {
                return(deferredTracerProviderBuilder.Configure((sp, builder) =>
                {
                    AddConsoleExporter(builder, sp.GetOptions <ConsoleExporterOptions>(), configure);
                }));
            }

            return(AddConsoleExporter(builder, new ConsoleExporterOptions(), configure));
        }
Example #8
0
        /// <summary>
        /// Enables AWS Instrumentation.
        /// </summary>
        /// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
        /// <param name="configure">AWS client configuration options.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
        public static TracerProviderBuilder AddAWSInstrumentation(
            this TracerProviderBuilder builder,
            Action <AWSClientInstrumentationOptions> configure = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var awsClientOptions = new AWSClientInstrumentationOptions();

            configure?.Invoke(awsClientOptions);

            new AWSClientsInstrumentation(awsClientOptions);
            builder.AddSource("Amazon.AWS.AWSClientInstrumentation");
            return(builder);
        }
Example #9
0
        /// <summary>
        /// Enables SqlClient instrumentation.
        /// </summary>
        /// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
        /// <param name="configureMySqlDataInstrumentationOptions">SqlClient configuration options.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
        public static TracerProviderBuilder AddMySqlDataInstrumentation(
            this TracerProviderBuilder builder,
            Action <MySqlDataInstrumentationOptions> configureMySqlDataInstrumentationOptions = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var sqlOptions = new MySqlDataInstrumentationOptions();

            configureMySqlDataInstrumentationOptions?.Invoke(sqlOptions);

            builder.AddInstrumentation(() => new MySqlDataInstrumentation(sqlOptions));
            builder.AddSource(MySqlActivitySourceHelper.ActivitySourceName);

            return(builder);
        }
        /// <summary>
        /// Enables the incoming requests automatic data collection for ASP.NET.
        /// </summary>
        /// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
        /// <param name="configureAspNetInstrumentationOptions">ASP.NET Request configuration options.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
        public static TracerProviderBuilder AddAspNetInstrumentation(
            this TracerProviderBuilder builder,
            Action <AspNetInstrumentationOptions> configureAspNetInstrumentationOptions = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var aspnetOptions = new AspNetInstrumentationOptions();

            configureAspNetInstrumentationOptions?.Invoke(aspnetOptions);

            builder.AddInstrumentation(() => new AspNetInstrumentation(aspnetOptions));
            builder.AddSource(TelemetryHttpModule.AspNetSourceName);

            return(builder);
        }
        /// <summary>
        /// Enables the outgoing requests automatic data collection for all supported activity sources.
        /// </summary>
        /// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
        /// <param name="configureHttpClientInstrumentationOptions">HttpClient configuration options.</param>
        /// <param name="configureSqlClientInstrumentationOptions">SqlClient configuration options.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
        public static TracerProviderBuilder AddDependencyInstrumentation(
            this TracerProviderBuilder builder,
            Action <HttpClientInstrumentationOptions> configureHttpClientInstrumentationOptions = null,
            Action <SqlClientInstrumentationOptions> configureSqlClientInstrumentationOptions   = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.AddHttpClientDependencyInstrumentation(configureHttpClientInstrumentationOptions);
            builder.AddSqlClientDependencyInstrumentation(configureSqlClientInstrumentationOptions);
            builder.AddGrpcClientDependencyInstrumentation();
#if NETFRAMEWORK
            builder.AddHttpWebRequestDependencyInstrumentation();
#endif
            return(builder);
        }
        /// <summary>
        /// Enables SqlClient instrumentation.
        /// </summary>
        /// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
        /// <param name="configureSqlClientInstrumentationOptions">SqlClient configuration options.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
        public static TracerProviderBuilder AddSqlClientInstrumentation(
            this TracerProviderBuilder builder,
            Action <SqlClientInstrumentationOptions> configureSqlClientInstrumentationOptions = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var sqlOptions = new SqlClientInstrumentationOptions();

            configureSqlClientInstrumentationOptions?.Invoke(sqlOptions);

            builder.AddInstrumentation(() => new SqlClientInstrumentation(sqlOptions));
            builder.AddSource(SqlClientDiagnosticListener.ActivitySourceName);

            return(builder);
        }
Example #13
0
        /// <summary>
        /// Enables Microsoft.EntityFrameworkCore instrumentation.
        /// </summary>
        /// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
        /// <param name="configureOptions">EntityFrameworkCore configuration options.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
        public static TracerProviderBuilder AddEntityFrameworkCoreInstrumentation(
            this TracerProviderBuilder builder,
            Action <EntityFrameworkInstrumentationOptions> configureOptions = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var options = new EntityFrameworkInstrumentationOptions();

            configureOptions?.Invoke(options);

            builder.AddInstrumentation((activitySource) => new EntityFrameworkInstrumentation(options));
            builder.AddSource(EntityFrameworkDiagnosticListener.ActivitySourceName);

            return(builder);
        }
        /// <summary>
        /// Enables the outgoing requests automatic data collection for Redis.
        /// </summary>
        /// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
        /// <param name="connection"><see cref="ConnectionMultiplexer"/> to instrument.</param>
        /// <param name="configureOptions">Redis configuration options.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
        public static TracerProviderBuilder AddRedisInstrumentation(
            this TracerProviderBuilder builder,
            ConnectionMultiplexer connection,
            Action <StackExchangeRedisCallsInstrumentationOptions> configureOptions = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            StackExchangeRedisCallsInstrumentationOptions options = new StackExchangeRedisCallsInstrumentationOptions();

            configureOptions?.Invoke(options);

            return(builder
                   .AddInstrumentation((activitySourceAdapter) => new StackExchangeRedisCallsInstrumentation(connection, options))
                   .AddSource(StackExchangeRedisCallsInstrumentation.ActivitySourceName));
        }
        /// <summary>
        /// Enables Elasticsearch client Instrumentation.
        /// </summary>
        /// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
        /// <param name="configure">Elasticsearch client configuration options.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
        public static TracerProviderBuilder AddElasticsearchClientInstrumentation(
            this TracerProviderBuilder builder,
            Action <ElasticsearchClientInstrumentationOptions> configure = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var elasticsearchClientOptions = new ElasticsearchClientInstrumentationOptions();

            configure?.Invoke(elasticsearchClientOptions);

            builder.AddInstrumentation(() => new ElasticsearchClientInstrumentation(elasticsearchClientOptions));
            builder.AddSource(ElasticsearchRequestPipelineDiagnosticListener.ActivitySourceName);
            builder.AddLegacySource("CallElasticsearch");

            return(builder);
        }
Example #16
0
        private static TracerProviderBuilder AddOtlpExporter(TracerProviderBuilder builder, OtlpExporterOptions exporterOptions, Action <OtlpExporterOptions> configure = null)
        {
            configure?.Invoke(exporterOptions);
            var otlpExporter = new OtlpTraceExporter(exporterOptions);

            if (exporterOptions.ExportProcessorType == ExportProcessorType.Simple)
            {
                return(builder.AddProcessor(new SimpleActivityExportProcessor(otlpExporter)));
            }
            else
            {
                return(builder.AddProcessor(new BatchActivityExportProcessor(
                                                otlpExporter,
                                                exporterOptions.BatchExportProcessorOptions.MaxQueueSize,
                                                exporterOptions.BatchExportProcessorOptions.ScheduledDelayMilliseconds,
                                                exporterOptions.BatchExportProcessorOptions.ExporterTimeoutMilliseconds,
                                                exporterOptions.BatchExportProcessorOptions.MaxExportBatchSize)));
            }
        }
Example #17
0
        /// <summary>
        /// Enables the incoming requests automatic data collection for ASP.NET Core.
        /// </summary>
        /// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
        /// <param name="configureAspNetCoreInstrumentationOptions">ASP.NET Core Request configuration options.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
        public static TracerProviderBuilder AddAspNetCoreInstrumentation(
            this TracerProviderBuilder builder,
            Action <AspNetCoreInstrumentationOptions> configureAspNetCoreInstrumentationOptions = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (builder is IDeferredTracerProviderBuilder deferredTracerProviderBuilder)
            {
                return(deferredTracerProviderBuilder.Configure((sp, builder) =>
                {
                    AddAspNetCoreInstrumentation(builder, sp.GetOptions <AspNetCoreInstrumentationOptions>(), configureAspNetCoreInstrumentationOptions);
                }));
            }

            return(AddAspNetCoreInstrumentation(builder, new AspNetCoreInstrumentationOptions(), configureAspNetCoreInstrumentationOptions));
        }
        /// <summary>
        /// Enables HttpClient instrumentation.
        /// </summary>
        /// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
        /// <param name="configureHttpClientInstrumentationOptions">HttpClient configuration options.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
        public static TracerProviderBuilder AddHttpClientInstrumentation(
            this TracerProviderBuilder builder,
            Action <HttpClientInstrumentationOptions> configureHttpClientInstrumentationOptions = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var httpClientOptions = new HttpClientInstrumentationOptions();

            configureHttpClientInstrumentationOptions?.Invoke(httpClientOptions);

            builder.AddInstrumentation(() => new HttpClientInstrumentation(httpClientOptions));
            builder.AddSource(HttpHandlerDiagnosticListener.ActivitySourceName);
            builder.AddLegacySource("System.Net.Http.HttpRequestOut");

            return(builder);
        }
        /// <summary>
        /// Enables HttpWebRequest instrumentation.
        /// </summary>
        /// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
        /// <param name="configureOptions">HttpWebRequest configuration options.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
        public static TracerProviderBuilder AddHttpWebRequestInstrumentation(
            this TracerProviderBuilder builder,
            Action <HttpWebRequestInstrumentationOptions> configureOptions = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            HttpWebRequestInstrumentationOptions options = new HttpWebRequestInstrumentationOptions();

            configureOptions?.Invoke(options);

            HttpWebRequestActivitySource.Options = options;

            builder.AddActivitySource(HttpWebRequestActivitySource.ActivitySourceName);

            return(builder);
        }
Example #20
0
        /// <summary>
        /// Enables the incoming requests automatic data collection for ASP.NET Core.
        /// </summary>
        /// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
        /// <param name="configureAspNetCoreInstrumentationOptions">ASP.NET Core Request configuration options.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
        public static TracerProviderBuilder AddAspNetCoreInstrumentation(
            this TracerProviderBuilder builder,
            Action <AspNetCoreInstrumentationOptions> configureAspNetCoreInstrumentationOptions = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var aspnetCoreOptions = new AspNetCoreInstrumentationOptions();

            configureAspNetCoreInstrumentationOptions?.Invoke(aspnetCoreOptions);
            builder.AddInstrumentation(() => new AspNetCoreInstrumentation(aspnetCoreOptions));
            builder.AddSource(HttpInListener.ActivitySourceName);
            builder.AddLegacySource(HttpInListener.ActivityOperationName);        // for the activities created by AspNetCore
            builder.AddLegacySource(HttpInListener.ActivityNameByHttpInListener); // for the sibling activities created by the instrumentation library

            return(builder);
        }
Example #21
0
        private static TracerProviderBuilder AddJaegerExporter(TracerProviderBuilder builder, JaegerExporterOptions options, Action <JaegerExporterOptions> configure = null)
        {
            configure?.Invoke(options);

            var jaegerExporter = new JaegerExporter(options);

            if (options.ExportProcessorType == ExportProcessorType.Simple)
            {
                return(builder.AddProcessor(new SimpleActivityExportProcessor(jaegerExporter)));
            }
            else
            {
                return(builder.AddProcessor(new BatchActivityExportProcessor(
                                                jaegerExporter,
                                                options.BatchExportProcessorOptions.MaxQueueSize,
                                                options.BatchExportProcessorOptions.ScheduledDelayMilliseconds,
                                                options.BatchExportProcessorOptions.ExporterTimeoutMilliseconds,
                                                options.BatchExportProcessorOptions.MaxExportBatchSize)));
            }
        }
        /// <summary>
        /// Enables the outgoing requests automatic data collection for WCF.
        /// </summary>
        /// <param name="builder"><see cref="TracerProviderBuilderExtensions"/> being configured.</param>
        /// <param name="configure">Wcf configuration options.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilderExtensions"/> to chain the calls.</returns>
        public static TracerProviderBuilder AddWcfInstrumentation(this TracerProviderBuilder builder, Action <WcfInstrumentationOptions> configure = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (WcfInstrumentationActivitySource.Options != null)
            {
                throw new NotSupportedException("WCF instrumentation has already been registered and doesn't support multiple registrations.");
            }

            var options = new WcfInstrumentationOptions();

            configure?.Invoke(options);

            WcfInstrumentationActivitySource.Options = options;

            return(builder.AddSource(WcfInstrumentationActivitySource.ActivitySourceName));
        }
Example #23
0
        /// <summary>
        /// Enables HttpClient instrumentation.
        /// </summary>
        /// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
        /// <param name="configureHttpClientInstrumentationOptions">HttpClient configuration options.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
        public static TracerProviderBuilder AddHttpClientInstrumentation(
            this TracerProviderBuilder builder,
            Action <HttpClientInstrumentationOptions> configureHttpClientInstrumentationOptions = null)
#endif
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var httpClientOptions = new HttpClientInstrumentationOptions();

            configureHttpClientInstrumentationOptions?.Invoke(httpClientOptions);

            builder.AddInstrumentation((activitySource) => new HttpClientInstrumentation(activitySource, httpClientOptions));

#if NETFRAMEWORK
            builder.AddHttpWebRequestInstrumentation(configureHttpWebRequestInstrumentationOptions);
#endif
            return(builder);
        }
Example #24
0
        public static TracerProviderBuilder AddInMemoryExporter(this TracerProviderBuilder builder, ICollection <Activity> exportedItems)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (exportedItems == null)
            {
                throw new ArgumentNullException(nameof(exportedItems));
            }

            if (builder is IDeferredTracerProviderBuilder deferredTracerProviderBuilder)
            {
                return(deferredTracerProviderBuilder.Configure((sp, builder) =>
                {
                    builder.AddProcessor(new SimpleActivityExportProcessor(new InMemoryExporter <Activity>(exportedItems)));
                }));
            }

            return(builder.AddProcessor(new SimpleActivityExportProcessor(new InMemoryExporter <Activity>(exportedItems))));
        }
        /// <summary>
        /// Enables automatic data collection of outgoing requests to Redis.
        /// </summary>
        /// <remarks>
        /// Note: If an <see cref="IConnectionMultiplexer"/> is not supplied
        /// using the <paramref name="connection"/> parameter it will be
        /// resolved using the application <see cref="IServiceProvider"/>.
        /// </remarks>
        /// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
        /// <param name="connection">Optional <see cref="IConnectionMultiplexer"/> to instrument.</param>
        /// <param name="configure">Optional callback to configure options.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
        public static TracerProviderBuilder AddRedisInstrumentation(
            this TracerProviderBuilder builder,
            IConnectionMultiplexer connection = null,
            Action <StackExchangeRedisCallsInstrumentationOptions> configure = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (builder is IDeferredTracerProviderBuilder deferredTracerProviderBuilder)
            {
                return(deferredTracerProviderBuilder.Configure((sp, builder) =>
                {
                    if (connection == null)
                    {
                        connection = (IConnectionMultiplexer)sp.GetService(typeof(IConnectionMultiplexer));
                        if (connection == null)
                        {
                            throw new InvalidOperationException("StackExchange.Redis IConnectionMultiplexer could not be resolved through application IServiceProvider.");
                        }
                    }

                    AddRedisInstrumentation(
                        builder,
                        connection,
                        sp.GetOptions <StackExchangeRedisCallsInstrumentationOptions>(),
                        configure);
                }));
            }

            if (connection == null)
            {
                throw new NotSupportedException("StackExchange.Redis IConnectionMultiplexer must be supplied when dependency injection is unavailable. To enable dependency injection use the OpenTelemetry.Extensions.Hosting package.");
            }

            return(AddRedisInstrumentation(builder, connection, new StackExchangeRedisCallsInstrumentationOptions(), configure));
        }
Example #26
0
        /// <summary>
        /// Enables the outgoing requests automatic data collection for MassTransit.
        /// </summary>
        /// <param name="builder"><see cref="TracerProviderBuilderExtensions"/> being configured.</param>
        /// <param name="configureMassTransitInstrumentationOptions">MassTransit configuration options.</param>
        /// <returns>The instance of <see cref="TracerProviderBuilderExtensions"/> to chain the calls.</returns>
        public static TracerProviderBuilder AddMassTransitInstrumentation(
            this TracerProviderBuilder builder,
            Action <MassTransitInstrumentationOptions> configureMassTransitInstrumentationOptions = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var options = new MassTransitInstrumentationOptions();

            configureMassTransitInstrumentationOptions?.Invoke(options);

            builder.AddInstrumentation(() => new MassTransitInstrumentation(options));
            builder.AddSource(MassTransitDiagnosticListener.ActivitySourceName);

            builder.AddLegacySource(OperationName.Consumer.Consume);
            builder.AddLegacySource(OperationName.Consumer.Handle);
            builder.AddLegacySource(OperationName.Transport.Send);
            builder.AddLegacySource(OperationName.Transport.Receive);

            return(builder);
        }
Example #27
0
 public static TracerProviderBuilder AddMongoDBInstrumentation(this TracerProviderBuilder builder)
 => builder.AddSource("MongoDB.Driver.Core.Extensions.DiagnosticSources");
 private static TracerProviderBuilder AddConsoleExporter(TracerProviderBuilder builder, ConsoleExporterOptions options, Action <ConsoleExporterOptions> configure = null)
 {
     configure?.Invoke(options);
     return(builder.AddProcessor(new SimpleActivityExportProcessor(new ConsoleActivityExporter(options))));
 }
 /// <summary>
 /// Enables HttpClient and HttpWebRequest instrumentation.
 /// </summary>
 /// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
 /// <param name="configureHttpClientInstrumentationOptions">HttpClient configuration options.</param>
 /// <param name="configureHttpWebRequestInstrumentationOptions">HttpWebRequest configuration options.</param>
 /// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
 public static TracerProviderBuilder AddHttpClientInstrumentation(
     this TracerProviderBuilder builder,
     Action <HttpClientInstrumentationOptions> configureHttpClientInstrumentationOptions         = null,
     Action <HttpWebRequestInstrumentationOptions> configureHttpWebRequestInstrumentationOptions = null)
Example #30
0
        /// <summary>
        /// Creates TracerProvider with the configuration provided.
        /// This sets up listeners for all configured ActivitySources and
        /// sends activities to the pipeline of Sampler, Processor and Exporter.
        /// </summary>
        /// <param name="configureTracerProviderBuilder">Action to configure TracerProviderBuilder.</param>
        /// <returns>TracerProvider instance, which must be disposed upon shutdown.</returns>
        public static TracerProvider CreateTracerProvider(Action <TracerProviderBuilder> configureTracerProviderBuilder)
        {
            var tracerProviderBuilder = new TracerProviderBuilder();

            configureTracerProviderBuilder?.Invoke(tracerProviderBuilder);

            var     tracerProviderSdk = new TracerProviderSdk();
            Sampler sampler           = tracerProviderBuilder.Sampler ?? new AlwaysOnSampler();

            ActivityProcessor activityProcessor;

            if (tracerProviderBuilder.ProcessingPipelines == null || !tracerProviderBuilder.ProcessingPipelines.Any())
            {
                // if there are no pipelines are configured, use noop processor
                activityProcessor = new NoopActivityProcessor();
            }
            else if (tracerProviderBuilder.ProcessingPipelines.Count == 1)
            {
                // if there is only one pipeline - use it's outer processor as a
                // single processor on the tracerSdk.
                var processorFactory = tracerProviderBuilder.ProcessingPipelines[0];
                activityProcessor = processorFactory.Build();
            }
            else
            {
                // if there are more pipelines, use processor that will broadcast to all pipelines
                var processors = new ActivityProcessor[tracerProviderBuilder.ProcessingPipelines.Count];

                for (int i = 0; i < tracerProviderBuilder.ProcessingPipelines.Count; i++)
                {
                    processors[i] = tracerProviderBuilder.ProcessingPipelines[i].Build();
                }

                activityProcessor = new BroadcastActivityProcessor(processors);
            }

            tracerProviderSdk.Resource = tracerProviderBuilder.Resource;

            var activitySource = new ActivitySourceAdapter(sampler, activityProcessor, tracerProviderSdk.Resource);

            if (tracerProviderBuilder.InstrumentationFactories != null)
            {
                foreach (var instrumentation in tracerProviderBuilder.InstrumentationFactories)
                {
                    tracerProviderSdk.Instrumentations.Add(instrumentation.Factory(activitySource));
                }
            }

            // This is what subscribes to Activities.
            // Think of this as the replacement for DiagnosticListener.AllListeners.Subscribe(onNext => diagnosticListener.Subscribe(..));
            tracerProviderSdk.ActivityListener = new ActivityListener
            {
                // Callback when Activity is started.
                ActivityStarted = (activity) =>
                {
                    if (activity.IsAllDataRequested)
                    {
                        activity.SetResource(tracerProviderSdk.Resource);
                    }

                    activityProcessor.OnStart(activity);
                },

                // Callback when Activity is stopped.
                ActivityStopped = activityProcessor.OnEnd,

                // Function which takes ActivitySource and returns true/false to indicate if it should be subscribed to
                // or not
                ShouldListenTo = (activitySource) => tracerProviderBuilder.ActivitySourceNames?.Contains(activitySource.Name.ToUpperInvariant()) ?? false,

                // The following parameter is not used now.
                GetRequestedDataUsingParentId = (ref ActivityCreationOptions <string> options) => ActivityDataRequest.AllData,

                // This delegate informs ActivitySource about sampling decision when the parent context is an ActivityContext.
                GetRequestedDataUsingContext = (ref ActivityCreationOptions <ActivityContext> options) => ComputeActivityDataRequest(options, sampler),
            };

            ActivitySource.AddActivityListener(tracerProviderSdk.ActivityListener);
            tracerProviderSdk.ActivityProcessor = activityProcessor;
            return(tracerProviderSdk);
        }