feat(oohelperd): add prometheus metrics (#897)

Closes https://github.com/ooni/probe/issues/2183

While there, avoid exposing nil values for optional fields of the
THResponse struct (i.e., "ip_info" and "tls_handshake").

While there, fix `measurexlite`'s `OperationLogger` test
and make it deterministic rather than racy.
This commit is contained in:
Simone Basso 2022-08-28 23:54:22 +02:00 committed by GitHub
commit dcdd8fb712
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 98 additions and 5 deletions

View file

@ -9,6 +9,7 @@ import (
"fmt"
"io"
"net/http"
"time"
"github.com/ooni/probe-cli/v3/internal/atomicx"
"github.com/ooni/probe-cli/v3/internal/model"
@ -45,29 +46,40 @@ var _ http.Handler = &handler{}
// ServeHTTP implements http.Handler.ServeHTTP.
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()
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()
w.WriteHeader(400)
return
}
var creq ctrlRequest
if err := json.Unmarshal(data, &creq); err != nil {
metricRequestsByStatusCode.WithLabelValues("400").Inc()
w.WriteHeader(400)
return
}
started := time.Now()
cresp, err := measure(req.Context(), h, &creq)
elapsed := time.Since(started)
metricMeasurementTime.Observe(float64(elapsed.Seconds()))
if err != nil {
metricRequestsByStatusCode.WithLabelValues("400").Inc()
w.WriteHeader(400)
return
}
metricRequestsByStatusCode.WithLabelValues("200").Inc()
// We assume that the following call cannot fail because it's a
// clearly-serializable data structure.
data, err = json.Marshal(cresp)