Skip to content

Commit

Permalink
da: reuse rpc metricer
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxcanfly committed Jan 31, 2024
1 parent e611b47 commit ad21a24
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 90 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ require (
github.com/fsnotify/fsnotify v1.7.0
github.com/go-chi/chi/v5 v5.0.10
github.com/go-chi/docgen v1.2.0
github.com/gogo/status v1.1.0
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb
github.com/google/go-cmp v0.6.0
github.com/google/gofuzz v1.2.1-0.20220503160820-4a35382e8fc8
Expand Down Expand Up @@ -100,7 +99,6 @@ require (
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
Expand Down
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,10 @@ github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5x
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/googleapis v0.0.0-20180223154316-0cd9801be74a/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s=
github.com/gogo/googleapis v1.4.1 h1:1Yx4Myt7BxzvUr5ldGSbwYiZG6t9wGBZ+8/fX3Wvtq0=
github.com/gogo/googleapis v1.4.1/go.mod h1:2lpHqI5OcWCtVElxXnPt+s8oJvMpySlOyM6xDCrzib4=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/gogo/status v1.1.0 h1:+eIkrewn5q6b30y+g/BJINVVdi2xH7je5MPJ3ZPK3JA=
github.com/gogo/status v1.1.0/go.mod h1:BFv9nrluPLmrS0EmGVvLaPNmRosr9KapBYd5/hpY1WM=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
Expand Down Expand Up @@ -956,15 +950,13 @@ google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9Ywl
google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/genproto v0.0.0-20180518175338-11a468237815/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20181029155118-b69ba1387ce2/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20181202183823-bd91e49a0898/go.mod h1:7Ep/1NZk928CDR8SjdVbjWNpdIf6nzjE3BTgJDr2Atg=
google.golang.org/genproto v0.0.0-20190306203927-b5d61aea6440/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231002182017-d307bd883b97 h1:6GQBEOdGkX6MMTLT9V+TjtIRZCw9VPD5Z+yHY9wMgS0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231002182017-d307bd883b97/go.mod h1:v7nGkzlmW8P3n/bKmWBn2WpBjpOEx8Q6gMueudAmKfY=
google.golang.org/grpc v1.12.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
google.golang.org/grpc v1.14.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
google.golang.org/grpc v1.16.0/go.mod h1:0JHn/cJsOMiMfNA9+DeHDlAU7KAAB5GDlYFpa9MZMio=
google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
Expand Down
2 changes: 1 addition & 1 deletion op-batcher/batcher/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func (l *BatchSubmitter) sendTransaction(txdata txData, queue *txmgr.Queue[txDat
data := txdata.Bytes()

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Duration(l.RollupConfig.BlockTime)*time.Second)
record := l.DAClient.Metrics.RecordDAClientRequest("da_blob_submit")
record := l.DAClient.Metrics.RecordRPCClientRequest("da_blob_submit")
ids, _, err := l.DAClient.Client.Submit(ctx, [][]byte{data}, -1)
record(err)
cancel()
Expand Down
2 changes: 0 additions & 2 deletions op-node/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ type Metricer interface {
RecordRPCServerRequest(method string) func()
RecordRPCClientRequest(method string) func(err error)
RecordRPCClientResponse(method string, err error)
RecordDAClientRequest(method string) func(err error)
RecordDAClientResponse(method string, err error)
SetDerivationIdle(status bool)
RecordPipelineReset()
RecordSequencingError()
Expand Down
2 changes: 1 addition & 1 deletion op-node/rollup/derive/calldata_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func DataFromEVMTransactions(config *rollup.Config, batcherAddr common.Address,
case celestia.DerivationVersionCelestia:
log.Info("celestia: blob request", "id", hex.EncodeToString(tx.Data()))
ctx, cancel := context.WithTimeout(context.Background(), daClient.GetTimeout)
record := daClient.Metrics.RecordDAClientRequest("da_blob_get")
record := daClient.Metrics.RecordRPCClientRequest("da_blob_get")
blobs, err := daClient.Client.Get(ctx, [][]byte{data[1:]})
record(err)
cancel()
Expand Down
76 changes: 0 additions & 76 deletions op-service/metrics/rpc_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,18 @@ import (

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/rpc"
"github.com/gogo/status"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc/codes"
)

const (
RPCServerSubsystem = "rpc_server"
RPCClientSubsystem = "rpc_client"
DAClientSubsystem = "da_client"
)

type RPCMetricer interface {
RecordRPCServerRequest(method string) func()
RecordRPCClientRequest(method string) func(err error)
RecordRPCClientResponse(method string, err error)
RecordDAClientRequest(method string) func(err error)
RecordDAClientResponse(method string, err error)
}

// RPCMetrics tracks all the RPC metrics for the op-service RPC.
Expand All @@ -32,9 +27,6 @@ type RPCMetrics struct {
RPCClientRequestsTotal *prometheus.CounterVec
RPCClientRequestDurationSeconds *prometheus.HistogramVec
RPCClientResponsesTotal *prometheus.CounterVec
DAClientRequestsTotal *prometheus.CounterVec
DAClientRequestDurationSeconds *prometheus.HistogramVec
DAClientResponsesTotal *prometheus.CounterVec
}

// MakeRPCMetrics creates a new RPCMetrics instance with the given process name, and
Expand Down Expand Up @@ -84,32 +76,6 @@ func MakeRPCMetrics(ns string, factory Factory) RPCMetrics {
"method",
"error",
}),
DAClientRequestsTotal: factory.NewCounterVec(prometheus.CounterOpts{
Namespace: ns,
Subsystem: DAClientSubsystem,
Name: "requests_total",
Help: "Total DA requests initiated by the opnode's DA client",
}, []string{
"method",
}),
DAClientRequestDurationSeconds: factory.NewHistogramVec(prometheus.HistogramOpts{
Namespace: ns,
Subsystem: DAClientSubsystem,
Name: "request_duration_seconds",
Buckets: []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10},
Help: "Histogram of DA client request durations",
}, []string{
"method",
}),
DAClientResponsesTotal: factory.NewCounterVec(prometheus.CounterOpts{
Namespace: ns,
Subsystem: DAClientSubsystem,
Name: "responses_total",
Help: "Total DA request responses received by the opnode's DA client",
}, []string{
"method",
"error",
}),
}
}

Expand Down Expand Up @@ -160,40 +126,6 @@ func (m *RPCMetrics) RecordRPCClientResponse(method string, err error) {
m.RPCClientResponsesTotal.WithLabelValues(method, errStr).Inc()
}

// RecordDAClientRequest is a helper method to record an DA client
// request. It bumps the requests metric, tracks the response
// duration, and records the response's error code.
func (m *RPCMetrics) RecordDAClientRequest(method string) func(err error) {
m.DAClientRequestsTotal.WithLabelValues(method).Inc()
timer := prometheus.NewTimer(m.DAClientRequestDurationSeconds.WithLabelValues(method))
return func(err error) {
m.RecordDAClientResponse(method, err)
timer.ObserveDuration()
}
}

// RecordDAClientResponse records an DA response, converting errors into metrics-friendly format.
// Nil errors are converted into <nil>, DA errors into grpc_<error code>, and everything else into <unknown>.
func (m *RPCMetrics) RecordDAClientResponse(method string, err error) {
var errStr string
// Handle DA client errors using gogo/status package
if err == nil {
errStr = "<nil>"
} else {
// Use gogo/status to get error status for DA errors
st, ok := status.FromError(err)
if ok {
// Convert DA error status into a formatted string
errStr = fmt.Sprintf("grpc_%s", codes.Code(st.Code()))
} else {
errStr = "<unknown>"
}
}

// Increment the metric for DA client responses
m.DAClientResponsesTotal.WithLabelValues(method, errStr).Inc()
}

type NoopRPCMetrics struct{}

func (n *NoopRPCMetrics) RecordRPCServerRequest(method string) func() {
Expand All @@ -203,15 +135,7 @@ func (n *NoopRPCMetrics) RecordRPCServerRequest(method string) func() {
func (n *NoopRPCMetrics) RecordRPCClientRequest(method string) func(err error) {
return func(err error) {}
}

func (n *NoopRPCMetrics) RecordRPCClientResponse(method string, err error) {
}

func (n *NoopRPCMetrics) RecordDAClientRequest(method string) func(err error) {
return func(err error) {}
}

func (n *NoopRPCMetrics) RecordDAClientResponse(method string, err error) {
}

var _ RPCMetricer = (*NoopRPCMetrics)(nil)

0 comments on commit ad21a24

Please sign in to comment.