private static MeterProviderBuilder AddConsoleExporter(
            MeterProviderBuilder builder,
            ConsoleExporterOptions exporterOptions,
            MetricReaderOptions metricReaderOptions,
            Action <ConsoleExporterOptions> configureExporter,
            Action <ConsoleExporterOptions, MetricReaderOptions> configureExporterAndMetricReader)
        {
            if (configureExporterAndMetricReader != null)
            {
                configureExporterAndMetricReader.Invoke(exporterOptions, metricReaderOptions);
            }
            else
            {
                configureExporter?.Invoke(exporterOptions);
            }

            var metricExporter = new ConsoleMetricExporter(exporterOptions);

            var metricReader = PeriodicExportingMetricReaderHelper.CreatePeriodicExportingMetricReader(
                metricExporter,
                metricReaderOptions,
                DefaultExportIntervalMilliseconds,
                DefaultExportTimeoutMilliseconds);

            return(builder.AddReader(metricReader));
        }
示例#2
0
        /// <summary>
        /// Adds InMemory exporter to the TracerProvider.
        /// </summary>
        /// <param name="builder"><see cref="MeterProviderBuilder"/> builder to use.</param>
        /// <param name="exportedItems">Collection which will be populated with the exported MetricItem.</param>
        /// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
        public static MeterProviderBuilder AddInMemoryExporter(this MeterProviderBuilder builder, ICollection <Metric> exportedItems)
        {
            Guard.ThrowIfNull(builder, nameof(builder));
            Guard.ThrowIfNull(exportedItems, nameof(exportedItems));

            return(builder.AddReader(new BaseExportingMetricReader(new InMemoryExporter <Metric>(exportedItems))));
        }
        /// <summary>
        /// Add metric view, which can be used to customize the Metrics outputted
        /// from the SDK. The views are applied in the order they are added.
        /// </summary>
        /// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
        /// <param name="instrumentName">Name of the instrument, to be used as part of Instrument selection criteria.</param>
        /// <param name="metricStreamConfiguration">Aggregation configuration used to produce metrics stream.</param>
        /// <returns><see cref="MeterProvider"/>.</returns>
        /// <remarks>See View specification here : https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#view.</remarks>
        public static MeterProviderBuilder AddView(this MeterProviderBuilder meterProviderBuilder, string instrumentName, MetricStreamConfiguration metricStreamConfiguration)
        {
            if (metricStreamConfiguration == null)
            {
                throw new ArgumentNullException($"Metric stream configuration cannot be null.", nameof(metricStreamConfiguration));
            }

            if (!MeterProviderBuilderSdk.IsValidViewName(metricStreamConfiguration.Name))
            {
                throw new ArgumentException($"Custom view name {metricStreamConfiguration.Name} is invalid.", nameof(metricStreamConfiguration.Name));
            }

            if (metricStreamConfiguration is ExplicitBucketHistogramConfiguration histogramConfiguration)
            {
                // Validate histogram boundaries
                if (histogramConfiguration.Boundaries != null && !IsSortedAndDistinct(histogramConfiguration.Boundaries))
                {
                    throw new ArgumentException($"Histogram boundaries must be in ascending order with distinct values", nameof(histogramConfiguration.Boundaries));
                }
            }

            if (meterProviderBuilder is MeterProviderBuilderBase meterProviderBuilderBase)
            {
                return(meterProviderBuilderBase.AddView(instrumentName, metricStreamConfiguration));
            }

            return(meterProviderBuilder);
        }
