[prometheus] make config variables private

Signed-off-by: Leo Chen <leo@harmony.one>
pull/3466/head
Leo Chen 4 years ago
parent 1c739d7581
commit 1ff326bc48
  1. 77
      api/service/prometheus/service.go

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

Loading…
Cancel
Save