-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use middleware to capture HTTP server metrics (#753)
* use middleware to capture HTTP server metrics * small refactor of package api * api: only record named path's metrics * update url names * add tests for metrics middleware * api metrics: add testcases for internal server error
- Loading branch information
Showing
16 changed files
with
245 additions
and
78 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
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,63 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package api | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/vechain/thor/v2/metrics" | ||
) | ||
|
||
var ( | ||
metricHttpReqCounter = metrics.LazyLoadCounterVec("api_request_count", []string{"name", "code", "method"}) | ||
metricHttpReqDuration = metrics.LazyLoadHistogramVec("api_duration_ms", []string{"name", "code", "method"}, metrics.BucketHTTPReqs) | ||
) | ||
|
||
// metricsResponseWriter is a wrapper around http.ResponseWriter that captures the status code. | ||
type metricsResponseWriter struct { | ||
http.ResponseWriter | ||
statusCode int | ||
} | ||
|
||
func newMetricsResponseWriter(w http.ResponseWriter) *metricsResponseWriter { | ||
return &metricsResponseWriter{w, http.StatusOK} | ||
} | ||
|
||
func (m *metricsResponseWriter) WriteHeader(code int) { | ||
m.statusCode = code | ||
m.ResponseWriter.WriteHeader(code) | ||
} | ||
|
||
// metricsMiddleware is a middleware that records metrics for each request. | ||
func metricsMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
rt := mux.CurrentRoute(r) | ||
|
||
var ( | ||
enabled = false | ||
name = "" | ||
) | ||
|
||
// all named route will be recorded | ||
if rt != nil && rt.GetName() != "" { | ||
enabled = true | ||
name = rt.GetName() | ||
} | ||
|
||
now := time.Now() | ||
mrw := newMetricsResponseWriter(w) | ||
|
||
next.ServeHTTP(mrw, r) | ||
|
||
if enabled { | ||
metricHttpReqCounter().AddWithLabel(1, map[string]string{"name": name, "code": strconv.Itoa(mrw.statusCode), "method": r.Method}) | ||
metricHttpReqDuration().ObserveWithLabels(time.Since(now).Milliseconds(), map[string]string{"name": name, "code": strconv.Itoa(mrw.statusCode), "method": r.Method}) | ||
} | ||
}) | ||
} |
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,117 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package api | ||
|
||
import ( | ||
"bytes" | ||
"crypto/rand" | ||
"io" | ||
"math" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/prometheus/common/expfmt" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/vechain/thor/v2/api/accounts" | ||
"github.com/vechain/thor/v2/chain" | ||
"github.com/vechain/thor/v2/cmd/thor/solo" | ||
"github.com/vechain/thor/v2/genesis" | ||
"github.com/vechain/thor/v2/metrics" | ||
"github.com/vechain/thor/v2/muxdb" | ||
"github.com/vechain/thor/v2/state" | ||
"github.com/vechain/thor/v2/thor" | ||
) | ||
|
||
func init() { | ||
metrics.InitializePrometheusMetrics() | ||
} | ||
|
||
func TestMetricsMiddleware(t *testing.T) { | ||
db := muxdb.NewMem() | ||
stater := state.NewStater(db) | ||
gene := genesis.NewDevnet() | ||
|
||
b, _, _, err := gene.Build(stater) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
repo, _ := chain.NewRepository(db, b) | ||
|
||
// inject some invalid data to db | ||
data := db.NewStore("chain.data") | ||
var blkID thor.Bytes32 | ||
rand.Read(blkID[:]) | ||
data.Put(blkID[:], []byte("invalid data")) | ||
|
||
// get summary should fail since the block data is not rlp encoded | ||
_, err = repo.GetBlockSummary(blkID) | ||
assert.NotNil(t, err) | ||
|
||
router := mux.NewRouter() | ||
acc := accounts.New(repo, stater, math.MaxUint64, thor.NoFork, solo.NewBFTEngine(repo)) | ||
acc.Mount(router, "/accounts") | ||
router.PathPrefix("/metrics").Handler(metrics.HTTPHandler()) | ||
router.Use(metricsMiddleware) | ||
ts := httptest.NewServer(router) | ||
|
||
httpGet(t, ts.URL+"/accounts/0x") | ||
httpGet(t, ts.URL+"/accounts/"+thor.Address{}.String()) | ||
|
||
_, code := httpGet(t, ts.URL+"/accounts/"+thor.Address{}.String()+"?revision="+blkID.String()) | ||
assert.Equal(t, 500, code) | ||
|
||
body, _ := httpGet(t, ts.URL+"/metrics") | ||
parser := expfmt.TextParser{} | ||
metrics, err := parser.TextToMetricFamilies(bytes.NewReader(body)) | ||
assert.Nil(t, err) | ||
|
||
m := metrics["thor_metrics_api_request_count"].GetMetric() | ||
assert.Equal(t, 3, len(m), "should be 3 metric entries") | ||
assert.Equal(t, float64(1), m[0].GetCounter().GetValue()) | ||
assert.Equal(t, float64(1), m[1].GetCounter().GetValue()) | ||
|
||
labels := m[0].GetLabel() | ||
assert.Equal(t, 3, len(labels)) | ||
assert.Equal(t, "code", labels[0].GetName()) | ||
assert.Equal(t, "200", labels[0].GetValue()) | ||
assert.Equal(t, "method", labels[1].GetName()) | ||
assert.Equal(t, "GET", labels[1].GetValue()) | ||
assert.Equal(t, "name", labels[2].GetName()) | ||
assert.Equal(t, "accounts_get_account", labels[2].GetValue()) | ||
|
||
labels = m[1].GetLabel() | ||
assert.Equal(t, 3, len(labels)) | ||
assert.Equal(t, "code", labels[0].GetName()) | ||
assert.Equal(t, "400", labels[0].GetValue()) | ||
assert.Equal(t, "method", labels[1].GetName()) | ||
assert.Equal(t, "GET", labels[1].GetValue()) | ||
assert.Equal(t, "name", labels[2].GetName()) | ||
assert.Equal(t, "accounts_get_account", labels[2].GetValue()) | ||
|
||
labels = m[2].GetLabel() | ||
assert.Equal(t, 3, len(labels)) | ||
assert.Equal(t, "code", labels[0].GetName()) | ||
assert.Equal(t, "500", labels[0].GetValue()) | ||
assert.Equal(t, "method", labels[1].GetName()) | ||
assert.Equal(t, "GET", labels[1].GetValue()) | ||
assert.Equal(t, "name", labels[2].GetName()) | ||
assert.Equal(t, "accounts_get_account", labels[2].GetValue()) | ||
} | ||
|
||
func httpGet(t *testing.T, url string) ([]byte, int) { | ||
res, err := http.Get(url) // nolint:gosec | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
r, err := io.ReadAll(res.Body) | ||
res.Body.Close() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return r, res.StatusCode | ||
} |
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
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
Oops, something went wrong.