2019-11-09 15:50:12 +01:00
|
|
|
package rushlink
|
2019-09-10 17:52:45 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
bolt "go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
2019-11-09 21:43:51 +01:00
|
|
|
func StartMetricsServer(addr string) {
|
2019-09-10 17:52:45 +02:00
|
|
|
var (
|
|
|
|
_ = promauto.NewGaugeFunc(prometheus.GaugeOpts{
|
|
|
|
Namespace: "rushlink",
|
2019-09-15 22:54:07 +02:00
|
|
|
Subsystem: "pastes",
|
2019-09-10 17:52:45 +02:00
|
|
|
Name: "urls_total",
|
2019-09-15 22:54:07 +02:00
|
|
|
Help: "The current amount of pastes in the database.",
|
2019-09-10 17:52:45 +02:00
|
|
|
}, func() float64 {
|
|
|
|
var metric float64
|
2019-11-09 15:50:12 +01:00
|
|
|
if err := DB.View(func(tx *bolt.Tx) error {
|
2019-09-15 22:54:07 +02:00
|
|
|
bucket := tx.Bucket([]byte("pastes"))
|
2019-09-10 17:52:45 +02:00
|
|
|
if bucket == nil {
|
2019-09-15 22:54:07 +02:00
|
|
|
return errors.New("bucket 'pastes' could not be found")
|
2019-09-10 17:52:45 +02:00
|
|
|
}
|
|
|
|
metric = float64(bucket.Stats().KeyN)
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2019-09-15 22:54:07 +02:00
|
|
|
log.Printf("error: %v", errors.Wrap(err, "fetching pastes_total metric"))
|
2019-09-10 17:52:45 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return metric
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
router := mux.NewRouter()
|
|
|
|
router.Handle("/metrics", promhttp.Handler()).Methods("GET")
|
|
|
|
srv := &http.Server{
|
|
|
|
Handler: router,
|
2019-11-09 21:43:51 +01:00
|
|
|
Addr: addr,
|
2019-09-10 17:52:45 +02:00
|
|
|
WriteTimeout: 15 * time.Second,
|
|
|
|
ReadTimeout: 15 * time.Second,
|
|
|
|
}
|
|
|
|
log.Fatal(srv.ListenAndServe())
|
|
|
|
}
|