public void HistogramWithOnlySumCount()
        {
            var boundaries     = new double[] { };
            var histogramPoint = new MetricPoint(AggregationType.HistogramSumCount, DateTimeOffset.Now, null, null, boundaries);

            histogramPoint.Update(-10);
            histogramPoint.Update(0);
            histogramPoint.Update(1);
            histogramPoint.Update(9);
            histogramPoint.Update(10);
            histogramPoint.Update(11);
            histogramPoint.Update(19);

            histogramPoint.TakeSnapshot(true);

            var count = histogramPoint.GetHistogramCount();
            var sum   = histogramPoint.GetHistogramSum();

            // Sum of all recordings
            Assert.Equal(40, sum);

            // Count  = # of recordings
            Assert.Equal(7, count);

            // There should be no enumeration of BucketCounts and ExplicitBounds for HistogramSumCount
            var enumerator = histogramPoint.GetHistogramBuckets().GetEnumerator();

            Assert.False(enumerator.MoveNext());
        }
        public void HistogramDistributeToAllBucketsCustom()
        {
            var boundaries     = new double[] { 10, 20 };
            var histogramPoint = new MetricPoint(AggregationType.Histogram, DateTimeOffset.Now, null, null, boundaries);

            // 5 recordings <=10
            histogramPoint.Update(-10);
            histogramPoint.Update(0);
            histogramPoint.Update(1);
            histogramPoint.Update(9);
            histogramPoint.Update(10);

            // 2 recordings >10, <=20
            histogramPoint.Update(11);
            histogramPoint.Update(19);

            histogramPoint.TakeSnapshot(true);

            var count = histogramPoint.GetHistogramCount();
            var sum   = histogramPoint.GetHistogramSum();

            // Sum of all recordings
            Assert.Equal(40, sum);

            // Count  = # of recordings
            Assert.Equal(7, count);

            int index                = 0;
            int actualCount          = 0;
            var expectedBucketCounts = new long[] { 5, 2, 0 };

            foreach (var histogramMeasurement in histogramPoint.GetHistogramBuckets())
            {
                Assert.Equal(expectedBucketCounts[index], histogramMeasurement.BucketCount);
                index++;
                actualCount++;
            }

            Assert.Equal(boundaries.Length + 1, actualCount);
        }
Example #3
0
        public MetricsData(int version, Metric metric, MetricPoint metricPoint) : base(version)
        {
            if (metric == null)
            {
                throw new ArgumentNullException(nameof(metric));
            }

            IList <MetricDataPoint> metricDataPoints = new List <MetricDataPoint>();
            MetricDataPoint         metricDataPoint  = null;

            switch (metric.MetricType)
            {
            case MetricType.DoubleSum:
                metricDataPoint = new MetricDataPoint(metric.Name, metricPoint.GetSumDouble())
                {
                    DataPointType = DataPointType.Aggregation
                };
                break;

            case MetricType.DoubleGauge:
                metricDataPoint = new MetricDataPoint(metric.Name, metricPoint.GetGaugeLastValueDouble())
                {
                    DataPointType = DataPointType.Measurement
                };
                break;

            case MetricType.LongSum:
                // potential for minor precision loss implicitly going from long->double
                // see: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/numeric-conversions#implicit-numeric-conversions
                metricDataPoint = new MetricDataPoint(metric.Name, metricPoint.GetSumLong())
                {
                    DataPointType = DataPointType.Aggregation
                };
                break;

            case MetricType.LongGauge:
                // potential for minor precision loss implicitly going from long->double
                // see: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/numeric-conversions#implicit-numeric-conversions
                metricDataPoint = new MetricDataPoint(metric.Name, metricPoint.GetGaugeLastValueLong())
                {
                    DataPointType = DataPointType.Measurement
                };
                break;

            case MetricType.Histogram:
                metricDataPoint = new MetricDataPoint(metric.Name, metricPoint.GetHistogramSum());
                metricDataPoint.DataPointType = DataPointType.Aggregation;
                long histogramCount = metricPoint.GetHistogramCount();
                // Current schema only supports int values for count
                // if the value is within integer range we will use it otherwise ignore it.
                metricDataPoint.Count = (histogramCount <= int.MaxValue && histogramCount >= int.MinValue) ? (int?)histogramCount : null;
                break;
            }

            metricDataPoints.Add(metricDataPoint);
            Metrics    = metricDataPoints;
            Properties = new ChangeTrackingDictionary <string, string>();
            foreach (var tag in metricPoint.Tags)
            {
                if (tag.Key.Length <= SchemaConstants.MetricsData_Properties_MaxKeyLength && tag.Value != null)
                {
                    // Note: if Key exceeds MaxLength or if Value is null, the entire KVP will be dropped.

                    Properties.Add(new KeyValuePair <string, string>(tag.Key, tag.Value.ToString().Truncate(SchemaConstants.MetricsData_Properties_MaxValueLength)));
                }
            }
            Properties.Add(AggregationIntervalMsKey, DefaultExportIntervalMilliseconds);
        }