Skip to content

Commit dd965f2

Browse files
author
Richard Patel
committed
[PRO-3786] Implement exporter
See merge request Blockdaemon/solana/pyth_exporter!1
2 parents c96e0ba + 4a802a9 commit dd965f2

File tree

14 files changed

+1638
-0
lines changed

14 files changed

+1638
-0
lines changed

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# IDE
2+
.idea/
3+
4+
# Pyth
5+
*keypair.json
6+
.pythd/
7+
keystore
8+
9+
# Env vars
10+
*.env
11+
!docker.example.env
12+
13+
# Binaries
14+
/pyth_exporter

.gitlab-ci.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
include:
2+
- template: Jobs/Build.gitlab-ci.yml
3+
- template: Jobs/Code-Intelligence.gitlab-ci.yml
4+
- template: Security/SAST.gitlab-ci.yml
5+
- template: Security/Container-Scanning.gitlab-ci.yml
6+
- template: Security/License-Scanning.gitlab-ci.yml
7+
- template: Security/Secret-Detection.gitlab-ci.yml
8+
- template: Security/Dependency-Scanning.gitlab-ci.yml
9+
10+
stages:
11+
- build
12+
- test
13+
14+
.go-cache:
15+
image: golang:1.17-alpine
16+
variables:
17+
GOPATH: $CI_PROJECT_DIR/.go
18+
GOPRIVATE: go.blockdaemon.com,gitlab.com/Blockdaemon
19+
before_script:
20+
- mkdir -p .go build/
21+
- apk add --no-cache build-base linux-headers git
22+
- git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/".insteadOf "https://gitlab.com/"
23+
cache:
24+
paths:
25+
- .go/pkg/mod/
26+
27+
lint:
28+
image: registry.gitlab.com/gitlab-org/gitlab-build-images:golangci-lint-alpine
29+
stage: test
30+
allow_failure: true
31+
extends: .go-cache
32+
needs: []
33+
script:
34+
- '[ -e .golangci.yml ] || cp /golangci/.golangci.yml .'
35+
- golangci-lint run --issues-exit-code 0 --out-format code-climate | tee gl-code-quality-report.json | jq -r '.[] | "\(.location.path):\(.location.lines.begin) \(.description)"'
36+
artifacts:
37+
reports:
38+
codequality: gl-code-quality-report.json
39+
paths:
40+
- gl-code-quality-report.json

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.1.0
4+
hooks:
5+
- id: trailing-whitespace
6+
- id: end-of-file-fixer
7+
- id: mixed-line-ending

Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM golang:1.17-alpine as builder
2+
WORKDIR /app
3+
COPY . .
4+
RUN go build -ldflags="-w -s" -o /app/pyth_exporter .
5+
6+
FROM alpine
7+
RUN apk add ca-certificates
8+
COPY --from=builder /app/pyth_exporter /pyth_exporter
9+
ENTRYPOINT ["/pyth_exporter"]

balance.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/gagliardetto/solana-go"
8+
"github.com/gagliardetto/solana-go/rpc"
9+
"github.com/prometheus/client_golang/prometheus"
10+
"go.blockdaemon.com/pyth_exporter/metrics"
11+
"go.uber.org/zap"
12+
)
13+
14+
// balanceScraper retrieves the Pyth publisher balances on request.
15+
type balanceScraper struct {
16+
*prometheus.GaugeVec
17+
18+
log *zap.Logger
19+
rpc *rpc.Client
20+
publishers []solana.PublicKey
21+
}
22+
23+
func newBalanceScraper(publishers []solana.PublicKey, rpcURL string, log *zap.Logger) *balanceScraper {
24+
return &balanceScraper{
25+
GaugeVec: metrics.PublisherBalances,
26+
rpc: rpc.New(rpcURL),
27+
log: log,
28+
publishers: publishers,
29+
}
30+
}
31+
32+
// Collect gets invoked by the Prometheus exporter when a new scrape is requested.
33+
func (b *balanceScraper) Collect(metrics chan<- prometheus.Metric) {
34+
const collectTimeout = 5 * time.Second
35+
ctx, cancel := context.WithTimeout(context.Background(), collectTimeout)
36+
defer cancel()
37+
b.update(ctx)
38+
b.GaugeVec.Collect(metrics)
39+
}
40+
41+
func (b *balanceScraper) update(ctx context.Context) {
42+
res, err := b.rpc.GetMultipleAccounts(ctx, b.publishers...)
43+
if err != nil {
44+
b.log.Warn("Failed to check publisher SOL balances", zap.Error(err))
45+
return
46+
}
47+
for i, acc := range res.Value {
48+
b.GaugeVec.
49+
WithLabelValues(b.publishers[i].String()).
50+
Set(float64(acc.Lamports))
51+
}
52+
}

