Beispiel #1
0
 public static TypedValue CreateTypedValue(
     IAggregation aggregation,
     IAggregationData aggregationData)
 {
     return(aggregationData.Match(
                v => new TypedValue {
         DoubleValue = v.Sum
     },                                               // Double
                v => new TypedValue {
         Int64Value = v.Sum
     },                                              // Long
                v => new TypedValue {
         Int64Value = v.Count
     },                                                // Count
                v => new TypedValue {
         DoubleValue = v.Count
     },                                                 // Mean
                v => new TypedValue {
         DistributionValue = CreateDistribution(v, ((IDistribution)aggregation).BucketBoundaries)
     },                                                                                                                    // Distribution
                v => new TypedValue {
         DoubleValue = v.LastValue
     },                                                     // LastValue Double
                v => new TypedValue {
         Int64Value = v.LastValue
     },                                                    // LastValue Long
                v => new TypedValue {
         BoolValue = false
     }));                                            // Default
 }
Beispiel #2
0
        public override IList <Metric> CreateMetrics(IViewData viewData, IAggregationData aggregation, TagValues tagValues, long timeStamp)
        {
            List <Metric> results = new List <Metric>();

            var unit = viewData.View.Measure.Unit;
            var tags = GetTagKeysAndValues(viewData.View.Columns, tagValues.Values);
            var name = viewData.View.Name.AsString;

            aggregation.Match <object>(
                (arg) =>
            {
                results.Add(new Metric(GetMetricName(name, string.Empty, tags), MetricType.GAUGE, timeStamp, unit, tags, arg.Sum));
                return(null);
            },
                (arg) =>
            {
                results.Add(new Metric(GetMetricName(name, string.Empty, tags), MetricType.GAUGE, timeStamp, unit, tags, arg.Sum));
                return(null);
            },
                (arg) =>
            {
                results.Add(new Metric(GetMetricName(name, string.Empty, tags), MetricType.GAUGE, timeStamp, unit, tags, arg.Count));
                return(null);
            },
                (arg) =>
            {
                results.Add(new Metric(GetMetricName(name, string.Empty, tags), MetricType.GAUGE, timeStamp, unit, tags, arg.Mean));
                return(null);
            },
                (arg) =>
            {
                results.Add(new Metric(GetMetricName(name, string.Empty, tags), MetricType.GAUGE, timeStamp, unit, tags, arg.Mean));
                results.Add(new Metric(GetMetricName(name, "max", tags), MetricType.GAUGE, timeStamp, unit, tags, arg.Max));
                results.Add(new Metric(GetMetricName(name, "min", tags), MetricType.GAUGE, timeStamp, unit, tags, arg.Min));
                var stdDeviation = Math.Sqrt((arg.SumOfSquaredDeviations / arg.Count) - 1);
                if (double.IsNaN(stdDeviation))
                {
                    stdDeviation = 0.0;
                }

                results.Add(new Metric(GetMetricName(name, "stddev", tags), MetricType.GAUGE, timeStamp, unit, tags, stdDeviation));
                return(null);
            },
                (arg) =>
            {
                results.Add(new Metric(GetMetricName(name, "value", tags), MetricType.GAUGE, timeStamp, unit, tags, arg.LastValue));
                return(null);
            },
                (arg) =>
            {
                results.Add(new Metric(GetMetricName(name, "value", tags), MetricType.GAUGE, timeStamp, unit, tags, arg.LastValue));
                return(null);
            },
                (arg) =>
            {
                return(null);
            });

            return(results);
        }
Beispiel #3
0
        internal static void AssertAggregationDataEquals(
            IAggregationData expected,
            IAggregationData actual,
            double tolerance)
        {
            expected.Match <object>(
                (arg) =>
            {
                Assert.IsType <SumDataDouble>(actual);
                Assert.InRange(((SumDataDouble)actual).Sum, arg.Sum - tolerance, arg.Sum + tolerance);
                return(null);
            },
                (arg) =>

            {
                Assert.IsType <SumDataLong>(actual);
                Assert.InRange(((SumDataLong)actual).Sum, arg.Sum - tolerance, arg.Sum + tolerance);
                return(null);
            },
                (arg) =>

            {
                Assert.IsType <CountData>(actual);
                Assert.Equal(arg.Count, ((CountData)actual).Count);
                return(null);
            },
                (arg) =>
            {
                Assert.IsType <MeanData>(actual);
                Assert.InRange(((MeanData)actual).Mean, arg.Mean - tolerance, arg.Mean + tolerance);
                return(null);
            },
                (arg) =>
            {
                Assert.IsType <DistributionData>(actual);
                AssertDistributionDataEquals(arg, (IDistributionData)actual, tolerance);
                return(null);
            },
                (arg) =>
            {
                Assert.IsType <LastValueDataDouble>(actual);
                Assert.InRange(((LastValueDataDouble)actual).LastValue, arg.LastValue - tolerance, arg.LastValue + tolerance);
                return(null);
            },
                (arg) =>
            {
                Assert.IsType <LastValueDataLong>(actual);
                Assert.Equal(arg.LastValue, ((LastValueDataLong)actual).LastValue);
                return(null);
            },
                (arg) =>
            {
                throw new ArgumentException();
            });
        }
