This repository has been archived on 2025-03-01. You can view files and clone it, but cannot push or open issues or pull requests.
trantor/lib/instrument/prometheus.go

62 lines
1.2 KiB
Go
Raw Normal View History

2017-06-05 16:17:14 +00:00
// +build !noprometheus
package instrument
import (
log "github.com/cihub/seelog"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
promAddr = ":9123"
)
type promInst struct {
visits *prometheus.CounterVec
reqDur *prometheus.HistogramVec
}
func Init() Instrument {
go promHandle()
visits := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "trantor_visits_total",
Help: "Number of visits.",
},
[]string{"section", "id", "search", "fmt"},
)
reqDur := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "trantor_request_duration_seconds",
Help: "Duration of the request in seconds.",
},
2017-06-07 22:00:17 +00:00
[]string{"section"},
2017-06-05 16:17:14 +00:00
)
prometheus.MustRegister(visits)
prometheus.MustRegister(reqDur)
return &promInst{
visits: visits,
reqDur: reqDur,
}
}
func promHandle() {
server := http.Server{
Addr: promAddr,
Handler: promhttp.Handler(),
}
log.Error(server.ListenAndServe())
}
2017-06-07 22:00:17 +00:00
func (in promInst) Request(req RequestData) {
in.visits.WithLabelValues(req.Section, req.ID, req.Search, req.Fmt).Inc()
in.reqDur.WithLabelValues(req.Section).Observe(req.Duration.Seconds())
2017-06-05 16:17:14 +00:00
}