2019-11-16 21:46:52 +01:00
|
|
|
package metric
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-11-12 00:05:18 +01:00
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
|
2021-10-08 03:28:04 +02:00
|
|
|
"github.com/TwiN/gatus/v3/core"
|
2019-11-16 21:46:52 +01:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
gauges = map[string]*prometheus.GaugeVec{}
|
|
|
|
rwLock sync.RWMutex
|
|
|
|
)
|
|
|
|
|
2021-10-23 22:47:12 +02:00
|
|
|
// PublishMetricsForEndpoint publishes metrics for the given endpoint and its result.
|
2020-10-23 22:12:53 +02:00
|
|
|
// These metrics will be exposed at /metrics if the metrics are enabled
|
2021-10-23 22:47:12 +02:00
|
|
|
func PublishMetricsForEndpoint(endpoint *core.Endpoint, result *core.Result) {
|
2021-05-19 04:29:15 +02:00
|
|
|
rwLock.Lock()
|
2021-10-23 22:47:12 +02:00
|
|
|
gauge, exists := gauges[fmt.Sprintf("%s_%s", endpoint.Name, endpoint.URL)]
|
2021-05-19 04:29:15 +02:00
|
|
|
if !exists {
|
|
|
|
gauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
|
2021-10-23 22:47:12 +02:00
|
|
|
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},
|
2021-05-19 04:29:15 +02:00
|
|
|
}, []string{"status", "success"})
|
2021-10-23 22:47:12 +02:00
|
|
|
gauges[fmt.Sprintf("%s_%s", endpoint.Name, endpoint.URL)] = gauge
|
2019-11-16 21:46:52 +01:00
|
|
|
}
|
2021-05-19 04:29:15 +02:00
|
|
|
rwLock.Unlock()
|
|
|
|
gauge.WithLabelValues(strconv.Itoa(result.HTTPStatus), strconv.FormatBool(result.Success)).Inc()
|
2019-11-16 21:46:52 +01:00
|
|
|
}
|