public void TestDefaultMetricsSourceExternalMetricsSubscription()
        {
            const string evalId                 = "evalId";
            const string taskId                 = "taskId";
            const string sourceContext          = "test";
            const string recordName             = "testrecord";
            const string subscribedCounterName  = "subscribedcounter";
            const long   subscribedCounterValue = 1001;

            DefaultMetricsSourceImpl metricsSource = TangFactory.GetTang().NewInjector(
                GenerateMetricsSourceConfiguration(evalId, taskId, sourceContext, recordName))
                                                     .GetInstance <DefaultMetricsSourceImpl>();

            IMetricsFactory factory = TangFactory.GetTang().NewInjector().GetInstance <IMetricsFactory>();

            MetricTestUtils.MetricsCollectorTestImpl collector = new MetricTestUtils.MetricsCollectorTestImpl();

            var extraCounter = factory.CreateCounter(subscribedCounterName,
                                                     subscribedCounterName,
                                                     subscribedCounterValue);

            extraCounter.Subscribe(metricsSource);

            metricsSource.GetMetrics(collector, true);
            var rb = collector.CurrentRecordBuilder;

            rb.Validate(subscribedCounterName, subscribedCounterValue);

            extraCounter.Increment();
            extraCounter.OnCompleted(); // This should remove the counter from the metrics source.
            metricsSource.GetMetrics(collector, true);
            rb = collector.CurrentRecordBuilder;
            Assert.False(rb.LongKeyPresent(subscribedCounterName), "The counter should not be present now in the source");
        }
        public void TestDefaultMetricsSourceRate()
        {
            const string evalId        = "evalId";
            const string taskId        = "taskId";
            const string sourceContext = "test";
            const string recordName    = "testrecord";
            const string rateName      = "rname1";

            double[] samples = { 2, 3 };

            DefaultMetricsSourceImpl metricsSource = TangFactory.GetTang().NewInjector(
                GenerateMetricsSourceConfiguration(evalId, taskId, sourceContext, recordName))
                                                     .GetInstance <DefaultMetricsSourceImpl>();

            MetricTestUtils.MetricsCollectorTestImpl collector = new MetricTestUtils.MetricsCollectorTestImpl();

            // Create rate.
            metricsSource.Rates.Create(rateName, rateName);

            // Update rate
            foreach (var sample in samples)
            {
                metricsSource.Rates[rateName].Sample(sample);
            }

            metricsSource.GetMetrics(collector, true);
            var rb = collector.CurrentRecordBuilder;

            // Validate rate
            rb.Validate(rateName + "-Num", samples.Length);
            rb.Validate(rateName + "-RunningAvg", samples.Sum() / samples.Length, 1e-10);
        }
        public void TestDefaultMetricsSourceMetricDeletion()
        {
            const string evalId          = "evalId";
            const string taskId          = "taskId";
            const string sourceContext   = "test";
            const string recordName      = "testrecord";
            string       counterName     = "cname1";
            string       longGaugeName   = "lname1";
            string       doubleGaugeName = "dname1";
            const string rateName        = "rname1";

            DefaultMetricsSourceImpl metricsSource = TangFactory.GetTang().NewInjector(
                GenerateMetricsSourceConfiguration(evalId, taskId, sourceContext, recordName))
                                                     .GetInstance <DefaultMetricsSourceImpl>();

            metricsSource.Counters.Create(counterName, counterName);
            metricsSource.LongGauges.Create(longGaugeName, longGaugeName);
            metricsSource.DoubleGauges.Create(doubleGaugeName, doubleGaugeName);
            metricsSource.Rates.Create(rateName, rateName);

            MetricTestUtils.MetricsCollectorTestImpl collector = new MetricTestUtils.MetricsCollectorTestImpl();

            metricsSource.Counters.Delete(counterName);
            Assert.Throws <MetricsException>(() => metricsSource.Counters[counterName]);
            metricsSource.GetMetrics(collector, true);
            var rb = collector.CurrentRecordBuilder;

            Assert.False(rb.LongKeyPresent(counterName), "Counter was removed, so it should not be present");
        }
        public void TestDefaultMetricsSourceGetMetricsFlag()
        {
            const string evalId        = "evalId";
            const string taskId        = "taskId";
            const string sourceContext = "test";
            const string recordName    = "testrecord";

            string[] counterNames = { "cname1", "cname2" };

            DefaultMetricsSourceImpl metricsSource = TangFactory.GetTang().NewInjector(
                GenerateMetricsSourceConfiguration(evalId, taskId, sourceContext, recordName))
                                                     .GetInstance <DefaultMetricsSourceImpl>();

            MetricTestUtils.MetricsCollectorTestImpl collector = new MetricTestUtils.MetricsCollectorTestImpl();

            // Create Counters
            foreach (var name in counterNames)
            {
                metricsSource.Counters.Create(name, name);
            }

            metricsSource.GetMetrics(collector, true);

            // Do not increment second counter.
            metricsSource.Counters[counterNames[0]].Increment();

            // Verifies that record builder does not get old stats if the flag is set to false.
            // Only first counter should be present.
            metricsSource.GetMetrics(collector, false);
            var rb = collector.CurrentRecordBuilder;

            Assert.True(rb.LongKeyPresent(counterNames[0]), "Counter was updated, so it should be present");
            Assert.False(rb.LongKeyPresent(counterNames[1]), "Counter was not updated, so it should not be present");
        }
        public void TestDefaultMetricsSourceTags()
        {
            const string evalId        = "evalId";
            const string taskId        = "taskId";
            const string sourceContext = "test";
            const string recordName    = "testrecord";

            string[] tagNames  = { "tname1", "tname2" };
            string[] tagValues = { "tvalue1", "tValue2" };

            DefaultMetricsSourceImpl metricsSource = TangFactory.GetTang().NewInjector(
                GenerateMetricsSourceConfiguration(evalId, taskId, sourceContext, recordName))
                                                     .GetInstance <DefaultMetricsSourceImpl>();

            MetricTestUtils.MetricsCollectorTestImpl collector = new MetricTestUtils.MetricsCollectorTestImpl();

            // Create tags
            for (int i = 0; i < tagNames.Length; i++)
            {
                metricsSource.AddTag(tagNames[i], tagNames[i], tagValues[i]);
            }

            metricsSource.GetMetrics(collector, true);
            var rb = collector.CurrentRecordBuilder;

            // Validate tags
            for (int i = 0; i < tagNames.Length; i++)
            {
                rb.Validate(tagNames[i], tagValues[i]);
            }

            // Verify GetTag functionality
            var tag = metricsSource.GetTag(tagNames[0]);

            Assert.NotNull(tag);
            Assert.Equal(tag.Name, tagNames[0]);
            Assert.Equal(tag.Value, tagValues[0]);

            string inValidName = "invalid";

            tag = metricsSource.GetTag(inValidName);
            Assert.Null(tag);
        }
        public void TestDefaultMetricsSourceCounters()
        {
            const string evalId        = "evalId";
            const string taskId        = "taskId";
            const string sourceContext = "test";
            const string recordName    = "testrecord";

            string[]   counterNames = { "cname1", "cname2" };
            const long counterIncr  = 2;

            DefaultMetricsSourceImpl metricsSource = TangFactory.GetTang().NewInjector(
                GenerateMetricsSourceConfiguration(evalId, taskId, sourceContext, recordName))
                                                     .GetInstance <DefaultMetricsSourceImpl>();

            MetricTestUtils.MetricsCollectorTestImpl collector = new MetricTestUtils.MetricsCollectorTestImpl();

            // Create Counters
            foreach (var name in counterNames)
            {
                metricsSource.Counters.Create(name, name);
            }

            // Update counters.
            int counter = 1;

            foreach (var name in counterNames)
            {
                metricsSource.Counters[name].Increment(counter * counterIncr);
                counter++;
            }

            metricsSource.GetMetrics(collector, true);
            var rb = collector.CurrentRecordBuilder;

            // Validate counters
            counter = 1;
            foreach (var name in counterNames)
            {
                rb.Validate(name, counter * counterIncr);
                counter++;
            }
        }
        public void TestDefaultMetricsSourceDoubleGauges()
        {
            const string evalId        = "evalId";
            const string taskId        = "taskId";
            const string sourceContext = "test";
            const string recordName    = "testrecord";

            string[]     doubleGaugeNames = { "dname1", "dname2" };
            const double doubleGaugeDecr  = 1.2;

            DefaultMetricsSourceImpl metricsSource = TangFactory.GetTang().NewInjector(
                GenerateMetricsSourceConfiguration(evalId, taskId, sourceContext, recordName))
                                                     .GetInstance <DefaultMetricsSourceImpl>();

            MetricTestUtils.MetricsCollectorTestImpl collector = new MetricTestUtils.MetricsCollectorTestImpl();

            // Create double gauge
            foreach (var name in doubleGaugeNames)
            {
                metricsSource.DoubleGauges.Create(name, name);
            }

            // Update double gauge
            int counter = 1;

            foreach (var name in doubleGaugeNames)
            {
                metricsSource.DoubleGauges[name].Decrement(counter * doubleGaugeDecr);
                counter++;
            }

            metricsSource.GetMetrics(collector, true);
            var rb = collector.CurrentRecordBuilder;

            // Validate double gauges
            counter = 1;
            foreach (var name in doubleGaugeNames)
            {
                rb.Validate(name, -counter * doubleGaugeDecr, 1e-10);
                counter++;
            }
        }
        public void TestMetricsSourceConfiguration()
        {
            const string evalId        = "evalId";
            const string taskId        = "taskId";
            const string sourceContext = "test";
            const string recordName    = "testrecord";

            DefaultMetricsSourceImpl metricsSource = TangFactory.GetTang().NewInjector(
                GenerateMetricsSourceConfiguration(evalId, taskId, sourceContext, recordName))
                                                     .GetInstance <DefaultMetricsSourceImpl>();

            Assert.NotNull(metricsSource);

            MetricTestUtils.MetricsCollectorTestImpl collector = new MetricTestUtils.MetricsCollectorTestImpl();
            metricsSource.GetMetrics(collector, true);
            Assert.Equal(collector.CurrentRecordBuilder.Name, recordName);
            Assert.Equal(collector.CurrentRecordBuilder.Context, sourceContext);
            collector.CurrentRecordBuilder.Validate("TaskOrContextName", taskId);
            collector.CurrentRecordBuilder.Validate("EvaluatorId", evalId);
        }
