55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
)
|
|
|
|
type PrometheusServer struct {
|
|
port string
|
|
coordinator *Coordinator
|
|
server *http.Server
|
|
//registry *prometheus.Registry
|
|
}
|
|
|
|
func NewPrometheusServer(port string, coordinator *Coordinator) *PrometheusServer {
|
|
// Создаем собственный реестр метрик
|
|
// registry := prometheus.NewRegistry()
|
|
|
|
return &PrometheusServer{
|
|
port: port,
|
|
coordinator: coordinator,
|
|
//registry: registry,
|
|
}
|
|
}
|
|
|
|
func (s *PrometheusServer) Start(ctx context.Context) error {
|
|
// Регистрируем coordinator как коллектор
|
|
// s.registry.MustRegister(s.coordinator)
|
|
prometheus.MustRegister(s.coordinator)
|
|
|
|
mux := http.NewServeMux()
|
|
// Используем наш реестр вместо глобального
|
|
// mux.Handle("/metrics", promhttp.HandlerFor(s.registry, promhttp.HandlerOpts{}))
|
|
mux.Handle("/metrics", promhttp.Handler())
|
|
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write([]byte("OK"))
|
|
})
|
|
|
|
s.server = &http.Server{
|
|
Addr: ":" + s.port,
|
|
Handler: mux,
|
|
}
|
|
|
|
go func() {
|
|
<-ctx.Done()
|
|
s.server.Shutdown(context.Background())
|
|
}()
|
|
|
|
return s.server.ListenAndServe()
|
|
}
|