-
Notifications
You must be signed in to change notification settings - Fork 176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Access] Fixed REST API metrics #4288
Changes from 11 commits
e32223c
72e18ba
921c246
242a02e
8e96cc3
8f55621
b79c693
f9c6520
63e922d
83d0d1e
bf45732
72c34c4
a9de6c1
83b4abf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,27 +6,24 @@ import ( | |
|
||
"github.com/prometheus/client_golang/prometheus" | ||
|
||
"github.com/onflow/flow-go/module" | ||
|
||
httpmetrics "github.com/slok/go-http-metrics/metrics" | ||
metricsProm "github.com/slok/go-http-metrics/metrics/prometheus" | ||
) | ||
|
||
// Example recorder taken from: | ||
// https://github.com/slok/go-http-metrics/blob/master/metrics/prometheus/prometheus.go | ||
type RestCollector interface { | ||
httpmetrics.Recorder | ||
AddTotalRequests(ctx context.Context, service string, id string) | ||
} | ||
|
||
type recorder struct { | ||
type RecorderCollector struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's call this |
||
httpRequestDurHistogram *prometheus.HistogramVec | ||
httpResponseSizeHistogram *prometheus.HistogramVec | ||
httpRequestsInflight *prometheus.GaugeVec | ||
httpRequestsTotal *prometheus.GaugeVec | ||
} | ||
|
||
// NewRestCollector returns a new metrics recorder that implements the recorder | ||
var _ module.RestMetrics = (*RecorderCollector)(nil) | ||
|
||
// NewRestCollector returns a new metrics RecorderCollector that implements the RecorderCollector | ||
// using Prometheus as the backend. | ||
func NewRestCollector(cfg metricsProm.Config) RestCollector { | ||
func NewRecorderCollector(cfg metricsProm.Config) module.RestMetrics { | ||
if len(cfg.DurationBuckets) == 0 { | ||
cfg.DurationBuckets = prometheus.DefBuckets | ||
} | ||
|
@@ -55,7 +52,7 @@ func NewRestCollector(cfg metricsProm.Config) RestCollector { | |
cfg.ServiceLabel = "service" | ||
} | ||
|
||
r := &recorder{ | ||
r := &RecorderCollector{ | ||
httpRequestDurHistogram: prometheus.NewHistogramVec(prometheus.HistogramOpts{ | ||
Namespace: cfg.Prefix, | ||
Subsystem: "http", | ||
|
@@ -87,23 +84,30 @@ func NewRestCollector(cfg metricsProm.Config) RestCollector { | |
}, []string{cfg.ServiceLabel, cfg.HandlerIDLabel}), | ||
} | ||
|
||
cfg.Registry.MustRegister( | ||
r.httpRequestDurHistogram, | ||
r.httpResponseSizeHistogram, | ||
r.httpRequestsInflight, | ||
r.httpRequestsTotal, | ||
) | ||
|
||
return r | ||
} | ||
|
||
// These methods are called automatically by go-http-metrics/middleware | ||
func (r recorder) ObserveHTTPRequestDuration(_ context.Context, p httpmetrics.HTTPReqProperties, duration time.Duration) { | ||
func (r *RecorderCollector) ObserveHTTPRequestDuration(_ context.Context, p httpmetrics.HTTPReqProperties, duration time.Duration) { | ||
r.httpRequestDurHistogram.WithLabelValues(p.Service, p.ID, p.Method, p.Code).Observe(duration.Seconds()) | ||
} | ||
|
||
func (r recorder) ObserveHTTPResponseSize(_ context.Context, p httpmetrics.HTTPReqProperties, sizeBytes int64) { | ||
func (r *RecorderCollector) ObserveHTTPResponseSize(_ context.Context, p httpmetrics.HTTPReqProperties, sizeBytes int64) { | ||
r.httpResponseSizeHistogram.WithLabelValues(p.Service, p.ID, p.Method, p.Code).Observe(float64(sizeBytes)) | ||
} | ||
|
||
func (r recorder) AddInflightRequests(_ context.Context, p httpmetrics.HTTPProperties, quantity int) { | ||
func (r *RecorderCollector) AddInflightRequests(_ context.Context, p httpmetrics.HTTPProperties, quantity int) { | ||
r.httpRequestsInflight.WithLabelValues(p.Service, p.ID).Add(float64(quantity)) | ||
} | ||
|
||
// New custom method to track all requests made for every REST API request | ||
func (r recorder) AddTotalRequests(_ context.Context, method string, id string) { | ||
func (r *RecorderCollector) AddTotalRequests(_ context.Context, method string, id string) { | ||
r.httpRequestsTotal.WithLabelValues(method, id).Inc() | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to create a unique value of
handler
for every single collection, causing too. much metrics to be collected