fix(oohelperd): metrics improvements after design review (#903)
This diff updates the metrics according to https://github.com/ooni/probe/issues/2183#issuecomment-1230327725
This commit is contained in:
parent
ffc2527fc5
commit
8c855ca597
2 changed files with 21 additions and 24 deletions
|
|
@ -48,38 +48,37 @@ var _ http.Handler = &handler{}
|
|||
func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
metricRequestsInflight.Inc()
|
||||
defer metricRequestsInflight.Dec()
|
||||
metricRequestsTotal.Inc()
|
||||
w.Header().Add("Server", fmt.Sprintf(
|
||||
"oohelperd/%s ooniprobe-engine/%s", version.Version, version.Version,
|
||||
))
|
||||
if req.Method != "POST" {
|
||||
metricRequestsByStatusCode.WithLabelValues("400").Inc()
|
||||
metricRequestsCount.WithLabelValues("400", "bad_request_method").Inc()
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
reader := &io.LimitedReader{R: req.Body, N: h.MaxAcceptableBody}
|
||||
data, err := netxlite.ReadAllContext(req.Context(), reader)
|
||||
if err != nil {
|
||||
metricRequestsByStatusCode.WithLabelValues("400").Inc()
|
||||
metricRequestsCount.WithLabelValues("400", "request_body_too_large").Inc()
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
var creq ctrlRequest
|
||||
if err := json.Unmarshal(data, &creq); err != nil {
|
||||
metricRequestsByStatusCode.WithLabelValues("400").Inc()
|
||||
metricRequestsCount.WithLabelValues("400", "cannot_unmarshal_request_body").Inc()
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
started := time.Now()
|
||||
cresp, err := measure(req.Context(), h, &creq)
|
||||
elapsed := time.Since(started)
|
||||
metricMeasurementTime.Observe(float64(elapsed.Seconds()))
|
||||
metricWCTaskDurationSeconds.Observe(float64(elapsed.Seconds()))
|
||||
if err != nil {
|
||||
metricRequestsByStatusCode.WithLabelValues("400").Inc()
|
||||
metricRequestsCount.WithLabelValues("400", "measurement_failed").Inc()
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
metricRequestsByStatusCode.WithLabelValues("200").Inc()
|
||||
metricRequestsCount.WithLabelValues("200", "ok").Inc()
|
||||
// We assume that the following call cannot fail because it's a
|
||||
// clearly-serializable data structure.
|
||||
data, err = json.Marshal(cresp)
|
||||
|
|
|
|||
Loading…
Reference in a new issue