public void MergeStatsEngine_Mix()
        {
            var metric5 = MetricWireModel.BuildMetric(_metricNameService, "DotNet/name", null, MetricDataWireModel.BuildTimingData(TimeSpan.FromSeconds(4), TimeSpan.FromSeconds(4)));
            var metric6 = MetricWireModel.BuildMetric(_metricNameService, "DotNet/another", null, MetricDataWireModel.BuildTimingData(TimeSpan.FromSeconds(8), TimeSpan.FromSeconds(6)));

            var engine1 = new MetricStatsCollection();
            MetricStatsDictionary <string, MetricDataWireModel> scoped1 = new MetricStatsDictionary <string, MetricDataWireModel>();

            scoped1.Merge("DotNet/name1", MetricDataWireModel.BuildTimingData(TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(1)), MetricDataWireModel.BuildAggregateData);
            scoped1.Merge("DotNet/name2", MetricDataWireModel.BuildTimingData(TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(2)), MetricDataWireModel.BuildAggregateData);
            engine1.MergeUnscopedStats(metric5);
            engine1.MergeScopedStats("engine1scope", scoped1);

            var engine2 = new MetricStatsCollection();
            MetricStatsDictionary <string, MetricDataWireModel> scoped2 = new MetricStatsDictionary <string, MetricDataWireModel>();

            scoped2.Merge("DotNet/name3", MetricDataWireModel.BuildTimingData(TimeSpan.FromSeconds(4), TimeSpan.FromSeconds(1)), MetricDataWireModel.BuildAggregateData);
            scoped2.Merge("DotNet/name4", MetricDataWireModel.BuildTimingData(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(2)), MetricDataWireModel.BuildAggregateData);
            engine1.MergeUnscopedStats(metric6);
            engine1.MergeScopedStats("engine2scope", scoped1);

            var engine3 = new MetricStatsCollection();

            engine3.Merge(engine1);
            engine3.Merge(engine2);

            IEnumerable <MetricWireModel> stats = engine3.ConvertToJsonForSending(_metricNameService);
            var count = 0;

            foreach (MetricWireModel current in stats)
            {
                count++;
            }
            Assert.AreEqual(6, count);
        }
        public void MergeScopedStats_TwoStatsSeparateEngines()
        {
            var metric1 = MetricWireModel.BuildMetric(_metricNameService, "name", "scope", MetricDataWireModel.BuildTimingData(TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(2)));
            var engine  = new MetricStatsCollection();
            MetricStatsDictionary <string, MetricDataWireModel> txStats1 = new MetricStatsDictionary <string, MetricDataWireModel>();

            txStats1.Merge(metric1.MetricName.Name, metric1.Data, MetricDataWireModel.BuildAggregateData);
            MetricStatsDictionary <string, MetricDataWireModel> txStats2 = new MetricStatsDictionary <string, MetricDataWireModel>();

            txStats2.Merge(metric1.MetricName.Name, metric1.Data, MetricDataWireModel.BuildAggregateData);
            engine.MergeScopedStats(metric1.MetricName.Scope, txStats1);
            engine.MergeScopedStats(metric1.MetricName.Scope, txStats2);

            IEnumerable <MetricWireModel> stats = engine.ConvertToJsonForSending(_metricNameService);
            var count = 0;

            foreach (MetricWireModel current in stats)
            {
                count++;
                Assert.AreEqual("name", current.MetricName.Name);
                Assert.AreEqual("scope", current.MetricName.Scope);
                Assert.AreEqual(2, current.Data.Value0);
                Assert.AreEqual(6, current.Data.Value1);
                Assert.AreEqual(4, current.Data.Value2);
            }
            Assert.AreEqual(1, count);
        }
        public void MergeUnscopeNotCreated_TwoDifferentSame()
        {
            var metric1 = MetricWireModel.BuildMetric(_metricNameService, "DotNet/name", null, MetricDataWireModel.BuildTimingData(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(4)));
            var metric2 = MetricWireModel.BuildMetric(_metricNameService, "DotNet/another", null, MetricDataWireModel.BuildTimingData(TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(2)));

            var engine = new MetricStatsCollection();

            engine.MergeUnscopedStats(metric1.MetricName.Name, metric1.Data);
            engine.MergeUnscopedStats(metric2.MetricName.Name, metric2.Data);
            IEnumerable <MetricWireModel> stats = engine.ConvertToJsonForSending(_metricNameService);
            var count = 0;

            foreach (MetricWireModel current in stats)
            {
                count++;
                if (current.MetricName.Name.Equals("DotNet/name"))
                {
                    Assert.AreEqual(1, current.Data.Value0);
                    Assert.AreEqual(5, current.Data.Value1);
                    Assert.AreEqual(4, current.Data.Value2);
                }
                else if (current.MetricName.Name.Equals("DotNet/another"))
                {
                    Assert.AreEqual(1, current.Data.Value0);
                    Assert.AreEqual(3, current.Data.Value1);
                    Assert.AreEqual(2, current.Data.Value2);
                }
                else
                {
                    Assert.Fail("Unexpected Metric: " + current.MetricName.Name);
                }
                Assert.AreEqual(null, current.MetricName.Scope);
            }
            Assert.AreEqual(2, count);
        }
        public void MergeScopedStats_VerifyRenaming()
        {
            IMetricNameService mNameService = Mock.Create <IMetricNameService>();

            Mock.Arrange(() => mNameService.RenameMetric(Arg.IsAny <string>())).Returns <string>(name => "IAmRenamed");
            var metric1     = MetricWireModel.BuildMetric(_metricNameService, "name", "myscope", MetricDataWireModel.BuildTimingData(TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(2)));
            var engine      = new MetricStatsCollection();
            var scopedStats = new MetricStatsDictionary <string, MetricDataWireModel>();

            scopedStats[metric1.MetricName.Name] = metric1.Data;
            engine.MergeScopedStats(metric1.MetricName.Scope, scopedStats);
            IEnumerable <MetricWireModel> stats = engine.ConvertToJsonForSending(mNameService);
            var count = 0;

            foreach (MetricWireModel current in stats)
            {
                count++;
                Assert.AreEqual("IAmRenamed", current.MetricName.Name);
                Assert.AreEqual("myscope", current.MetricName.Scope);
                Assert.AreEqual(1, current.Data.Value0);
                Assert.AreEqual(3, current.Data.Value1);
                Assert.AreEqual(2, current.Data.Value2);
            }
            Assert.AreEqual(1, count);
        }