예제 #9
0
        public void TestMetricsSourceConfiguration()
        {
            const string evalId = "evalId";
            const string taskId = "taskId";
            const string sourceContext = "test";
            const string recordName = "testrecord";

            DefaultMetricsSourceImpl metricsSource = TangFactory.GetTang().NewInjector(
                GenerateMetricsSourceConfiguration(evalId, taskId, sourceContext, recordName))
                .GetInstance<DefaultMetricsSourceImpl>();

            Assert.NotNull(metricsSource);

            MetricTestUtils.MetricsCollectorTestImpl collector = new MetricTestUtils.MetricsCollectorTestImpl();
            metricsSource.GetMetrics(collector, true);
            Assert.Equal(collector.CurrentRecordBuilder.Name, recordName);
            Assert.Equal(collector.CurrentRecordBuilder.Context, sourceContext);
            collector.CurrentRecordBuilder.Validate("TaskOrContextName", taskId);
            collector.CurrentRecordBuilder.Validate("EvaluatorId", evalId);
        }
예제 #10
0
        public void TestDefaultMetricsSourceCounters()
        {
            const string evalId = "evalId";
            const string taskId = "taskId";
            const string sourceContext = "test";
            const string recordName = "testrecord";
            string[] counterNames = { "cname1", "cname2" };
            const long counterIncr = 2;

            DefaultMetricsSourceImpl metricsSource = TangFactory.GetTang().NewInjector(
                GenerateMetricsSourceConfiguration(evalId, taskId, sourceContext, recordName))
                .GetInstance<DefaultMetricsSourceImpl>();

            MetricTestUtils.MetricsCollectorTestImpl collector = new MetricTestUtils.MetricsCollectorTestImpl();

            // Create Counters
            foreach (var name in counterNames)
            {
                metricsSource.Counters.Create(name, name);
            }
   
            // Update counters.
            int counter = 1;
            foreach (var name in counterNames)
            {
                metricsSource.Counters[name].Increment(counter * counterIncr);
                counter++;
            }

            metricsSource.GetMetrics(collector, true);
            var rb = collector.CurrentRecordBuilder;

            // Validate counters
            counter = 1;
            foreach (var name in counterNames)
            {
                rb.Validate(name, counter * counterIncr);
                counter++;
            }           
        }
