Exemple #1
0
        public void ShouldNotCrashWhenEventRecordAfterDisposed()
        {
            var sink = new DictionarySink();
            var e    = new MetricsCollector(sink, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(5));

            e.IncrementCounter("myCounter");
            e.Dispose();
            Assert.Single(sink.Counters);

            e.IncrementCounter("myCounter");
        }
        public void ShouldFlushOnDispose()
        {
            var clock = new DummyClock("11:00:00.000");
            var sink  = new DictionarySink();

            using (var e = new MetricsCollector(sink, clock, oneSecond, TimeSpan.FromSeconds(5), false))
            {
                clock.Set("12:00:01.500");

                // 10000 events per second for 10 virtual seconds, using 3 threads
                var options = new ParallelOptions {
                    MaxDegreeOfParallelism = 5
                };
                for (int i = 0; i < 10; i++)
                {
                    Parallel.For(0, 10000, options, (j, s) =>
                    {
                        e.IncrementCounter($"myevent-{Thread.CurrentThread.ManagedThreadId}");
                    });
                    clock.Advance(oneSecond);
                }
            }

            // Dispose method on event collect ensures all events are flushed immediately
            Assert.Equal(100000, sink.Counters.Values.Sum());
        }
        public void TestCounters()
        {
            var          collector = new MetricsCollector();
            const string numOfExceptionsMetricName = "NumOfExceptions";
            const string succeededMetricName       = "Succeeded";

            collector.IncrementCounter(numOfExceptionsMetricName, 0);
            collector.IncrementCounter(numOfExceptionsMetricName, 1);
            collector.IncrementCounter(succeededMetricName, 1);
            collector.IncrementCounter(succeededMetricName, 2);

            var metrics = collector.GetMetrics();

            Assert.Equal(2, metrics.Count);
            Assert.Equal("1", metrics[numOfExceptionsMetricName].FlushToString());
            Assert.Equal("3", metrics[succeededMetricName].FlushToString());
        }
        public void CrossMetricMergesAreNotAllowed()
        {
            var collector = new MetricsCollector();

            collector.IncrementCounter("Counter", 1);
            var collector2 = new MetricsCollector();

            collector2.Record("Counter", 10M, Unit.Second);

            collector.Merge(collector2);
        }
        public void CollectorShouldIgnoreInvalidMetricNames()
        {
            var          collector   = new MetricsCollector();
            const string invalidName = "Invalid@Name";

            collector.IncrementCounter(invalidName, 1);
            using (collector.StartTimer(invalidName))
            {
                collector.Record(invalidName, 0, Unit.None);
            }

            Assert.Equal(string.Empty, collector.FlushToString());
        }
        public void MergeOfTwoCollectorsShouldWork()
        {
            var collector1 = new MetricsCollector();
            var collector2 = new MetricsCollector();

            collector1.IncrementCounter("Counter", 1);
            collector2.IncrementCounter("Counter", 10);

            collector1.Merge(collector2);
            var flushed = collector1.FlushToString();

            Assert.Equal("m.Counter=11", flushed);
        }
        public void Run(IConfigurationRoot configuration)
        {
            var sink = ChooseSink();

            var metricsWriterSettings    = configuration.GetSection("MetricsWriter").Get <MetricsWriterSettings>();
            var metricsCollectorSettings = configuration.GetSection("MetricsCollector").Get <MetricsCollectorSettings>();

            if (sink is IDatabasePlugin databasePlugin)
            {
                var databaseConnectionSettings = configuration.GetSection("Database:Connection").Get <DatabaseConnectionSettings>();
                var databaseTablesSettings     = configuration.GetSection("Database:Tables").Get <DatabaseTablesSettings>();
                databasePlugin.Initialize(databaseConnectionSettings, databaseTablesSettings, metricsWriterSettings);
            }

            using (var metrics = new MetricsCollector(sink, metricsCollectorSettings))
            {
                Console.WriteLine("Press some keys; enter to exit");

                metrics.RegisterThresholds("latency", new int[] { 0, 50, 75, 100, 1000, 2000, 10000 });
                var stopwatch = new Stopwatch();

                char key;
                do
                {
                    stopwatch.Start();
                    key = Console.ReadKey(true).KeyChar;
                    stopwatch.Stop();

                    metrics.IncrementCounter(key.ToString());
                    metrics.IncrementBucket("latency", stopwatch.ElapsedMilliseconds);
                    metrics.Event(key.ToString());

                    // letters a-z: lowercase is start of interval, uppercase is end.
                    if (key >= 'a' && key <= 'z')
                    {
                        metrics.StartInterval("session", key.ToString());
                    }
                    else if (key >= 'A' && key <= 'Z')
                    {
                        metrics.EndInterval("session", key.ToString().ToLower());
                    }

                    stopwatch.Reset();
                } while (key != '\r' && key != '\n');
            }

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
Exemple #8
0
        public void ShouldCountEventsFromMultipleThreads()
        {
            var sink = new DictionarySink();

            using (var e = new MetricsCollector(sink, TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(200)))
            {
                var options = new ParallelOptions {
                    MaxDegreeOfParallelism = 5
                };
                Parallel.For(0, 100000, options, (j, s) =>
                {
                    e.IncrementCounter($"mycounter-{Thread.CurrentThread.ManagedThreadId}");
                });
            }
            Assert.Equal(100000, sink.Counters.Values.Sum());
        }
        public void EventShouldFlushAtMultiplesOf5000ms()
        {
            var clock = new DummyClock("11:00:00.000");

            var sink = new DictionarySink();

            using (var e = new MetricsCollector(sink, clock, oneSecond, TimeSpan.FromSeconds(5), false))
            {
                clock.Set("12:00:04.000");
                e.IncrementCounter("myevent");

                // should not fire event yet
                clock.Set("12:00:04.999");
                Assert.Empty(sink.Counters);

                clock.Set("12:00:05.000");

                // should now fire event on another thread and then return
                Assert.Single(sink.Counters);
            }
        }
Exemple #10
0
 public void IncrementCounter() =>
 metrics.IncrementCounter(names[random.Next() % 50]);