Skip to content

Commit

Permalink
Integrate Prometheus metrics server in COSI driver
Browse files Browse the repository at this point in the history
- In main.go, allow graceful shutdown of the metrics server.
- Added `metricsAddress` flag to configure the Prometheus metrics endpoint.
- Integrated `metrics.StartMetricsServer` to expose metrics at the
  configured address.
- Ensured graceful shutdown of the metrics server during service
  termination.
- Updated the `run` function to include metrics server lifecycle
  management.
- Maintains flexibility for metrics configuration using constants from
  the `pkg/constants` package.

Issue: COSI-65
  • Loading branch information
anurag4DSB committed Dec 20, 2024
1 parent 9f1bfde commit 7f9314b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
22 changes: 19 additions & 3 deletions cmd/scality-cosi-driver/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ import (
"context"
"flag"
"fmt"
"time"

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

c "github.com/scality/cosi-driver/pkg/constants"
"github.com/scality/cosi-driver/pkg/grpcfactory"
"github.com/scality/cosi-driver/pkg/metrics"
)

const (
Expand All @@ -33,8 +36,9 @@ const (
)

var (
driverAddress = flag.String("driver-address", "unix:///var/lib/cosi/cosi.sock", "driver address for the socket")
driverPrefix = flag.String("driver-prefix", "", "prefix for COSI driver, e.g. <prefix>.scality.com")
driverAddress = flag.String("driver-address", "unix:///var/lib/cosi/cosi.sock", "driver address for the socket")
driverPrefix = flag.String("driver-prefix", "", "prefix for COSI driver, e.g. <prefix>.scality.com")
metricsAddress = flag.String("metrics-address", c.MetricsAddress, "The address to expose Prometheus metrics.")
)

func init() {
Expand All @@ -53,6 +57,11 @@ func init() {
}

func run(ctx context.Context) error {
metricsServer, err := metrics.StartMetricsServer(*metricsAddress)
if err != nil {
return fmt.Errorf("failed to start metrics server: %w", err)
}

driverName := *driverPrefix + "." + provisionerName

identityServer, bucketProvisioner, err := driver.CreateDriver(ctx, driverName)
Expand All @@ -65,5 +74,12 @@ func run(ctx context.Context) error {
return fmt.Errorf("failed to start the provisioner server: %w", err)
}

return server.Run(ctx)
err = server.Run(ctx)
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer shutdownCancel()
if shutdownErr := metricsServer.Shutdown(shutdownCtx); shutdownErr != nil {
klog.ErrorS(shutdownErr, "Failed to gracefully shutdown metrics server")
}

return err
}
1 change: 1 addition & 0 deletions cmd/scality-cosi-driver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ func main() {
// Call the run function (defined in cmd.go)
if err := run(ctx); err != nil {
klog.ErrorS(err, "Scality COSI driver encountered an error, shutting down")
os.Exit(1)
}
}
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ const (
// Service initialization constants
const (
MetricsPath = "/metrics"
MetricsAddress = ":8080"
)

0 comments on commit 7f9314b

Please sign in to comment.