Skip to content

Commit

Permalink
Merge pull request #2244 from brave-intl/master
Browse files Browse the repository at this point in the history
production 2023-12-11_1
  • Loading branch information
husobee authored Dec 15, 2023
2 parents ffb0b06 + cf9c238 commit 2fd1c94
Show file tree
Hide file tree
Showing 45 changed files with 374 additions and 7,453 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
strategy:
matrix:
goversion:
- 1.18
- 1.19

steps:
- name: Checkout repository
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '1.18'
go-version: '1.19'
- name: golangci-lint-libs
uses: golangci/golangci-lint-action@v3
with:
Expand All @@ -34,7 +34,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '1.18'
go-version: '1.19'
- name: golangci-lint-services
uses: golangci/golangci-lint-action@v3
with:
Expand All @@ -49,7 +49,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '1.18'
go-version: '1.19'
- name: golangci-lint-tools
uses: golangci/golangci-lint-action@v3
with:
Expand All @@ -64,7 +64,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '1.18'
go-version: '1.19'
- name: golangci-lint-cmd
uses: golangci/golangci-lint-action@v3
with:
Expand All @@ -79,7 +79,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '1.18'
go-version: '1.19'
- name: golangci-lint-main
uses: golangci/golangci-lint-action@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
run:
go: "1.17" ## TODO update to 1.18 once all linters support it
go: "1.19"
timeout: 3m

linters-settings:
Expand Down
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.18-alpine as builder
FROM golang:1.19-alpine as builder

# Put certs in builder image.
RUN apk update
Expand All @@ -20,7 +20,8 @@ RUN cd main && go mod download && CGO_ENABLED=0 GOOS=linux go build \
-ldflags "-w -s -X main.version=${VERSION} -X main.buildTime=${BUILD_TIME} -X main.commit=${COMMIT}" \
-o bat-go main.go

FROM alpine:3.15 as base
# golang:1.19-alpine is based on alpine:3.18.
FROM alpine:3.18 as base

# Put certs in artifact from builder.
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# pass "go" and collect 200 BAT


