AppMetrics / AppMetrics

App Metrics is an open-source and cross-platform .NET library used to record and report metrics within an application.

Home Page:https://app-metrics.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Prometheus Exporter][Timers] Sum is taken from histogram, while count is taken from rates

qklanski opened this issue · comments

When the Timer is exported to Prometheus, the summary is created. The count is properly treated as monotonic value (without resetting), while sum is taken only from histogram. When histogram is rescaled, the count and sum are non-consistent values.

        public static IEnumerable<Metric> ToPrometheusMetrics(
            this TimerValueSource metric,
            Func<string, string> labelNameFormatter)
        {
            // Prometheus advocates always using seconds as a base unit for time
            var rescaledVal = metric.Value.Scale(TimeUnit.Seconds, TimeUnit.Seconds);
            var result = new List<Metric>
                         {
                             new Metric
                             {
                                 summary = new Summary
                                           {
                                               sample_count = (ulong)rescaledVal.Rate.Count,
                                               sample_sum = rescaledVal.Histogram.Sum,
                                               quantile =
                                               {
                                                   new Quantile { quantile = 0.5, value = rescaledVal.Histogram.Median },
                                                   new Quantile { quantile = 0.75, value = rescaledVal.Histogram.Percentile75 },
                                                   new Quantile { quantile = 0.95, value = rescaledVal.Histogram.Percentile95 },
                                                   // new Quantile(){quantile = 0.98, value = metric.Value.Histogram.Percentile98},
                                                   new Quantile { quantile = 0.99, value = rescaledVal.Histogram.Percentile99 },
                                                   // new Quantile(){quantile = 0.999, value = metric.Value.Histogram.Percentile999}
                                               }
                                           },
                                 label = metric.Tags.ToLabelPairs(labelNameFormatter)
                             }
                         };

            return result;
        }

The both values (sum and count) should be calculated from one data source.