Files
gatus/metric/metric.go
T

27 lines
982 B
Go
Raw Normal View History

2019-11-16 15:46:52 -05:00
package metric
import (
2020-11-11 18:05:18 -05:00
"strconv"
"github.com/TwiN/gatus/v3/core"
2019-11-16 15:46:52 -05:00
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
2021-12-13 19:24:18 -05: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 15:46:52 -05:00
)
2021-10-23 16:47:12 -04:00
// PublishMetricsForEndpoint publishes metrics for the given endpoint and its result.
2020-10-23 16:12:53 -04:00
// These metrics will be exposed at /metrics if the metrics are enabled
2021-10-23 16:47:12 -04:00
func PublishMetricsForEndpoint(endpoint *core.Endpoint, result *core.Result) {
2021-12-13 19:24:18 -05: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 15:46:52 -05:00
}
2021-12-13 19:24:18 -05:00
resultCount.WithLabelValues(endpoint.Key(), endpoint.Group, endpoint.Name, strconv.FormatBool(result.Success)).Inc()
2019-11-16 15:46:52 -05:00
}