2022-07-27 06:52:08 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2023-08-27 15:09:02 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/logs"
|
2022-07-27 06:52:08 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Service serves metrics.
|
|
|
|
type Service struct {
|
|
|
|
*http.Server
|
|
|
|
enabled bool
|
|
|
|
log *zap.Logger
|
|
|
|
serviceType string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config is a params to configure service.
|
|
|
|
type Config struct {
|
|
|
|
Address string
|
|
|
|
Enabled bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start runs http service with the exposed endpoint on the configured port.
|
|
|
|
func (ms *Service) Start() {
|
|
|
|
if ms.enabled {
|
2023-08-27 15:09:02 +00:00
|
|
|
ms.log.Info(logs.ServiceIsRunning, zap.String("endpoint", ms.Addr))
|
2022-07-27 06:52:08 +00:00
|
|
|
err := ms.ListenAndServe()
|
|
|
|
if err != nil && err != http.ErrServerClosed {
|
2023-08-27 15:09:02 +00:00
|
|
|
ms.log.Warn(logs.ServiceCouldntStartOnConfiguredPort)
|
2022-07-27 06:52:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-08-27 15:09:02 +00:00
|
|
|
ms.log.Info(logs.ServiceHasntStartedSinceItsDisabled)
|
2022-07-27 06:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShutDown stops the service.
|
|
|
|
func (ms *Service) ShutDown(ctx context.Context) {
|
2023-08-27 15:09:02 +00:00
|
|
|
ms.log.Info(logs.ShuttingDownService, zap.String("endpoint", ms.Addr))
|
2022-07-27 06:52:08 +00:00
|
|
|
err := ms.Shutdown(ctx)
|
|
|
|
if err != nil {
|
2023-12-27 10:33:09 +00:00
|
|
|
ms.log.Error(logs.CantGracefullyShutDownService, zap.Error(err))
|
|
|
|
if err = ms.Close(); err != nil {
|
|
|
|
ms.log.Panic(logs.CantShutDownService, zap.Error(err))
|
|
|
|
}
|
2022-07-27 06:52:08 +00:00
|
|
|
}
|
|
|
|
}
|