diff --git a/core/chains/evm/logpoller/observability.go b/core/chains/evm/logpoller/observability.go index abb3246585..b26607f4c1 100644 --- a/core/chains/evm/logpoller/observability.go +++ b/core/chains/evm/logpoller/observability.go @@ -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)) diff --git a/core/chains/evm/logpoller/observability_test.go b/core/chains/evm/logpoller/observability_test.go index eb81273af2..b0f5a8e634 100644 --- a/core/chains/evm/logpoller/observability_test.go +++ b/core/chains/evm/logpoller/observability_test.go @@ -2,6 +2,7 @@ package logpoller import ( "fmt" + "math" "math/big" "testing" "time" @@ -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" @@ -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 {