예제 #11
0
        public void TestDefaultMetricsSourceTags()
        {
            const string evalId = "evalId";
            const string taskId = "taskId";
            const string sourceContext = "test";
            const string recordName = "testrecord";
            string[] tagNames = { "tname1", "tname2" };
            string[] tagValues = { "tvalue1", "tValue2" };

            DefaultMetricsSourceImpl metricsSource = TangFactory.GetTang().NewInjector(
                GenerateMetricsSourceConfiguration(evalId, taskId, sourceContext, recordName))
                .GetInstance<DefaultMetricsSourceImpl>();

            MetricTestUtils.MetricsCollectorTestImpl collector = new MetricTestUtils.MetricsCollectorTestImpl();

            // Create tags
            for (int i = 0; i < tagNames.Length; i++)
            {
                metricsSource.AddTag(tagNames[i], tagNames[i], tagValues[i]);
            }

            metricsSource.GetMetrics(collector, true);
            var rb = collector.CurrentRecordBuilder;

            // Validate tags
            for (int i = 0; i < tagNames.Length; i++)
            {
                rb.Validate(tagNames[i], tagValues[i]);
            }

            // Verify GetTag functionality
            var tag = metricsSource.GetTag(tagNames[0]);
            Assert.NotNull(tag);
            Assert.Equal(tag.Name, tagNames[0]);
            Assert.Equal(tag.Value, tagValues[0]);

            string inValidName = "invalid";
            tag = metricsSource.GetTag(inValidName);
            Assert.Null(tag);
        }
