Skip to content

Commit ab04b0e

Browse files
committed
feature: add estimate sum for go histogram
1 parent 704aa40 commit ab04b0e

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

go_metrics.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ func writeRuntimeHistogramMetric(w io.Writer, name string, h *runtimemetrics.Flo
165165

166166
totalCount := uint64(0)
167167
iNext := 0.0
168+
sum := float64(0)
168169
WriteMetadataIfNeeded(w, name, "histogram")
169170
for i, count := range counts {
170171
totalCount += count
@@ -175,9 +176,14 @@ func writeRuntimeHistogramMetric(w io.Writer, name string, h *runtimemetrics.Flo
175176
fmt.Fprintf(w, `%s_bucket{le="%g"} %d`+"\n", name, le, totalCount)
176177
}
177178
}
179+
if count != 0 {
180+
sum += buckets[i] * float64(count)
181+
}
178182
}
179183
totalCount += tailCount
180184
fmt.Fprintf(w, `%s_bucket{le="+Inf"} %d`+"\n", name, totalCount)
185+
fmt.Fprintf(w, `%s_sum %g`+"\n", name, sum)
186+
fmt.Fprintf(w, `%s_count %d`+"\n", name, totalCount)
181187
}
182188

183189
// Limit the number of buckets for Go runtime histograms in order to prevent from high cardinality issues at scraper side.

go_metrics_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ func TestWriteRuntimeHistogramMetricOk(t *testing.T) {
3535
foo_bucket{le="3"} 3
3636
foo_bucket{le="4"} 6
3737
foo_bucket{le="+Inf"} 6
38+
foo_sum 14
39+
foo_count 6
3840
`)
3941

4042
f(&runtimemetrics.Float64Histogram{
@@ -44,6 +46,8 @@ foo_bucket{le="+Inf"} 6
4446
foo_bucket{le="3"} 25
4547
foo_bucket{le="4"} 26
4648
foo_bucket{le="+Inf"} 26
49+
foo_sum 53
50+
foo_count 26
4751
`)
4852

4953
f(&runtimemetrics.Float64Histogram{
@@ -59,6 +63,8 @@ foo_bucket{le="8"} 88
5963
foo_bucket{le="9"} 220
6064
foo_bucket{le="10"} 230
6165
foo_bucket{le="+Inf"} 230
66+
foo_sum 1580
67+
foo_count 230
6268
`)
6369

6470
f(&runtimemetrics.Float64Histogram{
@@ -67,5 +73,7 @@ foo_bucket{le="+Inf"} 230
6773
}, `foo_bucket{le="4"} 1
6874
foo_bucket{le="5"} 6
6975
foo_bucket{le="+Inf"} 6
76+
foo_sum -Inf
77+
foo_count 6
7078
`)
7179
}

0 commit comments

Comments
 (0)