2019-11-16 21:46:52 +01:00
|
|
|
package metric
|
|
|
|
|
|
|
|
import (
|
2020-11-12 00:05:18 +01:00
|
|
|
"strconv"
|
|
|
|
|
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 (
|
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
|
|
|
)
|
|
|
|
|
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-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
|
|
|
}
|