Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 45 additions & 14 deletions app/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"crypto/sha256"
"time"

"go.opentelemetry.io/otel/attribute"
otelmetrics "go.opentelemetry.io/otel/metric"

"github.com/sei-protocol/sei-chain/app/legacyabci"
"github.com/sei-protocol/sei-chain/sei-cosmos/tasks"
"github.com/sei-protocol/sei-chain/sei-cosmos/telemetry"
Expand All @@ -13,7 +16,7 @@ import (
"github.com/sei-protocol/sei-chain/sei-cosmos/types/legacytm"
abci "github.com/sei-protocol/sei-chain/sei-tendermint/abci/types"
"github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils"
"github.com/sei-protocol/sei-chain/utils/metrics"
utilmetrics "github.com/sei-protocol/sei-chain/utils/metrics"
)

func (app *App) BeginBlock(
Expand All @@ -27,14 +30,18 @@ func (app *App) BeginBlock(
defer beginBlockSpan.End()
ctx = ctx.WithTraceSpanContext(spanCtx)
ctx = ctx.WithEventManager(sdk.NewEventManager())
defer telemetry.MeasureSince(time.Now(), "abci", "begin_block")
beginBlockStart := time.Now()
defer func() {
telemetry.MeasureSince(beginBlockStart, "abci", "begin_block") // TODO(PLT-327): remove once app_abci_begin_block_duration_seconds verified
appMetrics.beginBlockDuration.Record(ctx.Context(), time.Since(beginBlockStart).Seconds())
}()
// inline begin block
if checkHeight {
if err := app.ValidateHeight(height); err != nil {
panic(err)
}
}
metrics.GaugeSeidVersionAndCommit(app.versionInfo.Version, app.versionInfo.GitCommit)
utilmetrics.GaugeSeidVersionAndCommit(app.versionInfo.Version, app.versionInfo.GitCommit) // TODO(PLT-327): remove once app_build_info observable gauge verified
// check if we've reached a target height, if so, execute any applicable handlers
if app.forkInitializer != nil {
app.forkInitializer(ctx)
Expand All @@ -59,9 +66,17 @@ func (app *App) EndBlock(ctx sdk.Context, height int64, blockGasUsed int64) (res
spanCtx, span := app.GetBaseApp().TracingInfo.StartWithContext("EndBlock", ctx.TraceSpanContext())
defer span.End()
ctx = ctx.WithTraceSpanContext(spanCtx)
defer telemetry.MeasureSince(time.Now(), "abci", "end_block")
endBlockStart := time.Now()
defer func() {
telemetry.MeasureSince(endBlockStart, "abci", "end_block") // TODO(PLT-327): remove once app_abci_end_block_duration_seconds verified
appMetrics.endBlockDuration.Record(ctx.Context(), time.Since(endBlockStart).Seconds())
}()
ctx = ctx.WithEventManager(sdk.NewEventManager())
defer telemetry.MeasureSince(time.Now(), "module", "total_end_block")
moduleEndBlockStart := time.Now()
defer func() {
telemetry.MeasureSince(moduleEndBlockStart, "module", "total_end_block") // TODO(PLT-327): remove once app_abci_module_end_block_duration_seconds verified
appMetrics.moduleEndBlockDuration.Record(ctx.Context(), time.Since(moduleEndBlockStart).Seconds())
}()
res.ValidatorUpdates = legacyabci.EndBlock(ctx, height, blockGasUsed, app.EndBlockKeepers)
res.Events = sdk.MarkEventsToIndex(ctx.EventManager().ABCIEvents(), app.IndexEvents)
if cp := app.GetConsensusParams(ctx); cp != nil {
Expand All @@ -83,7 +98,11 @@ func (app *App) CheckTx(ctx context.Context, req *abci.RequestCheckTxV2) *abci.R
}
_, span := app.GetBaseApp().TracingInfo.StartWithContext("CheckTx", ctx)
defer span.End()
defer telemetry.MeasureSince(time.Now(), "abci", "check_tx")
checkTxStart := time.Now()
defer func() {
telemetry.MeasureSince(checkTxStart, "abci", "check_tx") // TODO(PLT-327): remove once app_abci_check_tx_duration_seconds verified
appMetrics.checkTxDuration.Record(ctx, time.Since(checkTxStart).Seconds())
}()
sdkCtx := app.GetCheckTxContext(req.Tx, req.Type == abci.CheckTxTypeV2Recheck)
tx, err := app.txDecoder(req.Tx)
if err != nil {
Expand Down Expand Up @@ -121,22 +140,27 @@ func (app *App) CheckTx(ctx context.Context, req *abci.RequestCheckTxV2) *abci.R
}

func (app *App) DeliverTx(ctx sdk.Context, req abci.RequestDeliverTxV2, tx sdk.Tx, checksum [32]byte) abci.ResponseDeliverTx {
defer metrics.MeasureDeliverTxDuration(time.Now())
deliverTxStart := time.Now()
// ensure we carry the initial context from tracer here
spanCtx, span := app.GetBaseApp().TracingInfo.StartWithContext("DeliverTx", ctx.TraceSpanContext())
defer span.End()
// update context with trace span new context
ctx = ctx.WithTraceSpanContext(spanCtx)
defer telemetry.MeasureSince(time.Now(), "abci", "deliver_tx")
defer func() {
utilmetrics.MeasureDeliverTxDuration(deliverTxStart) // TODO(PLT-327): remove once app_abci_deliver_tx_duration_seconds verified
telemetry.MeasureSince(deliverTxStart, "abci", "deliver_tx") // TODO(PLT-327): remove once app_abci_deliver_tx_duration_seconds verified
appMetrics.deliverTxDuration.Record(ctx.Context(), time.Since(deliverTxStart).Seconds())
}()

gInfo := sdk.GasInfo{}
resultStr := "successful"

defer func() {
telemetry.IncrCounter(1, "tx", "count")
telemetry.IncrCounter(1, "tx", resultStr)
telemetry.SetGauge(float32(gInfo.GasUsed), "tx", "gas", "used")
telemetry.SetGauge(float32(gInfo.GasWanted), "tx", "gas", "wanted")
telemetry.IncrCounter(1, "tx", "count") // TODO(PLT-327): remove once app_tx_count_total verified
telemetry.IncrCounter(1, "tx", resultStr) // TODO(PLT-327): remove once app_tx_count_total verified
telemetry.SetGauge(float32(gInfo.GasUsed), "tx", "gas", "used") // TODO(PLT-327): remove once app_tx_gas_used verified
telemetry.SetGauge(float32(gInfo.GasWanted), "tx", "gas", "wanted") // TODO(PLT-327): remove once app_tx_gas_wanted verified
appMetrics.txCount.Add(ctx.Context(), 1, otelmetrics.WithAttributes(attribute.String("result", resultStr)))
}()
gInfo, result, anteEvents, resCtx, err := legacyabci.DeliverTx(ctx.WithTxBytes(req.Tx).WithTxSum(checksum), tx, app.GetTxConfig(), &app.DeliverTxKeepers, checksum, func(ctx sdk.Context) (sdk.Context, sdk.CacheMultiStore) {
return app.CacheTxContext(ctx, checksum)
Expand Down Expand Up @@ -177,7 +201,11 @@ func (app *App) DeliverTx(ctx sdk.Context, req abci.RequestDeliverTxV2, tx sdk.T

// DeliverTxBatch is not part of the ABCI specification, but this is here for code convention
func (app *App) DeliverTxBatch(ctx sdk.Context, req sdk.DeliverTxBatchRequest) (res sdk.DeliverTxBatchResponse) {
defer metrics.MeasureDeliverBatchTxDuration(time.Now())
deliverBatchStart := time.Now()
defer func() {
utilmetrics.MeasureDeliverBatchTxDuration(deliverBatchStart) // TODO(PLT-327): remove once app_abci_deliver_batch_tx_duration_seconds verified
appMetrics.deliverBatchTxDuration.Record(ctx.Context(), time.Since(deliverBatchStart).Seconds())
}()
spanCtx, span := app.GetBaseApp().TracingInfo.StartWithContext("DeliverTxBatch", ctx.TraceSpanContext())
defer span.End()
// update context with trace span new context
Expand Down Expand Up @@ -207,6 +235,9 @@ func (app *App) Commit(ctx context.Context) (res *abci.ResponseCommit, err error
defer span.End()
start := time.Now()
res, err = app.BaseApp.Commit(ctx)
app.RecordBenchmarkCommitTime(time.Since(start))
elapsed := time.Since(start)
// legacy: telemetry.MeasureSince in sei-cosmos/baseapp/abci.go TODO(PLT-327)
appMetrics.commitDuration.Record(ctx, elapsed.Seconds())
app.RecordBenchmarkCommitTime(elapsed)
return res, err
}
15 changes: 11 additions & 4 deletions app/ante/evm_checktx.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
upgradekeeper "github.com/sei-protocol/sei-chain/sei-cosmos/x/upgrade/keeper"
abci "github.com/sei-protocol/sei-chain/sei-tendermint/abci/types"
tmtypes "github.com/sei-protocol/sei-chain/sei-tendermint/types"
"go.opentelemetry.io/otel/attribute"
otelmetric "go.opentelemetry.io/otel/metric"

"github.com/sei-protocol/sei-chain/utils"
"github.com/sei-protocol/sei-chain/utils/helpers"
"github.com/sei-protocol/sei-chain/utils/metrics"
Expand Down Expand Up @@ -297,14 +300,16 @@ func CheckNonce(ctx sdk.Context, latestCtxGetter func() sdk.Context, ek *evmkeep
ctx = ctx.WithCheckTxCallback(func(priority int64) {
txHash := tmtypes.Tx(ctx.TxBytes()).Hash()
ek.AddPendingNonce(txHash, evmAddr, etx.Nonce(), priority)
metrics.IncrementPendingNonce("added")
metrics.IncrementPendingNonce("added") // TODO(PLT-327): remove once app_pending_nonce_total verified
appAnteMetrics.pendingNonce.Add(ctx.Context(), 1, otelmetric.WithAttributes(attribute.String("event", "added")))
})

// if the mempool expires a transaction, this handler is invoked
ctx = ctx.WithExpireTxHandler(func() {
txHash := tmtypes.Tx(ctx.TxBytes()).Hash()
ek.RemovePendingNonce(txHash)
metrics.IncrementPendingNonce("expired")
metrics.IncrementPendingNonce("expired") // TODO(PLT-327): remove once app_pending_nonce_total verified
appAnteMetrics.pendingNonce.Add(ctx.Context(), 1, otelmetric.WithAttributes(attribute.String("event", "expired")))
})

if txNonce > nextNonce {
Expand All @@ -323,7 +328,8 @@ func CheckNonce(ctx sdk.Context, latestCtxGetter func() sdk.Context, ek *evmkeep

if txNonce < nextNonceToBeMined {
// this nonce has already been mined, we cannot accept it again
metrics.IncrementPendingNonce("rejected")
metrics.IncrementPendingNonce("rejected") // TODO(PLT-327): remove once app_pending_nonce_total verified
appAnteMetrics.pendingNonce.Add(ctx.Context(), 1, otelmetric.WithAttributes(attribute.String("event", "rejected")))
return abci.Rejected
} else if txNonce < nextPendingNonce {
// check if the sender still has enough funds to pay for gas
Expand All @@ -335,7 +341,8 @@ func CheckNonce(ctx sdk.Context, latestCtxGetter func() sdk.Context, ek *evmkeep
// this nonce is allowed to process as it is part of the
// consecutive nonces from nextNonceToBeMined to nextPendingNonce
// This logic allows multiple nonces from an account to be processed in a block.
metrics.IncrementPendingNonce("accepted")
metrics.IncrementPendingNonce("accepted") // TODO(PLT-327): remove once app_pending_nonce_total verified
appAnteMetrics.pendingNonce.Add(ctx.Context(), 1, otelmetric.WithAttributes(attribute.String("event", "accepted")))
return abci.Accepted
}
return abci.Pending
Expand Down
31 changes: 31 additions & 0 deletions app/ante/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ante

import (
"sync"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
)

type anteMetrics struct {
once sync.Once

pendingNonce metric.Int64Counter
}

var appAnteMetrics anteMetrics

// InitAnteMetrics registers all OTel instruments for the ante package.
// Safe to call concurrently; instruments are registered exactly once.
func InitAnteMetrics() {
appAnteMetrics.once.Do(func() {
meter := otel.Meter("app_ante")
var err error
if appAnteMetrics.pendingNonce, err = meter.Int64Counter(
"app_pending_nonce_total",
metric.WithDescription("Pending nonce events by type (added, expired, rejected, accepted)"),
); err != nil {
panic("ante metrics: " + err.Error())
}
})
}
25 changes: 25 additions & 0 deletions app/ante/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ante

import (
"context"
"testing"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
otelmetric "go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/noop"
)

func TestInitAnteMetricsNoPanic(t *testing.T) {
otel.SetMeterProvider(noop.NewMeterProvider())
InitAnteMetrics()
}

func TestAnteMetricsPendingNonceAllEvents(t *testing.T) {
otel.SetMeterProvider(noop.NewMeterProvider())
InitAnteMetrics()
ctx := context.Background()
for _, event := range []string{"added", "expired", "rejected", "accepted"} {
appAnteMetrics.pendingNonce.Add(ctx, 1, otelmetric.WithAttributes(attribute.String("event", event)))
}
}
Loading
Loading