示例#4
0
        internal static MeterProviderBuilder AddOtlpExporter(
            MeterProviderBuilder builder,
            OtlpExporterOptions exporterOptions,
            MetricReaderOptions metricReaderOptions,
            Action <OtlpExporterOptions> configureExporter,
            Action <OtlpExporterOptions, MetricReaderOptions> configureExporterAndMetricReader,
            IServiceProvider serviceProvider,
            Func <BaseExporter <Metric>, BaseExporter <Metric> > configureExporterInstance = null)
        {
            if (configureExporterAndMetricReader != null)
            {
                configureExporterAndMetricReader.Invoke(exporterOptions, metricReaderOptions);
            }
            else
            {
                configureExporter?.Invoke(exporterOptions);
            }

            exporterOptions.TryEnableIHttpClientFactoryIntegration(serviceProvider, "OtlpMetricExporter");

            BaseExporter <Metric> metricExporter = new OtlpMetricExporter(exporterOptions);

            if (configureExporterInstance != null)
            {
                metricExporter = configureExporterInstance(metricExporter);
            }

            var metricReader = PeriodicExportingMetricReaderHelper.CreatePeriodicExportingMetricReader(
                metricExporter,
                metricReaderOptions,
                DefaultExportIntervalMilliseconds,
                DefaultExportTimeoutMilliseconds);

            return(builder.AddReader(metricReader));
        }
        /// <summary>
        /// Gets the application <see cref="IServiceCollection"/> attached to
        /// the <see cref="MeterProviderBuilder"/>.
        /// </summary>
        /// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
        /// <returns><see cref="IServiceCollection"/> or <see langword="null"/>
        /// if services are unavailable.</returns>
        public static IServiceCollection GetServices(this MeterProviderBuilder meterProviderBuilder)
        {
            if (meterProviderBuilder is MeterProviderBuilderHosting meterProviderBuilderHosting)
            {
                return(meterProviderBuilderHosting.Services);
            }

            return(null);
        }
        /// <summary>
        /// Adds a reader to the provider.
        /// </summary>
        /// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
        /// <param name="reader"><see cref="MetricReader"/>.</param>
        /// <returns><see cref="MeterProvider"/>.</returns>
        public static MeterProviderBuilder AddReader(this MeterProviderBuilder meterProviderBuilder, MetricReader reader)
        {
            if (meterProviderBuilder is MeterProviderBuilderBase meterProviderBuilderBase)
            {
                return(meterProviderBuilderBase.AddReader(reader));
            }

            return(meterProviderBuilder);
        }
        /// <summary>
        /// Sets the <see cref="ResourceBuilder"/> from which the Resource associated with
        /// this provider is built from. Overwrites currently set ResourceBuilder.
        /// </summary>
        /// <param name="meterProviderBuilder">MeterProviderBuilder instance.</param>
        /// <param name="resourceBuilder"><see cref="ResourceBuilder"/> from which Resource will be built.</param>
        /// <returns>Returns <see cref="MeterProviderBuilder"/> for chaining.</returns>
        public static MeterProviderBuilder SetResourceBuilder(this MeterProviderBuilder meterProviderBuilder, ResourceBuilder resourceBuilder)
        {
            if (meterProviderBuilder is MeterProviderBuilderBase meterProviderBuilderBase)
            {
                meterProviderBuilderBase.SetResourceBuilder(resourceBuilder);
            }

            return(meterProviderBuilder);
        }
        /// <summary>
        /// Add metric view, which can be used to customize the Metrics outputted
        /// from the SDK. The views are applied in the order they are added.
        /// </summary>
        /// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
        /// <param name="viewConfig">Function to configure aggregation based on the instrument.</param>
        /// <returns><see cref="MeterProvider"/>.</returns>
        /// <remarks>See View specification here : https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#view.</remarks>
        public static MeterProviderBuilder AddView(this MeterProviderBuilder meterProviderBuilder, Func <Instrument, MetricStreamConfiguration> viewConfig)
        {
            if (meterProviderBuilder is MeterProviderBuilderBase meterProviderBuilderBase)
            {
                return(meterProviderBuilderBase.AddView(viewConfig));
            }

            return(meterProviderBuilder);
        }
