Skip to content

Commit

Permalink
chore(updater): add prom metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
oyyblin committed Nov 21, 2024
1 parent 3369679 commit a614b27
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 40 deletions.
3 changes: 3 additions & 0 deletions charts/updater/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ spec:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- containerPort: 4014
name: http-prometheus
envFrom:
- configMapRef:
name: {{ include "updater.fullname" . }}
Expand Down
13 changes: 13 additions & 0 deletions charts/updater/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
name: {{include "updater.fullname" .}}
labels: {{- include "updater.labels" . | nindent 4}}
spec:
type: ClusterIP
ports:
- port: 4014
targetPort: http-prometheus
protocol: TCP
name: http-prometheus
selector: {{- include "updater.selectorLabels" . | nindent 4}}
2 changes: 1 addition & 1 deletion contracts/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ deploy-anvil:
SIGNER=0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 \
CHAIN_HASH=0x8990e7a9aaed2ffed73dbd7092123d6f289930540d7651336225dc172e51b2ce \
CHAIN=anvil \
forge script ./script/deploy/DrandOracle.s.sol:DrandOracleScript --private-key $(PRIVATE_KEY) --broadcast
forge script ./script/deploy/DrandOracle.s.sol:DrandOracleScript --broadcast
3 changes: 3 additions & 0 deletions updater/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ RUN GIT_TERMINAL_PROMPT=1 \
FROM gcr.io/distroless/cc-debian12
COPY --from=build /bin/app /

# Metrics port
EXPOSE 4014

