Skip to content

Commit

Permalink
metrics: use VictoriaMetrics instead of prometheus/client_golang
Browse files Browse the repository at this point in the history
Most notably:
* the syntax is much simpler (no Register, less verbose calls)
* now we can Get metrics (concurrent-safe, along with Set), so:
  * data/ipfs/metrics.go now uses Set and Get instead of atomic.Float64
  * vochaininfo also uses Set and Get for most metrics, out of vi.lock.Lock()

Additional changes:
* unify types of vochaininfo to uint64
* VochainInfo.Height now returns uint64
* drop metric vochain_vote_tree_increase_last_minute
* drop vs.MetricsAgent field
* drop package `go.vocdoni.io/dvote/metrics` altogether
  (NewAgent was refactored into a new method in HTTProuter.ExposePrometheusEndpoint)
  • Loading branch information
altergui authored and p4u committed Oct 31, 2023
1 parent 4436efc commit 775d9fd
Show file tree
Hide file tree
Showing 17 changed files with 154 additions and 259 deletions.
2 changes: 1 addition & 1 deletion api/api_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ type GenericTransactionWithInfo struct {

type ChainInfo struct {
ID string `json:"chainId" example:"azeno"`
BlockTime [5]int32 `json:"blockTime" example:"12000,11580,11000,11100,11100"`
BlockTime [5]uint64 `json:"blockTime" example:"12000,11580,11000,11100,11100"`
ElectionCount uint64 `json:"electionCount" example:"120"`
OrganizationCount uint64 `json:"organizationCount" example:"20"`
GenesisTime time.Time `json:"genesisTime" format:"date-time" example:"2022-11-17T18:00:57.379551614Z"`
Expand Down
6 changes: 3 additions & 3 deletions api/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func (a *API) chainEstimateHeightHandler(_ *apirest.APIdata, ctx *httprouter.HTT
// @Success 200 {object} object{date=string}
// @Router /chain/blockToDate/{height} [get]
func (a *API) chainEstimateDateHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) error {
height, err := strconv.ParseInt(ctx.URLParam("height"), 10, 64)
height, err := strconv.ParseUint(ctx.URLParam("height"), 10, 64)
if err != nil {
return err
}
Expand Down Expand Up @@ -737,11 +737,11 @@ func (a *API) chainValidatorsHandler(_ *apirest.APIdata, ctx *httprouter.HTTPCon
// @Success 200 {object} api.Block
// @Router /chain/blocks/{height} [get]
func (a *API) chainBlockHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) error {
height, err := strconv.ParseInt(ctx.URLParam("height"), 10, 64)
height, err := strconv.ParseUint(ctx.URLParam("height"), 10, 64)
if err != nil {
return err
}
tmblock := a.vocapp.GetBlockByHeight(height)
tmblock := a.vocapp.GetBlockByHeight(int64(height))
if tmblock == nil {
return ErrBlockNotFound
}
Expand Down
4 changes: 2 additions & 2 deletions api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func (a *API) electionSummary(pi *indexertypes.Process) ElectionSummary {
ElectionID: pi.ID,
OrganizationID: pi.EntityID,
Status: models.ProcessStatus_name[pi.Status],
StartDate: a.vocinfo.HeightTime(int64(pi.StartBlock)),
EndDate: a.vocinfo.HeightTime(int64(pi.EndBlock)),
StartDate: a.vocinfo.HeightTime(uint64(pi.StartBlock)),
EndDate: a.vocinfo.HeightTime(uint64(pi.EndBlock)),
FinalResults: pi.FinalResults,
VoteCount: pi.VoteCount,
ManuallyEnded: pi.EndBlock < pi.StartBlock+pi.BlockCount,
Expand Down
6 changes: 3 additions & 3 deletions cmd/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"go.vocdoni.io/dvote/httprouter"
"go.vocdoni.io/dvote/internal"
"go.vocdoni.io/dvote/log"
"go.vocdoni.io/dvote/metrics"
"go.vocdoni.io/dvote/service"
"go.vocdoni.io/dvote/types"
"go.vocdoni.io/dvote/vochain"
Expand Down Expand Up @@ -507,8 +506,9 @@ func main() {
}
// Enable metrics via proxy
if conf.Metrics.Enabled {
srv.MetricsAgent = metrics.NewAgent("/metrics",
time.Duration(conf.Metrics.RefreshInterval)*time.Second, srv.Router)
// This flag will make CometBFT register their metrics in prometheus
srv.Config.TendermintMetrics = true
srv.Router.ExposePrometheusEndpoint("/metrics")
}
}

Expand Down
4 changes: 1 addition & 3 deletions cmd/voconed/voconed.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"go.vocdoni.io/dvote/crypto/zk/circuit"
"go.vocdoni.io/dvote/internal"
"go.vocdoni.io/dvote/log"
"go.vocdoni.io/dvote/metrics"
"go.vocdoni.io/dvote/vochain/state"
"go.vocdoni.io/dvote/vocone"
"go.vocdoni.io/proto/build/go/models"
Expand Down Expand Up @@ -232,8 +231,7 @@ func main() {
log.Fatal(err)
}

vc.MetricsAgent = metrics.NewAgent("/metrics",
time.Duration(10)*time.Second, vc.Router)
vc.Router.ExposePrometheusEndpoint("/metrics")

// enable faucet if requested, this will create a new account and attach the faucet API to the vocone API
if config.enableFaucetWithAmount > 0 {
Expand Down
3 changes: 1 addition & 2 deletions data/ipfs/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ func (i *Handler) Init(d *types.DataStore) error {
}

go i.updateStats(time.Minute)
i.registerMetrics()

return nil
}
Expand Down Expand Up @@ -221,7 +220,7 @@ func (i *Handler) Unpin(ctx context.Context, path string) error {

// Stats returns stats about the IPFS node.
func (i *Handler) Stats() map[string]any {
return map[string]any{"peers": stats.Peers.Load(), "addresses": stats.KnownAddrs.Load(), "pins": stats.Pins.Load()}
return map[string]any{"peers": stats.Peers.Get(), "addresses": stats.KnownAddrs.Get(), "pins": stats.Pins.Get()}
}

func (i *Handler) countPins(ctx context.Context) (int, error) {
Expand Down
47 changes: 12 additions & 35 deletions data/ipfs/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,17 @@ import (
"sync"
"time"

"github.com/prometheus/client_golang/prometheus"
"go.uber.org/atomic"

"go.vocdoni.io/dvote/metrics"
"github.com/VictoriaMetrics/metrics"
)

var stats struct {
Peers atomic.Float64
KnownAddrs atomic.Float64
Pins atomic.Float64
}

// registerMetrics registers prometheus metrics
func (i *Handler) registerMetrics() {
metrics.Register(prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Namespace: "file",
Name: "peers",
Help: "The number of connected peers",
},
stats.Peers.Load))

metrics.Register(prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Namespace: "file",
Name: "addresses",
Help: "The number of registered addresses",
},
stats.KnownAddrs.Load))

metrics.Register(prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Namespace: "file",
Name: "pins",
Help: "The number of pinned files",
},
stats.Pins.Load))
var stats = struct {
Peers *metrics.Counter
KnownAddrs *metrics.Counter
Pins *metrics.Counter
}{
Peers: metrics.NewCounter("file_peers"),
KnownAddrs: metrics.NewCounter("file_addresses"),
Pins: metrics.NewCounter("file_pins"),
}

// updateStats constantly updates the ipfs stats (Peers, KnownAddrs, Pins)
Expand All @@ -55,7 +32,7 @@ func (i *Handler) updateStats(interval time.Duration) {
go func() {
list, err := i.CoreAPI.Swarm().Peers(ctx)
if err == nil {
stats.Peers.Store(float64(len(list)))
stats.Peers.Set(uint64(len(list)))
}
wg.Done()
}()
Expand All @@ -64,7 +41,7 @@ func (i *Handler) updateStats(interval time.Duration) {
go func() {
list, err := i.CoreAPI.Swarm().KnownAddrs(ctx)
if err == nil {
stats.KnownAddrs.Store(float64(len(list)))
stats.KnownAddrs.Set(uint64(len(list)))
}
wg.Done()
}()
Expand All @@ -73,7 +50,7 @@ func (i *Handler) updateStats(interval time.Duration) {
go func() {
count, err := i.countPins(ctx)
if err == nil {
stats.Pins.Store(float64(count))
stats.Pins.Set(uint64(count))
}
wg.Done()
}()
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go 1.21
require (
git.sr.ht/~sircmpwn/go-bare v0.0.0-20210406120253-ab86bc2846d9
github.com/766b/chi-prometheus v0.0.0-20211217152057-87afa9aa2ca8
github.com/VictoriaMetrics/metrics v1.24.0
github.com/arnaucube/go-blindsecp256k1 v0.0.0-20211204171003-644e7408753f
github.com/cockroachdb/pebble v0.0.0-20230620232302-06034ff014e0
github.com/cometbft/cometbft v0.38.0
Expand Down Expand Up @@ -58,7 +59,6 @@ require (
github.com/vocdoni/go-snark v0.0.0-20210709152824-f6e4c27d7319
github.com/vocdoni/storage-proofs-eth-go v0.1.6
go.mongodb.org/mongo-driver v1.12.1
go.uber.org/atomic v1.11.0
go.vocdoni.io/proto v1.15.4-0.20231023165811-02adcc48142a
golang.org/x/crypto v0.14.0
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
Expand Down Expand Up @@ -270,6 +270,8 @@ require (
github.com/tklauser/go-sysconf v0.3.10 // indirect
github.com/tklauser/numcpus v0.4.0 // indirect
github.com/ucarion/urlpath v0.0.0-20200424170820-7ccc79b76bbb // indirect
github.com/valyala/fastrand v1.1.0 // indirect
github.com/valyala/histogram v1.2.0 // indirect
github.com/wasmerio/wasmer-go v1.0.4 // indirect
github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc // indirect
github.com/whyrusleeping/cbor v0.0.0-20171005072247-63513f603b11 // indirect
Expand Down Expand Up @@ -298,6 +300,7 @@ require (
go.opentelemetry.io/otel/sdk v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/dig v1.17.0 // indirect
go.uber.org/fx v1.20.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ github.com/VictoriaMetrics/fastcache v1.5.3/go.mod h1:+jv9Ckb+za/P1ZRg/sulP5Ni1v
github.com/VictoriaMetrics/fastcache v1.5.7/go.mod h1:ptDBkNMQI4RtmVo8VS/XwRY6RoTu1dAWCbrk+6WsEM8=
github.com/VictoriaMetrics/fastcache v1.6.0 h1:C/3Oi3EiBCqufydp1neRZkqcwmEiuRT9c3fqvvgKm5o=
github.com/VictoriaMetrics/fastcache v1.6.0/go.mod h1:0qHz5QP0GMX4pfmMA/zt5RgfNuXJrTP0zS7DqpHGGTw=
github.com/VictoriaMetrics/metrics v1.24.0 h1:ILavebReOjYctAGY5QU2F9X0MYvkcrG3aEn2RKa1Zkw=
github.com/VictoriaMetrics/metrics v1.24.0/go.mod h1:eFT25kvsTidQFHb6U0oa0rTrDRdz4xTYjpL8+UPohys=
github.com/VividCortex/gohistogram v1.0.0 h1:6+hBz+qvs0JOrrNhhmR7lFxo5sINxBCGXrdtl/UvroE=
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
github.com/adlio/schema v1.3.3 h1:oBJn8I02PyTB466pZO1UZEn1TV5XLlifBSyMrmHl/1I=
Expand Down Expand Up @@ -1504,8 +1506,12 @@ github.com/urfave/cli/v2 v2.17.2-0.20221006022127-8f469abc00aa/go.mod h1:1CNUng3
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.6.0/go.mod h1:FstJa9V+Pj9vQ7OJie2qMHdwemEDaDiSdBnvPM1Su9w=
github.com/valyala/fastrand v1.1.0 h1:f+5HkLW4rsgzdNoleUOB69hyT9IlD2ZQh9GyDMfb5G8=
github.com/valyala/fastrand v1.1.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/valyala/histogram v1.2.0 h1:wyYGAZZt3CpwUiIb9AU/Zbllg1llXyrtApRS815OLoQ=
github.com/valyala/histogram v1.2.0/go.mod h1:Hb4kBwb4UxsaNbbbh+RRz8ZR6pdodR57tzWUS3BUzXY=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU=
github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM=
Expand Down
17 changes: 17 additions & 0 deletions httprouter/httprouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import (
"time"

chiprometheus "github.com/766b/chi-prometheus"
"github.com/VictoriaMetrics/metrics"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
reuse "github.com/libp2p/go-reuseport"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
"go.vocdoni.io/dvote/log"
"golang.org/x/crypto/acme"
Expand Down Expand Up @@ -170,6 +172,21 @@ func (r *HTTProuter) EnablePrometheusMetrics(prometheusID string) {
r.Mux.Use(chiprometheus.NewMiddleware(prometheusID))
}

// ExposePrometheusEndpoint registers a HTTPHandler at the passed path
// that will expose the metrics collected by VictoriaMetrics and Prometheus
func (r *HTTProuter) ExposePrometheusEndpoint(path string) {
r.AddRawHTTPHandler(path, "GET", func(w http.ResponseWriter, req *http.Request) {
// upstream packages (cometbft, libp2p) call prometheus.Register(), so expose those metrics first
promhttp.Handler().ServeHTTP(w, req)

// then append the metrics registered in VictoriaMetrics (our metrics, basically)
// don't exposeProcessMetrics here since the go_* and process_* are already part of the output of
// the previous promhttp.Handler().ServeHTTP()
metrics.WritePrometheus(w, false)
})
log.Infof("prometheus metrics ready at: %s", path)
}

// Address return the current network address used by the HTTP router
func (r *HTTProuter) Address() net.Addr {
return r.address
Expand Down
32 changes: 0 additions & 32 deletions metrics/metrics.go

This file was deleted.

2 changes: 0 additions & 2 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"go.vocdoni.io/dvote/data"
"go.vocdoni.io/dvote/data/downloader"
"go.vocdoni.io/dvote/httprouter"
"go.vocdoni.io/dvote/metrics"
"go.vocdoni.io/dvote/vochain"
"go.vocdoni.io/dvote/vochain/indexer"
"go.vocdoni.io/dvote/vochain/keykeeper"
Expand All @@ -20,7 +19,6 @@ type VocdoniService struct {
Config *config.VochainCfg
App *vochain.BaseApplication
Router *httprouter.HTTProuter
MetricsAgent *metrics.Agent
OffChainData *offchaindatahandler.OffChainDataHandler
DataDownloader *downloader.Downloader
CensusDB *censusdb.CensusDB
Expand Down
13 changes: 4 additions & 9 deletions service/vochain.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,6 @@ func (vs *VocdoniService) Vochain() error {
}
}

// Metrics agent (Prometheus)
if vs.MetricsAgent != nil {
vs.Config.TendermintMetrics = true
}

// Create the vochain node
vs.App = vochain.NewVochain(vs.Config, genesisBytes)

Expand Down Expand Up @@ -136,7 +131,7 @@ func (vs *VocdoniService) Start() error {

if !vs.Config.NoWaitSync {
log.Infof("waiting for vochain to synchronize")
var lastHeight int64
var lastHeight uint64
i := 0
timeSyncCounter := time.Now()
timeCounter := time.Now()
Expand Down Expand Up @@ -171,10 +166,10 @@ func (vs *VocdoniService) Start() error {

// VochainPrintInfo initializes the Vochain statistics recollection.
func VochainPrintInfo(interval time.Duration, vi *vochaininfo.VochainInfo) {
var a *[5]int32
var h int64
var a *[5]uint64
var h uint64
var p, v uint64
var m, vc, vxm int
var m, vc, vxm uint64
var b strings.Builder
for {
b.Reset()
Expand Down
Loading

0 comments on commit 775d9fd

Please sign in to comment.