Use sql database instead of bolt
This commit is contained in:
17
metrics.go
17
metrics.go
@@ -5,12 +5,11 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"gitea.hashru.nl/dsprenkels/rushlink/internal/boltdb"
|
||||
"gitea.hashru.nl/dsprenkels/rushlink/internal/db"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
bolt "go.etcd.io/bbolt"
|
||||
)
|
||||
|
||||
const metricNamespace = "rushlink"
|
||||
@@ -22,14 +21,14 @@ var metricRequestsTotalCounter = prometheus.NewCounterVec(prometheus.CounterOpts
|
||||
Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",
|
||||
}, []string{"code", "method"})
|
||||
|
||||
func metricURLsTotal(database *boltdb.Database) float64 {
|
||||
func metricURLsTotal(database *db.Database) float64 {
|
||||
var metric float64
|
||||
if err := database.Bolt.View(func(tx *bolt.Tx) error {
|
||||
bucket := tx.Bucket([]byte("pastes"))
|
||||
if bucket == nil {
|
||||
return errors.New("bucket 'pastes' could not be found")
|
||||
if err := database.Transaction(func(tx *db.Database) error {
|
||||
var count int64
|
||||
if err := database.Model(&db.Paste{}).Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
metric = float64(bucket.Stats().KeyN)
|
||||
metric = float64(count)
|
||||
return nil
|
||||
}); err != nil {
|
||||
log.Printf("error: %v", errors.Wrap(err, "fetching pastes_total metric"))
|
||||
@@ -39,7 +38,7 @@ func metricURLsTotal(database *boltdb.Database) float64 {
|
||||
}
|
||||
|
||||
// StartMetricsServer starts sering Prometheus metrics exports on addr
|
||||
func StartMetricsServer(addr string, database *boltdb.Database, fs *boltdb.FileStore) {
|
||||
func StartMetricsServer(addr string, database *db.Database, fs *db.FileStore) {
|
||||
prometheus.MustRegister(metricRequestsTotalCounter)
|
||||
|
||||
prometheus.MustRegister(prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
||||
|
||||
Reference in New Issue
Block a user