Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Histogram Prometheus-compatible BUG #32

Open
dylanoy opened this issue Nov 18, 2020 · 1 comment
Open

Histogram Prometheus-compatible BUG #32

dylanoy opened this issue Nov 18, 2020 · 1 comment

Comments

@dylanoy
Copy link

dylanoy commented Nov 18, 2020

code

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"time"

	"go.uber.org/net/metrics"
)

func main() {
	// First, construct a root and add some metrics.
	root := metrics.New()

	h, err := root.Scope().Histogram(metrics.HistogramSpec{
		Spec: metrics.Spec{
			Name: "selects_latency_ms",    // required, should indicate unit
			Help: "SELECT query latency.", // required
		},
		Unit:    time.Millisecond,                      // required
		Buckets: []int64{5, 10, 25, 50, 100, 200, 500}, // required
	})
	if err != nil {
		panic(err)
	}

	h.IncBucket(5)
	h.IncBucket(501)

	// Expose the root on your HTTP server of choice.
	mux := http.NewServeMux()
	mux.Handle("/debug/net/metrics", root)
	srv := httptest.NewServer(mux)
	defer srv.Close()

	// Your metrics are now exposed via a Prometheus-compatible handler. This
	// example shows text output, but clients can also request the protocol
	// buffer binary format.
	res, err := http.Get(fmt.Sprintf("%v/debug/net/metrics", srv.URL))
	if err != nil {
		panic(err)
	}
	text, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		panic(err)
	}
	fmt.Println(string(text))

}

output

# HELP selects_latency_ms SELECT query latency.
# TYPE selects_latency_ms histogram
selects_latency_ms_bucket{host="db01",table="trips",le="5"} 2
selects_latency_ms_bucket{host="db01",table="trips",le="10"} 2
selects_latency_ms_bucket{host="db01",table="trips",le="25"} 2
selects_latency_ms_bucket{host="db01",table="trips",le="50"} 2
selects_latency_ms_bucket{host="db01",table="trips",le="100"} 2
selects_latency_ms_bucket{host="db01",table="trips",le="200"} 2
selects_latency_ms_bucket{host="db01",table="trips",le="500"} 2
selects_latency_ms_bucket{host="db01",table="trips",le="+Inf"} 2
selects_latency_ms_sum{host="db01",table="trips"} 506
selects_latency_ms_count{host="db01",table="trips"} 2

system macos
golang 1.15

@dylanoy
Copy link
Author

dylanoy commented Nov 18, 2020

BUG on file
go.uber.org/net/[email protected]/histogram.go:162

func (h *Histogram) metric() *promproto.Metric {
	n := uint64(0)
	promBuckets := make([]*promproto.Bucket, 0, len(h.buckets)-1)
	for _, b := range h.buckets {
		n += uint64(b.Load())
		if b.upper == math.MaxInt64 {
			// Prometheus doesn't want us to export the final catch-all bucket.
			continue
		}
		upper := float64(b.upper)
		promBuckets = append(promBuckets, &promproto.Bucket{
			CumulativeCount: &n,
			UpperBound:      &upper,
		})
	}

	sum := float64(h.sum.Load())
	return &promproto.Metric{
		Label: h.tagPairs,
		Histogram: &promproto.Histogram{
			SampleCount: &n,
			SampleSum:   &sum,
			Bucket:      promBuckets,
		},
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant