onasty/internal/metrics/http_metrics.go (view raw)
| 1 | package metrics |
| 2 | |
| 3 | import ( |
| 4 | "time" |
| 5 | |
| 6 | "github.com/prometheus/client_golang/prometheus" |
| 7 | "github.com/prometheus/client_golang/prometheus/promauto" |
| 8 | ) |
| 9 | |
| 10 | var ( |
| 11 | successfulHTTPRequest = promauto.NewCounterVec(prometheus.CounterOpts{ //nolint:exhaustruct |
| 12 | Name: "http_successful_requests_total", |
| 13 | Help: "the total number of successful http requests", |
| 14 | ConstLabels: map[string]string{"status": "success"}, |
| 15 | }, []string{"method", "uri"}) |
| 16 | |
| 17 | failedHTTPRequest = promauto.NewCounterVec(prometheus.CounterOpts{ //nolint:exhaustruct |
| 18 | Name: "http_failed_requests_total", |
| 19 | Help: "the total number of failed http requests", |
| 20 | ConstLabels: map[string]string{"status": "failure"}, |
| 21 | }, []string{"method", "uri"}) |
| 22 | |
| 23 | latencyHTTPRequest = promauto.NewHistogramVec(prometheus.HistogramOpts{ //nolint:exhaustruct |
| 24 | Name: "http_request_latency_seconds", |
| 25 | Help: "the latency of http requests in seconds", |
| 26 | Buckets: prometheus.DefBuckets, |
| 27 | }, []string{"method", "uri"}) |
| 28 | ) |
| 29 | |
| 30 | func RecordSuccessfulRequestMetric(method, uri string) { |
| 31 | go successfulHTTPRequest.With(prometheus.Labels{ |
| 32 | "method": method, |
| 33 | "uri": uri, |
| 34 | }).Inc() |
| 35 | } |
| 36 | |
| 37 | func RecordFailedRequestMetric(method, uri string) { |
| 38 | go failedHTTPRequest.With(prometheus.Labels{ |
| 39 | "method": method, |
| 40 | "uri": uri, |
| 41 | }).Inc() |
| 42 | } |
| 43 | |
| 44 | func RecordLatencyRequestMetric(method, uri string, latency time.Duration) { |
| 45 | go latencyHTTPRequest.With(prometheus.Labels{ |
| 46 | "method": method, |
| 47 | "uri": uri, |
| 48 | }).Observe(latency.Seconds()) |
| 49 | } |