-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separated rpc call metrics into a collector (#20)
* separated rpc call metrics into a collector to remove circular dependencies and make it cleaner * fix eigen metrics example that was breaking lint * removed unneeded methods from noop metrics * added TODO comment in clients constructor * updated constructor comment following madhur's advise
- Loading branch information
Showing
11 changed files
with
170 additions
and
157 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,58 @@ | ||
package rpccalls | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
) | ||
|
||
// Collector contains instrumented metrics that should be incremented by the avs node using the methods below | ||
// it is used by the eigensdk's instrumented_client, but can also be used by avs teams to instrument their own client | ||
// if it differs from ours. | ||
type Collector struct { | ||
rpcRequestDurationSeconds *prometheus.HistogramVec | ||
rpcRequestTotal *prometheus.CounterVec | ||
} | ||
|
||
// NewCollector returns an rpccalls Collector that collects metrics for json-rpc calls | ||
func NewCollector(eigenNamespace, avsName string, reg prometheus.Registerer) *Collector { | ||
return &Collector{ | ||
rpcRequestDurationSeconds: promauto.With(reg).NewHistogramVec( | ||
prometheus.HistogramOpts{ | ||
Namespace: eigenNamespace, | ||
Name: "rpc_request_duration_seconds", | ||
Help: "Duration of json-rpc <method> in seconds", | ||
ConstLabels: prometheus.Labels{"avs_name": avsName}, | ||
}, | ||
// client_version is the client name and its current version. We don't separate them because | ||
// this string is returned as a single string by the web3_clientVersion jsonrpc call | ||
// which doesn't follow any standardized format so not possible to parse it... | ||
// https://ethereum.org/en/developers/docs/apis/json-rpc/#web3_clientversion | ||
[]string{"method", "client_version"}, | ||
), | ||
rpcRequestTotal: promauto.With(reg).NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Namespace: eigenNamespace, | ||
Name: "rpc_request_total", | ||
Help: "Total number of json-rpc <method> requests", | ||
ConstLabels: prometheus.Labels{"avs_name": avsName}, | ||
}, | ||
[]string{"method", "client_version"}, | ||
), | ||
} | ||
} | ||
|
||
// ObserveRPCRequestDurationSeconds observes the duration of a json-rpc request | ||
func (c *Collector) ObserveRPCRequestDurationSeconds(duration float64, method, clientVersion string) { | ||
c.rpcRequestDurationSeconds.With(prometheus.Labels{ | ||
"method": method, | ||
"client_version": clientVersion, | ||
}).Observe(duration) | ||
} | ||
|
||
// AddRPCRequestTotal adds a json-rpc request to the total number of requests | ||
func (c *Collector) AddRPCRequestTotal(method, clientVersion string) { | ||
c.rpcRequestTotal.With(prometheus.Labels{ | ||
"method": method, | ||
"client_version": clientVersion, | ||
}).Inc() | ||
} |
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,22 @@ | ||
package rpccalls | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/testutil" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRpcCallsCollector(t *testing.T) { | ||
reg := prometheus.NewRegistry() | ||
rpcCallsCollector := NewCollector("testavs", "testname", reg) | ||
|
||
rpcCallsCollector.ObserveRPCRequestDurationSeconds(1, "testmethod", "testclient/testversion") | ||
// TODO(samlaf): not sure how to test histogram values.. there's no mention of histograms in | ||
// https://pkg.go.dev/github.com/prometheus/client_golang/prometheus/testutil | ||
// assert.Equal(t, 1.0, testutil.ToFloat64(suite.metrics.rpcRequestDurationSeconds)) | ||
|
||
rpcCallsCollector.AddRPCRequestTotal("testmethod", "testclient/testversion") | ||
assert.Equal(t, 1.0, testutil.ToFloat64(rpcCallsCollector.rpcRequestTotal.WithLabelValues("testmethod", "testclient/testversion"))) | ||
} |
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