ENTRYPOINT ["/app"]
2 changes: 1 addition & 1 deletion updater/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ANVIL_RPC="http://localhost:8545"
ANVIL_DRAND_ORACLE_ADDRESS="0xF3C4a5FeEDA8eBd439f9C22DEF3f1a3Cb326540A"
ANVIL_DRAND_ORACLE_ADDRESS="0xa6F9208a7F9d8CFBDe24417CfAE3092AF31029d0"
ANVIL_SIGNER_PRIVATE_KEY="0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
ANVIL_SENDER_PRIVATE_KEY="0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d"
ANVIL_CHAIN_ID=31337
Expand Down
26 changes: 20 additions & 6 deletions updater/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ import (
"drand-oracle-updater/sender"
"drand-oracle-updater/signer"
"encoding/hex"
"fmt"
"net/http"
"os"
"strings"

"github.com/ethereum/go-ethereum/crypto"

"github.com/drand/drand/client"
"github.com/drand/drand/client/http"
drandHTTPClient "github.com/drand/drand/client/http"
drandLog "github.com/drand/drand/log"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/kelseyhightower/envconfig"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog/log"
"golang.org/x/sync/errgroup"
)
Expand All @@ -41,7 +44,7 @@ func main() {
Str("chain_hash", hex.EncodeToString(chainHash)).
Msg("Initializing drand client...")
drandClient, err := client.New(
client.From(http.ForURLs(cfg.DrandURLs, chainHash)...),
client.From(drandHTTPClient.ForURLs(cfg.DrandURLs, chainHash)...),
client.WithChainHash(chainHash),
client.WithLogger(drandLog.NewLogger(os.Stdout, drandLog.LogError)), // Only log errors
)
Expand All @@ -57,8 +60,9 @@ func main() {
}

// Initialize contract binding
log.Info().Str("address", cfg.DrandOracleAddress).Msg("Initializing DrandOraclecontract binding...")
binding, err := binding.NewBinding(common.HexToAddress(cfg.DrandOracleAddress), rpcClient)
contractAddress := common.HexToAddress(cfg.DrandOracleAddress)
log.Info().Str("address", contractAddress.Hex()).Msg("Initializing DrandOracle contract binding...")
binding, err := binding.NewBinding(contractAddress, rpcClient)
if err != nil {
log.Fatal().Err(err).Msg("error creating binding")
}
Expand All @@ -69,7 +73,7 @@ func main() {
if err != nil {
log.Fatal().Err(err).Msg("error parsing private key")
}
signer := signer.NewSigner(cfg.ChainID, common.HexToAddress(cfg.DrandOracleAddress), signerPrivateKey)
signer := signer.NewSigner(cfg.ChainID, contractAddress, signerPrivateKey)
log.Info().Str("address", signer.Address().Hex()).Msg("Signer initialized")

// Initialize sender
Expand All @@ -83,7 +87,7 @@ func main() {

// Initialize updater service
log.Info().Msg("Initializing updater service...")
updater, err := service.NewUpdater(drandClient, rpcClient, cfg.SetRandomnessGasLimit, binding, cfg.GenesisRound, signer, sender)
updater, err := service.NewUpdater(drandClient, rpcClient, cfg.SetRandomnessGasLimit, cfg.ChainID, contractAddress, binding, cfg.GenesisRound, signer, sender)
if err != nil {
log.Fatal().Err(err).Msg("error creating updater")
}
Expand All @@ -98,6 +102,16 @@ func main() {
return nil
})

// Start metrics server
errGroup.Go(func() error {
log.Info().Int("port", cfg.MetricsPort).Msg("Starting metrics server...")
http.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(fmt.Sprintf(":%d", cfg.MetricsPort), nil); err != nil {
return err
}
return nil
})

if err := errGroup.Wait(); err != nil {
log.Fatal().Err(err).Msg("service error")
}
Expand Down
1 change: 1 addition & 0 deletions updater/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ type Config struct {
SignerPrivateKey string `envconfig:"SIGNER_PRIVATE_KEY" required:"true"`
SenderPrivateKey string `envconfig:"SENDER_PRIVATE_KEY" required:"true"`
GenesisRound uint64 `envconfig:"GENESIS_ROUND" required:"true"`
MetricsPort int `envconfig:"METRICS_PORT" default:"4014"`
}
17 changes: 9 additions & 8 deletions updater/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/drand/drand v1.5.11
github.com/ethereum/go-ethereum v1.14.11
github.com/kelseyhightower/envconfig v1.4.0
github.com/prometheus/client_golang v1.20.5
github.com/rs/zerolog v1.33.0
golang.org/x/sync v0.7.0
)
Expand Down Expand Up @@ -40,26 +41,26 @@ require (
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/holiman/uint256 v1.3.1 // indirect
github.com/kilic/bls12-381 v0.1.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nikkolasg/hexjson v0.1.0 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/supranational/blst v0.3.13 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.25.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
Expand Down
42 changes: 20 additions & 22 deletions updater/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,12 @@ github.com/gogo/status v1.1.1 h1:DuHXlSFHNKqTQ+/ACf5Vs6r4X/dH2EgIzR9Vr+H65kg=
github.com/gogo/status v1.1.1/go.mod h1:jpG3dM5QPcqu19Hg8lkUhBFBa3TcLs1DG7+2Jqci7oU=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb h1:PBC98N2aIaM3XXiurYmW7fx4GZkL8feAMVq7nEjURHk=
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
Expand Down Expand Up @@ -144,8 +143,8 @@ github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dv
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/kilic/bls12-381 v0.1.0 h1:encrdjqKMEvabVQ7qYOKu1OvhqpK4s47wDYtNiPtlp4=
github.com/kilic/bls12-381 v0.1.0/go.mod h1:vDTTHJONJ6G+P2R74EhnyotQDTliQDnFEwhdmfzw1ig=
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand All @@ -164,15 +163,15 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag=
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/pointerstructure v1.2.0 h1:O+i9nHnXS3l/9Wu7r4NrEdwA2VFTicjUEN1uBnDo34A=
github.com/mitchellh/pointerstructure v1.2.0/go.mod h1:BRAsLI5zgXmw97Lf6s25bs8ohIXc3tViBH44KcwB2g4=
github.com/mmcloughlin/addchain v0.4.0 h1:SobOdjm2xLj1KkXN5/n0xTIWyZA2+s99UCY1iPfkHRY=
github.com/mmcloughlin/addchain v0.4.0/go.mod h1:A86O+tHqZLMNO4w6ZZ4FlVQEadcoqkyU72HC5wJ4RlU=
github.com/mmcloughlin/profile v0.1.1/go.mod h1:IhHD7q1ooxgwTgjxQYkACGA77oFTDdFVejUS1/tS/qU=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/nikkolasg/hexjson v0.1.0 h1:Cgi1MSZVQFoJKYeRpBNEcdF3LB+Zo4fYKsDz7h8uJYQ=
github.com/nikkolasg/hexjson v0.1.0/go.mod h1:fbGbWFZ0FmJMFbpCMtJpwb0tudVxSSZ+Es2TsCg57cA=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
Expand All @@ -187,14 +186,14 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.16.0 h1:yk/hx9hDbrGHovbci4BY+pRMfSuuat626eFsHb7tmT8=
github.com/prometheus/client_golang v1.16.0/go.mod h1:Zsulrv/L9oM40tJ7T815tM89lFEugiJ9HzIqaAx4LKc=
github.com/prometheus/client_model v0.4.0 h1:5lQXD3cAg1OXBf4Wq03gTrXHeaV0TQvGfUooCfx1yqY=
github.com/prometheus/client_model v0.4.0/go.mod h1:oMQmHW1/JoDwqLtg57MGgP/Fb1CJEYF2imWWhWtMkYU=
github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdOOfY=
github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY=
github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwaUuI=
github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY=
github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc=
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
Expand Down Expand Up @@ -254,13 +253,12 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c=
go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa h1:FRnLl4eNAQl8hwxVVC17teOw8kdjVDVAiFMtgUdTSRQ=
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -274,8 +272,8 @@ golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc h1:8DyZCyvI8mE1IdLy/60bS+52xfymkE72wv1asokgtao=
Expand Down
124 changes: 124 additions & 0 deletions updater/internal/service/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package service

import (
"encoding/hex"
"fmt"

"github.com/drand/drand/chain"
"github.com/ethereum/go-ethereum/common"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

const (
// Label names
labelChainHash = "chain_hash"
labelChainID = "chain_id"
labelOracleAddress = "oracle_address"

// Drand info metric labels
labelPublicKey = "public_key"
labelID = "id"
labelPeriod = "period"
labelScheme = "scheme"
labelGenesisTime = "genesis_time"
labelGenesisSeed = "genesis_seed"
)

// Metrics holds all Prometheus metrics for the updater
type Metrics struct {
drandRoundTotal *prometheus.GaugeVec
oracleRoundTotal *prometheus.GaugeVec
setRandomnessSuccessTotal *prometheus.CounterVec
setRandomnessFailureTotal *prometheus.CounterVec

// New info metric
drandInfo *prometheus.GaugeVec

// Store label values
chainHash string
chainID int64
oracleAddress common.Address
}

// NewMetrics creates and registers all Prometheus metrics
func NewMetrics(chainID int64, oracleAddress common.Address, drandInfo *chain.Info) *Metrics {
m := &Metrics{
chainHash: drandInfo.HashString(),
chainID: chainID,
oracleAddress: oracleAddress,
}

m.drandRoundTotal = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "drand_round_number_network",
Help: "Current round number from the Drand network",
}, []string{labelChainHash})

m.oracleRoundTotal = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "drand_round_number_oracle",
Help: "Current round number processed by the Oracle",
}, []string{labelChainID, labelOracleAddress})