예제 #12
0
        public void TestDefaultMetricsSourceExternalMetricsSubscription()
        {
            const string evalId = "evalId";
            const string taskId = "taskId";
            const string sourceContext = "test";
            const string recordName = "testrecord";
            const string subscribedCounterName = "subscribedcounter";
            const long subscribedCounterValue = 1001;

            DefaultMetricsSourceImpl metricsSource = TangFactory.GetTang().NewInjector(
                GenerateMetricsSourceConfiguration(evalId, taskId, sourceContext, recordName))
                .GetInstance<DefaultMetricsSourceImpl>();

            IMetricsFactory factory = TangFactory.GetTang().NewInjector().GetInstance<IMetricsFactory>();
            MetricTestUtils.MetricsCollectorTestImpl collector = new MetricTestUtils.MetricsCollectorTestImpl();
         
            var extraCounter = factory.CreateCounter(subscribedCounterName,
                subscribedCounterName,
                subscribedCounterValue);
            extraCounter.Subscribe(metricsSource);

            metricsSource.GetMetrics(collector, true);
            var rb = collector.CurrentRecordBuilder;
            rb.Validate(subscribedCounterName, subscribedCounterValue);

            extraCounter.Increment();
            extraCounter.OnCompleted(); // This should remove the counter from the metrics source.
            metricsSource.GetMetrics(collector, true);
            rb = collector.CurrentRecordBuilder;
            Assert.False(rb.LongKeyPresent(subscribedCounterName), "The counter should not be present now in the source");
        }