示例#9
0
        /// <summary>
        /// Sets the maximum number of MetricPoints allowed per metric stream.
        /// This limits the number of unique combinations of key/value pairs used
        /// for reporting measurements.
        /// </summary>
        /// <param name="meterProviderBuilder">MeterProviderBuilder instance.</param>
        /// <param name="maxMetricPointsPerMetricStream">Maximum maximum number of metric points allowed per metric stream.</param>
        /// <returns>Returns <see cref="MeterProviderBuilder"/> for chaining.</returns>
        /// <remarks>
        /// If a particular key/value pair combination is used at least once,
        /// it will contribute to the limit for the life of the process.
        /// This may change in the future. See: https://github.com/open-telemetry/opentelemetry-dotnet/issues/2360.
        /// </remarks>
        public static MeterProviderBuilder SetMaxMetricPointsPerMetricStream(this MeterProviderBuilder meterProviderBuilder, int maxMetricPointsPerMetricStream)
        {
            if (meterProviderBuilder is MeterProviderBuilderBase meterProviderBuilderBase)
            {
                meterProviderBuilderBase.SetMaxMetricPointsPerMetricStream(maxMetricPointsPerMetricStream);
            }

            return(meterProviderBuilder);
        }
        /// <summary>
        /// Add metric view, which can be used to customize the Metrics outputted
        /// from the SDK. The views are applied in the order they are added.
        /// </summary>
        /// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
        /// <param name="instrumentName">Name of the instrument, to be used as part of Instrument selection criteria.</param>
        /// <param name="metricStreamConfiguration">Aggregation configuration used to produce metrics stream.</param>
        /// <returns><see cref="MeterProvider"/>.</returns>
        /// <remarks>See View specification here : https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#view.</remarks>
        public static MeterProviderBuilder AddView(this MeterProviderBuilder meterProviderBuilder, string instrumentName, MetricStreamConfiguration metricStreamConfiguration)
        {
            if (meterProviderBuilder is MeterProviderBuilderBase meterProviderBuilderBase)
            {
                return(meterProviderBuilderBase.AddView(instrumentName, metricStreamConfiguration));
            }

            return(meterProviderBuilder);
        }
        /// <summary>
        /// Add metric view, which can be used to customize the Metrics outputted
        /// from the SDK. The views are applied in the order they are added.
        /// </summary>
        /// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
        /// <param name="instrumentName">Name of the instrument, to be used as part of Instrument selection criteria.</param>
        /// <param name="name">Name of the view. This will be used as name of resulting metrics stream.</param>
        /// <returns><see cref="MeterProvider"/>.</returns>
        /// <remarks>See View specification here : https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#view.</remarks>
        public static MeterProviderBuilder AddView(this MeterProviderBuilder meterProviderBuilder, string instrumentName, string name)
        {
            if (meterProviderBuilder is MeterProviderBuilderBase meterProviderBuilderBase)
            {
                return(meterProviderBuilderBase.AddView(instrumentName, name));
            }

            return(meterProviderBuilder);
        }
        /// <summary>
        /// Run the given actions to initialize the <see cref="MeterProvider"/>.
        /// </summary>
        /// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
        /// <returns><see cref="MeterProvider"/>.</returns>
        public static MeterProvider Build(this MeterProviderBuilder meterProviderBuilder)
        {
            if (meterProviderBuilder is MeterProviderBuilderSdk meterProviderBuilderSdk)
            {
                return(meterProviderBuilderSdk.Build());
            }

            return(null);
        }
        /// <summary>
        /// Add metric processor.
        /// </summary>
        /// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
        /// <param name="processor">Measurement Processors.</param>
        /// <returns><see cref="MeterProvider"/>.</returns>
        public static MeterProviderBuilder AddMetricProcessor(this MeterProviderBuilder meterProviderBuilder, MetricProcessor processor)
        {
            if (meterProviderBuilder is MeterProviderBuilderSdk meterProviderBuilderSdk)
            {
                return(meterProviderBuilderSdk.AddMetricProcessor(processor));
            }

            return(meterProviderBuilder);
        }
        private static MeterProviderBuilder AddPrometheusExporter(MeterProviderBuilder builder, PrometheusExporterOptions options, Action <PrometheusExporterOptions> configure = null)
        {
            configure?.Invoke(options);

            var exporter = new PrometheusExporter(options);
            var reader   = new BaseExportingMetricReader(exporter);

            return(builder.AddReader(reader));
        }
        /// <summary>
        /// Register a callback action to configure the <see
        /// cref="MeterProviderBuilder"/> once the application <see
        /// cref="IServiceProvider"/> is available.
        /// </summary>
        /// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
        /// <param name="configure">Configuration callback.</param>
        /// <returns>The supplied <see cref="MeterProviderBuilder"/> for chaining.</returns>
        public static MeterProviderBuilder Configure(this MeterProviderBuilder meterProviderBuilder, Action <IServiceProvider, MeterProviderBuilder> configure)
        {
            if (meterProviderBuilder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder)
            {
                deferredMeterProviderBuilder.Configure(configure);
            }

            return(meterProviderBuilder);
        }
        private static MeterProviderBuilder AddOtlpExporter(MeterProviderBuilder builder, OtlpExporterOptions options, Action <OtlpExporterOptions> configure = null)
        {
            configure?.Invoke(options);

            var metricExporter = new OtlpMetricExporter(options);
            var metricReader   = new PeriodicExportingMetricReader(metricExporter, options.MetricExportIntervalMilliseconds);

            metricReader.PreferredAggregationTemporality = options.AggregationTemporality;
            return(builder.AddReader(metricReader));
        }
        /// <summary>
        /// Enables the incoming requests automatic data collection for ASP.NET.
        /// </summary>
        /// <param name="builder"><see cref="MeterProviderBuilder"/> being configured.</param>
        /// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
        public static MeterProviderBuilder AddAspNetInstrumentation(
            this MeterProviderBuilder builder)
        {
            Guard.ThrowIfNull(builder);

            var instrumentation = new AspNetMetrics();

            builder.AddMeter(AspNetMetrics.InstrumentationName);
            return(builder.AddInstrumentation(() => instrumentation));
        }
        /// <summary>
        /// Adds a reader to the provider.
        /// </summary>
        /// <typeparam name="T">Reader type.</typeparam>
        /// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
        /// <returns>The supplied <see cref="MeterProviderBuilder"/> for chaining.</returns>
        public static MeterProviderBuilder AddReader <T>(this MeterProviderBuilder meterProviderBuilder)
            where T : MetricReader
        {
            if (meterProviderBuilder is MeterProviderBuilderHosting meterProviderBuilderHosting)
            {
                meterProviderBuilderHosting.Configure((sp, builder) => builder
                                                      .AddReader(sp.GetRequiredService <T>()));
            }

            return(meterProviderBuilder);
        }
        /// <summary>
        /// Adds instrumentation to the provider.
        /// </summary>
        /// <typeparam name="T">Instrumentation type.</typeparam>
        /// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
        /// <returns>The supplied <see cref="MeterProviderBuilder"/> for chaining.</returns>
        public static MeterProviderBuilder AddInstrumentation <T>(this MeterProviderBuilder meterProviderBuilder)
            where T : class
        {
            if (meterProviderBuilder is MeterProviderBuilderHosting meterProviderBuilderHosting)
            {
                meterProviderBuilderHosting.Configure((sp, builder) => builder
                                                      .AddInstrumentation(() => sp.GetRequiredService <T>()));
            }

            return(meterProviderBuilder);
        }
