Example #1
0
        /// <summary>
        /// Creates MeterProvider with the configuration provided.
        /// Configuration involves MetricProcessor, Exporter and push internval.
        /// </summary>
        /// <param name="configure">Action to configure MeterBuilder.</param>
        /// <returns>MeterProvider instance, which must be disposed upon shutdown.</returns>
        public static MeterProvider CreateMeterProvider(Action <MeterBuilder> configure)
        {
            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }

            var meterBuilder = new MeterBuilder();

            configure(meterBuilder);

            var metricProcessor         = meterBuilder.MetricProcessor ?? new NoOpMetricProcessor();
            var metricExporter          = meterBuilder.MetricExporter ?? new NoOpMetricExporter();
            var cancellationTokenSource = new CancellationTokenSource();
            var meterRegistry           = new Dictionary <MeterRegistryKey, MeterSdk>();

            // We only have PushMetricController now with only configurable thing being the push interval
            var controller = new PushMetricController(
                meterRegistry,
                metricProcessor,
                metricExporter,
                meterBuilder.MetricPushInterval == default(TimeSpan) ? defaultPushInterval : meterBuilder.MetricPushInterval,
                cancellationTokenSource);

            var meterProviderSdk = new MeterProviderSdk(metricProcessor, meterRegistry, controller, cancellationTokenSource);

            return(meterProviderSdk);
        }
 internal MeterProviderSdk(MetricProcessor metricProcessor, Dictionary <MeterRegistryKey, MeterSdk> registry, PushMetricController controller, CancellationTokenSource cts)
 {
     this.MetricProcessor         = metricProcessor;
     this.PushMetricController    = controller;
     this.CancellationTokenSource = cts;
     this.defaultMeter            = new MeterSdk(string.Empty, this.MetricProcessor);
     this.MeterRegistry           = registry;
     this.MeterRegistry.Add(new MeterRegistryKey(string.Empty, null), this.defaultMeter);
 }
        public MeterProvider Build()
        {
            var cancellationTokenSource = new CancellationTokenSource();
            var meterRegistry           = new Dictionary <MeterRegistryKey, MeterSdk>();

            var controller = new PushMetricController(
                meterRegistry,
                this.metricProcessor,
                this.metricExporter,
                this.metricPushInterval,
                cancellationTokenSource);

            return(new MeterProviderSdk(this.metricProcessor, meterRegistry, controller, cancellationTokenSource));
        }
        private MeterFactory(MeterBuilder meterBuilder)
        {
            this.metricProcessor = meterBuilder.MetricProcessor ?? new NoOpMetricProcessor();
            this.metricExporter  = meterBuilder.MetricExporter ?? new NoOpMetricExporter();

            // We only have PushMetricController now with only configurable thing being the push interval
            this.pushMetricController = new PushMetricController(
                this.meterRegistry,
                this.metricProcessor,
                this.metricExporter,
                meterBuilder.MetricPushInterval == default(TimeSpan) ? this.defaultPushInterval : meterBuilder.MetricPushInterval,
                new CancellationTokenSource());

            this.defaultMeter = new MeterSdk(string.Empty, this.metricProcessor);
        }
Example #5
0
        public void PushControllerCollectsAllMeters()
        {
            // Setup controller to collect every 25 msec.
            var controllerPushIntervalInMsec = 25;
            var collectionCountExpectedMin   = 3;
            var maxWaitInMsec = (controllerPushIntervalInMsec * collectionCountExpectedMin) + 2000;

            int exportCalledCount = 0;
            var testExporter      = new TestMetricExporter(() => exportCalledCount++);

            var testProcessor = new TestMetricProcessor();

            // Setup 2 meters whose Collect will increment the collect count.
            int meter1CollectCount = 0;
            int meter2CollectCount = 0;
            var meters             = new Dictionary <MeterRegistryKey, MeterSdk>();
            var testMeter1         = new TestMeter("meter1", testProcessor, () => meter1CollectCount++);

            meters.Add(new MeterRegistryKey("meter1", string.Empty), testMeter1);
            var testMeter2 = new TestMeter("meter2", testProcessor, () => meter2CollectCount++);

            meters.Add(new MeterRegistryKey("meter2", string.Empty), testMeter2);

            var pushInterval   = TimeSpan.FromMilliseconds(controllerPushIntervalInMsec);
            var pushController = new PushMetricController(
                meters,
                testProcessor,
                testExporter,
                pushInterval,
                new CancellationTokenSource());

            // Validate that collect is called on Meter1, Meter2.
            this.ValidateMeterCollect(ref meter1CollectCount, collectionCountExpectedMin, "meter1", TimeSpan.FromMilliseconds(maxWaitInMsec));
            this.ValidateMeterCollect(ref meter2CollectCount, collectionCountExpectedMin, "meter2", TimeSpan.FromMilliseconds(maxWaitInMsec));

            // Export must be called same no: of times as Collect.
            Assert.True(exportCalledCount >= collectionCountExpectedMin);
        }