cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/promapi/metrics.go
54lines · modecode
| 1 | package promapi |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "net" |
| 7 | |
| 8 | v1 "github.com/prometheus/client_golang/api/prometheus/v1" |
| 9 | "github.com/prometheus/client_golang/prometheus" |
| 10 | ) |
| 11 | |
| 12 | var ( |
| 13 | prometheusCacheHitsTotal = prometheus.NewCounterVec( |
| 14 | prometheus.CounterOpts{ |
| 15 | Name: "pint_prometheus_cache_hits_total", |
| 16 | Help: "Total number of all prometheus queries served from a cache", |
| 17 | }, |
| 18 | []string{"name", "endpoint"}, |
| 19 | ) |
| 20 | prometheusQueriesTotal = prometheus.NewCounterVec( |
| 21 | prometheus.CounterOpts{ |
| 22 | Name: "pint_prometheus_queries_total", |
| 23 | Help: "Total number of all prometheus queries", |
| 24 | }, |
| 25 | []string{"name", "endpoint"}, |
| 26 | ) |
| 27 | prometheusQueryErrorsTotal = prometheus.NewCounterVec( |
| 28 | prometheus.CounterOpts{ |
| 29 | Name: "pint_prometheus_query_errors_total", |
| 30 | Help: "Total number of failed prometheus queries", |
| 31 | }, |
| 32 | []string{"name", "endpoint", "reason"}, |
| 33 | ) |
| 34 | ) |
| 35 | |
| 36 | func RegisterMetrics() { |
| 37 | prometheus.MustRegister(prometheusCacheHitsTotal) |
| 38 | prometheus.MustRegister(prometheusQueriesTotal) |
| 39 | prometheus.MustRegister(prometheusQueryErrorsTotal) |
| 40 | } |
| 41 | |
| 42 | func errReason(err error) string { |
| 43 | var neterr net.Error |
| 44 | if ok := errors.As(err, &neterr); ok && neterr.Timeout() { |
| 45 | return "connection/timeout" |
| 46 | } |
| 47 | |
| 48 | var v1err *v1.Error |
| 49 | if ok := errors.As(err, &v1err); ok { |
| 50 | return fmt.Sprintf("api/%s", v1err.Type) |
| 51 | } |
| 52 | |
| 53 | return "connection/error" |
| 54 | } |
| 55 | |