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
code
output
system macos
golang 1.15