BREAKING: Replace metrics

This commit is contained in:
TwiN
2021-12-13 19:24:18 -05:00
parent eb9acef9b5
commit b67701ff6d
3 changed files with 228 additions and 113 deletions

View File

@ -1,9 +1,7 @@
package metric
import (
"fmt"
"strconv"
"sync"
"github.com/TwiN/gatus/v3/core"
"github.com/prometheus/client_golang/prometheus"
@ -11,24 +9,19 @@ import (
)
var (
gauges = map[string]*prometheus.GaugeVec{}
rwLock sync.RWMutex
// This will be initialized once PublishMetricsForEndpoint.
// The reason why we're doing this is that if metrics are disabled, we don't want to initialize it unnecessarily.
resultCount *prometheus.CounterVec = nil
)
// PublishMetricsForEndpoint publishes metrics for the given endpoint and its result.
// These metrics will be exposed at /metrics if the metrics are enabled
func PublishMetricsForEndpoint(endpoint *core.Endpoint, result *core.Result) {
rwLock.Lock()
gauge, exists := gauges[fmt.Sprintf("%s_%s", endpoint.Name, endpoint.URL)]
if !exists {
gauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
Subsystem: "gatus",
Name: "tasks",
// TODO: remove the "service" key in v4.0.0, as it is only kept for backward compatibility
ConstLabels: prometheus.Labels{"service": endpoint.Name, "endpoint": endpoint.Name, "url": endpoint.URL},
}, []string{"status", "success"})
gauges[fmt.Sprintf("%s_%s", endpoint.Name, endpoint.URL)] = gauge
if resultCount == nil {
resultCount = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "gatus_results_total",
Help: "Number of results per endpoint",
}, []string{"key", "group", "name", "success"})
}
rwLock.Unlock()
gauge.WithLabelValues(strconv.Itoa(result.HTTPStatus), strconv.FormatBool(result.Success)).Inc()
resultCount.WithLabelValues(endpoint.Key(), endpoint.Group, endpoint.Name, strconv.FormatBool(result.Success)).Inc()
}