Skip to content

enrich rpc related metrics with bool size classifier #47

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

Merged
merged 1 commit into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions rpcserver/jsonrpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ var (
)

const (
maxOriginIDLength = 255
maxOriginIDLength = 255
requestSizeThreshold = 50_000
)

type (
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -272,27 +275,27 @@ 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
result, err := method.call(ctx, req.Params)
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)
if err != nil {
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
}

Expand All @@ -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 {
Expand Down
18 changes: 9 additions & 9 deletions rpcserver/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down