Skip to content

Commit

Permalink
feat(cchain): improve lookback metrics (#1604)
Browse files Browse the repository at this point in the history
Include the chain name, and set better buckets for these metrics.

issue: #1526
  • Loading branch information
arajasek authored Jul 30, 2024
1 parent b5e8d10 commit 216cec3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
4 changes: 2 additions & 2 deletions e2e/test/attestations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestApprovedAttestations(t *testing.T) {
t.Helper()
client, err := node.Client()
require.NoError(t, err)
cprov := provider.NewABCIProvider(client, network.ID, nil)
cprov := provider.NewABCIProvider(client, network.ID, netconf.ChainVersionNamer(netconf.Simnet))

ctx := context.Background()
for _, portal := range portals {
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestApprovedValUpdates(t *testing.T) {

client, err := node.Client()
require.NoError(t, err)
cprov := provider.NewABCIProvider(client, network.ID, nil)
cprov := provider.NewABCIProvider(client, network.ID, netconf.ChainVersionNamer(netconf.Simnet))

addr, err := k1util.PubKeyToAddress(node.PrivvalKey.PubKey())
require.NoError(t, err)
Expand Down
14 changes: 8 additions & 6 deletions lib/cchain/provider/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewABCIProvider(abci rpcclient.Client, network netconf.ID, chainNamer func(
gcl := genserve.NewQueryClient(rpcAdaptor{abci: abci})

return Provider{
fetch: newABCIFetchFunc(acl, abci),
fetch: newABCIFetchFunc(acl, abci, chainNamer),
latest: newABCILatestFunc(acl),
window: newABCIWindowFunc(acl),
valset: newABCIValsetFunc(vcl),
Expand Down Expand Up @@ -121,14 +121,16 @@ func newABCIValsetFunc(cl vtypes.QueryClient) valsetFunc {
}
}

func newABCIFetchFunc(cl atypes.QueryClient, client rpcclient.Client) fetchFunc {
func newABCIFetchFunc(cl atypes.QueryClient, client rpcclient.Client, chainNamer func(xchain.ChainVersion) string) fetchFunc {
return func(ctx context.Context, chainVer xchain.ChainVersion, fromOffset uint64) ([]xchain.Attestation, error) {
const endpoint = "fetch_attestations"
defer latency(endpoint)()

ctx, span := tracer.Start(ctx, spanName(endpoint))
defer span.End()

chainName := chainNamer(chainVer)

// try fetching from latest height
atts, ok, err := attsFromAtHeight(ctx, cl, chainVer, fromOffset, 0)
if err != nil {
Expand All @@ -137,7 +139,7 @@ func newABCIFetchFunc(cl atypes.QueryClient, client rpcclient.Client) fetchFunc
}

if ok {
fetchStepsMetrics(0, 0)
fetchStepsMetrics(chainName, 0, 0)
return atts, nil
}

Expand All @@ -154,7 +156,7 @@ func newABCIFetchFunc(cl atypes.QueryClient, client rpcclient.Client) fetchFunc
return []xchain.Attestation{}, nil
}

offsetHeight, err := searchOffsetInHistory(ctx, client, cl, chainVer, fromOffset)
offsetHeight, err := searchOffsetInHistory(ctx, client, cl, chainVer, chainName, fromOffset)
if err != nil {
incQueryErr(endpoint)
return nil, errors.Wrap(err, "searching offset in history")
Expand Down Expand Up @@ -340,7 +342,7 @@ func spanName(endpoint string) string {
// searchOffsetInHistory searches the consensus state history and
// returns a historical consensus block height that contains an approved attestation
// for the provided chain version and fromOffset.
func searchOffsetInHistory(ctx context.Context, client rpcclient.Client, cl atypes.QueryClient, chainVer xchain.ChainVersion, fromOffset uint64) (uint64, error) {
func searchOffsetInHistory(ctx context.Context, client rpcclient.Client, cl atypes.QueryClient, chainVer xchain.ChainVersion, chainName string, fromOffset uint64) (uint64, error) {
const endpoint = "search_offset"
defer latency(endpoint)

Expand Down Expand Up @@ -435,7 +437,7 @@ func searchOffsetInHistory(ctx context.Context, client rpcclient.Client, cl atyp
}

if fromOffset >= earliestAtt.AttestOffset && fromOffset <= latestAtt.AttestOffset {
fetchStepsMetrics(lookbackStepsCounter, binarySearchStepsCounter)
fetchStepsMetrics(chainName, lookbackStepsCounter, binarySearchStepsCounter)
return midHeightIndex, nil
}

Expand Down
16 changes: 9 additions & 7 deletions lib/cchain/provider/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,26 @@ var (
Help: "Total number of query errors per endpoint. Alert if growing.",
}, []string{"endpoint"})

fetchLookbackSteps = promauto.NewHistogram(prometheus.HistogramOpts{
fetchLookbackSteps = promauto.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "lib",
Subsystem: "cprovider",
Name: "fetch_lookback_steps",
Buckets: []float64{0, 1, 2, 4, 8, 16, 32, 64, 128, 256},
Help: "Number of steps in the exponential backoff process to find a start for binary search",
})
}, []string{"chain_version"})

fetchBinarySearchSteps = promauto.NewHistogram(prometheus.HistogramOpts{
fetchBinarySearchSteps = promauto.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "lib",
Subsystem: "cprovider",
Name: "fetch_binary_search_steps",
Buckets: []float64{0, 1, 2, 4, 8, 16, 32, 64, 128, 256},
Help: "Number of steps in the binary search process to find the right height",
})
}, []string{"chain_version"})
)

func fetchStepsMetrics(lookbackSteps, binarySearchSteps uint64) {
fetchLookbackSteps.Observe(float64(lookbackSteps))
fetchBinarySearchSteps.Observe(float64(binarySearchSteps))
func fetchStepsMetrics(chainName string, lookbackSteps, binarySearchSteps uint64) {
fetchLookbackSteps.WithLabelValues(chainName).Observe(float64(lookbackSteps))
fetchBinarySearchSteps.WithLabelValues(chainName).Observe(float64(binarySearchSteps))
}

func latency(endpoint string) func() {
Expand Down

0 comments on commit 216cec3

Please sign in to comment.