From 9fcf3a64777f9b6eea7a2478f57948cad38188f8 Mon Sep 17 00:00:00 2001 From: TymKh Date: Tue, 20 May 2025 14:33:31 +0200 Subject: [PATCH] enrich rpc related metrics with bool size classifier --- rpcserver/jsonrpc_server.go | 25 ++++++++++++++----------- rpcserver/metrics.go | 18 +++++++++--------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/rpcserver/jsonrpc_server.go b/rpcserver/jsonrpc_server.go index 2c34989..123e249 100644 --- a/rpcserver/jsonrpc_server.go +++ b/rpcserver/jsonrpc_server.go @@ -37,7 +37,8 @@ var ( ) const ( - maxOriginIDLength = 255 + maxOriginIDLength = 255 + requestSizeThreshold = 50_000 ) type ( @@ -150,12 +151,12 @@ func (h *JSONRPCHandler) writeJSONRPCError(w http.ResponseWriter, id any, code i func (h *JSONRPCHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { startAt := time.Now() methodForMetrics := unknownMethodLabel - + bigRequest := false ctx := r.Context() defer func() { - incRequestCount(methodForMetrics, h.ServerName) - incRequestDuration(time.Since(startAt), methodForMetrics, h.ServerName) + incRequestCount(methodForMetrics, h.ServerName, bigRequest) + incRequestDuration(time.Since(startAt), methodForMetrics, h.ServerName, bigRequest) }() stepStartAt := time.Now() @@ -193,13 +194,15 @@ func (h *JSONRPCHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { incIncorrectRequest(h.ServerName) return } + bodySize := len(body) + bigRequest = bodySize > requestSizeThreshold defer func(size int) { incRequestSizeBytes(size, methodForMetrics, h.ServerName) - }(len(body)) + }(bodySize) stepTime := time.Since(stepStartAt) defer func(stepTime time.Duration) { - incRequestDurationStep(stepTime, methodForMetrics, h.ServerName, "io") + incRequestDurationStep(stepTime, methodForMetrics, h.ServerName, "io", bigRequest) }(stepTime) stepStartAt = time.Now() @@ -272,7 +275,7 @@ func (h *JSONRPCHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } methodForMetrics = req.Method - incRequestDurationStep(time.Since(stepStartAt), methodForMetrics, h.ServerName, "parse") + incRequestDurationStep(time.Since(stepStartAt), methodForMetrics, h.ServerName, "parse", bigRequest) stepStartAt = time.Now() // call method @@ -280,11 +283,11 @@ func (h *JSONRPCHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if err != nil { h.writeJSONRPCError(w, req.ID, CodeCustomError, err.Error()) incRequestErrorCount(methodForMetrics, h.ServerName) - incRequestDurationStep(time.Since(stepStartAt), methodForMetrics, h.ServerName, "call") + incRequestDurationStep(time.Since(stepStartAt), methodForMetrics, h.ServerName, "call", bigRequest) return } - incRequestDurationStep(time.Since(stepStartAt), methodForMetrics, h.ServerName, "call") + incRequestDurationStep(time.Since(stepStartAt), methodForMetrics, h.ServerName, "call", bigRequest) stepStartAt = time.Now() marshaledResult, err := json.Marshal(result) @@ -292,7 +295,7 @@ func (h *JSONRPCHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.writeJSONRPCError(w, req.ID, CodeInternalError, err.Error()) incInternalErrors(h.ServerName) - incRequestDurationStep(time.Since(stepStartAt), methodForMetrics, h.ServerName, "response") + incRequestDurationStep(time.Since(stepStartAt), methodForMetrics, h.ServerName, "response", bigRequest) return } @@ -306,7 +309,7 @@ func (h *JSONRPCHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } h.writeJSONRPCResponse(w, res) - incRequestDurationStep(time.Since(stepStartAt), methodForMetrics, h.ServerName, "response") + incRequestDurationStep(time.Since(stepStartAt), methodForMetrics, h.ServerName, "response", bigRequest) } func GetHighPriority(ctx context.Context) bool { diff --git a/rpcserver/metrics.go b/rpcserver/metrics.go index 1b87485..37e7eaf 100644 --- a/rpcserver/metrics.go +++ b/rpcserver/metrics.go @@ -19,20 +19,20 @@ const ( internalErrorsCounter = `goutils_rpcserver_internal_errors_total{server_name="%s"}` // incremented when request comes in - requestCountLabel = `goutils_rpcserver_request_count{method="%s",server_name="%s"}` + requestCountLabel = `goutils_rpcserver_request_count{method="%s",server_name="%s",is_big="%t"}` // incremented when handler method returns JSONRPC error errorCountLabel = `goutils_rpcserver_error_count{method="%s",server_name="%s"}` // total duration of the request - requestDurationLabel = `goutils_rpcserver_request_duration_milliseconds{method="%s",server_name="%s"}` + requestDurationLabel = `goutils_rpcserver_request_duration_milliseconds{method="%s",server_name="%s",is_big="%t"}` // partial duration of the request - requestDurationStepLabel = `goutils_rpcserver_request_step_duration_milliseconds{method="%s",server_name="%s",step="%s"}` + requestDurationStepLabel = `goutils_rpcserver_request_step_duration_milliseconds{method="%s",server_name="%s",step="%s",is_big="%t"}` // request size in bytes requestSizeBytes = `goutils_rpcserver_request_size_bytes{method="%s",server_name="%s"}` ) -func incRequestCount(method, serverName string) { - l := fmt.Sprintf(requestCountLabel, method, serverName) +func incRequestCount(method, serverName string, isBig bool) { + l := fmt.Sprintf(requestCountLabel, method, serverName, isBig) metrics.GetOrCreateCounter(l).Inc() } @@ -46,9 +46,9 @@ func incRequestErrorCount(method, serverName string) { metrics.GetOrCreateCounter(l).Inc() } -func incRequestDuration(duration time.Duration, method string, serverName string) { +func incRequestDuration(duration time.Duration, method string, serverName string, isBig bool) { millis := float64(duration.Microseconds()) / 1000.0 - l := fmt.Sprintf(requestDurationLabel, method, serverName) + l := fmt.Sprintf(requestDurationLabel, method, serverName, isBig) metrics.GetOrCreateSummary(l).Update(millis) } @@ -57,9 +57,9 @@ func incInternalErrors(serverName string) { metrics.GetOrCreateCounter(l).Inc() } -func incRequestDurationStep(duration time.Duration, method, serverName, step string) { +func incRequestDurationStep(duration time.Duration, method, serverName, step string, isBig bool) { millis := float64(duration.Microseconds()) / 1000.0 - l := fmt.Sprintf(requestDurationStepLabel, method, serverName, step) + l := fmt.Sprintf(requestDurationStepLabel, method, serverName, step, isBig) metrics.GetOrCreateSummary(l).Update(millis) }