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:
* drop metric vochain_vote_tree_increase_last_minute
* drop package `go.vocdoni.io/dvote/metrics` altogether
* unify types of vochaininfo to uint64
* drop vs.MetricsAgent field
  • Loading branch information
altergui committed Oct 31, 2023
1 parent 734dd3f commit ea020fd
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 244 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
12 changes: 9 additions & 3 deletions cmd/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"syscall"
"time"

"github.com/VictoriaMetrics/metrics"
"github.com/google/uuid"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
Expand All @@ -30,7 +31,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 +507,14 @@ 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 eventually be passed to CometBFT to report metrics as well
srv.Config.TendermintMetrics = true

path := "/metrics"
srv.Router.AddRawHTTPHandler(path, "GET", func(w http.ResponseWriter, req *http.Request) {
metrics.WritePrometheus(w, true)
})
log.Infof("prometheus metrics ready at: %s", path)
}
}

Expand Down
10 changes: 7 additions & 3 deletions cmd/voconed/voconed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/hex"
"fmt"
"net/http"
"os"
"os/signal"
"path/filepath"
Expand All @@ -11,6 +12,7 @@ import (
"syscall"
"time"

"github.com/VictoriaMetrics/metrics"
"github.com/ethereum/go-ethereum/common"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
Expand All @@ -19,7 +21,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 +233,11 @@ func main() {
log.Fatal(err)
}

vc.MetricsAgent = metrics.NewAgent("/metrics",
time.Duration(10)*time.Second, vc.Router)
path := "/metrics"
vc.Router.AddRawHTTPHandler(path, "GET", func(w http.ResponseWriter, req *http.Request) {
metrics.WritePrometheus(w, true)
})
log.Infof("prometheus metrics ready at: %s", path)

// 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 @@ -132,7 +132,6 @@ func (i *Handler) Init(d *types.DataStore) error {
}

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

return nil
}
Expand Down Expand Up @@ -211,7 +210,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
7 changes: 5 additions & 2 deletions 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 @@ -50,15 +51,13 @@ require (
github.com/multiformats/go-multicodec v0.9.0
github.com/multiformats/go-multihash v0.2.3
github.com/pressly/goose/v3 v3.10.0
github.com/prometheus/client_golang v1.16.0
github.com/rs/zerolog v1.30.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.16.0
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a
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 @@ -245,6 +244,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/polydawn/refmt v0.89.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
Expand All @@ -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
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
9 changes: 2 additions & 7 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 @@ -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 a *[5]uint64
var h int64
var p, v uint64
var m, vc, vxm int
var m, vc, vxm uint64
var b strings.Builder
for {
b.Reset()
Expand Down
17 changes: 6 additions & 11 deletions subpub/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@ import (
"context"
"time"

"github.com/VictoriaMetrics/metrics"
corediscovery "github.com/libp2p/go-libp2p/core/discovery"
libpeer "github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
discrouting "github.com/libp2p/go-libp2p/p2p/discovery/routing"
discutil "github.com/libp2p/go-libp2p/p2p/discovery/util"
multiaddr "github.com/multiformats/go-multiaddr"
"github.com/prometheus/client_golang/prometheus"
"go.vocdoni.io/dvote/log"
"go.vocdoni.io/dvote/metrics"
)

// Metrics exported via prometheus
var (
dhtLatency = prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: "file",
Name: "peers_dht_latency",
Help: "The time it takes FindPeers to discover peers",
})
// The time it takes FindPeers to discover peers
dhtLatency = metrics.NewHistogram("file_peers_dht_latency")
)

// setupDiscovery creates a DHT discovery service and attaches it to the libp2p Host.
Expand All @@ -39,8 +35,6 @@ func (s *SubPub) setupDiscovery(ctx context.Context) {
s.routing = discrouting.NewRoutingDiscovery(s.node.DHT)
discutil.Advertise(ctx, s.routing, s.Topic)

metrics.Register(dhtLatency)

// Discover new peers periodically
go func() { // this spawns a single background task per instance
for {
Expand All @@ -58,7 +52,8 @@ func (s *SubPub) setupDiscovery(ctx context.Context) {
}

func (s *SubPub) discover(ctx context.Context) {
dhtLatencyTimer := prometheus.NewTimer(dhtLatency)
startTime := time.Now()

// Now, look for others who have announced.
// This is like your friend telling you the location to meet you.
log.Debugf("looking for peers in topic %s", s.Topic)
Expand All @@ -83,7 +78,7 @@ func (s *SubPub) discover(ctx context.Context) {
}
// new peer; let's connect to it
// first update the latency metrics
dhtLatencyTimer.ObserveDuration()
dhtLatency.UpdateDuration(startTime)
connectCtx, cancel := context.WithTimeout(ctx, time.Second*10)
if err := s.node.PeerHost.Connect(connectCtx, peer); err != nil {
cancel()
Expand Down
Loading

0 comments on commit ea020fd

Please sign in to comment.