Esempio n. 5
0
 public void Merge(MetricStatsCollection engine)
 {
     _unscopedStats.Merge(engine._unscopedStats, _mergeFunction);
     _preCreatedUnscopedStats.Merge(engine._preCreatedUnscopedStats, _mergeUnscopedFunction);
     foreach (KeyValuePair <string, MetricStatsDictionary <string, MetricDataWireModel> > current in engine._scopedStats)
     {
         MergeScopedStats(current.Key, current.Value);
     }
 }
            /// <summary>
            /// Take a stats engine (metrics aggregate Dictionary) off the queue and merge metric into it, then put it back
            /// on the queue.  Create one if all the existing ones are "checked out" -- others can use it later.
            ///
            /// We are one of (possibly) several readers of the stats engine queue under the readwrite lock, so harvest will
            /// wait for us to finish before fetching/replacing the entire queue
            /// </summary>
            /// <param name="statsEngineQueue"></param>
            /// <param name="metric"></param>
            private void MergeMetricUnderLock(Queue <MetricStatsCollection> statsEngineQueue, IAllMetricStatsCollection metrics)
            {
                MetricStatsCollection statsEngineToMergeWith = null;

                try
                {
                    lock (_queueReadersLock)
                    {
                        if (statsEngineQueue.Count > 0)
                        {
                            statsEngineToMergeWith = statsEngineQueue.Dequeue();
                        }
                    }

                    // make a new stats engine if there aren't enough to go around right now
                    if (statsEngineToMergeWith == null)
                    {
                        statsEngineToMergeWith = CreateMetricStatsEngine();
                        Interlocked.Increment(ref _statsEngineCount);
                    }

                    metrics.AddMetricsToEngine(statsEngineToMergeWith);
                }
                catch (Exception e)
                {
                    Log.Warn($"Exception dequeueing/creating stats engine: {e}");
                }
                finally
                {
                    if (statsEngineToMergeWith != null)
                    {
                        try
                        {
                            lock (_queueReadersLock)
                            {
                                statsEngineQueue.Enqueue(statsEngineToMergeWith);
                            }
                        }
                        catch (Exception e)
                        {
                            // should never happen
                            Log.Warn($"Exception returning stats engine to queue: {e}");
                        }
                    }
                }
            }
            private MetricStatsCollection GetStatsEngineForHarvest(Queue <MetricStatsCollection> statsEngines)
            {
                MetricStatsCollection harvestMetricsStatsEngine = CreateMetricStatsEngine();

                int actualStatsEngineCount = 0;

                foreach (MetricStatsCollection statsEngine in statsEngines)
                {
                    harvestMetricsStatsEngine.Merge(statsEngine);
                    actualStatsEngineCount++;
                }

                if (actualStatsEngineCount != _statsEngineCount)
                {
                    Log.Warn($"Error draining stats engine queue. Expected: {_statsEngineCount} actual: {actualStatsEngineCount}");
                }

                return(harvestMetricsStatsEngine);
            }
        public void MergeScopedStats_OneStat_StringData()
        {
            var metric1 = MetricWireModel.BuildMetric(_metricNameService, "DotNet/name", "myScope", MetricDataWireModel.BuildTimingData(TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(2)));
            var engine  = new MetricStatsCollection();

            engine.MergeScopedStats(metric1.MetricName.Scope, metric1.MetricName.Name, metric1.Data);
            IEnumerable <MetricWireModel> stats = engine.ConvertToJsonForSending(_metricNameService);
            var count = 0;

            foreach (MetricWireModel current in stats)
            {
                count++;
                Assert.AreEqual("DotNet/name", current.MetricName.Name);
                Assert.AreEqual("myScope", current.MetricName.Scope);
                Assert.AreEqual(1, current.Data.Value0);
                Assert.AreEqual(3, current.Data.Value1);
                Assert.AreEqual(2, current.Data.Value2);
            }
            Assert.AreEqual(1, count);
        }
        public void MergeScopedStats_TwoDifferentSame()
        {
            var metric1 = MetricWireModel.BuildMetric(_metricNameService, "DotNet/name", "scope", MetricDataWireModel.BuildTimingData(TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(1)));
            var metric2 = MetricWireModel.BuildMetric(_metricNameService, "DotNet/another", "scope", MetricDataWireModel.BuildTimingData(TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(2)));

            var engine = new MetricStatsCollection();
            MetricStatsDictionary <string, MetricDataWireModel> txStats1 = new MetricStatsDictionary <string, MetricDataWireModel>();

            txStats1.Merge(metric1.MetricName.Name, metric1.Data, MetricDataWireModel.BuildAggregateData);
            MetricStatsDictionary <string, MetricDataWireModel> txStats2 = new MetricStatsDictionary <string, MetricDataWireModel>();

            txStats2.Merge(metric2.MetricName.Name, metric2.Data, MetricDataWireModel.BuildAggregateData);
            engine.MergeScopedStats(metric2.MetricName.Scope, txStats1);
            engine.MergeScopedStats(metric2.MetricName.Scope, txStats2);

            IEnumerable <MetricWireModel> stats = engine.ConvertToJsonForSending(_metricNameService);
            var count = 0;

            foreach (MetricWireModel current in stats)
            {
                count++;
                if (current.MetricName.Name.Equals("DotNet/name"))
                {
                    Assert.AreEqual(1, current.Data.Value0);
                    Assert.AreEqual(2, current.Data.Value1);
                    Assert.AreEqual(1, current.Data.Value2);
                }
                else if (current.MetricName.Name.Equals("DotNet/another"))
                {
                    Assert.AreEqual(1, current.Data.Value0);
                    Assert.AreEqual(3, current.Data.Value1);
                    Assert.AreEqual(2, current.Data.Value2);
                }
                else
                {
                    Assert.Fail("Unexpected metric: " + current.MetricName.Name);
                }
                Assert.AreEqual("scope", current.MetricName.Scope);
            }
            Assert.AreEqual(2, count);
        }
        public void MergeUnscopedNotCreated_TwoStatsSame()
        {
            var metric1 = MetricWireModel.BuildMetric(_metricNameService, "name", null, MetricDataWireModel.BuildTimingData(TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(2)));
            var engine  = new MetricStatsCollection();

            engine.MergeUnscopedStats(metric1.MetricName.Name, metric1.Data);
            engine.MergeUnscopedStats(metric1.MetricName.Name, metric1.Data);
            IEnumerable <MetricWireModel> stats = engine.ConvertToJsonForSending(_metricNameService);
            var count = 0;

            foreach (MetricWireModel current in stats)
            {
                count++;
                Assert.AreEqual("name", current.MetricName.Name);
                Assert.AreEqual(null, current.MetricName.Scope);
                Assert.AreEqual(2, current.Data.Value0);
                Assert.AreEqual(6, current.Data.Value1);
                Assert.AreEqual(4, current.Data.Value2);
            }
            Assert.AreEqual(1, count);

            var metric2 = MetricWireModel.BuildMetric(_metricNameService, "name", null, MetricDataWireModel.BuildTimingData(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(4)));

            engine.MergeUnscopedStats(metric2.MetricName.Name, metric2.Data);
            engine.MergeUnscopedStats(metric2.MetricName.Name, metric2.Data);
            stats = engine.ConvertToJsonForSending(_metricNameService);

            count = 0;

            foreach (MetricWireModel current in stats)
            {
                count++;
                Assert.AreEqual("name", current.MetricName.Name);
                Assert.AreEqual(null, current.MetricName.Scope);
                Assert.AreEqual(4, current.Data.Value0);
                Assert.AreEqual(16, current.Data.Value1);
                Assert.AreEqual(12, current.Data.Value2);
            }
            Assert.AreEqual(1, count);
        }
Esempio n. 11
0
 public void AddMetricsToEngine(MetricStatsCollection engine)
 {
     Thread.Sleep(5);
     txStats.AddMetricsToEngine(engine);
 }
Esempio n. 12
0
 public void AddMetricsToEngine(MetricStatsCollection engine)
 {
     engine.MergeUnscopedStats(ConvertMetricNames(unscopedStats));
     engine.MergeScopedStats(transactionName.PrefixedName, ConvertMetricNames(scopedStats));
 }