예제 #13
0
        public void TestDefaultMetricsSourceMetricDeletion()
        {
            const string evalId = "evalId";
            const string taskId = "taskId";
            const string sourceContext = "test";
            const string recordName = "testrecord";
            string counterName = "cname1";
            string longGaugeName = "lname1";
            string doubleGaugeName = "dname1";
            const string rateName = "rname1";

            DefaultMetricsSourceImpl metricsSource = TangFactory.GetTang().NewInjector(
                GenerateMetricsSourceConfiguration(evalId, taskId, sourceContext, recordName))
                .GetInstance<DefaultMetricsSourceImpl>();

            metricsSource.Counters.Create(counterName, counterName);
            metricsSource.LongGauges.Create(longGaugeName, longGaugeName);
            metricsSource.DoubleGauges.Create(doubleGaugeName, doubleGaugeName);
            metricsSource.Rates.Create(rateName, rateName);

            MetricTestUtils.MetricsCollectorTestImpl collector = new MetricTestUtils.MetricsCollectorTestImpl();

            metricsSource.Counters.Delete(counterName);
            Assert.Throws<MetricsException>(() => metricsSource.Counters[counterName]);
            metricsSource.GetMetrics(collector, true);
            var rb = collector.CurrentRecordBuilder;
            Assert.False(rb.LongKeyPresent(counterName), "Counter was removed, so it should not be present");
        }
예제 #14
0
        public void TestDefaultMetricsSourceRate()
        {
            const string evalId = "evalId";
            const string taskId = "taskId";
            const string sourceContext = "test";
            const string recordName = "testrecord";
            const string rateName = "rname1";
            double[] samples = { 2, 3 };

            DefaultMetricsSourceImpl metricsSource = TangFactory.GetTang().NewInjector(
                GenerateMetricsSourceConfiguration(evalId, taskId, sourceContext, recordName))
                .GetInstance<DefaultMetricsSourceImpl>();

            MetricTestUtils.MetricsCollectorTestImpl collector = new MetricTestUtils.MetricsCollectorTestImpl();

            // Create rate.
            metricsSource.Rates.Create(rateName, rateName);

            // Update rate
            foreach (var sample in samples)
            {
                metricsSource.Rates[rateName].Sample(sample);
            }

            metricsSource.GetMetrics(collector, true);
            var rb = collector.CurrentRecordBuilder;
          
            // Validate rate
            rb.Validate(rateName + "-Num", samples.Length);
            rb.Validate(rateName + "-RunningAvg", samples.Sum() / samples.Length, 1e-10);
        }
        public void TestDefaultMetricsSourceHetergeneousFunctionalities()
        {
            const string evalId        = "evalId";
            const string taskId        = "taskId";
            const string sourceContext = "test";
            const string recordName    = "testrecord";

            string[]     counterNames     = { "cname1", "cname2" };
            string[]     longGaugeNames   = { "lname1", "lname2" };
            string[]     doubleGaugeNames = { "dname1", "dname2" };
            const string rateName         = "rname1";

            double[]     samples                = { 2, 3 };
            string[]     tagNames               = { "tname1", "tname2" };
            string[]     tagValues              = { "tvalue1", "tValue2" };
            const long   counterIncr            = 2;
            const long   longGaugeIncr          = 3;
            const double doubleGaugeDecr        = 1.2;
            const string subscribedCounterName  = "subscribedcounter";
            const long   subscribedCounterValue = 1001;

            DefaultMetricsSourceImpl metricsSource = TangFactory.GetTang().NewInjector(
                GenerateMetricsSourceConfiguration(evalId, taskId, sourceContext, recordName))
                                                     .GetInstance <DefaultMetricsSourceImpl>();

            IMetricsFactory factory = TangFactory.GetTang().NewInjector().GetInstance <IMetricsFactory>();

            MetricTestUtils.MetricsCollectorTestImpl collector = new MetricTestUtils.MetricsCollectorTestImpl();

            // Create Counters
            foreach (var name in counterNames)
            {
                metricsSource.Counters.Create(name, name);
            }

            // Create long gauge
            foreach (var name in longGaugeNames)
            {
                metricsSource.LongGauges.Create(name, name);
            }

            // Create double gauge
            foreach (var name in doubleGaugeNames)
            {
                metricsSource.DoubleGauges.Create(name, name);
            }

            // Create rate.
            metricsSource.Rates.Create(rateName, rateName);

            // Update counters.
            int counter = 1;

            foreach (var name in counterNames)
            {
                metricsSource.Counters[name].Increment(counter * counterIncr);
                counter++;
            }

            // Update long gauge
            counter = 1;
            foreach (var name in longGaugeNames)
            {
                metricsSource.LongGauges[name].Increment(counter * longGaugeIncr);
                counter++;
            }

            // Update double gauge
            counter = 1;
            foreach (var name in doubleGaugeNames)
            {
                metricsSource.DoubleGauges[name].Decrement(counter * doubleGaugeDecr);
                counter++;
            }

            // Update rate
            foreach (var sample in samples)
            {
                metricsSource.Rates[rateName].Sample(sample);
            }

            // Create tags
            for (int i = 0; i < tagNames.Length; i++)
            {
                metricsSource.AddTag(tagNames[i], tagNames[i], tagValues[i]);
            }

            var extraCounter = factory.CreateCounter(subscribedCounterName,
                                                     subscribedCounterName,
                                                     subscribedCounterValue);

            extraCounter.Subscribe(metricsSource);

            metricsSource.GetMetrics(collector, true);
            var rb = collector.CurrentRecordBuilder;

            // Validate counters
            counter = 1;
            foreach (var name in counterNames)
            {
                rb.Validate(name, counter * counterIncr);
                counter++;
            }
            rb.Validate(subscribedCounterName, subscribedCounterValue);

            // Validate long gauges
            counter = 1;
            foreach (var name in longGaugeNames)
            {
                rb.Validate(name, counter * longGaugeIncr);
                counter++;
            }

            // Validate double gauges
            counter = 1;
            foreach (var name in doubleGaugeNames)
            {
                rb.Validate(name, -counter * doubleGaugeDecr, 1e-10);
                counter++;
            }

            // Validate tags
            for (int i = 0; i < tagNames.Length; i++)
            {
                rb.Validate(tagNames[i], tagValues[i]);
            }

            // Validate rate
            rb.Validate(rateName + "-Num", samples.Length);
            rb.Validate(rateName + "-RunningAvg", samples.Sum() / samples.Length, 1e-10);
        }
