Skip to content

Commit

Permalink
fix: parse batch request json
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanViast committed Oct 21, 2024
1 parent b84615a commit f107124
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
20 changes: 11 additions & 9 deletions pkg/filters/proxies/providerproxy/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type (

RequestMetrics struct {
Provider string
RpcMethod string
RpcMethod []string
StatusCode int
Duration time.Duration
}
Expand Down Expand Up @@ -45,13 +45,15 @@ func (m *ProviderProxy) newMetrics() *metrics {
}

func (m *ProviderProxy) collectMetrics(requestMetrics RequestMetrics) {
labels := prometheus.Labels{
"policy": m.spec.Policy,
"statusCode": strconv.Itoa(requestMetrics.StatusCode),
"provider": requestMetrics.Provider,
"rpcMethod": requestMetrics.RpcMethod,
for _, method := range requestMetrics.RpcMethod {
labels := prometheus.Labels{
"policy": m.spec.Policy,
"statusCode": strconv.Itoa(requestMetrics.StatusCode),
"provider": requestMetrics.Provider,
"rpcMethod": method,
}

m.metrics.TotalRequests.With(labels).Inc()
m.metrics.RequestsDuration.With(labels).Observe(float64(requestMetrics.Duration.Milliseconds() / int64(len(requestMetrics.RpcMethod))))
}

m.metrics.TotalRequests.With(labels).Inc()
m.metrics.RequestsDuration.With(labels).Observe(float64(requestMetrics.Duration.Milliseconds()))
}
38 changes: 27 additions & 11 deletions pkg/filters/proxies/providerproxy/providerproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,40 @@ func (m *ProviderProxy) SelectNode() (*url.URL, error) {
return url.Parse(rpcUrl)
}

func (m *ProviderProxy) ParsePayloadMethod(payload []byte) string {
defaultValue := "UNKNOWN"
func (m *ProviderProxy) ParsePayloadMethod(payload []byte) []string {
defaultValue := []string{"UNKNOWN"}
if len(payload) <= 0 {
return defaultValue
}

jsonBody := map[string]interface{}{}
err := json.Unmarshal(payload, &jsonBody)
if err == nil {
method, exists := jsonBody["method"].(string)
if !exists {
return defaultValue
}
return []string{method}
}

// parse batch call json array
var jsonBodyArr []map[string]interface{}
err = json.Unmarshal(payload, &jsonBodyArr)
if err != nil {
logger.Errorf("parse batch call err: %s, Body: %s", err, string(payload))
return defaultValue
}

method, exists := jsonBody["method"].(string)
if !exists {
return defaultValue
methods := make([]string, 0)

for _, item := range jsonBodyArr {
method, exists := item["method"].(string)
if !exists {
methods = append(methods, "UNKNOWN")
}
methods = append(methods, method)
}
return method
return methods
}

func (m *ProviderProxy) Handle(ctx *context.Context) (result string) {
Expand Down Expand Up @@ -110,16 +127,15 @@ func (m *ProviderProxy) Handle(ctx *context.Context) (result string) {
}

response, err := m.client.Do(forwardReq)

requestMetrics.Duration = fasttime.Since(startTime)
requestMetrics.StatusCode = response.StatusCode
defer m.collectMetrics(requestMetrics)

if err != nil {
logger.Errorf(err.Error())
return err.Error()
}

requestMetrics.Duration = fasttime.Since(startTime)
requestMetrics.StatusCode = response.StatusCode
defer m.collectMetrics(requestMetrics)

outputResponse, err := httpprot.NewResponse(response)
outputResponse.Body = response.Body
if err != nil {
Expand Down
13 changes: 8 additions & 5 deletions pkg/filters/proxies/providerproxy/providerproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,22 @@ urls:
proxy := newTestProviderProxy(yamlConfig, assert)

method := proxy.ParsePayloadMethod([]byte("{\"method\":\"eth_blockNumber\",\"params\":[],\"id\":1,\"jsonrpc\":\"2.0\"}"))
assert.Equal("eth_blockNumber", method)
assert.Equal([]string{"eth_blockNumber"}, method)

method = proxy.ParsePayloadMethod([]byte("{\"method\":\"eth_getBlockByNumber\",\"params\":[\"0xc5043f\",false],\"id\":1,\"jsonrpc\":\"2.0\"}"))
assert.Equal("eth_getBlockByNumber", method)
assert.Equal([]string{"eth_getBlockByNumber"}, method)

method = proxy.ParsePayloadMethod([]byte("test unknown payload"))
assert.Equal("UNKNOWN", method)
assert.Equal([]string{"UNKNOWN"}, method)

method = proxy.ParsePayloadMethod([]byte{})
assert.Equal("UNKNOWN", method)
assert.Equal([]string{"UNKNOWN"}, method)

method = proxy.ParsePayloadMethod([]byte("{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"suix_getAllBalances\",\"params\":[\"0x94f1a597b4e8f709a396f7f6b1482bdcd65a673d111e49286c527fab7c2d0961\"]}"))
assert.Equal("suix_getAllBalances", method)
assert.Equal([]string{"suix_getAllBalances"}, method)

method = proxy.ParsePayloadMethod([]byte("[{\"jsonrpc\": \"2.0\", \"method\": \"eth_getTransactionReceipt\", \"params\": [\"0x7363bf80269875c6ddd3de0089baf0a9af28586dd0e536753d1cbb5eb9d6535b\"], \"id\": 0}, {\"jsonrpc\": \"2.0\", \"method\": \"eth_getTransactionReceipt\", \"params\": [\"0x29696eba0fa0eb5eb4c1495174c6fb37a9e64a3707dc8b64c9d19a650c8e1b5f\"], \"id\": 1}, {\"jsonrpc\": \"2.0\", \"method\": \"eth_getTransactionReceipt\", \"params\": [\"0x8f9631d0fd6a056e422ed0eea938a84b1eca3344729789f4465861c6406c2114\"], \"id\": 2}, {\"jsonrpc\": \"2.0\", \"method\": \"eth_getTransactionReceipt\", \"params\": [\"0x4787c3d707ff6d204590c77f672917dca49c36ec0381d6dea10d84981d008ec5\"], \"id\": 3}, {\"jsonrpc\": \"2.0\", \"method\": \"eth_getTransactionReceipt\", \"params\": [\"0x11132ec86262aa67b27f64a5f5a6550c7b86f85dabe3d824dbab0f20283f9aa6\"], \"id\": 4}, {\"jsonrpc\": \"2.0\", \"method\": \"eth_getTransactionReceipt\", \"params\": [\"0x8efac98473a7cdb5c40ca503089794b06723c09791833c2df5b4732ea5a10451\"], \"id\": 5}, {\"jsonrpc\": \"2.0\", \"method\": \"eth_getTransactionReceipt\", \"params\": [\"0x7df0d4c1b2d1a570a1367a8bd09301f8e6af5ffa03a2b97a693ba440bb87b002\"], \"id\": 6}, {\"jsonrpc\": \"2.0\", \"method\": \"eth_getTransactionReceipt\", \"params\": [\"0x7d473c856b0b8d207c43e46a3327217e809142ad5497109d01ec72d6a1bde45c\"], \"id\": 7}, {\"jsonrpc\": \"2.0\", \"method\": \"eth_getTransactionReceipt\", \"params\": [\"0x512013ff4a44b6604b81f63e330d57a63b03f23dae011753be8c168ce8f6fcc7\"], \"id\": 8}, {\"jsonrpc\": \"2.0\", \"method\": \"eth_getTransactionReceipt\", \"params\": [\"0x438abc4bfc9a46296f191ad3ecc742bbd7da9286a2a92fe8f27f2d0168a19661\"], \"id\": 9}]"))
assert.Equal([]string{"eth_getTransactionReceipt", "eth_getTransactionReceipt", "eth_getTransactionReceipt", "eth_getTransactionReceipt", "eth_getTransactionReceipt", "eth_getTransactionReceipt", "eth_getTransactionReceipt", "eth_getTransactionReceipt", "eth_getTransactionReceipt", "eth_getTransactionReceipt"}, method)

proxy.Close()
}

0 comments on commit f107124

Please sign in to comment.