cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/promapi/metrics.go
70lines · 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 | prometheusQueriesRunning = prometheus.NewGaugeVec( |
| 14 | prometheus.GaugeOpts{ |
| 15 | Name: "pint_prometheus_queries_running", |
| 16 | Help: "Total number of in-flight prometheus queries", |
| 17 | }, |
| 18 | []string{"name", "endpoint"}, |
| 19 | ) |
| 20 | prometheusCacheSize = prometheus.NewGaugeVec( |
| 21 | prometheus.GaugeOpts{ |
| 22 | Name: "pint_prometheus_cache_size", |
| 23 | Help: "Total number of entries currently stored in Prometheus query cache", |
| 24 | }, |
| 25 | []string{"name"}, |
| 26 | ) |
| 27 | prometheusCacheHitsTotal = prometheus.NewCounterVec( |
| 28 | prometheus.CounterOpts{ |
| 29 | Name: "pint_prometheus_cache_hits_total", |
| 30 | Help: "Total number of all prometheus queries served from a cache", |
| 31 | }, |
| 32 | []string{"name", "endpoint"}, |
| 33 | ) |
| 34 | prometheusQueriesTotal = prometheus.NewCounterVec( |
| 35 | prometheus.CounterOpts{ |
| 36 | Name: "pint_prometheus_queries_total", |
| 37 | Help: "Total number of all prometheus queries", |
| 38 | }, |
| 39 | []string{"name", "endpoint"}, |
| 40 | ) |
| 41 | prometheusQueryErrorsTotal = prometheus.NewCounterVec( |
| 42 | prometheus.CounterOpts{ |
| 43 | Name: "pint_prometheus_query_errors_total", |
| 44 | Help: "Total number of failed prometheus queries", |
| 45 | }, |
| 46 | []string{"name", "endpoint", "reason"}, |
| 47 | ) |
| 48 | ) |
| 49 | |
| 50 | func RegisterMetrics() { |
| 51 | prometheus.MustRegister(prometheusQueriesRunning) |
| 52 | prometheus.MustRegister(prometheusCacheSize) |
| 53 | prometheus.MustRegister(prometheusCacheHitsTotal) |
| 54 | prometheus.MustRegister(prometheusQueriesTotal) |
| 55 | prometheus.MustRegister(prometheusQueryErrorsTotal) |
| 56 | } |
| 57 | |
| 58 | func errReason(err error) string { |
| 59 | var neterr net.Error |
| 60 | if ok := errors.As(err, &neterr); ok && neterr.Timeout() { |
| 61 | return "connection/timeout" |
| 62 | } |
| 63 | |
| 64 | var v1err *v1.Error |
| 65 | if ok := errors.As(err, &v1err); ok { |
| 66 | return fmt.Sprintf("api/%s", v1err.Type) |
| 67 | } |
| 68 | |
| 69 | return "connection/error" |
| 70 | } |
| 71 | |