Skip to content

Commit

Permalink
switched to polling to allow metrics collection
Browse files Browse the repository at this point in the history
  • Loading branch information
e-asphyx committed Jul 21, 2024
1 parent f2763fb commit 945b5e3
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 138 deletions.
105 changes: 105 additions & 0 deletions bootstrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package main

import (
"context"
"errors"
"sync"
"time"

tz "github.com/ecadlabs/gotez/v2"
"github.com/ecadlabs/gotez/v2/client"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)

type BootstrapPollerConfig struct {
Client Client
ChainID *tz.ChainID
Timeout time.Duration
Interval time.Duration
Reg prometheus.Registerer
}

type BootstrapPoller struct {
cfg BootstrapPollerConfig

mtx sync.RWMutex
status client.BootstrappedResponse

cancel context.CancelFunc
done chan struct{}
metric prometheus.Gauge
}

func (c *BootstrapPollerConfig) New() *BootstrapPoller {
b := &BootstrapPoller{
cfg: *c,
metric: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "tezos",
Subsystem: "node",
Name: "bootstrapped",
Help: "Returns 1 if the node has synchronized its chain with a few peers.",
}),
}
if c.Reg != nil {
c.Reg.MustRegister(b.metric)
}
return b
}

func (b *BootstrapPoller) Start() {
ctx, cancel := context.WithCancel(context.Background())
b.cancel = cancel
b.done = make(chan struct{})
go b.loop(ctx)
}

func (b *BootstrapPoller) Stop(ctx context.Context) error {
b.cancel()
select {
case <-b.done:
return nil
case <-ctx.Done():
return ctx.Err()
}
}

func (b *BootstrapPoller) Status() client.BootstrappedResponse {
b.mtx.RLock()
defer b.mtx.RUnlock()
return b.status
}

func (b *BootstrapPoller) loop(ctx context.Context) {
t := time.NewTicker(b.cfg.Interval)
defer func() {
t.Stop()
close(b.done)
}()
for {
c, cancel := context.WithTimeout(ctx, b.cfg.Timeout)
resp, err := b.cfg.Client.IsBootstrapped(c, b.cfg.ChainID)
cancel()
if err != nil {
if errors.Is(err, context.Canceled) {
return
}
log.WithField("chain_id", b.cfg.ChainID).Warn(err)
} else {
b.mtx.Lock()
b.status = *resp
b.mtx.Unlock()
v := 0.0
if resp.SyncState == client.SyncStateSynced && resp.Bootstrapped {
v = 1
}
b.metric.Set(v)
}

select {
case <-t.C:
case <-ctx.Done():
return
}
}
}
20 changes: 10 additions & 10 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
)

type Config struct {
Listen string `yaml:"listen"`
URL string `yaml:"url"`
ChainID *tz.ChainID `yaml:"chain_id"`
Timeout time.Duration `yaml:"timeout"`
Tolerance time.Duration `yaml:"tolerance"`
ReconnectDelay time.Duration `yaml:"reconnect_delay"`
UseTimestamps bool `yaml:"use_timestamps"`
CheckBlockDelay bool `yaml:"check_block_delay"`
CheckBootstrapped bool `yaml:"check_bootstrapped"`
CheckSyncState bool `yaml:"check_sync_state"`
Listen string `yaml:"listen"`
URL string `yaml:"url"`
ChainID *tz.ChainID `yaml:"chain_id"`
Timeout time.Duration `yaml:"timeout"`
Tolerance time.Duration `yaml:"tolerance"`
ReconnectDelay time.Duration `yaml:"reconnect_delay"`
UseTimestamps bool `yaml:"use_timestamps"`
BootstrappedPollInterval time.Duration `yaml:"bootstrapped_poll_interval"`
HealthUseBootstrapped bool `yaml:"health_use_bootstrapped"`
HealthUseBlockDelay bool `yaml:"health_use_block_delay"`
}
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ require (
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/ecadlabs/pretty v0.0.0-20230412124801-f948fc689a04 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -13,6 +17,14 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
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.19.1 h1:wZWJDwK+NameRJuPGDhlnFgx8e8HN3XHQeLaYJFJBOE=
github.com/prometheus/client_golang v1.19.1/go.mod h1:mP78NwGzrVks5S2H6ab8+ZZGJLZUq1hoULYBAYBw1Ho=
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE=
github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
Expand All @@ -27,6 +39,8 @@ golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5D
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
65 changes: 0 additions & 65 deletions health_checker.go

This file was deleted.

Loading

0 comments on commit 945b5e3

Please sign in to comment.