private void TestCurrentRequestCounterIsIncrementedAndDecremented(string name, Action <Action> instrument)
        {
            string fullName = string.Format("{0}_current_requests", name);
            DefaultCollectorRegistry registry = GetEmptyRegistry();

            instrument(() =>
            {
                Assert.AreEqual(1, registry.CollectAll().First(x => x.name == fullName).metric[0].gauge.value);
            });
            Assert.AreEqual(0, registry.CollectAll().First(x => x.name == fullName).metric[0].gauge.value);
        }
Exemplo n.º 2
0
        public void CreatingLabelledMetric_AfterObservingLabelledData_DoesNotExportUnlabelled()
        {
            var registry = new DefaultCollectorRegistry();
            var factory  = Metrics.WithCustomRegistry(registry);

            var gauge     = factory.CreateGauge("gauge", "", "labelname");
            var counter   = factory.CreateCounter("counter", "", "labelname");
            var summary   = factory.CreateSummary("summary", "", "labelname");
            var histogram = factory.CreateHistogram("histogram", "", null, "labelname");

            // Touch some labelled metrics.
            gauge.Labels("labelvalue").Inc();
            counter.Labels("labelvalue").Inc();
            summary.Labels("labelvalue").Observe(123);
            histogram.Labels("labelvalue").Observe(123);

            // Without touching any unlabelled metrics, there should be only labelled output.
            var exported = registry.CollectAll().ToArray();

            // There is a family for each of the above, in each family we expect to see 1 metric (for the labelled case).
            Assert.AreEqual(4, exported.Length);

            foreach (var family in exported)
            {
                Assert.AreEqual(1, family.metric.Count, $"Family {family.type} had unexpected metric count.");
            }
        }
        public void TestResponsesTotalRecordsFailureViaLabel()
        {
            string metricName = "test";
            DefaultCollectorRegistry registry = GetEmptyRegistry();
            string totalMetricName            = string.Format("{0}_responses_total", metricName);

            try
            {
                Metrics.Instrument(metricName, () =>
                {
                    throw new ApplicationException("Simulate error during request");
                });
                Assert.Fail("Metrics.Instrument should not swallow exceptions");
            }
            catch (ApplicationException)
            {
            }

            var family = registry.CollectAll().First(x => x.name == totalMetricName);
            var metric = family.metric[0];
            var label  = metric.label.Find(l => l.name == "success");

            Assert.IsNotNull(label, "success label not found");
            Assert.AreEqual("0", label.value);
        }
        public void CreatingUnlabelledMetric_WithInitialValueSuppression_ExportsNothingByDefault()
        {
            var registry = new DefaultCollectorRegistry();
            var factory  = Metrics.WithCustomRegistry(registry);

            var gauge = factory.CreateGauge("gauge", "", new GaugeConfiguration
            {
                SuppressInitialValue = true
            });
            var counter = factory.CreateCounter("counter", "", new CounterConfiguration
            {
                SuppressInitialValue = true
            });
            var summary = factory.CreateSummary("summary", "", new SummaryConfiguration
            {
                SuppressInitialValue = true
            });
            var histogram = factory.CreateHistogram("histogram", "", new HistogramConfiguration
            {
                SuppressInitialValue = true
            });

            var exported = registry.CollectAll().ToArray();

            // There is a family for each of the above, in each family we expect to see 0 metrics.
            Assert.AreEqual(4, exported.Length);

            foreach (var family in exported)
            {
                Assert.AreEqual(0, family.metric.Count, $"Family {family.type} had unexpected metric count.");
            }
        }
        public void CreatingLabelledMetric_WithoutObservingAnyData_ExportsImmediately()
        {
            var registry = new DefaultCollectorRegistry();
            var factory  = Metrics.WithCustomRegistry(registry);

            var gauge = factory.CreateGauge("gauge", "", new GaugeConfiguration
            {
                LabelNames = new[] { "foo" }
            }).WithLabels("bar");
            var counter = factory.CreateCounter("counter", "", new CounterConfiguration
            {
                LabelNames = new[] { "foo" }
            }).WithLabels("bar");
            var summary = factory.CreateSummary("summary", "", new SummaryConfiguration
            {
                LabelNames = new[] { "foo" }
            }).WithLabels("bar");
            var histogram = factory.CreateHistogram("histogram", "", new HistogramConfiguration
            {
                LabelNames = new[] { "foo" }
            }).WithLabels("bar");

            // Without touching any metrics, there should be output for all because default config publishes immediately.
            var exported = registry.CollectAll().ToArray();

            // There is a family for each of the above, in each family we expect to see 1 metrics.
            Assert.AreEqual(4, exported.Length);

            foreach (var family in exported)
            {
                Assert.AreEqual(1, family.metric.Count, $"Family {family.type} had unexpected metric count.");
            }
        }
        private DefaultCollectorRegistry GetEmptyRegistry()
        {
            DefaultCollectorRegistry registry = DefaultCollectorRegistry.Instance;

            registry.Clear();
            Assert.IsFalse(registry.CollectAll().Any());
            return(registry);
        }
        private void TestCounterIsIncremented(string name, Action instrument)
        {
            DefaultCollectorRegistry registry = GetEmptyRegistry();

            instrument();
            var counter = registry.CollectAll().First(x => x.name == name);

            Assert.AreEqual(1, counter.metric.Count);
        }
        private void TestHistogramIsCreated(string name, Action instrument)
        {
            DefaultCollectorRegistry registry = GetEmptyRegistry();

            instrument();
            var histogram = registry.CollectAll().First(x => x.name == string.Format("{0}_request_duration_seconds", name));

            Assert.IsTrue(histogram.metric.Count > 0);
        }
