generated from mrz1836/go-template
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #492 from bitcoin-sv/feat-576-request-metrics
feat(BUX-576) request metrics
- Loading branch information
Showing
8 changed files
with
161 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package metrics | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
var notFoundContextKey = "routeNotFound" | ||
|
||
func requestMetricsMiddleware() gin.HandlerFunc { | ||
if metrics, enabled := Get(); enabled { | ||
return func(c *gin.Context) { | ||
tracker := metrics.httpRequests.Track(c.Request.Method, c.Request.URL.Path) | ||
tracker.Start() | ||
defer func() { | ||
if _, noRoute := c.Get(notFoundContextKey); noRoute { | ||
tracker.EndWithNoRoute() | ||
} else { | ||
// note that the status code will be correct only if higher middleware doesn't change the status code; | ||
// order of middlewares matters | ||
tracker.End(c.Writer.Status()) | ||
} | ||
}() | ||
|
||
c.Next() | ||
} | ||
} | ||
|
||
return func(c *gin.Context) { | ||
c.Next() | ||
} | ||
} | ||
|
||
// NoRoute is a middleware to set a flag in the context if the route is actually not found | ||
// this is needed to distinguish no-route 404 from other 404s | ||
func NoRoute(c *gin.Context) { | ||
c.Set(notFoundContextKey, true) | ||
c.Next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
package metrics | ||
|
||
const appName = "spv-wallet" | ||
|
||
const ( | ||
requestMetricBaseName = "http_request" | ||
requestCounterName = requestMetricBaseName + "_total" | ||
requestDurationSecName = requestMetricBaseName + "_duration_seconds" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package metrics | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
enginemetrics "github.com/bitcoin-sv/spv-wallet/engine/metrics" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
// RequestMetrics is the metrics for the http requests | ||
type RequestMetrics struct { | ||
requestsTotal *prometheus.CounterVec | ||
requestDuration *prometheus.HistogramVec | ||
} | ||
|
||
func registerRequestMetrics(collector enginemetrics.Collector) *RequestMetrics { | ||
requestsTotal := collector.RegisterCounterVec(requestCounterName, "method", "path", "status", "classification") | ||
requestDuration := collector.RegisterHistogramVec(requestDurationSecName, "method", "path") | ||
|
||
return &RequestMetrics{ | ||
requestsTotal: requestsTotal, | ||
requestDuration: requestDuration, | ||
} | ||
} | ||
|
||
// Track will return a RequestTracker to track the request | ||
func (m *RequestMetrics) Track(method, path string) *RequestTracker { | ||
return &RequestTracker{ | ||
method: method, | ||
path: path, | ||
metrics: m, | ||
} | ||
} | ||
|
||
// RequestTracker is used to track the duration and status of a request | ||
type RequestTracker struct { | ||
method string | ||
path string | ||
startTime time.Time | ||
metrics *RequestMetrics | ||
} | ||
|
||
// Start will start the tracking of the request | ||
func (r *RequestTracker) Start() { | ||
r.startTime = time.Now() | ||
} | ||
|
||
// End will end the tracking of the request | ||
func (r *RequestTracker) End(status int) { | ||
r.writeCounter(status, r.path) | ||
r.writeDuration() | ||
} | ||
|
||
// EndWithNoRoute will end the tracking of the request with a 404 status | ||
func (r *RequestTracker) EndWithNoRoute() { | ||
// This is a safeguard against attacks where the server is flooded with requests having unique paths, | ||
// which would lead to the creation of a large number of metrics | ||
r.writeCounter(404, "UNKNOWN_ROUTE") | ||
} | ||
|
||
func (r *RequestTracker) writeCounter(status int, path string) { | ||
r.metrics.requestsTotal.WithLabelValues(r.method, path, fmt.Sprint(status), requestClassification(status)).Inc() | ||
} | ||
|
||
func (r *RequestTracker) writeDuration() { | ||
r.metrics.requestDuration.WithLabelValues(r.method, r.path).Observe(time.Since(r.startTime).Seconds()) | ||
} | ||
|
||
func requestClassification(status int) string { | ||
if status >= 200 && status < 400 { | ||
return "success" | ||
} | ||
return "failure" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters