diff --git a/internal/proxy/app_conn.go b/internal/proxy/app_conn.go index feaf2f02d..52c5c82a0 100644 --- a/internal/proxy/app_conn.go +++ b/internal/proxy/app_conn.go @@ -4,6 +4,7 @@ import ( "context" "time" + "github.com/go-kit/kit/metrics" abciclient "github.com/tendermint/tendermint/abci/client" "github.com/tendermint/tendermint/abci/types" ) @@ -80,11 +81,7 @@ func (app *appConnConsensus) InitChainSync( ctx context.Context, req types.RequestInitChain, ) (*types.ResponseInitChain, error) { - start := time.Now() - defer func() { - app.metrics.MethodTiming.With("method", "init_chain", - "type", "sync").Observe(time.Since(start).Seconds()) - }() + defer addTimeSample(app.metrics.MethodTiming.With("method", "init_chain", "type", "sync"))() return app.appConn.InitChainSync(ctx, req) } @@ -92,11 +89,7 @@ func (app *appConnConsensus) BeginBlockSync( ctx context.Context, req types.RequestBeginBlock, ) (*types.ResponseBeginBlock, error) { - start := time.Now() - defer func() { - app.metrics.MethodTiming.With("method", "begin_block", - "type", "sync").Observe(time.Since(start).Seconds()) - }() + defer addTimeSample(app.metrics.MethodTiming.With("method", "begin_block", "type", "sync"))() return app.appConn.BeginBlockSync(ctx, req) } @@ -104,11 +97,7 @@ func (app *appConnConsensus) DeliverTxAsync( ctx context.Context, req types.RequestDeliverTx, ) (*abciclient.ReqRes, error) { - start := time.Now() - defer func() { - app.metrics.MethodTiming.With("method", "deliver_tx", - "type", "aync").Observe(time.Since(start).Seconds()) - }() + defer addTimeSample(app.metrics.MethodTiming.With("method", "deliver_tx", "type", "aync"))() return app.appConn.DeliverTxAsync(ctx, req) } @@ -116,20 +105,12 @@ func (app *appConnConsensus) EndBlockSync( ctx context.Context, req types.RequestEndBlock, ) (*types.ResponseEndBlock, error) { - start := time.Now() - defer func() { - app.metrics.MethodTiming.With("method", "deliver_tx", - "type", "sync").Observe(time.Since(start).Seconds()) - }() + defer addTimeSample(app.metrics.MethodTiming.With("method", "deliver_tx", "type", "sync"))() return app.appConn.EndBlockSync(ctx, req) } func (app *appConnConsensus) CommitSync(ctx context.Context) (*types.ResponseCommit, error) { - start := time.Now() - defer func() { - app.metrics.MethodTiming.With("method", "commit", - "type", "sync").Observe(time.Since(start).Seconds()) - }() + defer addTimeSample(app.metrics.MethodTiming.With("method", "commit", "type", "sync"))() return app.appConn.CommitSync(ctx) } @@ -157,20 +138,12 @@ func (app *appConnMempool) Error() error { } func (app *appConnMempool) FlushAsync(ctx context.Context) (*abciclient.ReqRes, error) { - start := time.Now() - defer func() { - app.metrics.MethodTiming.With("method", "flush", - "type", "sync").Observe(time.Since(start).Seconds()) - }() + defer addTimeSample(app.metrics.MethodTiming.With("method", "flush", "type", "sync"))() return app.appConn.FlushAsync(ctx) } func (app *appConnMempool) FlushSync(ctx context.Context) error { - start := time.Now() - defer func() { - app.metrics.MethodTiming.With("method", "flush", - "type", "sync").Observe(time.Since(start).Seconds()) - }() + defer addTimeSample(app.metrics.MethodTiming.With("method", "flush", "type", "sync"))() return app.appConn.FlushSync(ctx) } @@ -182,11 +155,7 @@ func (app *appConnMempool) CheckTxAsync(ctx context.Context, req types.RequestCh } func (app *appConnMempool) CheckTxSync(ctx context.Context, req types.RequestCheckTx) (*types.ResponseCheckTx, error) { - start := time.Now() - defer func() { - app.metrics.MethodTiming.With("method", "check_tx", - "type", "sync").Observe(time.Since(start).Seconds()) - }() + defer addTimeSample(app.metrics.MethodTiming.With("method", "check_tx", "type", "sync"))() return app.appConn.CheckTxSync(ctx, req) } @@ -210,29 +179,17 @@ func (app *appConnQuery) Error() error { } func (app *appConnQuery) EchoSync(ctx context.Context, msg string) (*types.ResponseEcho, error) { - start := time.Now() - defer func() { - app.metrics.MethodTiming.With("method", "echo", - "type", "sync").Observe(time.Since(start).Seconds()) - }() + defer addTimeSample(app.metrics.MethodTiming.With("method", "echo", "type", "sync"))() return app.appConn.EchoSync(ctx, msg) } func (app *appConnQuery) InfoSync(ctx context.Context, req types.RequestInfo) (*types.ResponseInfo, error) { - start := time.Now() - defer func() { - app.metrics.MethodTiming.With("method", "info", - "type", "sync").Observe(time.Since(start).Seconds()) - }() + defer addTimeSample(app.metrics.MethodTiming.With("method", "info", "type", "sync"))() return app.appConn.InfoSync(ctx, req) } func (app *appConnQuery) QuerySync(ctx context.Context, reqQuery types.RequestQuery) (*types.ResponseQuery, error) { - start := time.Now() - defer func() { - app.metrics.MethodTiming.With("method", "query", - "type", "sync").Observe(time.Since(start).Seconds()) - }() + defer addTimeSample(app.metrics.MethodTiming.With("method", "query", "type", "sync"))() return app.appConn.QuerySync(ctx, reqQuery) } @@ -259,11 +216,7 @@ func (app *appConnSnapshot) ListSnapshotsSync( ctx context.Context, req types.RequestListSnapshots, ) (*types.ResponseListSnapshots, error) { - start := time.Now() - defer func() { - app.metrics.MethodTiming.With("method", "list_snapshots", - "type", "sync").Observe(time.Since(start).Seconds()) - }() + defer addTimeSample(app.metrics.MethodTiming.With("method", "list_snapshots", "type", "sync"))() return app.appConn.ListSnapshotsSync(ctx, req) } @@ -271,32 +224,25 @@ func (app *appConnSnapshot) OfferSnapshotSync( ctx context.Context, req types.RequestOfferSnapshot, ) (*types.ResponseOfferSnapshot, error) { - start := time.Now() - defer func() { - app.metrics.MethodTiming.With("method", "offer_snapshot", - "type", "sync").Observe(time.Since(start).Seconds()) - }() + defer addTimeSample(app.metrics.MethodTiming.With("method", "offer_snapshot", "type", "sync"))() return app.appConn.OfferSnapshotSync(ctx, req) } func (app *appConnSnapshot) LoadSnapshotChunkSync( ctx context.Context, req types.RequestLoadSnapshotChunk) (*types.ResponseLoadSnapshotChunk, error) { - start := time.Now() - defer func() { - app.metrics.MethodTiming.With("method", "load_snapshot_chunk", - "type", "sync").Observe(time.Since(start).Seconds()) - }() + defer addTimeSample(app.metrics.MethodTiming.With("method", "load_snapshot_chunk", "type", "sync"))() return app.appConn.LoadSnapshotChunkSync(ctx, req) } func (app *appConnSnapshot) ApplySnapshotChunkSync( ctx context.Context, req types.RequestApplySnapshotChunk) (*types.ResponseApplySnapshotChunk, error) { - start := time.Now() - defer func() { - app.metrics.MethodTiming.With("method", "apply_snapshot_chunk", - "type", "sync").Observe(time.Since(start).Seconds()) - }() + defer addTimeSample(app.metrics.MethodTiming.With("method", "apply_snapshot_chunk", "type", "sync"))() return app.appConn.ApplySnapshotChunkSync(ctx, req) } + +func addTimeSample(m metrics.Histogram) func() { + start := time.Now() + return func() { m.Observe(time.Since(start).Seconds()) } +}