Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added metrics of libp2p with supporting prometheus #588

Merged
merged 4 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/example_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
# Default is false.
## enable_mdns = false

# `enable_metrics` provides network metrics for prometheus.
# Metrics provides information about network usage that you can show them in prometheus.
amirvalhalla marked this conversation as resolved.
Show resolved Hide resolved
# Default is false.
## enable_metrics = false

# `network.bootstrap` contains configuration for bootstrapping the node.
[network.bootstrap]

Expand Down
7 changes: 7 additions & 0 deletions docs/metrics/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
amirvalhalla marked this conversation as resolved.
Show resolved Hide resolved
prometheus:
image: prom/prometheus:latest
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
31 changes: 31 additions & 0 deletions docs/metrics/metrics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Metrics
amirvalhalla marked this conversation as resolved.
Show resolved Hide resolved

Before starting to get metrics of the Pactus software, there are a few important steps that need to be followed.
Please follow the instructions below:

1. after you `Init` the Pactus in your working directory you need to update `config.toml` file before starting the node.

by default metrics in the Pactus is not enable!

1.1. open config.toml file which its in your working directory.
1.2. go to network section.
1.3.find enable_metrics = false
1.4. change it to enable_metrics = true
1.5. done!, now you can save the file.

2. start the Pactus node.


3. now metrics are available, for example if you want to get metrics in the Pactus testnet you can find it at below url

```
http://localhost:8080/metrics/prometheus
```

4. if you want to use metrics in monitoring tools like `Prometheus` you can use the `docker-compose` file.

just be careful about the addresses and ports that they're existing in `docker-compose` and `prometheus` yml.

if you run the Pactus node on docker container you should change targets of `pactus-testnet` job in `prometheus.yml`


13 changes: 13 additions & 0 deletions docs/metrics/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
global:
amirvalhalla marked this conversation as resolved.
Show resolved Hide resolved
scrape_interval: 1m

scrape_configs:
- job_name: "prometheus"
scrape_interval: 1m
static_configs:
- targets: [ "127.0.0.1:9090" ]

- job_name: "pactus-metrics"
metrics_path: /metrics/prometheus
static_configs:
- targets: [ "127.0.0.1:8080" ]
30 changes: 16 additions & 14 deletions network/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
)

type Config struct {
Name string `toml:"name"`
Listens []string `toml:"listens"`
NetworkKey string `toml:"network_key"`
EnableNAT bool `toml:"enable_nat"`
EnableRelay bool `toml:"enable_relay"`
RelayAddrs []string `toml:"relay_addresses"`
EnableMdns bool `toml:"enable_mdns"`
Bootstrap *BootstrapConfig `toml:"bootstrap"`
Name string `toml:"name"`
Listens []string `toml:"listens"`
NetworkKey string `toml:"network_key"`
EnableNAT bool `toml:"enable_nat"`
EnableRelay bool `toml:"enable_relay"`
RelayAddrs []string `toml:"relay_addresses"`
EnableMdns bool `toml:"enable_mdns"`
EnableMetrics bool `toml:"enable_metrics"`
Bootstrap *BootstrapConfig `toml:"bootstrap"`
}

// BootstrapConfig holds all configuration options related to bootstrap nodes.
Expand Down Expand Up @@ -47,12 +48,13 @@ func DefaultConfig() *Config {
}

return &Config{
Name: "pactus",
Listens: []string{"/ip4/0.0.0.0/tcp/21777", "/ip6/::/tcp/21777"},
NetworkKey: "network_key",
EnableNAT: true,
EnableRelay: false,
EnableMdns: false,
Name: "pactus",
Listens: []string{"/ip4/0.0.0.0/tcp/21777", "/ip6/::/tcp/21777"},
NetworkKey: "network_key",
EnableNAT: true,
EnableRelay: false,
EnableMdns: false,
EnableMetrics: false,
Bootstrap: &BootstrapConfig{
Addresses: addresses,
MinThreshold: 8,
Expand Down
27 changes: 27 additions & 0 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
lp2pcrypto "github.com/libp2p/go-libp2p/core/crypto"
lp2phost "github.com/libp2p/go-libp2p/core/host"
lp2ppeer "github.com/libp2p/go-libp2p/core/peer"
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
rcmgrObs "github.com/libp2p/go-libp2p/p2p/host/resource-manager/obs"
ma "github.com/multiformats/go-multiaddr"
"github.com/pactus-project/pactus/util"
"github.com/pactus-project/pactus/util/errors"
"github.com/pactus-project/pactus/util/logger"
"github.com/pactus-project/pactus/version"
"github.com/prometheus/client_golang/prometheus"
)

var _ Network = &network{}
Expand Down Expand Up @@ -81,12 +84,36 @@
return nil, errors.Errorf(errors.ErrNetwork, err.Error())
}

//libp2p prometheus metrics
amirvalhalla marked this conversation as resolved.
Show resolved Hide resolved
if conf.EnableMetrics {
rcmgrObs.MustRegisterWith(prometheus.DefaultRegisterer)

Check warning on line 89 in network/network.go

View check run for this annotation

Codecov / codecov/patch

network/network.go#L89

Added line #L89 was not covered by tests
}

str, err := rcmgrObs.NewStatsTraceReporter()
if err != nil {
return nil, errors.Errorf(errors.ErrNetwork, err.Error())

Check warning on line 94 in network/network.go

View check run for this annotation

Codecov / codecov/patch

network/network.go#L94

Added line #L94 was not covered by tests
}

rmgr, err := rcmgr.NewResourceManager(
rcmgr.NewFixedLimiter(rcmgr.DefaultLimits.AutoScale()),
rcmgr.WithTraceReporter(str),
)

if err != nil {
return nil, errors.Errorf(errors.ErrNetwork, err.Error())

Check warning on line 103 in network/network.go

View check run for this annotation

Codecov / codecov/patch

network/network.go#L103

Added line #L103 was not covered by tests
}

opts = append(opts,
lp2p.Identity(networkKey),
lp2p.ListenAddrStrings(conf.Listens...),
lp2p.UserAgent(version.Agent()),
lp2p.ResourceManager(rmgr),
)

if !conf.EnableMetrics {
opts = append(opts, lp2p.DisableMetrics())
}

if conf.EnableNAT {
opts = append(opts,
lp2p.EnableNATService(),
Expand Down
3 changes: 3 additions & 0 deletions www/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"time"

"github.com/prometheus/client_golang/prometheus/promhttp"

"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/pactus-project/pactus/util"
Expand Down Expand Up @@ -72,6 +74,7 @@ func (s *Server) StartServer(grpcServer string) error {
s.router.HandleFunc("/account/number/{number}", s.GetAccountByNumberHandler)
s.router.HandleFunc("/validator/address/{address}", s.GetValidatorHandler)
s.router.HandleFunc("/validator/number/{number}", s.GetValidatorByNumberHandler)
http.Handle("/metrics/prometheus", promhttp.Handler())
amirvalhalla marked this conversation as resolved.
Show resolved Hide resolved
http.Handle("/", handlers.RecoveryHandler()(s.router))

l, err := net.Listen("tcp", s.config.Listen)
Expand Down
Loading