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

66 lines
1.3 KiB
Go

// +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", "search"},
)
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, search string, duration time.Duration) {
in.reqDur.WithLabelValues(section, search).Observe(duration.Seconds())
}