2019-10-29 17:51:17 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2019-12-04 06:48:32 +00:00
|
|
|
// Service serves metrics.
|
2019-10-29 17:51:17 +00:00
|
|
|
type Service struct {
|
|
|
|
*http.Server
|
2019-12-04 06:48:32 +00:00
|
|
|
config Config
|
|
|
|
serviceType string
|
2019-10-29 17:51:17 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 06:48:32 +00:00
|
|
|
// Config config used for monitoring.
|
|
|
|
type Config struct {
|
2019-10-29 17:51:17 +00:00
|
|
|
Enabled bool `yaml:"Enabled"`
|
2019-11-05 12:22:07 +00:00
|
|
|
Address string `yaml:"Address"`
|
2019-10-29 17:51:17 +00:00
|
|
|
Port string `yaml:"Port"`
|
|
|
|
}
|
|
|
|
|
2019-12-04 06:48:32 +00:00
|
|
|
// Start runs http service with exposed endpoint on configured port.
|
2019-10-29 17:51:17 +00:00
|
|
|
func (ms *Service) Start() {
|
|
|
|
if ms.config.Enabled {
|
2019-12-04 06:48:32 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"endpoint": ms.Addr,
|
|
|
|
"service": ms.serviceType,
|
|
|
|
}).Info("service running")
|
2019-10-29 17:51:17 +00:00
|
|
|
err := ms.ListenAndServe()
|
2019-12-04 06:48:32 +00:00
|
|
|
if err != nil && err != http.ErrServerClosed {
|
|
|
|
log.Warnf("%s service couldn't start on configured port", ms.serviceType)
|
2019-10-29 17:51:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-12-04 06:48:32 +00:00
|
|
|
log.Infof("%s service hasn't started since it's disabled", ms.serviceType)
|
2019-10-29 17:51:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShutDown stops service.
|
|
|
|
func (ms *Service) ShutDown() {
|
|
|
|
log.WithFields(log.Fields{
|
2019-12-04 06:48:32 +00:00
|
|
|
"endpoint": ms.Addr,
|
|
|
|
"service": ms.serviceType,
|
|
|
|
}).Info("shutting down service")
|
2019-10-29 17:51:17 +00:00
|
|
|
err := ms.Shutdown(context.Background())
|
|
|
|
if err != nil {
|
2019-12-04 06:48:32 +00:00
|
|
|
log.Fatalf("can't shut down %s service", ms.serviceType)
|
2019-10-29 17:51:17 +00:00
|
|
|
}
|
|
|
|
}
|