neoneo-go/pkg/network/metrics/metrics.go

40 lines
915 B
Go
Raw Normal View History

package metrics
import (
"context"
"net/http"
"github.com/nspcc-dev/neo-go/pkg/config"
2019-12-30 07:43:05 +00:00
"go.uber.org/zap"
)
// Service serves metrics.
type Service struct {
*http.Server
config config.BasicService
2019-12-30 07:43:05 +00:00
log *zap.Logger
serviceType string
}
// Start runs http service with the exposed endpoint on the configured port.
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))
err := ms.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
2019-12-30 07:43:05 +00:00
ms.log.Warn("service couldn't start on configured port")
}
} else {
2019-12-30 07:43:05 +00:00
ms.log.Info("service hasn't started since it's disabled")
}
}
// ShutDown stops the service.
func (ms *Service) ShutDown() {
2019-12-30 07:43:05 +00:00
ms.log.Info("shutting down service", zap.String("endpoint", ms.Addr))
err := ms.Shutdown(context.Background())
if err != nil {
2019-12-30 07:43:05 +00:00
ms.log.Panic("can't shut down service")
}
}