示例#20
0
        /// <summary>
        /// Adds OpenTelemetry Protocol (OTLP) exporter to the MeterProvider.
        /// </summary>
        /// <param name="builder"><see cref="MeterProviderBuilder"/> builder to use.</param>
        /// <param name="configure">Exporter configuration options.</param>
        /// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
        public static MeterProviderBuilder AddOtlpExporter(this MeterProviderBuilder builder, Action <OtlpExporterOptions> configure = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var options = new OtlpExporterOptions();

            configure?.Invoke(options);
            return(builder.AddMetricProcessor(new PushMetricProcessor(new OtlpMetricsExporter(options), options.MetricExportIntervalMilliseconds, options.IsDelta)));
        }
        /// <summary>
        /// Add metric view, which can be used to customize the Metrics outputted
        /// from the SDK. The views are applied in the order they are added.
        /// </summary>
        /// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
        /// <param name="instrumentName">Name of the instrument, to be used as part of Instrument selection criteria.</param>
        /// <param name="name">Name of the view. This will be used as name of resulting metrics stream.</param>
        /// <returns><see cref="MeterProvider"/>.</returns>
        /// <remarks>See View specification here : https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#view.</remarks>
        public static MeterProviderBuilder AddView(this MeterProviderBuilder meterProviderBuilder, string instrumentName, string name)
        {
            if (!MeterProviderBuilderSdk.IsValidInstrumentName(name))
            {
                throw new ArgumentException($"Custom view name {name} is invalid.", nameof(name));
            }

            if (meterProviderBuilder is MeterProviderBuilderBase meterProviderBuilderBase)
            {
                return(meterProviderBuilderBase.AddView(instrumentName, name));
            }

            return(meterProviderBuilder);
        }
        /// <summary>
        /// Run the configured actions to initialize the <see cref="MeterProvider"/>.
        /// </summary>
        /// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
        /// <param name="serviceProvider"><see cref="IServiceProvider"/>.</param>
        /// <returns><see cref="MeterProvider"/>.</returns>
        public static MeterProvider Build(this MeterProviderBuilder meterProviderBuilder, IServiceProvider serviceProvider)
        {
            if (meterProviderBuilder is MeterProviderBuilderHosting meterProviderBuilderHosting)
            {
                return(meterProviderBuilderHosting.Build(serviceProvider));
            }

            if (meterProviderBuilder is MeterProviderBuilderBase meterProviderBuilderBase)
            {
                return(meterProviderBuilderBase.Build());
            }

            return(null);
        }
        /// <summary>
        /// Adds <see cref="OtlpMetricExporter"/> to the <see cref="MeterProviderBuilder"/>.
        /// </summary>
        /// <param name="builder"><see cref="MeterProviderBuilder"/> builder to use.</param>
        /// <param name="configure">Exporter configuration options.</param>
        /// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
        public static MeterProviderBuilder AddOtlpExporter(this MeterProviderBuilder builder, Action <OtlpExporterOptions> configure = null)
        {
            Guard.Null(builder, nameof(builder));

            if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder)
            {
                return(deferredMeterProviderBuilder.Configure((sp, builder) =>
                {
                    AddOtlpExporter(builder, sp.GetOptions <OtlpExporterOptions>(), configure);
                }));
            }

            return(AddOtlpExporter(builder, new OtlpExporterOptions(), configure));
        }
