cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/promapi/metrics.go
77lines · modecode
| 1 | package promapi |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "net" |
| 7 | |
| 8 | "github.com/prometheus/client_golang/prometheus" |
| 9 | ) |
| 10 | |
| 11 | var ( |
| 12 | prometheusQueriesRunning = prometheus.NewGaugeVec( |
| 13 | prometheus.GaugeOpts{ |
| 14 | Name: "pint_prometheus_queries_running", |
| 15 | Help: "Total number of in-flight prometheus queries", |
| 16 | }, |
| 17 | []string{"name", "endpoint"}, |
| 18 | ) |
| 19 | prometheusCacheSize = prometheus.NewGaugeVec( |
| 20 | prometheus.GaugeOpts{ |
| 21 | Name: "pint_prometheus_cache_size", |
| 22 | Help: "Total number of entries currently stored in Prometheus query cache", |
| 23 | }, |
| 24 | []string{"name"}, |
| 25 | ) |
| 26 | prometheusCacheHitsTotal = prometheus.NewCounterVec( |
| 27 | prometheus.CounterOpts{ |
| 28 | Name: "pint_prometheus_cache_hits_total", |
| 29 | Help: "Total number of all prometheus queries served from a cache", |
| 30 | }, |
| 31 | []string{"name", "endpoint"}, |
| 32 | ) |
| 33 | prometheusCacheMissTotal = prometheus.NewCounterVec( |
| 34 | prometheus.CounterOpts{ |
| 35 | Name: "pint_prometheus_cache_miss_total", |
| 36 | Help: "Total number of all prometheus queries resulting in a cache miss", |
| 37 | }, |
| 38 | []string{"name", "endpoint"}, |
| 39 | ) |
| 40 | prometheusQueriesTotal = prometheus.NewCounterVec( |
| 41 | prometheus.CounterOpts{ |
| 42 | Name: "pint_prometheus_queries_total", |
| 43 | Help: "Total number of all prometheus queries", |
| 44 | }, |
| 45 | []string{"name", "endpoint"}, |
| 46 | ) |
| 47 | prometheusQueryErrorsTotal = prometheus.NewCounterVec( |
| 48 | prometheus.CounterOpts{ |
| 49 | Name: "pint_prometheus_query_errors_total", |
| 50 | Help: "Total number of failed prometheus queries", |
| 51 | }, |
| 52 | []string{"name", "endpoint", "reason"}, |
| 53 | ) |
| 54 | ) |
| 55 | |
| 56 | func RegisterMetrics() { |
| 57 | prometheus.MustRegister(prometheusQueriesRunning) |
| 58 | prometheus.MustRegister(prometheusCacheSize) |
| 59 | prometheus.MustRegister(prometheusCacheHitsTotal) |
| 60 | prometheus.MustRegister(prometheusCacheMissTotal) |
| 61 | prometheus.MustRegister(prometheusQueriesTotal) |
| 62 | prometheus.MustRegister(prometheusQueryErrorsTotal) |
| 63 | } |
| 64 | |
| 65 | func errReason(err error) string { |
| 66 | var neterr net.Error |
| 67 | if ok := errors.As(err, &neterr); ok && neterr.Timeout() { |
| 68 | return "connection/timeout" |
| 69 | } |
| 70 | |
| 71 | var e1 APIError |
| 72 | if ok := errors.As(err, &e1); ok { |
| 73 | return fmt.Sprintf("api/%s", e1.ErrorType) |
| 74 | } |
| 75 | |
| 76 | return "connection/error" |
| 77 | } |
| 78 | |