Skip to content

Commit

Permalink
feat: Add Prometheus HTTP Metric Server for Enhanced Monitoring (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
ItayLevyOfficial authored Jul 24, 2023
1 parent ff2fc27 commit 479bfb8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
20 changes: 16 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ type NodeConfig struct {
// parameters below are dymint specific and read from config
Aggregator bool `mapstructure:"aggregator"`
BlockManagerConfig `mapstructure:",squash"`
DALayer string `mapstructure:"da_layer"`
DAConfig string `mapstructure:"da_config"`
SettlementLayer string `mapstructure:"settlement_layer"`
SettlementConfig settlement.Config `mapstructure:",squash"`
DALayer string `mapstructure:"da_layer"`
DAConfig string `mapstructure:"da_config"`
SettlementLayer string `mapstructure:"settlement_layer"`
SettlementConfig settlement.Config `mapstructure:",squash"`
Instrumentation *InstrumentationConfig `mapstructure:"instrumentation"`
}

// BlockManagerConfig consists of all parameters required by BlockManagerConfig
Expand Down Expand Up @@ -131,3 +132,14 @@ func (c NodeConfig) Validate() error {

return nil
}

// InstrumentationConfig defines the configuration for metrics reporting.
type InstrumentationConfig struct {
// When true, Prometheus metrics are served under /metrics on
// PrometheusListenAddr.
// Check out the documentation for the list of available metrics.
Prometheus bool `mapstructure:"prometheus"`

// Address to listen for Prometheus collector(s) connections.
PrometheusListenAddr string `mapstructure:"prometheus_listen_addr"`
}
4 changes: 4 additions & 0 deletions config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func DefaultConfig(home, chainId string) *NodeConfig {
BlockBatchMaxSizeBytes: 1500000},
DALayer: "mock",
SettlementLayer: "mock",
Instrumentation: &InstrumentationConfig{
Prometheus: false,
PrometheusListenAddr: ":2112",
},
}

if home == "" {
Expand Down
15 changes: 15 additions & 0 deletions config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,19 @@ gas_fees = "{{ .SettlementConfig.GasFees }}"
keyring_backend = "{{ .SettlementConfig.KeyringBackend }}"
keyring_home_dir = "{{ .SettlementConfig.KeyringHomeDir }}"
dym_account_name = "{{ .SettlementConfig.DymAccountName }}"
#######################################################
### Instrumentation Configuration Options ###
#######################################################
[instrumentation]
# When true, Prometheus metrics are served under /metrics on
# PrometheusListenAddr.
# Check out the documentation for the list of available metrics.
prometheus = {{ .Instrumentation.Prometheus }}
# Address to listen for Prometheus collector(s) connections
prometheus_listen_addr = "{{ .Instrumentation.PrometheusListenAddr }}"
`
27 changes: 27 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"sync"
"time"

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

"github.com/libp2p/go-libp2p/core/crypto"

Expand Down Expand Up @@ -263,6 +267,12 @@ func (n *Node) OnStart() error {
if err != nil {
return fmt.Errorf("error while starting settlement layer client: %w", err)
}
go func() {
if err := n.startPrometheusServer(); err != nil {
panic(err)
}
}()

n.baseLayersHealthStatus = BaseLayersHealthStatus{
settlementHealthy: true,
daHealthy: true,
Expand Down Expand Up @@ -400,5 +410,22 @@ func (n *Node) healthStatusHandler(err error) {
if err = n.pubsubServer.PublishWithEvents(n.ctx, healthStatusEvent, map[string][]string{events.EventNodeTypeKey: {events.EventHealthStatus}}); err != nil {
panic(err)
}

}
}

func (n *Node) startPrometheusServer() error {
if n.conf.Instrumentation != nil && n.conf.Instrumentation.Prometheus {
http.Handle("/metrics", promhttp.Handler())
srv := &http.Server{
Addr: n.conf.Instrumentation.PrometheusListenAddr,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
Handler: http.DefaultServeMux,
}
if err := srv.ListenAndServe(); err != nil {
return err
}
}
return nil
}

0 comments on commit 479bfb8

Please sign in to comment.