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