gatus/metric/metric.go

28 lines
982 B
Go
Raw Normal View History

2019-11-16 21:46:52 +01:00
package metric
import (
"strconv"
"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 (
2021-12-14 01:24:18 +01:00
// 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
2019-11-16 21:46:52 +01: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
func PublishMetricsForEndpoint(endpoint *core.Endpoint, result *core.Result) {
2021-12-14 01:24:18 +01:00
if resultCount == nil {
resultCount = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "gatus_results_total",
Help: "Number of results per endpoint",
}, []string{"key", "group", "name", "success"})
2019-11-16 21:46:52 +01:00
}
2021-12-14 01:24:18 +01:00
resultCount.WithLabelValues(endpoint.Key(), endpoint.Group, endpoint.Name, strconv.FormatBool(result.Success)).Inc()
2019-11-16 21:46:52 +01:00
}