neo-go/pkg/network/metrics/metrics.go
Vsevolod Brekelov 11ce73af28 server: add log-path and address configuration
- LogPath can be configured through config
- node,rpc and monitoring address can be configured thought command line
or config
2019-11-06 15:58:54 +03:00

60 lines
1.5 KiB
Go

package metrics
import (
"context"
"net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
// Service serves metrics provided by prometheus.
type Service struct {
*http.Server
config PrometheusConfig
}
// PrometheusConfig config for Prometheus used for monitoring.
// Additional information about Prometheus could be found here: https://prometheus.io/docs/guides/go-application.
type PrometheusConfig struct {
Enabled bool `yaml:"Enabled"`
Address string `yaml:"Address"`
Port string `yaml:"Port"`
}
// NewMetricsService created new service for gathering metrics.
func NewMetricsService(cfg PrometheusConfig) *Service {
return &Service{
&http.Server{
Addr: cfg.Address + ":" + cfg.Port,
Handler: promhttp.Handler(),
}, cfg,
}
}
// Start runs http service with exposed `/metrics` endpoint on configured port.
func (ms *Service) Start() {
if ms.config.Enabled {
err := ms.ListenAndServe()
if err != nil {
log.WithFields(log.Fields{
"endpoint": ms.Addr,
}).Info("metrics service up and running")
} else {
log.Warn("metrics service couldn't start on configured port")
}
} else {
log.Infof("metrics service hasn't started since it's disabled")
}
}
// ShutDown stops service.
func (ms *Service) ShutDown() {
log.WithFields(log.Fields{
"endpoint": ms.config.Port,
}).Info("shutting down monitoring-service")
err := ms.Shutdown(context.Background())
if err != nil {
log.Fatal("can't shut down monitoring service")
}
}