mirror of
https://github.com/TwiN/gatus.git
synced 2024-11-07 08:34:15 +01:00
6ed93d4b82
* Add clarifications in comments * #191: Rename Service to Endpoint
35 lines
1.1 KiB
Go
35 lines
1.1 KiB
Go
package metric
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"sync"
|
|
|
|
"github.com/TwiN/gatus/v3/core"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
)
|
|
|
|
var (
|
|
gauges = map[string]*prometheus.GaugeVec{}
|
|
rwLock sync.RWMutex
|
|
)
|
|
|
|
// 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
|
|
}
|
|
rwLock.Unlock()
|
|
gauge.WithLabelValues(strconv.Itoa(result.HTTPStatus), strconv.FormatBool(result.Success)).Inc()
|
|
}
|