go.mod

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
module go.blockdaemon.com/pyth_exporter
2+
3+
go 1.17
4+
5+
require (
6+
github.com/cenkalti/backoff/v4 v4.1.2
7+
github.com/gagliardetto/binary v0.6.1
8+
github.com/gagliardetto/solana-go v1.3.0
9+
github.com/prometheus/client_golang v1.12.1
10+
go.uber.org/zap v1.16.0
11+
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
12+
)
13+
14+
require (
15+
contrib.go.opencensus.io/exporter/stackdriver v0.13.4 // indirect
16+
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
17+
github.com/andres-erbsen/clock v0.0.0-20160526145045-9e14626cd129 // indirect
18+
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59 // indirect
19+
github.com/beorn7/perks v1.0.1 // indirect
20+
github.com/blendle/zapdriver v1.3.1 // indirect
21+
github.com/buger/jsonparser v1.1.1 // indirect
22+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
23+
github.com/davecgh/go-spew v1.1.1 // indirect
24+
github.com/dfuse-io/logging v0.0.0-20210109005628-b97a57253f70 // indirect
25+
github.com/fatih/color v1.9.0 // indirect
26+
github.com/gagliardetto/treeout v0.1.4 // indirect
27+
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
28+
github.com/golang/protobuf v1.5.2 // indirect
29+
github.com/gorilla/rpc v1.2.0 // indirect
30+
github.com/gorilla/websocket v1.4.2 // indirect
31+
github.com/json-iterator/go v1.1.12 // indirect
32+
github.com/klauspost/compress v1.13.6 // indirect
33+
github.com/logrusorgru/aurora v2.0.3+incompatible // indirect
34+
github.com/mattn/go-colorable v0.1.4 // indirect
35+
github.com/mattn/go-isatty v0.0.11 // indirect
36+
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
37+
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
38+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
39+
github.com/modern-go/reflect2 v1.0.2 // indirect
40+
github.com/mostynb/zstdpool-freelist v0.0.0-20201229113212-927304c0c3b1 // indirect
41+
github.com/mr-tron/base58 v1.2.0 // indirect
42+
github.com/prometheus/client_model v0.2.0 // indirect
43+
github.com/prometheus/common v0.32.1 // indirect
44+
github.com/prometheus/procfs v0.7.3 // indirect
45+
github.com/teris-io/shortid v0.0.0-20201117134242-e59966efd125 // indirect
46+
github.com/tidwall/gjson v1.9.3 // indirect
47+
github.com/tidwall/match v1.1.1 // indirect
48+
github.com/tidwall/pretty v1.2.0 // indirect
49+
go.opencensus.io v0.22.5 // indirect
50+
go.uber.org/atomic v1.7.0 // indirect
51+
go.uber.org/multierr v1.6.0 // indirect
52+
go.uber.org/ratelimit v0.2.0 // indirect
53+
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
54+
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
55+
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf // indirect
56+
google.golang.org/protobuf v1.26.0 // indirect
57+
)
58+
59+
replace github.com/gagliardetto/solana-go => github.com/Blockdaemon/solana-go v0.5.2-0.20220222095515-4a3ecddb010b

0 commit comments

Comments
 (0)