2019-10-29 17:51:17 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-02 11:29:47 +00:00
|
|
|
"errors"
|
2019-10-29 17:51:17 +00:00
|
|
|
"net/http"
|
|
|
|
|
2022-07-08 16:10:46 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
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
|
2022-07-08 16:10:46 +00:00
|
|
|
config config.BasicService
|
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
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Start runs http service with the exposed endpoint on the 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()
|
2022-09-02 11:29:47 +00:00
|
|
|
if err != nil && !errors.Is(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// ShutDown stops the service.
|
2019-10-29 17:51:17 +00:00
|
|
|
func (ms *Service) ShutDown() {
|
2022-07-26 14:19:30 +00:00
|
|
|
if !ms.config.Enabled {
|
|
|
|
return
|
|
|
|
}
|
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 {
|
2022-07-28 14:49:30 +00:00
|
|
|
ms.log.Error("can't shut service down", zap.Error(err))
|
2019-10-29 17:51:17 +00:00
|
|
|
}
|
|
|
|
}
|