예제 #16
0
        public void TestDefaultMetricsSourceHetergeneousFunctionalities()
        {
            const string evalId = "evalId";
            const string taskId = "taskId";
            const string sourceContext = "test";
            const string recordName = "testrecord";
            string[] counterNames = { "cname1", "cname2" };
            string[] longGaugeNames = { "lname1", "lname2" };
            string[] doubleGaugeNames = { "dname1", "dname2" };
            const string rateName = "rname1";
            double[] samples = { 2, 3 };
            string[] tagNames = { "tname1", "tname2" };
            string[] tagValues = { "tvalue1", "tValue2" };
            const long counterIncr = 2;
            const long longGaugeIncr = 3;
            const double doubleGaugeDecr = 1.2;
            const string subscribedCounterName = "subscribedcounter";
            const long subscribedCounterValue = 1001;

            DefaultMetricsSourceImpl metricsSource = TangFactory.GetTang().NewInjector(
                GenerateMetricsSourceConfiguration(evalId, taskId, sourceContext, recordName))
                .GetInstance<DefaultMetricsSourceImpl>();

            IMetricsFactory factory = TangFactory.GetTang().NewInjector().GetInstance<IMetricsFactory>();

            MetricTestUtils.MetricsCollectorTestImpl collector = new MetricTestUtils.MetricsCollectorTestImpl();

            // Create Counters
            foreach (var name in counterNames)
            {
                metricsSource.Counters.Create(name, name);
            }

            // Create long gauge
            foreach (var name in longGaugeNames)
            {
                metricsSource.LongGauges.Create(name, name);
            }

            // Create double gauge
            foreach (var name in doubleGaugeNames)
            {
                metricsSource.DoubleGauges.Create(name, name);
            }

            // Create rate.
            metricsSource.Rates.Create(rateName, rateName);

            // Update counters.
            int counter = 1;
            foreach (var name in counterNames)
            {
                metricsSource.Counters[name].Increment(counter * counterIncr);
                counter++;
            }

            // Update long gauge
            counter = 1;
            foreach (var name in longGaugeNames)
            {
                metricsSource.LongGauges[name].Increment(counter * longGaugeIncr);
                counter++;
            }

            // Update double gauge
            counter = 1;
            foreach (var name in doubleGaugeNames)
            {
                metricsSource.DoubleGauges[name].Decrement(counter * doubleGaugeDecr);
                counter++;
            }

            // Update rate
            foreach (var sample in samples)
            {
                metricsSource.Rates[rateName].Sample(sample);
            }

            // Create tags
            for (int i = 0; i < tagNames.Length; i++)
            {
                metricsSource.AddTag(tagNames[i], tagNames[i], tagValues[i]);
            }

            var extraCounter = factory.CreateCounter(subscribedCounterName,
                subscribedCounterName,
                subscribedCounterValue);
            extraCounter.Subscribe(metricsSource);

            metricsSource.GetMetrics(collector, true);
            var rb = collector.CurrentRecordBuilder;

            // Validate counters
            counter = 1;
            foreach (var name in counterNames)
            {
                rb.Validate(name, counter * counterIncr);
                counter++;
            }
            rb.Validate(subscribedCounterName, subscribedCounterValue);

            // Validate long gauges
            counter = 1;
            foreach (var name in longGaugeNames)
            {
                rb.Validate(name, counter * longGaugeIncr);
                counter++;
            }

            // Validate double gauges
            counter = 1;
            foreach (var name in doubleGaugeNames)
            {
                rb.Validate(name, -counter * doubleGaugeDecr, 1e-10);
                counter++;
            }

            // Validate tags
            for (int i = 0; i < tagNames.Length; i++)
            {
                rb.Validate(tagNames[i], tagValues[i]);
            }

            // Validate rate
            rb.Validate(rateName + "-Num", samples.Length);
            rb.Validate(rateName + "-RunningAvg", samples.Sum() / samples.Length, 1e-10);         
        }
