Skip to content

Commit

Permalink
Merge pull request #119 from FachschaftMathPhysInfo/prometheus-metrics
Browse files Browse the repository at this point in the history
Prometheus Metrics
  • Loading branch information
christian-heusel authored Jan 9, 2023
2 parents 61fee10 + 2402d1a commit 620ff90
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
7 changes: 6 additions & 1 deletion server/graph/schema.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/dustin/go-humanize"
"github.com/gabriel-vasile/mimetype"
minio "github.com/minio/minio-go/v7"
"github.com/satori/go.uuid"
uuid "github.com/satori/go.uuid"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -138,6 +138,9 @@ func (r *mutationResolver) CreateExam(ctx context.Context, input model.NewExam)
return nil, uploadErr
}

// update the TotalExams metric
utils.UpdateTotalExamsMetric(r.DB)

return &exam, nil
}

Expand Down Expand Up @@ -202,6 +205,8 @@ func (r *mutationResolver) RequestMarkedExam(ctx context.Context, stringUUID str
return nil, err
}

utils.ExamsMarkedMetric.Inc()

return &stringUUID, nil
}

Expand Down
10 changes: 8 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ func main() {
}

router := chi.NewRouter()
m := chiprometheus.NewMiddleware("altklausur_web_service")

router.Use(m)
prometheusMiddleware := chiprometheus.NewMiddleware("altklausur_web_service")
router.Use(prometheusMiddleware)

// Add CORS middleware around every request
// See https://github.com/rs/cors for full option listing
router.Use(cors.New(cors.Options{
Expand Down Expand Up @@ -104,7 +105,12 @@ func main() {
r.Handle("/query", srv)
r.Get("/adminlogin", authHelper.AdminLoginHandler)
})

// set the TotalExams metric initially
utils.UpdateTotalExamsMetric(db)

router.Handle("/metrics", promhttp.Handler())

fmt.Print(
"==========================================\n",
"Started the backend listening on Port "+port+"\n",
Expand Down
29 changes: 29 additions & 0 deletions utils/prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package utils

import (
"github.com/FachschaftMathPhysInfo/altklausur-ausleihe/server/graph/model"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"gorm.io/gorm"
)

var (
// ExamsMarkedMetric is a prometheus metric for the amount of requested exams for watermarking
ExamsMarkedMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "altklausur_ausleihe_exams_marked",
Help: "The total number of requested exams",
})

// TotalExamsMetric is a prometheus metric for the total amount of exams in the database
TotalExamsMetric = promauto.NewGauge(prometheus.GaugeOpts{
Name: "altklausur_ausleihe_exams_total",
Help: "The total number of exams",
})
)

// UpdateTotalExamsMetric updates the TotalExamsMetric with the current value from the database
func UpdateTotalExamsMetric(database *gorm.DB) {
var count int64
database.Model(&model.Exam{}).Count(&count)
TotalExamsMetric.Set(float64(count))
}

0 comments on commit 620ff90

Please sign in to comment.