Skip to content

Commit 95e8103

Browse files
authored
Feat/ implement sentry integration (#80)
* feat: implement sentry integration * fix: lint error * fix: lint * fix: as coderabbit * fix: as comment
1 parent b815d14 commit 95e8103

File tree

13 files changed

+254
-13
lines changed

13 files changed

+254
-13
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ build/
1414
# Dependency directories
1515
vendor/
1616

17-
test_key/
17+
test_key/
18+
19+
.env*

challenger/challenger.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/initia-labs/opinit-bots/challenger/child"
1111
challengerdb "github.com/initia-labs/opinit-bots/challenger/db"
1212
"github.com/initia-labs/opinit-bots/challenger/host"
13+
"github.com/initia-labs/opinit-bots/sentry_integration"
1314
"github.com/initia-labs/opinit-bots/server"
1415

1516
bottypes "github.com/initia-labs/opinit-bots/bot/types"
@@ -45,7 +46,12 @@ type Challenger struct {
4546
}
4647

4748
func NewChallenger(cfg *challengertypes.Config, db types.DB, sv *server.Server) *Challenger {
48-
err := cfg.Validate()
49+
err := sentry_integration.Init("opinit-bots", string(bottypes.BotTypeChallenger))
50+
if err != nil {
51+
panic(err)
52+
}
53+
54+
err = cfg.Validate()
4955
if err != nil {
5056
panic(err)
5157
}

challenger/handler.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package challenger
22

33
import (
4+
"fmt"
45
"sort"
56

7+
"github.com/getsentry/sentry-go"
68
challengerdb "github.com/initia-labs/opinit-bots/challenger/db"
79
challengertypes "github.com/initia-labs/opinit-bots/challenger/types"
10+
"github.com/initia-labs/opinit-bots/sentry_integration"
811
"github.com/initia-labs/opinit-bots/types"
912
"go.uber.org/zap"
1013
)
@@ -70,7 +73,10 @@ func (c *Challenger) getLatestChallenges() []challengertypes.Challenge {
7073
}
7174

7275
func (c *Challenger) handleChallenge(ctx types.Context, challenge challengertypes.Challenge) error {
73-
// TODO: warning log or send to alerting system
76+
sentry_integration.CaptureCurrentHubException(
77+
fmt.Errorf("challenge: %v", challenge),
78+
sentry.LevelWarning,
79+
)
7480
ctx.Logger().Error("challenge", zap.Any("challenge", challenge))
7581

7682
return nil

executor/batchsubmitter/batch.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
executortypes "github.com/initia-labs/opinit-bots/executor/types"
1616
"github.com/initia-labs/opinit-bots/node"
1717
btypes "github.com/initia-labs/opinit-bots/node/broadcaster/types"
18+
"github.com/initia-labs/opinit-bots/sentry_integration"
1819
"github.com/initia-labs/opinit-bots/types"
1920
)
2021

@@ -115,7 +116,11 @@ func (bs *BatchSubmitter) handleBatch(blockBytes []byte) (int, error) {
115116
// finalizeBatch finalizes the batch by writing the last block's commit to the batch file.
116117
// it creates batch messages for the batch data and adds them to the processed messages.
117118
// the batch data is divided into chunks and each chunk is added to the processed messages.
118-
func (bs *BatchSubmitter) finalizeBatch(ctx types.Context, blockHeight int64) error {
119+
func (bs *BatchSubmitter) finalizeBatch(parentCtx types.Context, blockHeight int64) error {
120+
transaction, ctx := sentry_integration.StartSentryTransaction(parentCtx, "finalizeBatch", "Finalizes the batch")
121+
defer transaction.Finish()
122+
transaction.SetTag("height", fmt.Sprintf("%d", blockHeight))
123+
119124
// write last block's commit to batch file
120125
rawCommit, err := bs.node.GetRPCClient().QueryRawCommit(ctx, blockHeight)
121126
if err != nil {

executor/executor.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/initia-labs/opinit-bots/executor/celestia"
1010
"github.com/initia-labs/opinit-bots/executor/child"
1111
"github.com/initia-labs/opinit-bots/executor/host"
12+
"github.com/initia-labs/opinit-bots/sentry_integration"
1213
"github.com/initia-labs/opinit-bots/server"
1314

1415
bottypes "github.com/initia-labs/opinit-bots/bot/types"
@@ -36,7 +37,12 @@ type Executor struct {
3637
}
3738

3839
func NewExecutor(cfg *executortypes.Config, db types.DB, sv *server.Server) *Executor {
39-
err := cfg.Validate()
40+
err := sentry_integration.Init("opinit-bots", string(bottypes.BotTypeExecutor))
41+
if err != nil {
42+
panic(err)
43+
}
44+
45+
err = cfg.Validate()
4046
if err != nil {
4147
panic(err)
4248
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ require (
99
cosmossdk.io/x/feegrant v0.1.1
1010
cosmossdk.io/x/tx v0.13.7
1111
github.com/celestiaorg/go-square/v2 v2.0.0
12+
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d
1213
github.com/cometbft/cometbft v0.38.12
1314
github.com/cosmos/cosmos-sdk v0.50.11
1415
github.com/cosmos/go-bip39 v1.0.0
1516
github.com/cosmos/gogoproto v1.7.0
1617
github.com/cosmos/ibc-go/v8 v8.5.0
18+
github.com/getsentry/sentry-go v0.27.0
1719
github.com/gofiber/fiber/v2 v2.52.5
1820
github.com/initia-labs/OPinit v0.6.2
1921
github.com/pkg/errors v0.9.1
@@ -76,7 +78,6 @@ require (
7678
github.com/fatih/color v1.17.0 // indirect
7779
github.com/felixge/httpsnoop v1.0.4 // indirect
7880
github.com/fsnotify/fsnotify v1.7.0 // indirect
79-
github.com/getsentry/sentry-go v0.27.0 // indirect
8081
github.com/go-kit/kit v0.13.0 // indirect
8182
github.com/go-kit/log v0.2.1 // indirect
8283
github.com/go-logfmt/logfmt v0.6.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,8 @@ github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyY
884884
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
885885
github.com/census-instrumentation/opencensus-proto v0.3.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
886886
github.com/census-instrumentation/opencensus-proto v0.4.1/go.mod h1:4T9NM4+4Vw91VeyqjLS6ao50K5bOcLKN6Q42XnYaRYw=
887+
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d h1:S2NE3iHSwP0XV47EEXL8mWmRdEfGscSJ+7EgePNgt0s=
888+
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
887889
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
888890
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
889891
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=

node/block_handler.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@ import (
99
rpccoretypes "github.com/cometbft/cometbft/rpc/core/types"
1010
comettypes "github.com/cometbft/cometbft/types"
1111
nodetypes "github.com/initia-labs/opinit-bots/node/types"
12+
"github.com/initia-labs/opinit-bots/sentry_integration"
1213
"github.com/initia-labs/opinit-bots/types"
1314
"github.com/pkg/errors"
1415
)
1516

1617
// handleBeginBlock handles the begin block.
1718
func (n *Node) handleBeginBlock(ctx types.Context, blockID []byte, protoBlock *prototypes.Block, latestHeight int64) error {
19+
span, ctx := sentry_integration.StartSentrySpan(ctx, "handleBeginBlock", "Handles the begin block")
20+
defer span.Finish()
21+
span.SetTag("height", fmt.Sprintf("%d", latestHeight))
22+
1823
if n.beginBlockHandler != nil {
1924
return n.beginBlockHandler(ctx, nodetypes.BeginBlockArgs{
2025
BlockID: blockID,
@@ -27,6 +32,10 @@ func (n *Node) handleBeginBlock(ctx types.Context, blockID []byte, protoBlock *p
2732

2833
// handleBlockTxs handles the block transactions.
2934
func (n *Node) handleBlockTxs(ctx types.Context, block *rpccoretypes.ResultBlock, blockResult *rpccoretypes.ResultBlockResults, latestHeight int64) error {
35+
span, ctx := sentry_integration.StartSentrySpan(ctx, "handleBlockTxs", "Handles the block transactions")
36+
defer span.Finish()
37+
span.SetTag("height", fmt.Sprintf("%d", latestHeight))
38+
3039
if len(block.Block.Txs) != len(blockResult.TxsResults) {
3140
return fmt.Errorf("mismatch in transactions and results count: %d vs %d", len(block.Block.Txs), len(blockResult.TxsResults))
3241
}
@@ -55,6 +64,10 @@ func (n *Node) handleBlockTxs(ctx types.Context, block *rpccoretypes.ResultBlock
5564

5665
// handleFinalizeBlock handles the finalize block.
5766
func (n *Node) handleFinalizeBlock(ctx types.Context, blockHeight int64, blockTime time.Time, blockResult *rpccoretypes.ResultBlockResults, latestHeight int64) error {
67+
span, ctx := sentry_integration.StartSentrySpan(ctx, "handleFinalizeBlock", "Handles the finalize block")
68+
defer span.Finish()
69+
span.SetTag("height", fmt.Sprintf("%d", latestHeight))
70+
5871
return n.handleEvents(ctx, blockHeight, blockTime, blockResult.FinalizeBlockEvents, latestHeight, nil, 0)
5972
}
6073

@@ -73,6 +86,10 @@ func (n *Node) handleEvents(ctx types.Context, blockHeight int64, blockTime time
7386

7487
// handleEndBlock handles the end block.
7588
func (n *Node) handleEndBlock(ctx types.Context, blockID []byte, protoBlock *prototypes.Block, latestHeight int64) error {
89+
span, ctx := sentry_integration.StartSentrySpan(ctx, "handleEndBlock", "Handles the end block")
90+
defer span.Finish()
91+
span.SetTag("height", fmt.Sprintf("%d", latestHeight))
92+
7693
if n.endBlockHandler != nil {
7794
return n.endBlockHandler(ctx, nodetypes.EndBlockArgs{
7895
BlockID: blockID,
@@ -84,8 +101,13 @@ func (n *Node) handleEndBlock(ctx types.Context, blockID []byte, protoBlock *pro
84101
}
85102

86103
// handleRawBlock handles the raw block bytes.
87-
func (n *Node) handleRawBlock(ctx types.Context, blockHeight int64, latestHeight int64, blockBytes []byte) error {
104+
func (n *Node) handleRawBlock(parentCtx types.Context, blockHeight int64, latestHeight int64, blockBytes []byte) error {
88105
if n.rawBlockHandler != nil {
106+
transactions, ctx := sentry_integration.StartSentryTransaction(parentCtx, "handleRawBlock", "Handles the raw block bytes")
107+
defer transactions.Finish()
108+
transactions.SetTag("height", fmt.Sprintf("%d", blockHeight))
109+
transactions.SetTag("latest_height", fmt.Sprintf("%d", latestHeight))
110+
89111
return n.rawBlockHandler(ctx, nodetypes.RawBlockArgs{
90112
BlockHeight: blockHeight,
91113
LatestHeight: latestHeight,

node/broadcaster/account.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import (
44
"context"
55
"fmt"
66
"math"
7+
"strings"
78

89
abci "github.com/cometbft/cometbft/abci/types"
10+
"github.com/getsentry/sentry-go"
911
"github.com/initia-labs/opinit-bots/keys"
1012
btypes "github.com/initia-labs/opinit-bots/node/broadcaster/types"
1113
"github.com/initia-labs/opinit-bots/node/rpcclient"
14+
"github.com/initia-labs/opinit-bots/sentry_integration"
1215
"github.com/initia-labs/opinit-bots/txutils"
1316
"github.com/initia-labs/opinit-bots/types"
1417

@@ -19,6 +22,7 @@ import (
1922
"github.com/cosmos/cosmos-sdk/codec"
2023
"github.com/cosmos/cosmos-sdk/crypto/keyring"
2124
sdk "github.com/cosmos/cosmos-sdk/types"
25+
2226
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
2327
"github.com/cosmos/cosmos-sdk/types/tx/signing"
2428
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
@@ -221,6 +225,13 @@ func (b BroadcasterAccount) CalculateGas(ctx context.Context, msgs ...sdk.Msg) (
221225

222226
res, err := b.rpcClient.QueryABCI(ctx, simQuery)
223227
if err != nil {
228+
for _, e := range sentryCapturedErrors {
229+
if strings.Contains(err.Error(), e.Error()) {
230+
sentry_integration.CaptureCurrentHubException(err, sentry.LevelError)
231+
return txtypes.SimulateResponse{}, 0, err
232+
}
233+
}
234+
224235
return txtypes.SimulateResponse{}, 0, err
225236
}
226237

node/broadcaster/process.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import (
77
"strings"
88
"time"
99

10+
"github.com/getsentry/sentry-go"
1011
"github.com/pkg/errors"
1112
"go.uber.org/zap"
1213

1314
rpccoretypes "github.com/cometbft/cometbft/rpc/core/types"
1415

1516
btypes "github.com/initia-labs/opinit-bots/node/broadcaster/types"
17+
"github.com/initia-labs/opinit-bots/sentry_integration"
1618
"github.com/initia-labs/opinit-bots/types"
1719
)
1820

@@ -122,7 +124,9 @@ func (b *Broadcaster) Start(ctx types.Context) error {
122124
}
123125
}
124126
if err != nil {
125-
return errors.Wrap(err, "failed to handle processed msgs")
127+
err = errors.Wrap(err, "failed to handle processed msgs")
128+
sentry_integration.CaptureCurrentHubException(err, sentry.LevelWarning)
129+
return err
126130
}
127131
}
128132
}

0 commit comments

Comments
 (0)