-
Notifications
You must be signed in to change notification settings - Fork 10
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
Refactor metric for request duration #747
Conversation
WalkthroughThe changes update the HTTP request handling in the server by introducing a new variable to capture the JSON-RPC method from the request body. The conditional logic is streamlined to consolidate checks for a valid HTTP handler and path, while integrating request duration measurements tied to the extracted method. Additionally, the entire metrics middleware in Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Server
participant Collector
Client->>Server: HTTP Request (with JSON body)
Server->>Server: Extract JSON-RPC method (requestMethod)
Server->>Collector: Measure request duration using requestMethod
Server->>Client: Return HTTP Response
Assessment against linked issues
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (2)
💤 Files with no reviewable changes (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
HttpHandler
middlewareThere 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.
Actionable comments posted: 0
🔭 Outside diff range comments (1)
api/server.go (1)
250-272
: Add request body size limit to prevent memory issues.The code reads the entire request body into memory without any size limits. This could lead to memory exhaustion if a malicious client sends a very large request.
Consider adding a size limit to the request body reader:
- if b, err := io.ReadAll(r.Body); err == nil { + maxBodySize := int64(10 * 1024 * 1024) // 10MB limit + if b, err := io.ReadAll(io.LimitReader(r.Body, maxBodySize)); err == nil {
🧹 Nitpick comments (1)
api/server.go (1)
289-299
: Improve error handling and context propagation.The request duration measurement could be enhanced in several ways:
- Error handling for metric collection is missing
- Request context is not propagated to the metric collection
Consider these improvements:
if h.httpHandler != nil && checkPath(r, "") { start := time.Now() + ctx := r.Context() h.httpHandler.ServeHTTP(logW, r) if eth.IsValidMethod(requestMethod) { - h.collector.MeasureRequestDuration(start, requestMethod) + if err := h.collector.MeasureRequestDuration(ctx, start, requestMethod); err != nil { + h.logger.Error().Err(err).Msg("failed to measure request duration") + } } return }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
api/server.go
(2 hunks)metrics/handler.go
(0 hunks)
💤 Files with no reviewable changes (1)
- metrics/handler.go
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: Test
- GitHub Check: Lint
0c38346
to
5c8b24e
Compare
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.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
api/server.go
(2 hunks)metrics/handler.go
(0 hunks)
💤 Files with no reviewable changes (1)
- metrics/handler.go
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: Test
🔇 Additional comments (2)
api/server.go (2)
251-251
: LGTM!The variable declaration is well-scoped and follows good initialization practices.
257-269
: LGTM!The method extraction and logging logic is robust with proper validation and type safety.
if h.httpHandler != nil && checkPath(r, "") { | ||
start := time.Now() | ||
|
||
h.httpHandler.ServeHTTP(logW, r) | ||
|
||
if eth.IsValidMethod(requestMethod) { | ||
h.collector.MeasureRequestDuration(start, requestMethod) | ||
} | ||
|
||
return | ||
} |
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.
🛠️ Refactor suggestion
Optimize request duration measurement.
The current implementation has a few areas for improvement:
- Request duration includes network latency as it's measured after
ServeHTTP
. - The
IsValidMethod
check is redundant sincerequestMethod
is only set for valid methods. - Invalid method requests are not measured, which could skew metrics.
Consider this improved implementation:
if h.httpHandler != nil && checkPath(r, "") {
start := time.Now()
h.httpHandler.ServeHTTP(logW, r)
- if eth.IsValidMethod(requestMethod) {
- h.collector.MeasureRequestDuration(start, requestMethod)
- }
+ // Measure duration for all requests, using method if available
+ method := requestMethod
+ if method == "" {
+ method = "invalid_method"
+ }
+ h.collector.MeasureRequestDuration(start, method)
return
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
if h.httpHandler != nil && checkPath(r, "") { | |
start := time.Now() | |
h.httpHandler.ServeHTTP(logW, r) | |
if eth.IsValidMethod(requestMethod) { | |
h.collector.MeasureRequestDuration(start, requestMethod) | |
} | |
return | |
} | |
if h.httpHandler != nil && checkPath(r, "") { | |
start := time.Now() | |
h.httpHandler.ServeHTTP(logW, r) | |
// Measure duration for all requests, using method if available | |
method := requestMethod | |
if method == "" { | |
method = "invalid_method" | |
} | |
h.collector.MeasureRequestDuration(start, method) | |
return | |
} |
5c8b24e
to
eb5b592
Compare
Closes: #739
Description
The
HttpHandler
middleware seemed to not be working properly for all cases, as can be seen in the linked issue above. So we remove it and move the request duration metric to the RPC server.For contributor use:
master
branchFiles changed
in the Github PR explorerSummary by CodeRabbit