feat: inc generic metric if error occurs #161

This commit is contained in:
2024-09-09 23:16:27 +02:00
parent 0e186940cc
commit 37c3b2a780
3 changed files with 33 additions and 14 deletions

View File

@@ -6,6 +6,9 @@ import (
"me-fit/types"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
type ContextKey string
@@ -14,6 +17,24 @@ const (
ContextKeyUser ContextKey = "user_id"
)
var (
errorMetric = promauto.NewCounter(
prometheus.CounterOpts{
Name: "mefit_error_total",
Help: "The total number of errors during processing",
},
)
)
func LogError(message string, err error) {
slog.Error(message + ": " + err.Error())
errorMetric.Inc()
}
func LogErrorMsg(message string) {
slog.Error(message)
errorMetric.Inc()
}
func DoRedirect(w http.ResponseWriter, r *http.Request, url string) {
isHtmx := r.Header.Get("HX-Request") == "true"
if isHtmx {