示例#24
0
        /// <summary>
        /// Adds <see cref="PrometheusExporter"/> to the <see cref="MeterProviderBuilder"/>.
        /// </summary>
        /// <param name="builder"><see cref="MeterProviderBuilder"/> builder to use.</param>
        /// <param name="configure">Exporter configuration options.</param>
        /// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
        public static MeterProviderBuilder AddPrometheusExporter(this MeterProviderBuilder builder, Action <PrometheusExporterOptions> configure = null)
        {
            Guard.ThrowIfNull(builder);

            if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder)
            {
                return(deferredMeterProviderBuilder.Configure((sp, builder) =>
                {
                    AddPrometheusExporter(builder, sp.GetOptions <PrometheusExporterOptions>(), configure);
                }));
            }

            return(AddPrometheusExporter(builder, new PrometheusExporterOptions(), configure));
        }
        /// <summary>
        /// Run the given actions to initialize the <see cref="MeterProvider"/>.
        /// </summary>
        /// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
        /// <returns><see cref="MeterProvider"/>.</returns>
        public static MeterProvider Build(this MeterProviderBuilder meterProviderBuilder)
        {
            if (meterProviderBuilder is IDeferredMeterProviderBuilder)
            {
                throw new NotSupportedException("DeferredMeterProviderBuilder requires a ServiceProvider to build.");
            }

            if (meterProviderBuilder is MeterProviderBuilderSdk meterProviderBuilderSdk)
            {
                return(meterProviderBuilderSdk.BuildSdk());
            }

            return(null);
        }
