Modify _vmbucket suffix to _bucket suffix for histogram buckets in order to be compatible with Prometheus histograms

This commit is contained in:
Aliaksandr Valialkin 2019-11-23 11:47:06 +02:00
parent 577f3b5e99
commit 83922c2aa8
2 changed files with 8 additions and 8 deletions

View File

@ -28,7 +28,7 @@ import (
// Each bucket contains a counter for values in the given range.
// Each non-zero bucket is exposed with the following name:
//
// <metric_name>_vmbucket{<optional_tags>,vmrange="<start>...<end>"} <counter>
// <metric_name>_bucket{<optional_tags>,vmrange="<start>...<end>"} <counter>
//
// Where:
//
@ -40,9 +40,9 @@ import (
// Only non-zero buckets are exposed.
//
// Histogram buckets can be converted to Prometheus-like buckets in VictoriaMetrics
// with `prometheus_buckets(<metric_name>_vmbucket)`:
// with `prometheus_buckets(<metric_name>_bucket)`:
//
// histogram_quantile(0.95, prometheus_buckets(rate(request_duration_vmbucket[5m])))
// prometheus_buckets(rate(request_duration_bucket[5m]))
//
// Histogram cannot be used for negative values.
type Histogram struct {
@ -140,7 +140,7 @@ func (h *Histogram) marshalBucket(prefix string, w io.Writer, idx int) {
tag := fmt.Sprintf(`vmrange="%s...%s"`, start, end)
prefix = addTag(prefix, tag)
name, filters := splitMetricName(prefix)
fmt.Fprintf(w, "%s_vmbucket%s %d\n", name, filters, v)
fmt.Fprintf(w, "%s_bucket%s %d\n", name, filters, v)
}
func getBucketIdx(v float64) uint {

View File

@ -117,8 +117,8 @@ func TestHistogramSerial(t *testing.T) {
}
// Make sure the histogram prints <prefix>_xbucket on marshalTo call
testMarshalTo(t, h, "prefix", "prefix_vmbucket{vmrange=\"8e1...9e1\"} 7\nprefix_vmbucket{vmrange=\"9e1...1e2\"} 10\nprefix_vmbucket{vmrange=\"1e2...2e2\"} 100\nprefix_vmbucket{vmrange=\"2e2...3e2\"} 100\nprefix_vmbucket{vmrange=\"3e2...4e2\"} 23\nprefix_sum 48840\nprefix_count 240\n")
testMarshalTo(t, h, ` m{foo="bar"}`, "\t m_vmbucket{foo=\"bar\",vmrange=\"8e1...9e1\"} 7\n\t m_vmbucket{foo=\"bar\",vmrange=\"9e1...1e2\"} 10\n\t m_vmbucket{foo=\"bar\",vmrange=\"1e2...2e2\"} 100\n\t m_vmbucket{foo=\"bar\",vmrange=\"2e2...3e2\"} 100\n\t m_vmbucket{foo=\"bar\",vmrange=\"3e2...4e2\"} 23\n\t m_sum{foo=\"bar\"} 48840\n\t m_count{foo=\"bar\"} 240\n")
testMarshalTo(t, h, "prefix", "prefix_bucket{vmrange=\"8e1...9e1\"} 7\nprefix_bucket{vmrange=\"9e1...1e2\"} 10\nprefix_bucket{vmrange=\"1e2...2e2\"} 100\nprefix_bucket{vmrange=\"2e2...3e2\"} 100\nprefix_bucket{vmrange=\"3e2...4e2\"} 23\nprefix_sum 48840\nprefix_count 240\n")
testMarshalTo(t, h, ` m{foo="bar"}`, "\t m_bucket{foo=\"bar\",vmrange=\"8e1...9e1\"} 7\n\t m_bucket{foo=\"bar\",vmrange=\"9e1...1e2\"} 10\n\t m_bucket{foo=\"bar\",vmrange=\"1e2...2e2\"} 100\n\t m_bucket{foo=\"bar\",vmrange=\"2e2...3e2\"} 100\n\t m_bucket{foo=\"bar\",vmrange=\"3e2...4e2\"} 23\n\t m_sum{foo=\"bar\"} 48840\n\t m_count{foo=\"bar\"} 240\n")
// Verify supported ranges
for i := -100; i < 100; i++ {
@ -148,7 +148,7 @@ func TestHistogramConcurrent(t *testing.T) {
if err != nil {
t.Fatal(err)
}
testMarshalTo(t, h, "prefix", "prefix_vmbucket{vmrange=\"0...0\"} 5\nprefix_vmbucket{vmrange=\"9e-1...1\"} 5\nprefix_vmbucket{vmrange=\"1...2\"} 5\nprefix_vmbucket{vmrange=\"2...3\"} 5\nprefix_vmbucket{vmrange=\"3...4\"} 5\nprefix_vmbucket{vmrange=\"4...5\"} 5\nprefix_vmbucket{vmrange=\"5...6\"} 5\nprefix_vmbucket{vmrange=\"6...7\"} 5\nprefix_vmbucket{vmrange=\"7...8\"} 5\nprefix_vmbucket{vmrange=\"8...9\"} 5\nprefix_sum 225\nprefix_count 50\n")
testMarshalTo(t, h, "prefix", "prefix_bucket{vmrange=\"0...0\"} 5\nprefix_bucket{vmrange=\"9e-1...1\"} 5\nprefix_bucket{vmrange=\"1...2\"} 5\nprefix_bucket{vmrange=\"2...3\"} 5\nprefix_bucket{vmrange=\"3...4\"} 5\nprefix_bucket{vmrange=\"4...5\"} 5\nprefix_bucket{vmrange=\"5...6\"} 5\nprefix_bucket{vmrange=\"6...7\"} 5\nprefix_bucket{vmrange=\"7...8\"} 5\nprefix_bucket{vmrange=\"8...9\"} 5\nprefix_sum 225\nprefix_count 50\n")
}
func TestHistogramWithTags(t *testing.T) {
@ -159,7 +159,7 @@ func TestHistogramWithTags(t *testing.T) {
var bb bytes.Buffer
WritePrometheus(&bb, false)
result := bb.String()
namePrefixWithTag := `TestHistogram_vmbucket{tag="foo",vmrange="1e2...2e2"} 1` + "\n"
namePrefixWithTag := `TestHistogram_bucket{tag="foo",vmrange="1e2...2e2"} 1` + "\n"
if !strings.Contains(result, namePrefixWithTag) {
t.Fatalf("missing histogram %s in the WritePrometheus output; got\n%s", namePrefixWithTag, result)
}