Ejemplo n.º 1
0
        /// <summary>add MagicOnion Telemetry.</summary>
        public static IServiceCollection AddMagicOnionOpenTelemetry(this IServiceCollection services,
                                                                    MagicOnionOpenTelemetryOptions options,
                                                                    Action <MagicOnionOpenTelemetryOptions, MagicOnionOpenTelemetryMeterFactoryOption> configureMeterProvider,
                                                                    Action <MagicOnionOpenTelemetryOptions, IServiceProvider, TracerProviderBuilder> configureTracerProvider)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            services.AddSingleton(options);

            // configure MeterFactory
            if (configureMeterProvider != null)
            {
                var meterFactoryOption = new MagicOnionOpenTelemetryMeterFactoryOption();
                configureMeterProvider(options, meterFactoryOption);

                MeterProvider.SetDefault(Sdk.CreateMeterProviderBuilder()
                                         .SetProcessor(meterFactoryOption.MetricProcessor)
                                         .SetExporter(meterFactoryOption.MetricExporter)
                                         .SetPushInterval(meterFactoryOption.MetricPushInterval)
                                         .Build());

                services.AddSingleton(meterFactoryOption.MetricExporter);
                services.AddSingleton(MeterProvider.Default);
            }

            // configure TracerFactory
            if (configureTracerProvider != null)
            {
                if (string.IsNullOrEmpty(options.ActivitySourceName))
                {
                    throw new NullReferenceException(nameof(options.ActivitySourceName));
                }

                var tracerFactory = services.AddOpenTelemetryTracerProvider((provider, builder) =>
                {
                    // ActivitySourceName must match to TracerName.
                    builder.AddSource(options.ActivitySourceName);
                    configureTracerProvider(options, provider, builder);
                });
                services.AddSingleton(tracerFactory);
                services.AddSingleton(new ActivitySource(options.ActivitySourceName));
            }

            return(services);
        }
        /// <summary>add MagicOnion Telemetry.</summary>
        public static IServiceCollection AddMagicOnionOpenTelemetry(this IServiceCollection services,
                                                                    MagicOnionOpenTelemetryOptions options,
                                                                    Action <MagicOnionOpenTelemetryOptions, MagicOnionOpenTelemetryMeterFactoryOption> configureMeterFactory,
                                                                    Action <MagicOnionOpenTelemetryOptions, TracerBuilder> configureTracerFactory)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (string.IsNullOrEmpty(options.ServiceName))
            {
                throw new ArgumentNullException($"{nameof(options)}.{nameof(options.ServiceName)}");
            }

            services.AddSingleton(options);

            // configure MeterFactory
            if (configureMeterFactory != null)
            {
                var meterFactoryOption = new MagicOnionOpenTelemetryMeterFactoryOption();
                configureMeterFactory(options, meterFactoryOption);

                var meterFactory = MeterFactory.Create(mb =>
                {
                    mb.SetMetricProcessor(meterFactoryOption.MetricProcessor);
                    mb.SetMetricExporter(meterFactoryOption.MetricExporter);
                    mb.SetMetricPushInterval(meterFactoryOption.MetricPushInterval);
                });

                services.AddSingleton(meterFactoryOption.MetricExporter);
                services.AddSingleton(meterFactory);
            }

            // configure TracerFactory
            if (configureTracerFactory != null)
            {
                var tracerFactory = TracerFactory.Create(tracerBuilder => configureTracerFactory(options, tracerBuilder));
                services.AddSingleton(tracerFactory);
            }

            return(services);
        }