m.setRandomnessSuccessTotal = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "drand_set_randomness_success_total",
Help: "Total number of successful SetRandomness transactions",
}, []string{labelChainID, labelOracleAddress})

m.setRandomnessFailureTotal = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "drand_set_randomness_failure_total",
Help: "Total number of failed SetRandomness transactions",
}, []string{labelChainID, labelOracleAddress})

// Add info metric
m.drandInfo = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "drand_network_info",
Help: "Static information about the Drand network configuration",
}, []string{
labelChainHash,
labelPublicKey,
labelPeriod,
labelScheme,
labelGenesisTime,
labelGenesisSeed,
})

// Set the info metric with a constant value of 1
m.drandInfo.WithLabelValues(
m.chainHash,
drandInfo.PublicKey.String(),
drandInfo.Period.String(),
drandInfo.Scheme,
fmt.Sprintf("%d", drandInfo.GenesisTime),
hex.EncodeToString(drandInfo.GenesisSeed),
).Set(1)

return m
}

// Helper methods for setting metrics with labels
func (m *Metrics) SetDrandRound(round float64) {
m.drandRoundTotal.WithLabelValues(
m.chainHash,
).Set(round)
}

func (m *Metrics) SetOracleRound(round float64) {
m.oracleRoundTotal.WithLabelValues(
fmt.Sprintf("%d", m.chainID),
m.oracleAddress.Hex(),
).Set(round)
}

func (m *Metrics) IncSetRandomnessSuccess() {
m.setRandomnessSuccessTotal.WithLabelValues(
fmt.Sprintf("%d", m.chainID),
m.oracleAddress.Hex(),
).Inc()
}

func (m *Metrics) IncSetRandomnessFailure() {
m.setRandomnessFailureTotal.WithLabelValues(
fmt.Sprintf("%d", m.chainID),
m.oracleAddress.Hex(),
).Inc()
}
Loading

0 comments on commit a614b27

Please sign in to comment.