// +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()) }