Пример #1
0
        public void Harvest_SendsReportedMetrics()
        {
            var sentMetrics = Enumerable.Empty <MetricWireModel>();

            Mock.Arrange(() => _dataTransportService.Send(Arg.IsAny <IEnumerable <MetricWireModel> >()))
            .DoInstead <IEnumerable <MetricWireModel> >(metrics => sentMetrics = metrics);

            _metricAggregator.Collect(MetricWireModel.BuildMetric(_metricNameService, "DotNet/metric1", null, MetricDataWireModel.BuildTimingData(TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(1))));
            _metricAggregator.Collect(MetricWireModel.BuildMetric(_metricNameService, "DotNet/metric2", "scope2", MetricDataWireModel.BuildTimingData(TimeSpan.FromSeconds(7), TimeSpan.FromSeconds(5))));

            _harvestAction();

            Assert.NotNull(sentMetrics);
            Assert.AreEqual(3, sentMetrics.Count());
            MetricWireModel sentMetric1 = null;
            MetricWireModel sentMetric2 = null;
            MetricWireModel sentMetric3 = null;

            foreach (MetricWireModel metric in sentMetrics)
            {
                if ("DotNet/metric1".Equals(metric.MetricName.Name))
                {
                    sentMetric1 = metric;
                }
                else if ("DotNet/metric2".Equals(metric.MetricName.Name))
                {
                    sentMetric2 = metric;
                }
                else if ("Supportability/MetricHarvest/transmit".Equals(metric.MetricName.Name))
                {
                    sentMetric3 = metric;
                }
                else
                {
                    Assert.Fail("Unexpected metric name " + metric.MetricName.Name);
                }
            }

            NrAssert.Multiple(
                () => Assert.AreEqual("DotNet/metric1", sentMetric1.MetricName.Name),
                () => Assert.AreEqual(null, sentMetric1.MetricName.Scope),
                () => Assert.AreEqual(1, sentMetric1.Data.Value0),
                () => Assert.AreEqual(3, sentMetric1.Data.Value1),
                () => Assert.AreEqual(1, sentMetric1.Data.Value2),
                () => Assert.AreEqual(3, sentMetric1.Data.Value3),
                () => Assert.AreEqual(3, sentMetric1.Data.Value4),
                () => Assert.AreEqual(9, sentMetric1.Data.Value5),

                () => Assert.AreEqual("DotNet/metric2", sentMetric2.MetricName.Name),
                () => Assert.AreEqual("scope2", sentMetric2.MetricName.Scope),
                () => Assert.AreEqual(1, sentMetric2.Data.Value0),
                () => Assert.AreEqual(7, sentMetric2.Data.Value1),
                () => Assert.AreEqual(5, sentMetric2.Data.Value2),
                () => Assert.AreEqual(7, sentMetric2.Data.Value3),
                () => Assert.AreEqual(7, sentMetric2.Data.Value4),
                () => Assert.AreEqual(49, sentMetric2.Data.Value5),

                () => Assert.AreEqual(MetricNames.SupportabilityMetricHarvestTransmit, sentMetric3.MetricName.Name),
                () => Assert.AreEqual(null, sentMetric3.MetricName.Scope),
                () => Assert.AreEqual(1, sentMetric3.Data.Value0),
                () => Assert.AreEqual(0, sentMetric3.Data.Value1),
                () => Assert.AreEqual(0, sentMetric3.Data.Value2),
                () => Assert.AreEqual(0, sentMetric3.Data.Value3),
                () => Assert.AreEqual(0, sentMetric3.Data.Value4),
                () => Assert.AreEqual(0, sentMetric3.Data.Value5)
                );
        }
Пример #2
0
        public void StatsEngineQueue_Is_Busy()
        {
            var dataTransportService  = Mock.Create <IDataTransportService>();
            var metricBuilder         = WireModels.Utilities.GetSimpleMetricBuilder();
            var outOfBandMetricSource = Mock.Create <IOutOfBandMetricSource>();
            var agentHealthReporter   = Mock.Create <IAgentHealthReporter>();
            var dnsStatic             = Mock.Create <IDnsStatic>();
            var processStatic         = Mock.Create <IProcessStatic>();

            var scheduler = Mock.Create <IScheduler>();

            Mock.Arrange(() => scheduler.ExecuteEvery(Arg.IsAny <Action>(), Arg.IsAny <TimeSpan>(), Arg.IsAny <TimeSpan?>()))
            .DoInstead <Action, TimeSpan, TimeSpan?>((action, _, __) => _harvestAction = action);
            _metricAggregator = new MetricAggregator(dataTransportService, metricBuilder, _metricNameService, new[] { outOfBandMetricSource }, processStatic, scheduler);

            EventBus <AgentConnectedEvent> .Publish(new AgentConnectedEvent());

            var sentMetrics = Enumerable.Empty <MetricWireModel>();

            Mock.Arrange(() => dataTransportService.Send(Arg.IsAny <IEnumerable <MetricWireModel> >()))
            .DoInstead <IEnumerable <MetricWireModel> >(metrics => sentMetrics = metrics);

            var           maxThreads = 25;
            List <Thread> threads    = new List <Thread>();

            for (int i = 0; i < maxThreads; i++)
            {
                var test = new TestMetricWireModel();
                test.CreateMetric(metricBuilder);

                Thread thread = new Thread(() =>
                {
                    _metricAggregator.Collect(test);
                });
                thread.IsBackground = true;
                threads.Add(thread);
                thread.Start();
            }

            for (int i = 0; i < maxThreads; i++)
            {
                threads[i].Join();
            }

            var queueCountBeforeHarvest = _metricAggregator.StatsEngineQueue.StatsEngineCount;

            //Check if the queue got queued up when multiple threads use the queue.

            if (queueCountBeforeHarvest == 1)
            {
                Assert.Inconclusive();
            }
            Assert.IsTrue(queueCountBeforeHarvest > 1);

            _harvestAction();

            var queueCountAfterHarvest = _metricAggregator.StatsEngineQueue.StatsEngineCount;

            //Check if the queue is empty after harvest.
            Assert.IsTrue(queueCountAfterHarvest == 0);

            //Check the number of metrics being sent up.
            Assert.IsTrue(sentMetrics.Count() == 3, "Count was " + sentMetrics.Count());
            // there should be one supportability and two DotNet (one scoped and one unscoped)
            string[] names = new string[] { "Supportability/MetricHarvest/transmit", "DotNet/test_metric" };
            foreach (MetricWireModel current in sentMetrics)
            {
                Assert.IsTrue(names.Contains(current.MetricName.Name), "Name is not present: " + current.MetricName.Name);
            }
        }