示例#26
0
        /// <summary>
        /// Adds <see cref="OtlpMetricExporter"/> to the <see cref="MeterProviderBuilder"/>.
        /// </summary>
        /// <param name="builder"><see cref="MeterProviderBuilder"/> builder to use.</param>
        /// <param name="configureExporter">Exporter configuration options.</param>
        /// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
        public static MeterProviderBuilder AddOtlpExporter(this MeterProviderBuilder builder, Action <OtlpExporterOptions> configureExporter)
        {
            Guard.ThrowIfNull(builder);

            if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder)
            {
                return(deferredMeterProviderBuilder.Configure((sp, builder) =>
                {
                    AddOtlpExporter(builder, sp.GetOptions <OtlpExporterOptions>(), sp.GetOptions <MetricReaderOptions>(), configureExporter, null, sp);
                }));
            }

            return(AddOtlpExporter(builder, new OtlpExporterOptions(), new MetricReaderOptions(), configureExporter, null, serviceProvider: null));
        }
        /// <summary>
        /// Adds InMemory metric exporter to the <see cref="MeterProviderBuilder"/> using default options.
        /// </summary>
        /// <param name="builder"><see cref="MeterProviderBuilder"/> builder to use.</param>
        /// <param name="exportedItems">Collection which will be populated with the exported MetricItem.</param>
        /// <param name="configureMetricReader"><see cref="MetricReader"/> configuration options.</param>
        /// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
        public static MeterProviderBuilder AddInMemoryExporter(this MeterProviderBuilder builder, ICollection <Metric> exportedItems, Action <MetricReaderOptions> configureMetricReader)
        {
            Guard.ThrowIfNull(builder);
            Guard.ThrowIfNull(exportedItems);

            if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder)
            {
                return(deferredMeterProviderBuilder.Configure((sp, builder) =>
                {
                    AddInMemoryExporter(builder, exportedItems, sp.GetOptions <MetricReaderOptions>(), configureMetricReader);
                }));
            }

            return(AddInMemoryExporter(builder, exportedItems, new MetricReaderOptions(), configureMetricReader));
        }
        /// <summary>
        /// Adds <see cref="ConsoleMetricExporter"/> to the <see cref="MeterProviderBuilder"/>.
        /// </summary>
        /// <param name="builder"><see cref="MeterProviderBuilder"/> builder to use.</param>
        /// <param name="configureExporterAndMetricReader">Exporter and <see cref="MetricReader"/> configuration options.</param>
        /// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
        public static MeterProviderBuilder AddConsoleExporter(
            this MeterProviderBuilder builder,
            Action <ConsoleExporterOptions, MetricReaderOptions> configureExporterAndMetricReader)
        {
            Guard.ThrowIfNull(builder, nameof(builder));

            if (builder is IDeferredMeterProviderBuilder deferredMeterProviderBuilder)
            {
                return(deferredMeterProviderBuilder.Configure((sp, builder) =>
                {
                    AddConsoleExporter(builder, sp.GetOptions <ConsoleExporterOptions>(), sp.GetOptions <MetricReaderOptions>(), null, configureExporterAndMetricReader);
                }));
            }

            return(AddConsoleExporter(builder, new ConsoleExporterOptions(), new MetricReaderOptions(), null, configureExporterAndMetricReader));
        }
示例#29
0
        private static MeterProviderBuilder AddOtlpExporter(MeterProviderBuilder builder, OtlpExporterOptions options, Action <OtlpExporterOptions> configure = null)
        {
            var initialEndpoint = options.Endpoint;

            configure?.Invoke(options);

            options.AppendExportPath(initialEndpoint, OtlpExporterOptions.MetricsExportPath);

            var metricExporter = new OtlpMetricExporter(options);

            var metricReader = options.MetricReaderType == MetricReaderType.Manual
                ? new BaseExportingMetricReader(metricExporter)
                : new PeriodicExportingMetricReader(metricExporter, options.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds);

            metricReader.Temporality = options.AggregationTemporality;
            return(builder.AddReader(metricReader));
        }
        public static MeterProviderBuilder AddInMemoryExporter(this MeterProviderBuilder builder, ICollection <MetricItem> exportedItems, Action <InMemoryExporterOptions> configure = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

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

            var options = new InMemoryExporterOptions();

            configure?.Invoke(options);
            return(builder.AddMetricProcessor(new PushMetricProcessor(new InMemoryExporter <MetricItem>(exportedItems), options.MetricExportIntervalMilliseconds, options.IsDelta)));
        }