Beispiel #4
0
        protected internal List <Metric> CreateMetrics(TagValues tagValues, IAggregationData agg, IViewData viewData, long timeStamp)
        {
            List <Metric> results = new List <Metric>();

            var unit      = viewData.View.Measure.Unit;
            var name      = viewData.View.Name.AsString;
            var tags      = GetTagKeysAndValues(viewData.View.Columns, tagValues.Values);
            var statistic = GetStatistic(viewData.View.Aggregation, viewData.View.Measure);

            agg.Match <object>(
                (arg) =>
            {
                if (statistic == "unknown")
                {
                    statistic = "total";
                }
                tags["statistic"] = statistic;
                results.Add(new Metric(name, MetricType.GAUGE, timeStamp, unit, tags, arg.Sum));
                return(null);
            },
                (arg) =>
            {
                if (statistic == "unknown")
                {
                    statistic = "total";
                }
                tags["statistic"] = statistic;
                results.Add(new Metric(name, MetricType.GAUGE, timeStamp, unit, tags, arg.Sum));
                return(null);
            },
                (arg) =>
            {
                if (statistic == "unknown")
                {
                    statistic = "count";
                }
                tags["statistic"] = statistic;
                results.Add(new Metric(name, MetricType.GAUGE, timeStamp, unit, tags, arg.Count));
                return(null);
            },
                (arg) =>
            {
                if (statistic == "unknown")
                {
                    statistic = "total";
                }

                IDictionary <string, string> copy = new Dictionary <string, string>(tags);
                copy["statistic"] = "count";
                results.Add(new Metric(name, MetricType.GAUGE, timeStamp, "count", copy, arg.Count));

                copy = new Dictionary <string, string>(tags);
                copy["statistic"] = "mean";
                results.Add(new Metric(name, MetricType.GAUGE, timeStamp, unit, copy, arg.Mean));

                tags["statistic"] = statistic;
                results.Add(new Metric(name, MetricType.GAUGE, timeStamp, unit, tags, arg.Count * arg.Mean));

                return(null);
            },
                (arg) =>
            {
                if (statistic == "unknown")
                {
                    statistic = "total";
                }
                IDictionary <string, string> copy = new Dictionary <string, string>(tags);
                copy["statistic"] = "count";
                results.Add(new Metric(name, MetricType.GAUGE, timeStamp, "count", copy, arg.Count));

                copy = new Dictionary <string, string>(tags);
                copy["statistic"] = "mean";
                results.Add(new Metric(name, MetricType.GAUGE, timeStamp, unit, copy, arg.Mean));

                copy = new Dictionary <string, string>(tags);
                copy["statistic"] = "max";
                results.Add(new Metric(name, MetricType.GAUGE, timeStamp, unit, copy, arg.Max));

                tags["statistic"] = statistic;
                results.Add(new Metric(name, MetricType.GAUGE, timeStamp, unit, tags, arg.Count * arg.Mean));

                return(null);
            },
                (arg) =>
            {
                if (statistic == "unknown")
                {
                    statistic = "value";
                }
                tags["statistic"] = statistic;
                results.Add(new Metric(name, MetricType.GAUGE, timeStamp, unit, tags, arg.LastValue));
                return(null);
            },
                (arg) =>
            {
                if (statistic == "unknown")
                {
                    statistic = "value";
                }
                tags["statistic"] = statistic;
                results.Add(new Metric(name, MetricType.GAUGE, timeStamp, unit, tags, arg.LastValue));
                return(null);
            },
                (arg) =>
            {
                return(null);
            });

            return(results);
        }
        private static void Sum(MutableAggregation combined, IAggregationData data)
        {
            data.Match <object>(
                (arg) =>
            {
                MutableSum sum = combined as MutableSum;
                if (sum != null)
                {
                    sum.Add(arg.Sum);
                }
                return(null);
            },
                (arg) =>
            {
                MutableSum sum = combined as MutableSum;
                if (sum != null)
                {
                    sum.Add(arg.Sum);
                }
                return(null);
            },
                (arg) =>
            {
                MutableCount count = combined as MutableCount;
                if (count != null)
                {
                    count.Add(arg.Count);
                }
                return(null);
            },
                (arg) =>
            {
                MutableMean mean = combined as MutableMean;
                if (mean != null)
                {
                    mean.Count = mean.Count + arg.Count;
                    mean.Sum   = mean.Sum + (arg.Count * arg.Mean);
                    if (arg.Min < mean.Min)
                    {
                        mean.Min = arg.Min;
                    }
                    if (arg.Max > mean.Max)
                    {
                        mean.Max = arg.Max;
                    }
                }
                return(null);
            },
                (arg) =>
            {
                MutableDistribution dist = combined as MutableDistribution;
                if (dist != null)
                {
                    // Algorithm for calculating the combination of sum of squared deviations:
                    // https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm.
                    if (dist.Count + arg.Count > 0)
                    {
                        double delta = arg.Mean - dist.Mean;
                        dist.SumOfSquaredDeviations =
                            dist.SumOfSquaredDeviations
                            + arg.SumOfSquaredDeviations
                            + Math.Pow(delta, 2)
                            * dist.Count
                            * arg.Count
                            / (dist.Count + arg.Count);
                    }

                    dist.Count += arg.Count;
                    dist.Sum   += (arg.Mean * arg.Count);
                    dist.Mean   = dist.Sum / dist.Count;

                    if (arg.Min < dist.Min)
                    {
                        dist.Min = arg.Min;
                    }
                    if (arg.Max > dist.Max)
                    {
                        dist.Max = arg.Max;
                    }

                    IList <long> bucketCounts = arg.BucketCounts;
                    for (int i = 0; i < bucketCounts.Count; i++)
                    {
                        dist.BucketCounts[i] += bucketCounts[i];
                    }
                }
                return(null);
            },
                (arg) =>
            {
                MutableLastValue lastValue = combined as MutableLastValue;
                if (lastValue != null)
                {
                    lastValue.initialized = true;
                    if (Double.IsNaN(lastValue.LastValue))
                    {
                        lastValue.LastValue = arg.LastValue;
                    }
                    else
                    {
                        lastValue.LastValue += arg.LastValue;
                    }
                }
                return(null);
            },
                (arg) =>
            {
                MutableLastValue lastValue = combined as MutableLastValue;
                if (lastValue != null)
                {
                    lastValue.initialized = true;
                    if (Double.IsNaN(lastValue.LastValue))
                    {
                        lastValue.LastValue = arg.LastValue;
                    }
                    else
                    {
                        lastValue.LastValue += arg.LastValue;
                    }
                }
                return(null);
            },
                (arg) =>
            {
                throw new ArgumentException();
            });
        }
