Skip to content

Commit

Permalink
Use total.recursion.time.avg to reconstruct the histogram sum.
Browse files Browse the repository at this point in the history
Wouter Wijngaards mentioned in the NLnet bug tracker that this metric
provides exactly the value that we need.
  • Loading branch information
Ed Schouten committed Dec 4, 2017
1 parent 0193520 commit 4f36729
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions unbound_exporter.go
Expand Up @@ -254,10 +254,10 @@ func newUnboundMetric(name string, description string, valueType prometheus.Valu
func CollectFromReader(file io.Reader, ch chan<- prometheus.Metric) error {
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
histogramPattern := regexp.MustCompile("^histogram\\.(\\d+\\.\\d+)\\.to\\.(\\d+\\.\\d+)$")
histogramPattern := regexp.MustCompile("^histogram\\.\\d+\\.\\d+\\.to\\.(\\d+\\.\\d+)$")

histogramCount := uint64(0)
histogramSum := float64(0)
histogramAvg := float64(0)
histogramBuckets := make(map[float64]uint64)

for scanner.Scan() {
Expand Down Expand Up @@ -286,22 +286,30 @@ func CollectFromReader(file io.Reader, ch chan<- prometheus.Metric) error {
}

if matches := histogramPattern.FindStringSubmatch(fields[0]); matches != nil {
begin, _ := strconv.ParseFloat(matches[1], 64)
end, _ := strconv.ParseFloat(matches[2], 64)
end, err := strconv.ParseFloat(matches[1], 64)
if err != nil {
return err
}
value, err := strconv.ParseUint(fields[1], 10, 64)

if err != nil {
return err
}
histogramBuckets[end] = value
histogramCount += value
// There are no real data points to calculate the sum in the unbound stats
// Therefore the mean latency times the amount of samples is calculated and summed
histogramSum += (end + begin) / 2 * float64(value)
} else if fields[0] == "total.recursion.time.avg" {
value, err := strconv.ParseFloat(fields[1], 64)
if err != nil {
return err
}
histogramAvg = value
}
}

// Convert the metrics to a cumulative prometheus histogram
// Convert the metrics to a cumulative Prometheus histogram.
// Reconstruct the sum of all samples from the average value
// provided by Unbound. Hopefully this does not break
// monotonicity.
keys := []float64{}
for k := range histogramBuckets {
keys = append(keys, k)
Expand All @@ -312,7 +320,11 @@ func CollectFromReader(file io.Reader, ch chan<- prometheus.Metric) error {
histogramBuckets[i] += prev
prev = histogramBuckets[i]
}
ch <- prometheus.MustNewConstHistogram(unboundHistogram, histogramCount, histogramSum, histogramBuckets)
ch <- prometheus.MustNewConstHistogram(
unboundHistogram,
histogramCount,
histogramAvg*float64(histogramCount),
histogramBuckets)

return scanner.Err()
}
Expand Down

0 comments on commit 4f36729

Please sign in to comment.