![CI](https://github.com/brave-intl/bat-go/workflows/CI/badge.svg)


## Developer Setup

1. [Install Go 1.12](https://golang.org/doc/install) (NOTE: Go 1.10 and earlier will not work!)
Expand Down
3 changes: 1 addition & 2 deletions docker-compose.dev-refresh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
# every time a file changes.
dev-refresh:
container_name: grant-dev-refresh
image: golang:1.18
image: golang:1.19
ports:
- "3335:3333"
- "6061:6061"
Expand Down Expand Up @@ -39,7 +39,6 @@ services:
- "DATABASE_MIGRATIONS_URL=file:///src/migrations"
- "DATABASE_URL=postgres://grants:password@postgres/grants?sslmode=disable"
- ENCRYPTION_KEY=MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0
- FEATURE_MERCHANT=true
- DONOR_WALLET_CARD_ID
- DONOR_WALLET_PRIVATE_KEY
- DONOR_WALLET_PUBLIC_KEY
Expand Down
1 change: 0 additions & 1 deletion ecs-vars-dev.env
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ DATABASE_MIGRATIONS_URL=file://migrations
DEBUG=0
ENABLE_JOB_WORKERS=true
ENABLE_LINKING_DRAINING=true
FEATURE_MERCHANT=true
GRANT_SIGNATOR_PUBLIC_KEY=90f006eecd80f21bd26dc363155355c2b857cdd9d77b550db23bccaa2866c3c3
GRANT_WALLET_CARD_ID=9094c3f2-b3ae-438f-bd59-92aaad92de5c
GRANT_WALLET_PUBLIC_KEY=37bb424aa56661058768d6d074163162d50c8f6f9ed64f2f491aac307f0acade
Expand Down
59 changes: 0 additions & 59 deletions libs/clients/bitflyer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ import (
"net/url"
"os"
"runtime/debug"
"strings"
"time"

"github.com/prometheus/client_golang/prometheus"

"github.com/brave-intl/bat-go/libs/altcurrency"
"github.com/brave-intl/bat-go/libs/clients"
appctx "github.com/brave-intl/bat-go/libs/context"
Expand All @@ -27,69 +24,13 @@ import (
)

var (
bfBalanceGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "bitflyer_account_balance",
Help: "A gauge of the current account balance in bitflyer",
})

validSourceFrom = map[string]bool{
"tipping": true,
"adrewards": true,
"userdrain": true,
}
)

func init() {
prometheus.MustRegister(bfBalanceGauge)
}

// WatchBitflyerBalance periodically checks bitflyer inventory balance for BAT
func WatchBitflyerBalance(ctx context.Context, duration time.Duration) error {
client, err := New()
if err != nil {
return fmt.Errorf("failed to create bitflyer client: %w", err)
}

_, err = client.RefreshToken(ctx, TokenPayloadFromCtx(ctx))
if err != nil {
return fmt.Errorf("failed to get bitflyer access token: %w", err)
}

for {
select {
case <-ctx.Done():
return nil
case <-time.After(duration):
go func() {
result, err := client.FetchBalance(ctx)
if err != nil {
logging.FromContext(ctx).Error().Err(err).
Msg("bitflyer client error")
} else {
found := false
for _, inv := range result.Inventory {
if strings.ToLower(inv.CurrencyCode) == "bat" {
found = true
if inv.Amount.LessThan(decimal.NewFromFloat(1.0)) {
logging.FromContext(ctx).Error().Err(errors.New("account is empty")).
Msg("bitflyer account error")
} else {
tmp, _ := inv.Amount.Float64()
bfBalanceGauge.Set(tmp)
}
break
}
}
if !found {
logging.FromContext(ctx).Error().Err(errors.New("currency code BAT not found in response")).
Msg("bitflyer response error")
}
}
}()
}
}
}

// Quote returns a quote of BAT prices
type Quote struct {
ProductCode string `json:"product_code"`
Expand Down
4 changes: 2 additions & 2 deletions libs/clients/bitflyer/instrumented_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions libs/clients/cbr/instrumented_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions libs/clients/coingecko/instrumented_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 0 additions & 68 deletions libs/clients/gemini/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/brave-intl/bat-go/libs/cryptography"
"github.com/brave-intl/bat-go/libs/custodian"
errorutils "github.com/brave-intl/bat-go/libs/errors"
"github.com/brave-intl/bat-go/libs/logging"
"github.com/google/go-querystring/query"
"github.com/prometheus/client_golang/prometheus"
"github.com/shengdoushi/base58"
Expand All @@ -41,11 +40,6 @@ func isIssueCountryEnabled() bool {
}

var (
balanceGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "gemini_account_balance",
Help: "A gauge of the current account balance in gemini",
})

countGeminiWalletAccountValidation = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "count_gemini_wallet_account_validation",
Expand Down Expand Up @@ -74,72 +68,10 @@ var (
var ErrNoAcceptedDocumentType = errors.New("no accepted document type")

func init() {
prometheus.MustRegister(balanceGauge)
prometheus.MustRegister(countGeminiWalletAccountValidation)
prometheus.MustRegister(countGeminiDocumentTypeByIssuingCountry)
}

// WatchGeminiBalance - when called reports the balance to prometheus
func WatchGeminiBalance(ctx context.Context) error {
logger := logging.Logger(ctx, "WatchGeminiBalance")
// create a new gemini client
client, err := New()
if err != nil {
logger.Error().Err(err).Msg("failed to get gemini client")
return fmt.Errorf("failed to get gemini client: %w", err)
}

// get api secret from context
apiSecret, err := appctx.GetStringFromContext(ctx, appctx.GeminiAPISecretCTXKey)
if err != nil {
logger.Error().Err(err).Msg("failed to get gemini api secret")
return fmt.Errorf("failed to get gemini api secret: %w", err)
}
apiKey, err := appctx.GetStringFromContext(ctx, appctx.GeminiAPIKeyCTXKey)
if err != nil {
logger.Error().Err(err).Msg("failed to get gemini api key")
return fmt.Errorf("failed to get gemini api key: %w", err)
}
//create a new hmac hasher
signer := cryptography.NewHMACHasher([]byte(apiSecret))
for {
select {
case <-ctx.Done():
return nil
// check every 10 min
case <-time.After(2 * 60 * time.Second):
// create the gemini payload
payload, err := json.Marshal(NewBalancesPayload(nil))
if err != nil {
logger.Error().Err(err).Msg("failed to create gemini balance payload")
// okay to error, retry in 10 min
continue
}

go func() {
defer func() {
if r := recover(); r != nil {
logger.Error().Str("panic", fmt.Sprintf("%+v", r)).Msg("failed to fetch gemini balance, panic")
}
}()
result, err := client.FetchBalances(ctx, apiKey, signer, string(payload))
if err != nil {
logger.Error().Err(err).Msg("failed to fetch gemini balance")
} else {
// dont care about float downsampling from decimal errs
if result == nil || len(*result) < 1 {
logger.Error().Msg("gemini result is empty")
} else {
b := *result
available, _ := b[0].Available.Float64()
balanceGauge.Set(available)
}
}
}()
}
}
}

// PrivateRequestSequence handles the ability to sign a request multiple times
type PrivateRequestSequence struct {
// the baseline object, corresponds to the signature in the first item
Expand Down
4 changes: 2 additions & 2 deletions libs/clients/gemini/instrumented_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 14 additions & 11 deletions libs/clients/radom/instrumented.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

type InstrumentedClient struct {
Expand All @@ -14,19 +13,23 @@ type InstrumentedClient struct {
vec *prometheus.SummaryVec
}

// newInstrucmentedClient returns an instance of the Client decorated with prometheus summary metric.
func newInstrucmentedClient(name string, cl *Client) *InstrumentedClient {
// newInstrumentedClient returns an instance of the Client decorated with prometheus summary metric.
// This function panics if it cannot register the metric.
func newInstrumentedClient(name string, cl *Client) *InstrumentedClient {
v := prometheus.NewSummaryVec(prometheus.SummaryOpts{
Name: "radom_client_duration_seconds",
Help: "client runtime duration and result",
MaxAge: time.Minute,
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
},
[]string{"instance_name", "method", "result"},
)
prometheus.MustRegister(v)

result := &InstrumentedClient{
name: name,
cl: cl,
vec: promauto.NewSummaryVec(prometheus.SummaryOpts{
Name: "client_duration_seconds",
Help: "client runtime duration and result",
MaxAge: time.Minute,
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
},
[]string{"instance_name", "method", "result"},
),
vec: v,
}

return result
Expand Down
2 changes: 1 addition & 1 deletion libs/clients/radom/radom.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func NewInstrumented(srvURL, secret, proxyAddr string) (*InstrumentedClient, err
return nil, err
}

return newInstrucmentedClient("radom_client", cl), nil
return newInstrumentedClient("radom_client", cl), nil
}

func newClient(srvURL, secret, proxyAddr string) (*Client, error) {
Expand Down
4 changes: 2 additions & 2 deletions libs/clients/ratios/instrumented_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2fd1c94

Please sign in to comment.