Beispiel #6
0
        protected internal List <MetricSample> GetMetricSamples(IAggregationData agg, IViewData viewData)
        {
            List <MetricSample> results   = new List <MetricSample>();
            MetricStatistic     statistic = GetStatistic(viewData.View.Aggregation, viewData.View.Measure);

            agg.Match <object>(
                (arg) =>
            {
                if (statistic == MetricStatistic.UNKNOWN)
                {
                    statistic = MetricStatistic.TOTAL;
                }
                results.Add(new MetricSample(statistic, arg.Sum));
                return(null);
            },
                (arg) =>
            {
                if (statistic == MetricStatistic.UNKNOWN)
                {
                    statistic = MetricStatistic.TOTAL;
                }
                results.Add(new MetricSample(statistic, arg.Sum));
                return(null);
            },
                (arg) =>
            {
                if (statistic == MetricStatistic.UNKNOWN)
                {
                    statistic = MetricStatistic.COUNT;
                }
                results.Add(new MetricSample(statistic, arg.Count));
                return(null);
            },
                (arg) =>
            {
                results.Add(new MetricSample(MetricStatistic.COUNT, arg.Count));
                if (statistic == MetricStatistic.UNKNOWN)
                {
                    statistic = MetricStatistic.TOTAL;
                }
                results.Add(new MetricSample(statistic, arg.Mean * arg.Count));
                return(null);
            },
                (arg) =>
            {
                results.Add(new MetricSample(MetricStatistic.COUNT, arg.Count));
                results.Add(new MetricSample(MetricStatistic.MAX, arg.Max));
                if (statistic == MetricStatistic.UNKNOWN)
                {
                    statistic = MetricStatistic.TOTAL;
                }
                results.Add(new MetricSample(statistic, arg.Mean * arg.Count));
                return(null);
            },
                (arg) =>
            {
                if (statistic == MetricStatistic.UNKNOWN)
                {
                    statistic = MetricStatistic.VALUE;
                }
                results.Add(new MetricSample(statistic, arg.LastValue));
                return(null);
            },
                (arg) =>
            {
                if (statistic == MetricStatistic.UNKNOWN)
                {
                    statistic = MetricStatistic.VALUE;
                }
                results.Add(new MetricSample(statistic, arg.LastValue));
                return(null);
            },
                (arg) =>
            {
                return(null);
            });

            return(results);
        }