예제 #17
0
        public void TestDefaultMetricsSourceGetMetricsFlag()
        {
            const string evalId = "evalId";
            const string taskId = "taskId";
            const string sourceContext = "test";
            const string recordName = "testrecord";
            string[] counterNames = { "cname1", "cname2" };
 
            DefaultMetricsSourceImpl metricsSource = TangFactory.GetTang().NewInjector(
                GenerateMetricsSourceConfiguration(evalId, taskId, sourceContext, recordName))
                .GetInstance<DefaultMetricsSourceImpl>();

            MetricTestUtils.MetricsCollectorTestImpl collector = new MetricTestUtils.MetricsCollectorTestImpl();

            // Create Counters
            foreach (var name in counterNames)
            {
                metricsSource.Counters.Create(name, name);
            }

            metricsSource.GetMetrics(collector, true);

            // Do not increment second counter.
            metricsSource.Counters[counterNames[0]].Increment();
            
            // Verifies that record builder does not get old stats if the flag is set to false. 
            // Only first counter should be present.
            metricsSource.GetMetrics(collector, false);
            var rb = collector.CurrentRecordBuilder;
            Assert.True(rb.LongKeyPresent(counterNames[0]), "Counter was updated, so it should be present");
            Assert.False(rb.LongKeyPresent(counterNames[1]), "Counter was not updated, so it should not be present");
        }