Exemplo n.º 9
0
        public void custom_registry()
        {
            var myRegistry = new DefaultCollectorRegistry();
            var counter1   = Metrics.WithCustomRegistry(myRegistry).CreateCounter("counter1", "help1"); //registered on a custom registry

            var counter2 = Metrics.CreateCounter("counter1", "help1");                                  //created on different registry - same name is hence permitted

            counter1.Inc(3);
            counter2.Inc(4);

            Assert.Equal(3, myRegistry.CollectAll().ToArray()[0].metric[0].counter.value);                        //counter1 == 3
            Assert.Equal(4, DefaultCollectorRegistry.Instance.CollectAll().ToArray()[0].metric[0].counter.value); //counter2 == 4
        }
        public void TestRequestDurationRecordsSuccessViaLabel()
        {
            string metricName                 = "test";
            string durationMetricName         = string.Format("{0}_request_duration_seconds", metricName);
            DefaultCollectorRegistry registry = GetEmptyRegistry();

            Metrics.Instrument(metricName, () => { });

            var family = registry.CollectAll().First(x => x.name == durationMetricName);
            var metric = family.metric[1];
            var label  = metric.label.Find(l => l.name == "success");

            Assert.IsNotNull(label, "success label not found");
            Assert.AreEqual("1", label.value);
        }
        public void TestResponsesTotalRecordsSuccessViaLabel()
        {
            string metricName = "test";

            Metrics.Instrument(metricName, () => { });

            DefaultCollectorRegistry registry = GetEmptyRegistry();
            string totalMetricName            = string.Format("{0}_responses_total", metricName);

            Metrics.Instrument(metricName, () => { });

            var family = registry.CollectAll().First(x => x.name == totalMetricName);
            var metric = family.metric[0];
            var label  = metric.label.Find(l => l.name == "success");

            Assert.IsNotNull(label, "success label not found");
            Assert.AreEqual("1", label.value);
        }
        public void GenerateData()
        {
            for (var metricIndex = 0; metricIndex < _metricCount; metricIndex++)
            {
                for (var variantIndex = 0; variantIndex < _variantCount; variantIndex++)
                {
                    _counters[metricIndex].Labels(_labelValueRows[metricIndex][variantIndex]).Inc();
                    _gauges[metricIndex].Labels(_labelValueRows[metricIndex][variantIndex]).Inc();
                    _summaries[metricIndex].Labels(_labelValueRows[metricIndex][variantIndex]).Observe(variantIndex);
                    _histograms[metricIndex].Labels(_labelValueRows[metricIndex][variantIndex]).Observe(variantIndex);
                }
            }

            // Have to transform to array in order to materialize the iterator's results.
            _data = _registry.CollectAll().ToArray();

            // Use a preallocated fixed size buffer to prevent MemoryStream reallocations in benchmarks.
            _outputBuffer = new byte[32 * 1024 * 1024];
        }
Exemplo n.º 13
0
        public void CreatingLabelledMetric_WithoutObservingAnyData_DoesNotExportUnlabelled()
        {
            var registry = new DefaultCollectorRegistry();
            var factory  = Metrics.WithCustomRegistry(registry);

            var gauge     = factory.CreateGauge("gauge", "", "labelname");
            var counter   = factory.CreateCounter("counter", "", "labelname");
            var summary   = factory.CreateSummary("summary", "", "labelname");
            var histogram = factory.CreateHistogram("histogram", "", null, "labelname");

            // Without touching any metrics, there should be no output.
            var exported = registry.CollectAll().ToArray();

            // There is a family for each of the above, in each family we expect to see 0 metrics.
            Assert.AreEqual(4, exported.Length);

            foreach (var family in exported)
            {
                Assert.AreEqual(0, family.metric.Count, $"Family {family.type} had unexpected metric count.");
            }
        }
        public void TestRequestCounterIsDecrementedOnException()
        {
            const string             name     = "request_counter_dec_on_exception";
            DefaultCollectorRegistry registry = GetEmptyRegistry();

            try
            {
                Metrics.Instrument(name, () =>
                {
                    throw new ApplicationException("Simulate exception during request.");
                });
            }
            catch (ApplicationException)
            {
                var family = registry.CollectAll().First(x => x.name == string.Format("{0}_current_requests", name));
                Assert.AreEqual(0, family.metric[0].gauge.value);
                return;
            }

            Assert.Fail("Metrics.Instrument should not swallow exceptions");
        }
        public void CreatingUnlabelledMetric_WithInitialValueSuppression_ExportsAfterValueChange()
        {
            var registry = new DefaultCollectorRegistry();
            var factory  = Metrics.WithCustomRegistry(registry);

            var gauge = factory.CreateGauge("gauge", "", new GaugeConfiguration
            {
                SuppressInitialValue = true
            });
            var counter = factory.CreateCounter("counter", "", new CounterConfiguration
            {
                SuppressInitialValue = true
            });
            var summary = factory.CreateSummary("summary", "", new SummaryConfiguration
            {
                SuppressInitialValue = true
            });
            var histogram = factory.CreateHistogram("histogram", "", new HistogramConfiguration
            {
                SuppressInitialValue = true
            });

            gauge.Set(123);
            counter.Inc();
            summary.Observe(123);
            histogram.Observe(31);

            // Without touching any metrics, there should be output for all because default config publishes immediately.
            var exported = registry.CollectAll().ToArray();

            // There is a family for each of the above, in each family we expect to see 1 metric.
            Assert.AreEqual(4, exported.Length);

            foreach (var family in exported)
            {
                Assert.AreEqual(1, family.metric.Count, $"Family {family.type} had unexpected metric count.");
            }
        }