[prometheus] move config to prometheus package

Signed-off-by: Leo Chen <leo@harmony.one>
pull/3466/head
Leo Chen 4 years ago
parent c5bae75f87
commit 1b15d5a9d7
  1. 9
      api/service/prometheus/pushgateway.go
  2. 55
      api/service/prometheus/service.go
  3. 2
      cmd/harmony/flags.go
  4. 22
      cmd/harmony/main.go
  5. 4
      consensus/metrics.go
  6. 42
      internal/configs/node/config.go
  7. 2
      node/api.go
  8. 4
      node/metrics.go

@ -1,6 +1,7 @@
package utils
package prometheus
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/push"
"sync"
@ -14,14 +15,14 @@ var (
)
// Pusher returns the pusher, initialized once only
func PromPusher(job string, instance string) *push.Pusher {
func PromPusher(config PrometheusConfig) *push.Pusher {
onceForPusher.Do(func() {
if registry == nil {
registry = prometheus.NewRegistry()
}
pusher = push.New("https://gateway.harmony.one", job).
pusher = push.New(config.Gateway, fmt.Sprintf("%s/%d", config.Network, config.Shard)).
Gatherer(registry).
Grouping("instance", instance)
Grouping("instance", config.Instance)
})
return pusher
}

@ -10,11 +10,21 @@ import (
"runtime/pprof"
"time"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
"github.com/harmony-one/harmony/internal/utils"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// PrometheusConfig is the config for the prometheus service
type PrometheusConfig struct {
Enabled bool
IP string
Port int
Gateway string // address of the pushgateway
Network string // network type, used as job prefix
Shard uint32 // shard id, used as job suffix
Instance string //identifier of the instance in prometheus metrics
}
// Service provides Prometheus metrics via the /metrics route. This route will
// show all the metrics registered with the Prometheus DefaultRegisterer.
type Service struct {
@ -29,13 +39,14 @@ type Handler struct {
}
var (
svc = &Service{}
svc = &Service{}
config = PrometheusConfig{}
)
// NewService sets up a new instance for a given address host:port.
// An empty host will match with any IP so an address like ":19000" is perfectly acceptable.
func NewService(config nodeconfig.PrometheusServerConfig, additionalHandlers ...Handler) {
if !config.HTTPEnabled {
func NewService(additionalHandlers ...Handler) {
if !config.Enabled {
utils.Logger().Info().Msg("Prometheus http server disabled...")
return
}
@ -49,26 +60,26 @@ func NewService(config nodeconfig.PrometheusServerConfig, additionalHandlers ...
mux.HandleFunc(h.Path, h.Handler)
}
utils.Logger().Debug().Int("port", config.HTTPPort).
Str("ip", config.HTTPIp).
utils.Logger().Debug().Int("port", config.Port).
Str("ip", config.IP).
Msg("Starting Prometheus server")
endpoint := fmt.Sprintf("%s:%d", config.HTTPIp, config.HTTPPort)
endpoint := fmt.Sprintf("%s:%d", config.IP, config.Port)
svc.server = &http.Server{Addr: endpoint, Handler: mux}
// start pusher to push metrics to prometheus pushgateway
// every minute
go func(job string, instance string) {
go func(config PrometheusConfig) {
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if err := utils.PromPusher(job, instance).Add(); err != nil {
if err := PromPusher(config).Add(); err != nil {
utils.Logger().Warn().Err(err).Msg("Pushgateway Error")
}
}
}
}(fmt.Sprintf("%s/%d", config.Network, config.Shard), config.Instance)
}(config)
svc.Start()
}
@ -113,3 +124,27 @@ func (s *Service) Status() error {
}
return nil
}
// SetConfig initialize the prometheus config
func SetConfig(
enabled bool,
ip string,
port int,
gateway string,
network string,
shard uint32,
instance string,
) {
config.Enabled = enabled
config.IP = ip
config.Port = port
config.Gateway = gateway
config.Network = network
config.Shard = shard
config.Instance = instance
}
// GetConfig return the prometheus config
func GetConfig() PrometheusConfig {
return config
}

@ -170,6 +170,7 @@ var (
prometheusEnabledFlag,
prometheusIPFlag,
prometheusPortFlag,
prometheusGatewayFlag,
}
)
@ -257,6 +258,7 @@ func getRootFlags() []cli.Flag {
flags = append(flags, devnetFlags...)
flags = append(flags, revertFlags...)
flags = append(flags, legacyMiscFlags...)
flags = append(flags, prometheusFlags...)
return flags
}

@ -19,6 +19,7 @@ import (
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/api/service/prometheus"
"github.com/harmony-one/harmony/api/service/syncing"
"github.com/harmony-one/harmony/common/fdlimit"
"github.com/harmony-one/harmony/common/ntp"
@ -330,17 +331,6 @@ func setupNodeAndRun(hc harmonyConfig) {
HTTPPort: hc.HTTP.RosettaPort,
}
// Pares Prometheus config
nodeConfig.PrometheusServer = nodeconfig.PrometheusServerConfig{
HTTPEnabled: hc.Prometheus.Enabled,
HTTPIp: hc.Prometheus.IP,
HTTPPort: hc.Prometheus.Port,
Gateway: hc.Prometheus.Gateway,
Network: hc.Network.NetworkType,
Shard: nodeConfig.ShardID,
Instance: nodeconfig.GetPeerID().String(),
}
if hc.Revert != nil && hc.Revert.RevertBefore != 0 && hc.Revert.RevertTo != 0 {
chain := currentNode.Blockchain()
if hc.Revert.RevertBeacon {
@ -383,6 +373,16 @@ func setupNodeAndRun(hc harmonyConfig) {
nodeconfig.SetPeerID(myHost.GetID())
prometheus.SetConfig(
hc.Prometheus.Enabled,
hc.Prometheus.IP,
hc.Prometheus.Port,
hc.Prometheus.Gateway,
hc.Network.NetworkType,
nodeConfig.ShardID,
myHost.GetID().Pretty(),
)
currentNode.SupportSyncing()
currentNode.ServiceManagerSetup()
currentNode.RunServices()

@ -1,7 +1,7 @@
package consensus
import (
"github.com/harmony-one/harmony/internal/utils"
prom "github.com/harmony-one/harmony/api/service/prometheus"
"github.com/prometheus/client_golang/prometheus"
)
@ -88,7 +88,7 @@ func (consensus *Consensus) UpdateLeaderMetrics(numCommits float64, blockNum flo
}
func initMetrics() {
utils.PromRegistry().MustRegister(
prom.PromRegistry().MustRegister(
consensusCounterVec,
consensusVCCounterVec,
consensusSyncCounterVec,

@ -68,22 +68,21 @@ var peerID peer.ID // PeerID of the node
// ConfigType is the structure of all node related configuration variables
type ConfigType struct {
// The three groupID design, please refer to https://github.com/harmony-one/harmony/blob/master/node/node.md#libp2p-integration
beacon GroupID // the beacon group ID
group GroupID // the group ID of the shard (note: for beacon chain node, the beacon and shard group are the same)
client GroupID // the client group ID of the shard
isClient bool // whether this node is a client node, such as wallet
ShardID uint32 // ShardID of this node; TODO ek – revisit when resharding
role Role // Role of the node
Port string // Port of the node.
IP string // IP of the node.
RPCServer RPCServerConfig // RPC server port and ip
RosettaServer RosettaServerConfig // rosetta server port and ip
PrometheusServer PrometheusServerConfig // prometheus server port and ip
IsOffline bool
NtpServer string
StringRole string
P2PPriKey p2p_crypto.PrivKey
ConsensusPriKey multibls.PrivateKeys
beacon GroupID // the beacon group ID
group GroupID // the group ID of the shard (note: for beacon chain node, the beacon and shard group are the same)
client GroupID // the client group ID of the shard
isClient bool // whether this node is a client node, such as wallet
ShardID uint32 // ShardID of this node; TODO ek – revisit when resharding
role Role // Role of the node
Port string // Port of the node.
IP string // IP of the node.
RPCServer RPCServerConfig // RPC server port and ip
RosettaServer RosettaServerConfig // rosetta server port and ip
IsOffline bool
NtpServer string
StringRole string
P2PPriKey p2p_crypto.PrivKey
ConsensusPriKey multibls.PrivateKeys
// Database directory
DBDir string
networkType NetworkType
@ -115,17 +114,6 @@ type RosettaServerConfig struct {
HTTPPort int
}
// PrometheusServerConfig is the config for the prometheus server
type PrometheusServerConfig struct {
HTTPEnabled bool
HTTPIp string
HTTPPort int
Gateway string // address of the pushgateway
Network string // network type, used as job prefix
Shard uint32 // shard id, used as job suffix
Instance string //identifier of the instance in prometheus metrics
}
// configs is a list of node configuration.
// It has at least one configuration.
// The first one is the default, global node configuration

@ -84,7 +84,7 @@ func (node *Node) StopRPC() error {
// StartPrometheus start promtheus metrics service
func (node *Node) StartPrometheus() error {
prometheus.NewService(node.NodeConfig.PrometheusServer)
prometheus.NewService()
return nil
}

@ -1,7 +1,7 @@
package node
import (
"github.com/harmony-one/harmony/internal/utils"
prom "github.com/harmony-one/harmony/api/service/prometheus"
"github.com/prometheus/client_golang/prometheus"
)
@ -57,7 +57,7 @@ var (
)
func initMetrics() {
utils.PromRegistry().MustRegister(
prom.PromRegistry().MustRegister(
nodeStringCounterVec,
nodeP2PMessageCounterVec,
nodeConsensusMessageCounterVec,

Loading…
Cancel
Save