예제 #18
0
        public void TestMetricsRecordBuilder()
        {
            const string   name        = "collName";
            const string   desc        = "coll desc";
            const string   context     = "context";
            IList <string> counterName = new List <string>()
            {
                "counter1", "counter2"
            };
            IList <long> counterValue = new List <long>()
            {
                2, 6
            };
            const string   doubleGaugeName  = "doublegauge";
            const double   doubleGaugeValue = 3.0;
            const string   longGaugeName    = "longgauge";
            const long     longGaugeValue   = 4;
            IList <string> tagName          = new List <string>()
            {
                "tagName1", "tagName2", "tagName3"
            };
            IList <string> tagValue = new List <string>()
            {
                "tagValue1", "tagValue2", "tagValue3"
            };
            IList <string> tagDesc = new List <string>()
            {
                "tagName1", "tagDesc2", "tagDesc3"
            };

            var collector           = new MetricTestUtils.MetricsCollectorTestImpl();
            MetricsRecordBuilder rb = new MetricsRecordBuilder(collector, new MetricsInfoImpl(name, desc));

            // Push different metrics via relevant function calls.
            rb.AddCounter(new MetricsInfoImpl(counterName[0], counterName[0]), counterValue[0])
            .Add(new ImmutableCounter(new MetricsInfoImpl(counterName[1], counterName[1]), counterValue[1]))
            .AddGauge(new MetricsInfoImpl(longGaugeName, longGaugeName), longGaugeValue)
            .AddGauge(new MetricsInfoImpl(doubleGaugeName, doubleGaugeName), doubleGaugeValue)
            .AddTag(tagName[0], tagValue[0])
            .AddTag(new MetricsInfoImpl(tagName[1], tagDesc[1]), tagValue[1])
            .Add(new MetricsTag(new MetricsInfoImpl(tagName[2], tagDesc[2]), tagValue[2]))
            .SetContext(context)
            .EndRecord();

            TimeSpan t = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1));
            long     expectedTimeStamp = (long)t.TotalSeconds;

            // Get the created record.
            var record = rb.GetRecord();

            Assert.Equal(name, record.Name);
            Assert.Equal(desc, record.Description);
            Assert.True(record.Timestamp >= expectedTimeStamp);
            Assert.Equal(context, record.Context);

            int counter = 0;

            Assert.Equal(tagName.Count + 1, record.Tags.Count());

            // Verify that relevant tags are present.
            foreach (var tag in record.Tags)
            {
                if (counter < tagName.Count)
                {
                    Assert.Equal(tagName[counter], tag.Name);
                    Assert.Equal(tagValue[counter], tag.Value);
                    Assert.Equal(tagDesc[counter], tag.Description);
                }
                else
                {
                    Assert.Equal(MetricsSystemConstants.Context, tag.Name);
                    Assert.Equal(MetricsSystemConstants.Context, tag.Description);
                    Assert.Equal(context, tag.Value);
                }
                counter++;
            }

            counter = 0;
            Assert.Equal(counterName.Count + 2, record.Metrics.Count());

            // Verify that relevant counters and gauges are there.
            foreach (var metric in record.Metrics)
            {
                if (counter < 2)
                {
                    Assert.Equal(MetricType.Counter, metric.TypeOfMetric);
                    Assert.Equal(counterName[counter], metric.Info.Name);
                    Assert.Equal(counterValue[counter], metric.LongValue);
                }
                else
                {
                    Assert.Equal(MetricType.Gauge, metric.TypeOfMetric);

                    if (counter == 2)
                    {
                        Assert.Equal(longGaugeName, metric.Info.Name);
                        Assert.Equal(longGaugeValue, metric.LongValue);
                    }
                    else
                    {
                        Assert.Equal(doubleGaugeName, metric.Info.Name);
                        Assert.Equal(doubleGaugeValue, metric.NumericValue);
                    }
                }
                counter++;
            }
        }
예제 #19
0
        public void TestDefaultMetricsSourceDoubleGauges()
        {
            const string evalId = "evalId";
            const string taskId = "taskId";
            const string sourceContext = "test";
            const string recordName = "testrecord";
            string[] doubleGaugeNames = { "dname1", "dname2" };
            const double doubleGaugeDecr = 1.2;

            DefaultMetricsSourceImpl metricsSource = TangFactory.GetTang().NewInjector(
                GenerateMetricsSourceConfiguration(evalId, taskId, sourceContext, recordName))
                .GetInstance<DefaultMetricsSourceImpl>();

            MetricTestUtils.MetricsCollectorTestImpl collector = new MetricTestUtils.MetricsCollectorTestImpl();

            // Create double gauge
            foreach (var name in doubleGaugeNames)
            {
                metricsSource.DoubleGauges.Create(name, name);
            }

            // Update double gauge
            int counter = 1;
            foreach (var name in doubleGaugeNames)
            {
                metricsSource.DoubleGauges[name].Decrement(counter * doubleGaugeDecr);
                counter++;
            }
        
            metricsSource.GetMetrics(collector, true);
            var rb = collector.CurrentRecordBuilder;

            // Validate double gauges
            counter = 1;
            foreach (var name in doubleGaugeNames)
            {
                rb.Validate(name, -counter * doubleGaugeDecr, 1e-10);
                counter++;
            }
        }