From 2f132c202617b94ec0ce424ed65a6affd8e63281 Mon Sep 17 00:00:00 2001 From: amirvalhalla Date: Fri, 14 Jul 2023 00:20:04 +0330 Subject: [PATCH 1/4] feat: added metrics of libp2p with supporting prometheus --- config/example_config.toml | 5 +++++ network/config.go | 30 ++++++++++++++++-------------- network/network.go | 27 +++++++++++++++++++++++++++ www/http/server.go | 3 +++ 4 files changed, 51 insertions(+), 14 deletions(-) diff --git a/config/example_config.toml b/config/example_config.toml index 0431ab0ad..3829312db 100644 --- a/config/example_config.toml +++ b/config/example_config.toml @@ -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. + # Default is false. + ## enable_metrics = false + # `network.bootstrap` contains configuration for bootstrapping the node. [network.bootstrap] diff --git a/network/config.go b/network/config.go index ffd545cac..27e28ea98 100644 --- a/network/config.go +++ b/network/config.go @@ -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. @@ -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, diff --git a/network/network.go b/network/network.go index 31233e1f5..ff2a08905 100644 --- a/network/network.go +++ b/network/network.go @@ -11,11 +11,14 @@ import ( 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{} @@ -81,12 +84,36 @@ func newNetwork(conf *Config, opts []lp2p.Option) (*network, error) { return nil, errors.Errorf(errors.ErrNetwork, err.Error()) } + //libp2p prometheus metrics + if conf.EnableMetrics { + rcmgrObs.MustRegisterWith(prometheus.DefaultRegisterer) + } + + str, err := rcmgrObs.NewStatsTraceReporter() + if err != nil { + return nil, errors.Errorf(errors.ErrNetwork, err.Error()) + } + + rmgr, err := rcmgr.NewResourceManager( + rcmgr.NewFixedLimiter(rcmgr.DefaultLimits.AutoScale()), + rcmgr.WithTraceReporter(str), + ) + + if err != nil { + return nil, errors.Errorf(errors.ErrNetwork, err.Error()) + } + 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(), diff --git a/www/http/server.go b/www/http/server.go index 74f28d833..c44d76a3d 100644 --- a/www/http/server.go +++ b/www/http/server.go @@ -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" @@ -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()) http.Handle("/", handlers.RecoveryHandler()(s.router)) l, err := net.Listen("tcp", s.config.Listen) From 1967e2bc1555f1dba996136eec49a15bf9981b3a Mon Sep 17 00:00:00 2001 From: amirvalhalla Date: Fri, 14 Jul 2023 00:54:59 +0330 Subject: [PATCH 2/4] docs: add documentation of metrics --- docs/metrics/docker-compose.yml | 7 +++++++ docs/metrics/metrics.md | 31 +++++++++++++++++++++++++++++++ docs/metrics/prometheus.yml | 13 +++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 docs/metrics/docker-compose.yml create mode 100644 docs/metrics/metrics.md create mode 100644 docs/metrics/prometheus.yml diff --git a/docs/metrics/docker-compose.yml b/docs/metrics/docker-compose.yml new file mode 100644 index 000000000..bf04f3a19 --- /dev/null +++ b/docs/metrics/docker-compose.yml @@ -0,0 +1,7 @@ +services: + prometheus: + image: prom/prometheus:latest + ports: + - "9090:9090" + volumes: + - ./prometheus.yml:/etc/prometheus/prometheus.yml diff --git a/docs/metrics/metrics.md b/docs/metrics/metrics.md new file mode 100644 index 000000000..b171009d1 --- /dev/null +++ b/docs/metrics/metrics.md @@ -0,0 +1,31 @@ +# Metrics + +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` + + diff --git a/docs/metrics/prometheus.yml b/docs/metrics/prometheus.yml new file mode 100644 index 000000000..e31985e27 --- /dev/null +++ b/docs/metrics/prometheus.yml @@ -0,0 +1,13 @@ +global: + 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" ] From 51bfd5c76a83914d60481853fd7b87d246daff8d Mon Sep 17 00:00:00 2001 From: amirvalhalla Date: Fri, 14 Jul 2023 21:59:39 +0330 Subject: [PATCH 3/4] refactor: resolve request changes --- config/example_config.toml | 3 +-- docs/metrics/docker-compose.yml | 7 ----- docs/metrics/metrics.md | 46 +++++++++++++++++++++------------ docs/metrics/prometheus.yml | 13 ---------- network/network.go | 1 - 5 files changed, 30 insertions(+), 40 deletions(-) delete mode 100644 docs/metrics/docker-compose.yml delete mode 100644 docs/metrics/prometheus.yml diff --git a/config/example_config.toml b/config/example_config.toml index 3829312db..aa1f07b7f 100644 --- a/config/example_config.toml +++ b/config/example_config.toml @@ -49,8 +49,7 @@ # 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. + # `enable_metrics` if enabled, it provides network metrics for the Prometheus software. # Default is false. ## enable_metrics = false diff --git a/docs/metrics/docker-compose.yml b/docs/metrics/docker-compose.yml deleted file mode 100644 index bf04f3a19..000000000 --- a/docs/metrics/docker-compose.yml +++ /dev/null @@ -1,7 +0,0 @@ -services: - prometheus: - image: prom/prometheus:latest - ports: - - "9090:9090" - volumes: - - ./prometheus.yml:/etc/prometheus/prometheus.yml diff --git a/docs/metrics/metrics.md b/docs/metrics/metrics.md index b171009d1..0c23d544f 100644 --- a/docs/metrics/metrics.md +++ b/docs/metrics/metrics.md @@ -1,31 +1,43 @@ # Metrics -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: +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). -1. after you `Init` the Pactus in your working directory you need to update `config.toml` file before starting the node. +After these changes, restart the Pactus node; you should now be able to view the metrics. -by default metrics in the Pactus is not enable! +## Prometheus Configuration - 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. +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: -2. start the Pactus node. +1- Ensure [Docker](https://www.docker.com/) is installed on your system. +2- Pull the Prometheus Docker image: -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 +```text +docker pull prom/prometheus ``` -4. if you want to use metrics in monitoring tools like `Prometheus` you can use the `docker-compose` file. +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: -just be careful about the addresses and ports that they're existing in `docker-compose` and `prometheus` yml. +```yaml +global: + scrape_interval: 1m -if you run the Pactus node on docker container you should change targets of `pactus-testnet` job in `prometheus.yml` +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" ] +``` +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. diff --git a/docs/metrics/prometheus.yml b/docs/metrics/prometheus.yml deleted file mode 100644 index e31985e27..000000000 --- a/docs/metrics/prometheus.yml +++ /dev/null @@ -1,13 +0,0 @@ -global: - 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" ] diff --git a/network/network.go b/network/network.go index ff2a08905..02c207e8e 100644 --- a/network/network.go +++ b/network/network.go @@ -84,7 +84,6 @@ func newNetwork(conf *Config, opts []lp2p.Option) (*network, error) { return nil, errors.Errorf(errors.ErrNetwork, err.Error()) } - //libp2p prometheus metrics if conf.EnableMetrics { rcmgrObs.MustRegisterWith(prometheus.DefaultRegisterer) } From 8cd9a7f20a8a4224eca3efde06e16b3c0fd54a41 Mon Sep 17 00:00:00 2001 From: amirvalhalla Date: Sat, 15 Jul 2023 23:21:23 +0330 Subject: [PATCH 4/4] refactor: change grpc port plus add prometheus metrics to html --- config/config.go | 2 +- www/http/server.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index f709b7376..f485e9011 100644 --- a/config/config.go +++ b/config/config.go @@ -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 diff --git a/www/http/server.go b/www/http/server.go index c44d76a3d..04d73f569 100644 --- a/www/http/server.go +++ b/www/http/server.go @@ -133,6 +133,7 @@ func (s *Server) RootHandler(w http.ResponseWriter, _ *http.Request) { return } + buf.WriteString("/metrics/prometheus
") buf.WriteString("") s.writeHTML(w, buf.String()) }