public void DelayingPublish()
        {
            var singleValues  = new ConcurrentBag <SingleMetricHolder>();
            var summaryValues = new ConcurrentBag <SummaryMetricHolder>();

            using (var factory = new MetricFactory())
            {
                factory.AddCallback("callback1", 1, (object userState, string name, string[] labels, double?value, CancellationToken cancellationToken) =>
                {
                    singleValues.Add(new SingleMetricHolder {
                        SampleTime = DateTime.UtcNow, Labels = labels, Name = name, UserState = userState, Value = value
                    });
                    return(Task.CompletedTask);
                },
                                    (object userState, string name, string[] labels, int?count, double?minimum, double?maximum, double?mean, double?standardDeviation, double?p50, double?p90, double?p95, double?p98, double?p99, CancellationToken cancellationToken) =>
                {
                    summaryValues.Add(new SummaryMetricHolder {
                        SampleTime = DateTime.UtcNow, Labels = labels, Name = name, UserState = userState, Count = count, Minimum = minimum, Maximum = maximum, Mean = mean, StandardDeviation = standardDeviation, p50 = p50, p90 = p90, p95 = p95, p98 = p98, p99 = p99
                    });
                    return(Task.CompletedTask);
                }, "test");

                factory.AddProvider("debug", new DebugMetricsProvider(1, true));
                factory.AddProvider("trace", new TraceMetricsProvider(1, true));
                factory.AddProvider("console", new ConsoleMetricsProvider(1, true));

                var counter = factory.CreateSummary("Metric1", "Test metric for #1", 1, true, new string[] { "label1", "label2" });

                Task.Delay(5000).GetAwaiter().GetResult();
            }

            Assert.IsTrue(summaryValues.IsEmpty, "Single value count not expected");
            Assert.IsTrue(summaryValues.IsEmpty, "Summary value count not expected");
        }
Пример #2
0
 public void ReplaceNamedProvider()
 {
     using (var factory = new MetricFactory())
     {
         factory.AddProvider("test1", (IMetricProvider) new NonDisposableProvider());
         factory.AddProvider("test1", (IMetricProvider) new NonDisposableProvider());
     }
 }
Пример #3
0
 public void NonDisposable()
 {
     using (var factory = new MetricFactory())
     {
         factory.AddProvider("test1", new NonDisposableProvider());
         factory.AddProvider("test2", new NonDisposableProvider());
     }
 }
        public void SimpleCountSetMultiProvider()
        {
            var singleValues  = new ConcurrentBag <SingleMetricHolder>();
            var summaryValues = new ConcurrentBag <SummaryMetricHolder>();
            var testValue     = 123.5678;

            using (var factory = new MetricFactory())
            {
                factory.AddCallback("callback1", 1, (object userState, string name, string[] labels, double?value, CancellationToken cancellationToken) =>
                {
                    singleValues.Add(new SingleMetricHolder {
                        SampleTime = DateTime.UtcNow, Labels = labels, Name = name, UserState = userState, Value = value
                    });
                    return(Task.CompletedTask);
                },
                                    (object userState, string name, string[] labels, int?count, double?minimum, double?maximum, double?mean, double?standardDeviation, double?p50, double?p90, double?p95, double?p98, double?p99, CancellationToken cancellationToken) =>
                {
                    summaryValues.Add(new SummaryMetricHolder {
                        SampleTime = DateTime.UtcNow, Labels = labels, Name = name, UserState = userState, Count = count, Minimum = minimum, Maximum = maximum, Mean = mean, StandardDeviation = standardDeviation, p50 = p50, p90 = p90, p95 = p95, p98 = p98, p99 = p99
                    });
                    return(Task.CompletedTask);
                }, "test");

                factory.AddProvider("debug", new DebugMetricsProvider(1, true));
                factory.AddProvider("trace", new TraceMetricsProvider(1, true));
                factory.AddProvider("console", new ConsoleMetricsProvider(1, true));

                var counter = factory.CreateCounter("Metric1", "Test metric for #1", true, new string[] { "label1", "label2" });

                for (var index = 0; index < 500; index++)
                {
                    counter.SetTo(testValue);
                    if (index < 499)
                    {
                        Task.Delay(10).GetAwaiter().GetResult();
                    }
                }

                Task.Delay(1000).GetAwaiter().GetResult();
            }

            var groupings = singleValues.GroupBy(item => item.Name).ToArray();

            Assert.IsTrue(groupings.Length == 1, "Metric count not expected");

            foreach (var item in groupings)
            {
                var itemList = item.OrderBy(item => item.SampleTime).ToArray();
                var maxValue = itemList.Max(item => item.Value);

                Assert.IsTrue(itemList.Length >= 6, $"Single value count within groupings not within expected tolerance (value { itemList.Length })");
                Assert.IsTrue(maxValue.HasValue, "Max value should not be null");
                Assert.IsTrue(maxValue == testValue, "Maximum value not expected");
            }

            Assert.IsTrue(summaryValues.Count == 0, "Summary value count not expected");
        }
        public void SimpleCountIterationMultiProvider()
        {
            var singleValues  = new ConcurrentBag <SingleMetricHolder>();
            var summaryValues = new ConcurrentBag <SummaryMetricHolder>();

            using (var factory = new MetricFactory())
            {
                factory.AddCallback("callback1", 1, (object userState, string name, string[] labels, double?value, CancellationToken cancellationToken) =>
                {
                    singleValues.Add(new SingleMetricHolder {
                        SampleTime = DateTime.UtcNow, Labels = labels, Name = name, UserState = userState, Value = value
                    });
                    return(Task.CompletedTask);
                },
                                    (object userState, string name, string[] labels, int?count, double?minimum, double?maximum, double?mean, double?standardDeviation, double?p50, double?p90, double?p95, double?p98, double?p99, CancellationToken cancellationToken) =>
                {
                    summaryValues.Add(new SummaryMetricHolder {
                        SampleTime = DateTime.UtcNow, Labels = labels, Name = name, UserState = userState, Count = count, Minimum = minimum, Maximum = maximum, Mean = mean, StandardDeviation = standardDeviation, p50 = p50, p90 = p90, p95 = p95, p98 = p98, p99 = p99
                    });
                    return(Task.CompletedTask);
                }, "test");

                factory.AddProvider("debug", new DebugMetricsProvider(1, true));
                factory.AddProvider("trace", new TraceMetricsProvider(1, true));
                factory.AddProvider("console", new ConsoleMetricsProvider(1, true));

                var summary = factory.CreateSummary("Metric1", "Test metric for #1", 5, true, new string[] { "label1", "label2" });

                for (var index = 0; index < 1500; index++)
                {
                    summary.Observe(_doubleValues[index % _doubleValues.Length]);
                    if (index < 1499)
                    {
                        Task.Delay(10).GetAwaiter().GetResult();
                    }
                }

                Task.Delay(1000).GetAwaiter().GetResult();
            }

            var groupings = summaryValues.GroupBy(item => item.Name).ToArray();

            Assert.IsTrue(groupings.Length == 1, "Metric count not expected");

            var start = 0;

            foreach (var item in groupings)
            {
                var itemList = item.OrderBy(item => item.SampleTime).ToArray();

                start += itemList.Length;
            }

            Assert.IsTrue(singleValues.IsEmpty, "single value count not expected");
        }
Пример #6
0
 public void NullProviderException()
 {
     using (var factory = new MetricFactory())
     {
         factory.AddProvider("test1", null);
     }
 }