2021-01-13 17:07:04 +00:00
|
|
|
package metrics
|
2020-10-02 09:56:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2020-12-26 13:11:49 +00:00
|
|
|
"net/http"
|
|
|
|
|
2020-10-02 09:56:48 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2020-12-26 13:11:49 +00:00
|
|
|
"gitlab.com/inetmock/inetmock/pkg/api"
|
|
|
|
"gitlab.com/inetmock/inetmock/pkg/config"
|
|
|
|
"gitlab.com/inetmock/inetmock/pkg/logging"
|
2020-10-02 09:56:48 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
name = "metrics_exporter"
|
|
|
|
)
|
|
|
|
|
|
|
|
type metricsExporter struct {
|
|
|
|
logger logging.Logger
|
|
|
|
server *http.Server
|
|
|
|
}
|
|
|
|
|
2020-12-26 13:11:49 +00:00
|
|
|
func (m *metricsExporter) Start(_ api.PluginContext, config config.HandlerConfig) (err error) {
|
2020-10-02 09:56:48 +00:00
|
|
|
exporterOptions := metricsExporterOptions{}
|
|
|
|
if err = config.Options.Unmarshal(&exporterOptions); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.logger = m.logger.With(
|
|
|
|
zap.String("handler_name", config.HandlerName),
|
|
|
|
zap.String("address", config.ListenAddr()),
|
|
|
|
)
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle(exporterOptions.Route, promhttp.Handler())
|
|
|
|
m.server = &http.Server{
|
|
|
|
Addr: config.ListenAddr(),
|
|
|
|
Handler: mux,
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := m.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
m.logger.Error(
|
|
|
|
"Error occurred while serving metrics",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metricsExporter) Shutdown(ctx context.Context) error {
|
|
|
|
return m.server.Shutdown(ctx)
|
|
|
|
}
|