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 all 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
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func SaveTestnetConfig(path string, numValidators int) error {
"/ip4/94.101.184.118/tcp/4002/p2p/12D3KooWCRHn8vjrKNBEQcut8uVCYX5q77RKidPaE6iMK31qEVHb",
}
conf.GRPC.Enable = true
conf.GRPC.Listen = "[::]:9090"
conf.GRPC.Listen = "[::]:5105"
conf.GRPC.Gateway.Enable = true
conf.GRPC.Gateway.Listen = "[::]:80"
conf.HTTP.Enable = true
Expand Down
4 changes: 4 additions & 0 deletions config/example_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
# Default is false.
## enable_mdns = false

# `enable_metrics` if enabled, it provides network metrics for the Prometheus software.
# Default is false.
## enable_metrics = false

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

Expand Down
43 changes: 43 additions & 0 deletions docs/metrics/metrics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Metrics
amirvalhalla marked this conversation as resolved.
Show resolved Hide resolved

The Pactus blockchain offers [Prometheus](https://prometheus.io/) metrics for its network module, enabling users to monitor and analyze various network-related statistics. To activate this feature, inside the `config.toml`, set the `enable_metrics` parameter to true. Also, ensure that the HTTP module is enabled. You can enable HTTP module under the `[http]` section of the `config.toml` file. Once enabled, the metrics can be accessed at [http://localhost:8080/metrics/prometheus](http://localhost:8080/metrics/prometheus).

After these changes, restart the Pactus node; you should now be able to view the metrics.

## Prometheus Configuration

Prometheus is an open-source monitoring and alerting tool that facilitates the collection and processing of metrics. A common method of running Prometheus is via Docker containers. To use Prometheus with Docker, follow these steps:

1- Ensure [Docker](https://www.docker.com/) is installed on your system.

2- Pull the Prometheus Docker image:

```text
docker pull prom/prometheus
```

3- Create a configuration file named `prometheus.yml` to define the Prometheus configuration. You can refer to the Prometheus [documentation](https://prometheus.io/docs/prometheus/latest/configuration/configuration/) for more guidance. As an example, here's a simple configuration:

```yaml
global:
scrape_interval: 1m

scrape_configs:
- job_name: "prometheus"
scrape_interval: 1m
static_configs:
- targets: [ "127.0.0.1:9090" ]
amirvalhalla marked this conversation as resolved.
Show resolved Hide resolved

- job_name: "pactus-metrics"
metrics_path: /metrics/prometheus
static_configs:
- targets: [ "127.0.0.1:8080" ]
```
4- Start Prometheus as a Docker container:

```text
docker run -p 9090:9090 -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
```
Replace `/path/to/prometheus.yml` with the actual path to your configuration file.

5- Prometheus should now be up and running. Access the Prometheus web interface by visiting [http://localhost:9090/](http://localhost:9090/) in your web browser.
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
26 changes: 26 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,35 @@
return nil, errors.Errorf(errors.ErrNetwork, err.Error())
}

if conf.EnableMetrics {
rcmgrObs.MustRegisterWith(prometheus.DefaultRegisterer)

Check warning on line 88 in network/network.go

View check run for this annotation

Codecov / codecov/patch

network/network.go#L88

Added line #L88 was not covered by tests
}

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

Check warning on line 93 in network/network.go

View check run for this annotation

Codecov / codecov/patch

network/network.go#L93

Added line #L93 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 102 in network/network.go

View check run for this annotation

Codecov / codecov/patch

network/network.go#L102

Added line #L102 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
4 changes: 4 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 Expand Up @@ -130,6 +133,7 @@ func (s *Server) RootHandler(w http.ResponseWriter, _ *http.Request) {
return
}

buf.WriteString("<a href=\"/metrics/prometheus\">/metrics/prometheus</a></br>")
buf.WriteString("</body></html>")
s.writeHTML(w, buf.String())
}
Expand Down
Loading