|
|
|
@ -17,22 +17,22 @@ import ( |
|
|
|
|
"github.com/prometheus/client_golang/prometheus/push" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// PrometheusConfig is the config for the prometheus service
|
|
|
|
|
type PrometheusConfig struct { |
|
|
|
|
Enabled bool |
|
|
|
|
IP string |
|
|
|
|
Port int |
|
|
|
|
EnablePush bool // enable pushgateway support
|
|
|
|
|
Gateway string // address of the pushgateway
|
|
|
|
|
Network string // network type, used as job prefix
|
|
|
|
|
Legacy bool // legacy or not, legacy is harmony internal node
|
|
|
|
|
Type string // node type, validator or exlorer node
|
|
|
|
|
Shard uint32 // shard id, used as job suffix
|
|
|
|
|
Instance string //identifier of the instance in prometheus metrics
|
|
|
|
|
// Config is the config for the prometheus service
|
|
|
|
|
type Config struct { |
|
|
|
|
enabled bool |
|
|
|
|
ip string |
|
|
|
|
port int |
|
|
|
|
enablePush bool // enable pushgateway support
|
|
|
|
|
gateway string // address of the pushgateway
|
|
|
|
|
network string // network type, used as job prefix
|
|
|
|
|
legacy bool // legacy or not, legacy is harmony internal node
|
|
|
|
|
nodetype string // node type, validator or exlorer node
|
|
|
|
|
shard uint32 // shard id, used as job suffix
|
|
|
|
|
instance string //identifier of the instance in prometheus metrics
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (p PrometheusConfig) String() string { |
|
|
|
|
return fmt.Sprintf("%v, %v:%v, %v/%v, %v/%v/%v/%v:%v", p.Enabled, p.IP, p.Port, p.EnablePush, p.Gateway, p.Network, p.Legacy, p.Type, p.Shard, p.Instance) |
|
|
|
|
func (p Config) String() string { |
|
|
|
|
return fmt.Sprintf("%v, %v:%v, %v/%v, %v/%v/%v/%v:%v", p.enabled, p.ip, p.port, p.enablePush, p.gateway, p.network, p.legacy, p.nodetype, p.shard, p.instance) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Service provides Prometheus metrics via the /metrics route. This route will
|
|
|
|
@ -53,17 +53,17 @@ type Handler struct { |
|
|
|
|
var ( |
|
|
|
|
registryOnce sync.Once |
|
|
|
|
svc = &Service{} |
|
|
|
|
config PrometheusConfig |
|
|
|
|
config Config |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func getJobName(config PrometheusConfig) string { |
|
|
|
|
func getJobName(config Config) string { |
|
|
|
|
var node string |
|
|
|
|
|
|
|
|
|
// legacy nodes are harmony nodes: s0,s1,s2,s3
|
|
|
|
|
if config.Legacy { |
|
|
|
|
if config.legacy { |
|
|
|
|
node = "s" |
|
|
|
|
} else { |
|
|
|
|
if config.Type == "validator" { |
|
|
|
|
if config.nodetype == "validator" { |
|
|
|
|
// regular validator nodes are: v0,v1,v2,v3
|
|
|
|
|
node = "v" |
|
|
|
|
} else { |
|
|
|
@ -72,13 +72,13 @@ func getJobName(config PrometheusConfig) string { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s/%s%d", config.Network, node, config.Shard) |
|
|
|
|
return fmt.Sprintf("%s/%s%d", config.network, node, config.shard) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 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(additionalHandlers ...Handler) { |
|
|
|
|
if !config.Enabled { |
|
|
|
|
if !config.enabled { |
|
|
|
|
utils.Logger().Info().Msg("Prometheus http server disabled...") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
@ -101,21 +101,21 @@ func NewService(additionalHandlers ...Handler) { |
|
|
|
|
mux.HandleFunc(h.Path, h.Handler) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
utils.Logger().Debug().Int("port", config.Port). |
|
|
|
|
Str("ip", config.IP). |
|
|
|
|
utils.Logger().Debug().Int("port", config.port). |
|
|
|
|
Str("ip", config.ip). |
|
|
|
|
Msg("Starting Prometheus server") |
|
|
|
|
endpoint := fmt.Sprintf("%s:%d", config.IP, config.Port) |
|
|
|
|
endpoint := fmt.Sprintf("%s:%d", config.ip, config.port) |
|
|
|
|
svc.server = &http.Server{Addr: endpoint, Handler: mux} |
|
|
|
|
if config.EnablePush { |
|
|
|
|
if config.enablePush { |
|
|
|
|
job := getJobName(config) |
|
|
|
|
utils.Logger().Info().Str("Job", job).Msg("Prometheus enabled pushgateway support ...") |
|
|
|
|
svc.pusher = push.New(config.Gateway, job). |
|
|
|
|
svc.pusher = push.New(config.gateway, job). |
|
|
|
|
Gatherer(svc.registry). |
|
|
|
|
Grouping("instance", config.Instance) |
|
|
|
|
Grouping("instance", config.instance) |
|
|
|
|
|
|
|
|
|
// start pusher to push metrics to prometheus pushgateway
|
|
|
|
|
// every minute
|
|
|
|
|
go func(config PrometheusConfig) { |
|
|
|
|
go func(config Config) { |
|
|
|
|
ticker := time.NewTicker(time.Minute) |
|
|
|
|
defer ticker.Stop() |
|
|
|
|
for { |
|
|
|
@ -186,23 +186,24 @@ func SetConfig( |
|
|
|
|
shard uint32, |
|
|
|
|
instance string, |
|
|
|
|
) { |
|
|
|
|
config.Enabled = enabled |
|
|
|
|
config.IP = ip |
|
|
|
|
config.Port = port |
|
|
|
|
config.EnablePush = enablepush |
|
|
|
|
config.Gateway = gateway |
|
|
|
|
config.Network = network |
|
|
|
|
config.Legacy = legacy |
|
|
|
|
config.Type = nodetype |
|
|
|
|
config.Shard = shard |
|
|
|
|
config.Instance = instance |
|
|
|
|
config.enabled = enabled |
|
|
|
|
config.ip = ip |
|
|
|
|
config.port = port |
|
|
|
|
config.enablePush = enablepush |
|
|
|
|
config.gateway = gateway |
|
|
|
|
config.network = network |
|
|
|
|
config.legacy = legacy |
|
|
|
|
config.nodetype = nodetype |
|
|
|
|
config.shard = shard |
|
|
|
|
config.instance = instance |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// GetConfig return the prometheus config
|
|
|
|
|
func GetConfig() PrometheusConfig { |
|
|
|
|
func GetConfig() Config { |
|
|
|
|
return config |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// PromRegistry return the registry of prometheus service
|
|
|
|
|
func PromRegistry() *prometheus.Registry { |
|
|
|
|
registryOnce.Do(func() { |
|
|
|
|
if svc.registry == nil { |
|
|
|
|