Skip to content

Commit

Permalink
Fixing tracking delete sizes (#674)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-sekara authored Apr 2, 2024
1 parent 072ac72 commit ce5b690
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/chains/evm/logpoller/observability.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func withObservedExecAndRowsAffected(o *ObservedORM, queryName string, queryType
WithLabelValues(o.chainId, queryName, string(queryType)).
Observe(float64(time.Since(queryStarted)))

if err != nil {
if err == nil {
o.datasetSize.
WithLabelValues(o.chainId, queryName, string(queryType)).
Set(float64(rowsAffected))
Expand Down
36 changes: 36 additions & 0 deletions core/chains/evm/logpoller/observability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logpoller

import (
"fmt"
"math"
"math/big"
"testing"
"time"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/prometheus/client_golang/prometheus/testutil"

"github.com/smartcontractkit/chainlink-common/pkg/logger"

"github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils"
ubig "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
Expand Down Expand Up @@ -123,6 +125,40 @@ func TestCountersAreProperlyPopulatedForWrites(t *testing.T) {
assert.Equal(t, float64(2), testutil.ToFloat64(orm.blocksInserted.WithLabelValues("420")))
}

func TestCounterAreProperlyPopulatedForDeletes(t *testing.T) {
orm := createObservedORM(t, 420)
logs := generateRandomLogs(420, 20)

for _, log := range logs {
err := orm.InsertLogsWithBlock([]Log{log}, NewLogPollerBlock(utils.RandomBytes32(), log.BlockNumber, time.Now(), 0))
require.NoError(t, err)
}

// Delete 5 logs
removed, err := orm.DeleteBlocksBefore(logs[4].BlockNumber, 100)
require.NoError(t, err)
assert.Equal(t, int64(5), removed)
assert.Equal(t, float64(5), testutil.ToFloat64(orm.datasetSize.WithLabelValues("420", "DeleteBlocksBefore", "delete")))

// Delete 1 more log
removed, err = orm.DeleteBlocksBefore(logs[5].BlockNumber, 100)
require.NoError(t, err)
assert.Equal(t, int64(1), removed)
assert.Equal(t, float64(1), testutil.ToFloat64(orm.datasetSize.WithLabelValues("420", "DeleteBlocksBefore", "delete")))

// Delete all
removed, err = orm.DeleteBlocksBefore(logs[len(logs)-1].BlockNumber, 0)
require.NoError(t, err)
assert.Equal(t, int64(14), removed)
assert.Equal(t, float64(14), testutil.ToFloat64(orm.datasetSize.WithLabelValues("420", "DeleteBlocksBefore", "delete")))

// Nothing to be deleted
removed, err = orm.DeleteBlocksBefore(math.MaxInt, 0)
require.NoError(t, err)
assert.Equal(t, int64(0), removed)
assert.Equal(t, float64(0), testutil.ToFloat64(orm.datasetSize.WithLabelValues("420", "DeleteBlocksBefore", "delete")))
}

func generateRandomLogs(chainId, count int) []Log {
logs := make([]Log, count)
for i := range logs {
Expand Down

0 comments on commit ce5b690

Please sign in to comment.