Add instrumentation with prometheus
This commit is contained in:
parent
2a72154308
commit
f4ca9e2dbc
6 changed files with 138 additions and 32 deletions
14
lib/instrument/dummy.go
Normal file
14
lib/instrument/dummy.go
Normal file
|
@ -0,0 +1,14 @@
|
|||
// +build noprometheus
|
||||
|
||||
package instrument
|
||||
|
||||
import "time"
|
||||
|
||||
type dummyInst struct{}
|
||||
|
||||
func Init() Instrument {
|
||||
return &dummyInst{}
|
||||
}
|
||||
|
||||
func (i dummyInst) Visit(section string, id string, search string, fmt string) {}
|
||||
func (i dummyInst) Duration(section string, duration time.Duration) {}
|
8
lib/instrument/instrument.go
Normal file
8
lib/instrument/instrument.go
Normal file
|
@ -0,0 +1,8 @@
|
|||
package instrument
|
||||
|
||||
import "time"
|
||||
|
||||
type Instrument interface {
|
||||
Visit(section string, id string, search string, fmt string)
|
||||
Duration(section string, duration time.Duration)
|
||||
}
|
66
lib/instrument/prometheus.go
Normal file
66
lib/instrument/prometheus.go
Normal file
|
@ -0,0 +1,66 @@
|
|||
// +build !noprometheus
|
||||
|
||||
package instrument
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
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.",
|
||||
},
|
||||
[]string{"section"},
|
||||
)
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
func (in promInst) Visit(section string, id string, search string, fmt string) {
|
||||
in.visits.WithLabelValues(section, id, search, fmt).Inc()
|
||||
}
|
||||
|
||||
func (in promInst) Duration(section string, duration time.Duration) {
|
||||
in.reqDur.WithLabelValues(section).Observe(duration.Seconds())
|
||||
}
|
Reference in a new issue