2019-10-29 17:51:17 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2019-12-30 07:43:05 +00:00
|
|
|
"go.uber.org/zap"
|
2019-10-29 17:51:17 +00:00
|
|
|
)
|
|
|
|
|
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
|
2019-12-30 07:43:05 +00:00
|
|
|
log *zap.Logger
|
2019-12-04 06:48:32 +00:00
|
|
|
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-30 07:43:05 +00:00
|
|
|
ms.log.Info("service is running", zap.String("endpoint", ms.Addr))
|
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 {
|
2019-12-30 07:43:05 +00:00
|
|
|
ms.log.Warn("service couldn't start on configured port")
|
2019-10-29 17:51:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-12-30 07:43:05 +00:00
|
|
|
ms.log.Info("service hasn't started since it's disabled")
|
2019-10-29 17:51:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShutDown stops service.
|
|
|
|
func (ms *Service) ShutDown() {
|
2019-12-30 07:43:05 +00:00
|
|
|
ms.log.Info("shutting down service", zap.String("endpoint", ms.Addr))
|
2019-10-29 17:51:17 +00:00
|
|
|
err := ms.Shutdown(context.Background())
|
|
|
|
if err != nil {
|
2019-12-30 07:43:05 +00:00
|
|
|
ms.log.Panic("can't shut down service")
|
2019-10-29 17:51:17 +00:00
|
|
|
}
|
|
|
|
}
|