Skip to content

Commit

Permalink
consolidate contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
shahbazn committed Dec 1, 2023
1 parent eaecb18 commit 6adf410
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 24 deletions.
4 changes: 3 additions & 1 deletion cmd/relayer_exporter/relayer_exporter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"flag"
"fmt"
"net/http"
Expand Down Expand Up @@ -54,8 +55,9 @@ func main() {
zap.String("Testnet Directory", cfg.GitHub.TestnetsIBCDir),
)

ctx := context.Background()
// TODO: Add a feature to refresh paths at configured interval
paths, err := cfg.IBCPaths()
paths, err := cfg.IBCPaths(ctx)
if err != nil {
log.Fatal(err.Error())
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/collector/ibc_collector.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package collector

import (
"context"
"fmt"
"reflect"
"sync"
Expand Down Expand Up @@ -66,7 +67,7 @@ func (cc IBCCollector) Collect(ch chan<- prometheus.Metric) {
fmt.Sprintf("%s, %s", clientExpiryMetricName, channelStuckPacketsMetricName),
),
)

ctx := context.Background()

Check failure on line 70 in pkg/collector/ibc_collector.go

View workflow job for this annotation

GitHub Actions / GolangCI Lint

[GolangCI Lint] pkg/collector/ibc_collector.go#L70

assignments should only be cuddled with other assignments (wsl)
Raw output
pkg/collector/ibc_collector.go:70:2: assignments should only be cuddled with other assignments (wsl)
	ctx := context.Background()
	^
var wg sync.WaitGroup

Check failure on line 71 in pkg/collector/ibc_collector.go

View workflow job for this annotation

GitHub Actions / GolangCI Lint

[GolangCI Lint] pkg/collector/ibc_collector.go#L71

declarations should never be cuddled (wsl)
Raw output
pkg/collector/ibc_collector.go:71:2: declarations should never be cuddled (wsl)
	var wg sync.WaitGroup
	^

for _, p := range cc.Paths {
Expand All @@ -78,7 +79,7 @@ func (cc IBCCollector) Collect(ch chan<- prometheus.Metric) {
discordIDs := getDiscordIDs(path.Operators)

// Client info
ci, err := ibc.GetClientsInfo(path, cc.RPCs)
ci, err := ibc.GetClientsInfo(ctx, path, cc.RPCs)
status := successStatus

if err != nil {
Expand Down Expand Up @@ -116,7 +117,7 @@ func (cc IBCCollector) Collect(ch chan<- prometheus.Metric) {
// Stuck packets
status = successStatus

stuckPackets, err := ibc.GetChannelsInfo(path, cc.RPCs)
stuckPackets, err := ibc.GetChannelsInfo(ctx, path, cc.RPCs)
if err != nil {
status = errorStatus

Expand Down
5 changes: 3 additions & 2 deletions pkg/collector/wallet_balance_collector.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package collector

import (
"context"
"math/big"
"sync"

Expand Down Expand Up @@ -34,8 +35,8 @@ func (wb WalletBalanceCollector) Describe(ch chan<- *prometheus.Desc) {

func (wb WalletBalanceCollector) Collect(ch chan<- prometheus.Metric) {
log.Debug("Start collecting", zap.String("metric", walletBalanceMetricName))

var wg sync.WaitGroup

Check failure on line 38 in pkg/collector/wallet_balance_collector.go

View workflow job for this annotation

GitHub Actions / GolangCI Lint

[GolangCI Lint] pkg/collector/wallet_balance_collector.go#L38

declarations should never be cuddled (wsl)
Raw output
pkg/collector/wallet_balance_collector.go:38:2: declarations should never be cuddled (wsl)
	var wg sync.WaitGroup
	^
ctx := context.Background()

Check failure on line 39 in pkg/collector/wallet_balance_collector.go

View workflow job for this annotation

GitHub Actions / GolangCI Lint

[GolangCI Lint] pkg/collector/wallet_balance_collector.go#L39

assignments should only be cuddled with other assignments (wsl)
Raw output
pkg/collector/wallet_balance_collector.go:39:2: assignments should only be cuddled with other assignments (wsl)
	ctx := context.Background()
	^

for _, a := range wb.Accounts {
wg.Add(1)
Expand All @@ -46,7 +47,7 @@ func (wb WalletBalanceCollector) Collect(ch chan<- prometheus.Metric) {
balance := 0.0
status := successStatus

err := account.GetBalance(wb.RPCs)
err := account.GetBalance(ctx, wb.RPCs)
if err != nil {
status = errorStatus

Expand Down
14 changes: 5 additions & 9 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ type Discord struct {
ID string `json:"id"`
}

func (a *Account) GetBalance(rpcs *map[string]RPC) error {
func (a *Account) GetBalance(ctx context.Context, rpcs *map[string]RPC) error {
chain, err := chain.PrepChain(chain.Info{
ChainID: (*rpcs)[a.ChainName].ChainID,
RPCAddr: (*rpcs)[a.ChainName].URL,
Expand All @@ -109,8 +109,6 @@ func (a *Account) GetBalance(rpcs *map[string]RPC) error {
return err
}

ctx := context.Background()

coins, err := chain.ChainProvider.QueryBalanceWithAddress(ctx, a.Address)
if err != nil {
return err
Expand Down Expand Up @@ -152,7 +150,7 @@ func (c *Config) GetRPCsMap(ibcPaths []*IBCData) (*map[string]RPC, error) {
return &rpcs, nil
}

func (c *Config) IBCPaths() ([]*IBCData, error) {
func (c *Config) IBCPaths(ctx context.Context) ([]*IBCData, error) {
client := github.NewClient(nil)

if c.GitHub.Token != "" {
Expand All @@ -161,14 +159,14 @@ func (c *Config) IBCPaths() ([]*IBCData, error) {
client = github.NewClient(nil).WithAuthToken(c.GitHub.Token)
}

paths, err := c.getPaths(c.GitHub.IBCDir, client)
paths, err := c.getPaths(ctx, c.GitHub.IBCDir, client)
if err != nil {
return nil, err
}

testnetsPaths := []*IBCData{}
if c.GitHub.TestnetsIBCDir != "" {
testnetsPaths, err = c.getPaths(c.GitHub.TestnetsIBCDir, client)
testnetsPaths, err = c.getPaths(ctx, c.GitHub.TestnetsIBCDir, client)
if err != nil {
return nil, err
}
Expand All @@ -179,13 +177,11 @@ func (c *Config) IBCPaths() ([]*IBCData, error) {
return paths, nil
}

func (c *Config) getPaths(dir string, client *github.Client) ([]*IBCData, error) {
func (c *Config) getPaths(ctx context.Context, dir string, client *github.Client) ([]*IBCData, error) {
if client == nil {
return nil, ErrGitHubClient
}

ctx := context.Background()

_, ibcDir, _, err := client.Repositories.GetContents(ctx, c.GitHub.Org, c.GitHub.Repo, dir, nil)
if err != nil {
return nil, err
Expand Down
4 changes: 3 additions & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"context"
"errors"
"fmt"
"testing"
Expand Down Expand Up @@ -101,10 +102,11 @@ func TestGetRPC(t *testing.T) {

func TestGetPaths(t *testing.T) {
cfg := Config{}
ctx := context.Background()

expError := ErrGitHubClient

_, err := cfg.getPaths("_IBC", nil)
_, err := cfg.getPaths(ctx, "_IBC", nil)
if err == nil {
t.Fatalf("Expected error %q, got no error", expError)
}
Expand Down
12 changes: 4 additions & 8 deletions pkg/ibc/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Channel struct {
}
}

func GetClientsInfo(ibc *config.IBCData, rpcs *map[string]config.RPC) (ClientsInfo, error) {
func GetClientsInfo(ctx context.Context, ibc *config.IBCData, rpcs *map[string]config.RPC) (ClientsInfo, error) {
clientsInfo := ClientsInfo{}

cdA := chain.Info{
Expand All @@ -51,7 +51,7 @@ func GetClientsInfo(ibc *config.IBCData, rpcs *map[string]config.RPC) (ClientsIn

chainA, err := chain.PrepChain(cdA)
if err != nil {
return ClientsInfo{}, fmt.Errorf("Error: %w for %v", err, cdA)
return ClientsInfo{}, fmt.Errorf("%w for %v", err, cdA)
}

clientsInfo.ChainA = chainA
Expand All @@ -65,14 +65,11 @@ func GetClientsInfo(ibc *config.IBCData, rpcs *map[string]config.RPC) (ClientsIn

chainB, err := chain.PrepChain(cdB)
if err != nil {
return ClientsInfo{}, fmt.Errorf("Error: %w for %v", err, cdB)
return ClientsInfo{}, fmt.Errorf("%w for %v", err, cdB)
}

clientsInfo.ChainB = chainB

ctx := context.Background()

fmt.Sprintf("Querying client expiration for %v <-> %v", cdA, cdB)
clientsInfo.ChainAClientExpiration, clientsInfo.ChainAClientInfo, err = relayer.QueryClientExpiration(
ctx,
chainA,
Expand All @@ -94,8 +91,7 @@ func GetClientsInfo(ibc *config.IBCData, rpcs *map[string]config.RPC) (ClientsIn
return clientsInfo, nil
}

func GetChannelsInfo(ibc *config.IBCData, rpcs *map[string]config.RPC) (ChannelsInfo, error) {
ctx := context.Background()
func GetChannelsInfo(ctx context.Context, ibc *config.IBCData, rpcs *map[string]config.RPC) (ChannelsInfo, error) {
channelInfo := ChannelsInfo{}

// Init channel data
Expand Down

0 comments on commit 6adf410

Please sign in to comment.