initial commit
This commit is contained in:
commit
88c14b627e
224 files changed
+17204
No files matched your search
@@ -0,0 +1,49 @@
|
||||
package prometheus
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
client "github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/collectors"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
type Metrics struct {
|
||||
registry *client.Registry
|
||||
requests *client.CounterVec
|
||||
latency *client.HistogramVec
|
||||
}
|
||||
|
||||
func New() *Metrics {
|
||||
registry := client.NewRegistry()
|
||||
requests := client.NewCounterVec(client.CounterOpts{
|
||||
Name: "lincodex_http_requests_total",
|
||||
Help: "Total number of HTTP requests served by the API.",
|
||||
}, []string{"method", "route", "status"})
|
||||
latency := client.NewHistogramVec(client.HistogramOpts{
|
||||
Name: "lincodex_http_request_duration_seconds",
|
||||
Help: "HTTP request duration in seconds.",
|
||||
Buckets: client.DefBuckets,
|
||||
}, []string{"method", "route"})
|
||||
registry.MustRegister(collectors.NewGoCollector(), collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), requests, latency)
|
||||
return &Metrics{registry: registry, requests: requests, latency: latency}
|
||||
}
|
||||
|
||||
func (metrics *Metrics) ObserveRequest(method, route string, status int, duration time.Duration) {
|
||||
if route == "" {
|
||||
route = "unmatched"
|
||||
}
|
||||
switch method {
|
||||
case http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete, http.MethodHead, http.MethodOptions:
|
||||
default:
|
||||
method = "OTHER"
|
||||
}
|
||||
metrics.requests.WithLabelValues(method, route, strconv.Itoa(status)).Inc()
|
||||
metrics.latency.WithLabelValues(method, route).Observe(duration.Seconds())
|
||||
}
|
||||
|
||||
func (metrics *Metrics) Handler() http.Handler {
|
||||
return promhttp.HandlerFor(metrics.registry, promhttp.HandlerOpts{})
|
||||
}
|
||||
Reference in new issue
Block a user