Use sql database instead of bolt

This commit is contained in:
Daan Sprenkels
2020-10-25 17:33:51 +01:00
parent f36fa30eff
commit 0048004252
17 changed files with 1101 additions and 105 deletions

View File

@@ -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{