Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add sentry service layer #1247

Merged
merged 1 commit into from
Nov 24, 2023
Merged
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
103 changes: 102 additions & 1 deletion pkg/service/binance/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/defipod/mochi/pkg/logger"
"github.com/defipod/mochi/pkg/response"
bapdater "github.com/defipod/mochi/pkg/service/binance/adapter"
"github.com/defipod/mochi/pkg/service/sentrygo"
"github.com/defipod/mochi/pkg/util"
)

Expand All @@ -23,9 +24,10 @@ type Binance struct {
config *config.Config
logger logger.Logger
cache cache.Cache
sentry sentrygo.Service
}

func NewService(cfg *config.Config, l logger.Logger, cache cache.Cache) Service {
func NewService(cfg *config.Config, l logger.Logger, cache cache.Cache, sentry sentrygo.Service) Service {
return &Binance{
getExchangeInfoURL: "https://api.binance.com/api/v3/exchangeInfo",
getSymbolKlinesURL: "https://api.binance.com/api/v3/uiKlines?symbol=%s&interval=1h&limit=168", // 168h = 7d
Expand All @@ -34,9 +36,16 @@ func NewService(cfg *config.Config, l logger.Logger, cache cache.Cache) Service
config: cfg,
logger: l,
cache: cache,
sentry: sentry,
}
}

var (
sentryTags = map[string]string{
"type": "system",
}
)

func (b *Binance) GetExchangeInfo(symbol string) (*response.GetExchangeInfoResponse, error, int) {
b.logger.Debug("start binance.GetExchangeInfo()")
defer b.logger.Debug("end binance.GetExchangeInfo()")
Expand All @@ -59,6 +68,13 @@ func (b *Binance) GetExchangeInfo(symbol string) (*response.GetExchangeInfoRespo

statusCode, err := util.FetchData(url, res)
if err != nil || statusCode != http.StatusOK {
b.sentry.CaptureErrorEvent(sentrygo.SentryCapturePayload{
Message: fmt.Sprintf("[API mochi] - Binance - GetExchangeInfo failed %v", err),
Tags: sentryTags,
Extra: map[string]interface{}{
"symbols": symbol,
},
})
return nil, fmt.Errorf("binance.GetExchangeInfo() failed: %v", err), statusCode
}

Expand Down Expand Up @@ -93,6 +109,13 @@ func (b *Binance) GetTickerPrice(symbol string) (*response.GetTickerPriceRespons

statusCode, err := util.FetchData(url, res)
if err != nil || statusCode != http.StatusOK {
b.sentry.CaptureErrorEvent(sentrygo.SentryCapturePayload{
Message: fmt.Sprintf("[API mochi] - Binance - GetTickerPrice failed %v", err),
Tags: sentryTags,
Extra: map[string]interface{}{
"symbols": symbol,
},
})
return nil, fmt.Errorf("binance.GetTickerPrice() failed: %v", err), statusCode
}

Expand Down Expand Up @@ -126,6 +149,13 @@ func (b *Binance) GetAvgPriceBySymbol(symbol string) (*response.GetAvgPriceBySym

statusCode, err := util.FetchData(url, res)
if err != nil || statusCode != http.StatusOK {
b.sentry.CaptureErrorEvent(sentrygo.SentryCapturePayload{
Message: fmt.Sprintf("[API mochi] - Binance - GetAvgPriceBySymbol failed %v", err),
Tags: sentryTags,
Extra: map[string]interface{}{
"symbols": symbol,
},
})
return nil, fmt.Errorf("binance.GetAvgPriceBySymbol() failed: %v", err), statusCode
}

Expand Down Expand Up @@ -156,6 +186,13 @@ func (b *Binance) GetKlinesBySymbol(symbol string) ([]response.GetKlinesDataResp

statusCode, err := util.FetchData(fmt.Sprintf(b.getSymbolKlinesURL, symbol), &data)
if err != nil || statusCode != http.StatusOK {
b.sentry.CaptureErrorEvent(sentrygo.SentryCapturePayload{
Message: fmt.Sprintf("[API mochi] - Binance - GetKlinesBySymbol failed %v", err),
Tags: sentryTags,
Extra: map[string]interface{}{
"symbols": symbol,
},
})
return nil, fmt.Errorf("binance.GetKlinesBySymbol() failed: %v", err), statusCode
}

Expand Down Expand Up @@ -201,6 +238,14 @@ func (b *Binance) GetUserAsset(apiKey, apiSecret string) ([]response.BinanceUser

res, err = bapdater.GetUserAsset(apiKey, apiSecret)
if err != nil {
b.sentry.CaptureErrorEvent(sentrygo.SentryCapturePayload{
Message: fmt.Sprintf("[API mochi] - Binance - GetUserAsset failed %v", err),
Tags: sentryTags,
Extra: map[string]interface{}{
"apiKey": apiKey,
"apiSecret": apiSecret,
},
})
return nil, err
}

Expand Down Expand Up @@ -229,6 +274,14 @@ func (b *Binance) GetFundingAsset(apiKey, apiSecret string) ([]response.BinanceU

res, err = bapdater.GetFundingAsset(apiKey, apiSecret)
if err != nil {
b.sentry.CaptureErrorEvent(sentrygo.SentryCapturePayload{
Message: fmt.Sprintf("[API mochi] - Binance - GetFundingAsset failed %v", err),
Tags: sentryTags,
Extra: map[string]interface{}{
"apiKey": apiKey,
"apiSecret": apiSecret,
},
})
return nil, err
}

Expand Down Expand Up @@ -257,6 +310,14 @@ func (b *Binance) GetStakingProductPosition(apiKey, apiSecret string) ([]respons

res, err = bapdater.GetStakingProductPosition(apiKey, apiSecret)
if err != nil {
b.sentry.CaptureErrorEvent(sentrygo.SentryCapturePayload{
Message: fmt.Sprintf("[API mochi] - Binance - GetStakingProductPosition failed %v", err),
Tags: sentryTags,
Extra: map[string]interface{}{
"apiKey": apiKey,
"apiSecret": apiSecret,
},
})
return nil, err
}

Expand Down Expand Up @@ -285,6 +346,14 @@ func (b *Binance) GetLendingAccount(apiKey, apiSecret string) (*response.Binance

res, err = bapdater.GetLendingAccount(apiKey, apiSecret)
if err != nil {
b.sentry.CaptureErrorEvent(sentrygo.SentryCapturePayload{
Message: fmt.Sprintf("[API mochi] - Binance - GetLendingAccount failed %v", err),
Tags: sentryTags,
Extra: map[string]interface{}{
"apiKey": apiKey,
"apiSecret": apiSecret,
},
})
return nil, err
}

Expand Down Expand Up @@ -313,6 +382,14 @@ func (b *Binance) GetSimpleEarn(apiKey, apiSecret string) (*response.BinanceSimp

res, err = bapdater.GetSimpleEarnAccount(apiKey, apiSecret)
if err != nil {
b.sentry.CaptureErrorEvent(sentrygo.SentryCapturePayload{
Message: fmt.Sprintf("[API mochi] - Binance - GetSimpleEarn failed %v", err),
Tags: sentryTags,
Extra: map[string]interface{}{
"apiKey": apiKey,
"apiSecret": apiSecret,
},
})
return nil, err
}

Expand Down Expand Up @@ -341,6 +418,14 @@ func (b *Binance) GetFutureAccountBalance(apiKey, apiSecret string) ([]response.

res, err = bapdater.GetFutureAccountBalance(apiKey, apiSecret)
if err != nil {
b.sentry.CaptureErrorEvent(sentrygo.SentryCapturePayload{
Message: fmt.Sprintf("[API mochi] - Binance - GetFutureAccountBinance failed %v", err),
Tags: sentryTags,
Extra: map[string]interface{}{
"apiKey": apiKey,
"apiSecret": apiSecret,
},
})
return nil, err
}

Expand Down Expand Up @@ -369,6 +454,14 @@ func (b *Binance) GetFutureAccount(apiKey, apiSecret string) (*response.BinanceF

res, err = bapdater.GetFutureAccount(apiKey, apiSecret)
if err != nil {
b.sentry.CaptureErrorEvent(sentrygo.SentryCapturePayload{
Message: fmt.Sprintf("[API mochi] - Binance - GetFutureAccount failed %v", err),
Tags: sentryTags,
Extra: map[string]interface{}{
"apiKey": apiKey,
"apiSecret": apiSecret,
},
})
return nil, err
}

Expand Down Expand Up @@ -397,6 +490,14 @@ func (b *Binance) GetFutureAccountInfo(apiKey, apiSecret string) ([]response.Bin

res, err = bapdater.GetFutureAccountInfo(apiKey, apiSecret)
if err != nil {
b.sentry.CaptureErrorEvent(sentrygo.SentryCapturePayload{
Message: fmt.Sprintf("[API mochi] - Binance - GetFutureAccountInfo failed %v", err),
Tags: sentryTags,
Extra: map[string]interface{}{
"apiKey": apiKey,
"apiSecret": apiSecret,
},
})
return nil, err
}

Expand Down
15 changes: 14 additions & 1 deletion pkg/service/birdeye/birdeye.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,32 @@ import (
"github.com/defipod/mochi/pkg/cache"
"github.com/defipod/mochi/pkg/config"
"github.com/defipod/mochi/pkg/logger"
"github.com/defipod/mochi/pkg/service/sentrygo"
"github.com/defipod/mochi/pkg/util"
)

type birdeye struct {
config *config.Config
logger logger.Logger
cache cache.Cache
sentry sentrygo.Service
}

func NewService(cfg *config.Config, l logger.Logger, cache cache.Cache) Service {
func NewService(cfg *config.Config, l logger.Logger, cache cache.Cache, sentry sentrygo.Service) Service {
return &birdeye{
config: cfg,
logger: l,
cache: cache,
sentry: sentry,
}
}

var (
publicBirdeye = "https://public-api.birdeye.so"
birdeyeTokenPriceKey = "birdeye-token-price"
sentryTags = map[string]string{
"type": "system",
}
)

func (b *birdeye) GetTokenPrice(address string) (*TokenPrice, error) {
Expand Down Expand Up @@ -75,6 +81,13 @@ func (b *birdeye) doNetworkTokenPrice(address string) (*TokenPrice, error) {
err := b.fetchBirdeyeData(url, &res)
if err != nil {
b.logger.Fields(logger.Fields{"url": url}).Error(err, "[birdeye.GetTokenPrice] b.fetchBirdeyeData() failed")
b.sentry.CaptureErrorEvent(sentrygo.SentryCapturePayload{
Message: fmt.Sprintf("[API mochi] - Birdeye - doNetWorkTokenPrice failed %v", err),
Tags: sentryTags,
Extra: map[string]interface{}{
"url": url,
},
})
return nil, err
}

Expand Down
30 changes: 23 additions & 7 deletions pkg/service/chainexplorer/chainexplorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,32 @@ import (
"github.com/defipod/mochi/pkg/logger"
"github.com/defipod/mochi/pkg/model"
"github.com/defipod/mochi/pkg/response"
"github.com/defipod/mochi/pkg/service/sentrygo"
"github.com/defipod/mochi/pkg/util"
)

type chainExplorer struct {
cfg config.Config
log logger.Logger
cache cache.Cache
cfg config.Config
log logger.Logger
cache cache.Cache
sentry sentrygo.Service
}

func NewService(cfg config.Config, log logger.Logger, cache cache.Cache) Service {
func NewService(cfg config.Config, log logger.Logger, cache cache.Cache, sentry sentrygo.Service) Service {
return &chainExplorer{
cfg: cfg,
log: log,
cache: cache,
cfg: cfg,
log: log,
cache: cache,
sentry: sentry,
}
}

var (
sentryTags = map[string]string{
"type": "system",
}
)

func (c *chainExplorer) GetGasTracker(listChain []model.Chain) ([]response.GasTrackerResponse, error) {
apiKey := ""
gasTrackerResp := make([]response.GasTrackerResponse, 0)
Expand Down Expand Up @@ -92,6 +101,13 @@ func (c *chainExplorer) doNetworkGetGasTracker(url string, resp interface{}) err
}
statusCode, err := util.SendRequest(query)
if err != nil {
c.sentry.CaptureErrorEvent(sentrygo.SentryCapturePayload{
Message: fmt.Sprintf("[API mochi] - ChainExplorer - doNetWorkGetGasTracker failed %v", err),
Tags: sentryTags,
Extra: map[string]interface{}{
"url": url,
},
})
return fmt.Errorf("send request failed: %v", err)
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/service/coingecko/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/defipod/mochi/pkg/response"
"github.com/defipod/mochi/pkg/service/sentrygo"
"github.com/defipod/mochi/pkg/util"
)

Expand All @@ -27,6 +28,15 @@ func (c *CoinGecko) doNetworkCoinByContract(platformId, contractAddress string,
}
if err != nil || status != http.StatusOK {
if retry == 0 {
c.sentry.CaptureErrorEvent(sentrygo.SentryCapturePayload{
Message: fmt.Sprintf("[API mochi] - Coingecko - doNetworkCoinByContract failed - %v", err),
Tags: sentryTags,
Extra: map[string]interface{}{
"platformId": platformId,
"contractAddress": contractAddress,
"retry": retry,
},
})
return nil, fmt.Errorf("%d - %v", status, err)
} else {
return c.GetCoinByContract(platformId, contractAddress, retry-1)
Expand Down
Loading