Skip to content

Commit

Permalink
[fixup post review 1.2] share regiustry metrrics
Browse files Browse the repository at this point in the history
  • Loading branch information
anurag4DSB committed Dec 20, 2024
1 parent d0934b5 commit 9b5f19f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
12 changes: 10 additions & 2 deletions cmd/scality-cosi-driver/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/scality/cosi-driver/pkg/driver"
"k8s.io/klog/v2"

Expand Down Expand Up @@ -57,7 +58,14 @@ func init() {
}

func run(ctx context.Context) error {
metricsServer, err := metrics.StartMetricsServer(*metricsAddress)
registry := prometheus.NewRegistry()

if err := registry.Register(metrics.RequestsTotal); err != nil {
return fmt.Errorf("failed to register custom metrics: %w", err)
}

// Start the Prometheus metrics server with the shared registry
metricsServer, err := metrics.StartMetricsServerWithRegistry(*metricsAddress, registry)
if err != nil {
return fmt.Errorf("failed to start metrics server: %w", err)
}
Expand All @@ -74,7 +82,7 @@ func run(ctx context.Context) error {
return fmt.Errorf("failed to start the provisioner server: %w", err)
}

err = server.Run(ctx)
err = server.Run(ctx, registry)
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer shutdownCancel()
if shutdownErr := metricsServer.Shutdown(shutdownCtx); shutdownErr != nil {
Expand Down
6 changes: 4 additions & 2 deletions pkg/grpcfactory/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ type COSIProvisionerServer struct {
listenOpts []grpc.ServerOption
}

func (s *COSIProvisionerServer) Run(ctx context.Context) error {
func (s *COSIProvisionerServer) Run(ctx context.Context, registry prometheus.Registerer) error {
srvMetrics := grpcprom.NewServerMetrics()
prometheus.MustRegister(srvMetrics)
if err := registry.Register(srvMetrics); err != nil {
return fmt.Errorf("failed to register gRPC metrics: %w", err)
}

addr, err := url.Parse(s.address)
if err != nil {
Expand Down
17 changes: 9 additions & 8 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,22 @@ var (
)
)

func init() {
prometheus.MustRegister(RequestsTotal)
}

func StartMetricsServer(addr string) (*http.Server, error) {
// StartMetricsServerWithRegistry starts an HTTP metrics server with a custom Prometheus registry.
func StartMetricsServerWithRegistry(addr string, registry prometheus.Gatherer) (*http.Server, error) {
listener, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
return StartMetricsServerWithListener(listener)
return StartMetricsServerWithListenerAndRegistry(listener, registry)
}

func StartMetricsServerWithListener(listener net.Listener) (*http.Server, error) {
// StartMetricsServerWithListenerAndRegistry starts an HTTP server with a custom registry and listener.
func StartMetricsServerWithListenerAndRegistry(listener net.Listener, registry prometheus.Gatherer) (*http.Server, error) {
mux := http.NewServeMux()
mux.Handle(c.MetricsPath, promhttp.Handler())

mux.Handle(c.MetricsPath, promhttp.HandlerFor(registry, promhttp.HandlerOpts{
EnableOpenMetrics: true,
}))

srv := &http.Server{
Handler: mux,
Expand Down

0 comments on commit 9b5f19f

Please sign in to comment.