All checks were successful
Release Pipeline / upload-assets (metadata@.service, systemd/metadata@.service) (push) Successful in 4s
Release Pipeline / upload-assets (run-dnsmasq-in-netns.sh, scripts/run-dnsmasq-in-netns.sh) (push) Successful in 4s
Release Pipeline / build (agent, amd64, linux) (push) Successful in 0s
Release Pipeline / build (metadata, amd64, linux) (push) Successful in 0s
Release Pipeline / checksums (push) Successful in 4s
Release Pipeline / set-release-target (push) Successful in 2s
Release Pipeline / upload-assets (agent.service, systemd/agent.service) (push) Successful in 4s
Release Pipeline / upload-assets (dnsmasq@.service, systemd/dnsmasq@.service) (push) Successful in 4s
Release Pipeline / release (push) Successful in 11s
Release Pipeline / publish (push) Successful in 0s
Release Pipeline / build (push) Successful in 1m30s
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
39 lines
1.1 KiB
Go
39 lines
1.1 KiB
Go
package promserver
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
)
|
|
|
|
// Server exposes the Prometheus metrics endpoint.
|
|
type Server struct {
|
|
srv *http.Server
|
|
}
|
|
|
|
// New builds the metrics server for the given address and registry.
|
|
func New(address string, registry *prometheus.Registry) *Server {
|
|
mux := http.NewServeMux()
|
|
mux.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{
|
|
EnableOpenMetrics: true,
|
|
}))
|
|
return &Server{srv: &http.Server{Addr: address, Handler: mux}}
|
|
}
|
|
|
|
// Start blocks until the server stops. A failure is logged, never fatal: an
|
|
// unavailable metrics endpoint must not bring the whole agent down.
|
|
func (s *Server) Start() {
|
|
log.Printf("Prometheus server listening on %s", s.srv.Addr)
|
|
if err := s.srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
log.Printf("Prometheus server stopped: %v", err)
|
|
}
|
|
}
|
|
|
|
// Shutdown stops the server, waiting for in-flight requests until ctx expires.
|
|
func (s *Server) Shutdown(ctx context.Context) error {
|
|
return s.srv.Shutdown(ctx)
|
|
}
|