initial commit
This commit is contained in:
54
internal/app/prometheus.go
Normal file
54
internal/app/prometheus.go
Normal file
@@ -0,0 +1,54 @@
|
||||
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()
|
||||
}
|
||||
Reference in New Issue
Block a user