statping/types/metrics/metrics.go

101 lines
2.3 KiB
Go
Raw Normal View History

package metrics
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
)
var (
utilsHttpRequestDur = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
2020-06-16 09:42:21 +00:00
Namespace: "statping",
Name: "http_requests_duration",
Help: "Duration for h",
},
[]string{"url", "method"},
)
utilsHttpRequestBytes = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
2020-06-16 09:42:21 +00:00
Namespace: "statping",
Name: "http_response_bytes",
Help: "Response in bytes for a HTTP services",
},
[]string{"url", "method"},
)
2020-06-16 09:42:21 +00:00
httpDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "statping",
Name: "http_duration_seconds",
Help: "Duration of HTTP requests from the utils package",
}, []string{"path"})
)
2020-06-16 09:42:21 +00:00
func InitMetrics() {
prometheus.MustRegister(
serviceOnline,
serviceFailures,
serviceSuccess,
2020-06-14 20:46:22 +00:00
serviceStatusCode,
2020-06-16 09:42:21 +00:00
serviceDuration,
utilsHttpRequestDur,
utilsHttpRequestBytes,
2020-06-16 09:42:21 +00:00
httpDuration,
2020-06-16 11:02:14 +00:00
databaseStats,
queryStats,
)
}
func Histo(method string, value float64, labels ...interface{}) {
switch method {
case "duration":
utilsHttpRequestDur.WithLabelValues(convert(labels)...).Observe(value)
case "bytes":
utilsHttpRequestBytes.WithLabelValues(convert(labels)...).Observe(value)
}
}
2020-06-16 09:42:21 +00:00
func Timer(labels ...interface{}) prometheus.Observer {
return httpDuration.WithLabelValues(convert(labels)...)
}
func ServiceTimer(labels ...interface{}) prometheus.Observer {
return serviceDuration.WithLabelValues(convert(labels)...)
}
2020-06-14 20:46:22 +00:00
func Gauge(method string, value float64, labels ...interface{}) {
switch method {
2020-06-16 09:42:21 +00:00
case "status_code":
2020-06-14 20:46:22 +00:00
serviceStatusCode.WithLabelValues(convert(labels)...).Set(value)
case "online":
serviceOnline.WithLabelValues(convert(labels)...).Set(value)
}
}
func Inc(method string, labels ...interface{}) {
switch method {
case "failure":
serviceFailures.WithLabelValues(convert(labels)...).Inc()
case "success":
serviceSuccess.WithLabelValues(convert(labels)...).Inc()
}
}
func Add(method string, value float64, labels ...interface{}) {
switch method {
case "failure":
serviceFailures.WithLabelValues(convert(labels)...).Add(value)
case "success":
serviceSuccess.WithLabelValues(convert(labels)...).Add(value)
}
}
func convert(vals []interface{}) []string {
var out []string
for _, v := range vals {
out = append(out, fmt.Sprintf("%v", v))
}
return out
}