Move several packages to root package

This commit is contained in:
2019-11-09 15:50:12 +01:00
parent ab0cf15c7b
commit 1b3ba9443a
8 changed files with 28 additions and 38 deletions

53
metrics.go Normal file
View File

@@ -0,0 +1,53 @@
package rushlink
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"
)
const (
METRICS_ADDR = "127.0.0.1:58614"
)
func StartMetricsServer() {
var (
_ = promauto.NewGaugeFunc(prometheus.GaugeOpts{
Namespace: "rushlink",
Subsystem: "pastes",
Name: "urls_total",
Help: "The current amount of pastes in the database.",
}, func() float64 {
var metric float64
if err := DB.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte("pastes"))
if bucket == nil {
return errors.New("bucket 'pastes' could not be found")
}
metric = float64(bucket.Stats().KeyN)
return nil
}); err != nil {
log.Printf("error: %v", errors.Wrap(err, "fetching pastes_total metric"))
return 0
}
return metric
})
)
router := mux.NewRouter()
router.Handle("/metrics", promhttp.Handler()).Methods("GET")
srv := &http.Server{
Handler: router,
Addr: METRICS_ADDR,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}