f-29: stop: add a proper stop
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>
This commit is contained in:
GnomeZworc 2026-08-18 00:41:01 +02:00
commit cdbb5012b1
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
9 changed files with 333 additions and 29 deletions

View file

@ -1,6 +1,8 @@
package promserver
import (
"context"
"errors"
"log"
"net/http"
@ -8,13 +10,30 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// Start launches the Prometheus metrics HTTP server on the given address.
// The provided registry is used to expose metrics at /metrics.
func Start(address string, registry *prometheus.Registry) {
// 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,
}))
log.Printf("Prometheus server listening on %s", address)
log.Fatal(http.ListenAndServe(address, mux))
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)
}