Skip to content

Commit

Permalink
Add Prometheus metrics instrumentation for S3 ops
Browse files Browse the repository at this point in the history
- Instrumented `CreateBucket` and `DeleteBucket` methods to record
  Prometheus metrics.
- Added tracking for:
  - Total number of S3 requests (`S3RequestsTotal`), categorized by
    method and status.
  - Duration of S3 requests (`S3RequestDuration`), categorized similarly
- Ensures metrics capture both successful and failed S3 API calls.
- Improves observability and debugging for S3 bucket operations.

Issue: COSI-19
  • Loading branch information
anurag4DSB committed Dec 19, 2024
1 parent 3433918 commit b046d9e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
26 changes: 26 additions & 0 deletions pkg/clients/s3/s3_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import (
"net/http"
"os"
"strings"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/aws/smithy-go/logging"
c "github.com/scality/cosi-driver/pkg/constants"
"github.com/scality/cosi-driver/pkg/metrics"
"github.com/scality/cosi-driver/pkg/util"
)

Expand Down Expand Up @@ -65,6 +68,8 @@ var InitS3Client = func(params util.StorageClientParameters) (*S3Client, error)
}

func (client *S3Client) CreateBucket(ctx context.Context, bucketName string, params util.StorageClientParameters) error {
method := "CreateBucket"
start := time.Now()

input := &s3.CreateBucketInput{
Bucket: &bucketName,
Expand All @@ -77,12 +82,33 @@ func (client *S3Client) CreateBucket(ctx context.Context, bucketName string, par
}

_, err := client.S3Service.CreateBucket(ctx, input)

duration := time.Since(start).Seconds()
status := c.StatusSuccess
if err != nil {
status = c.StatusError
}

metrics.S3RequestsTotal.WithLabelValues(method, status).Inc()
metrics.S3RequestDuration.WithLabelValues(method, status).Observe(duration)
return err
}

func (client *S3Client) DeleteBucket(ctx context.Context, bucketName string) error {
method := "DeleteBucket"
start := time.Now()

_, err := client.S3Service.DeleteBucket(ctx, &s3.DeleteBucketInput{
Bucket: &bucketName,
})
duration := time.Since(start).Seconds()

status := c.StatusSuccess
if err != nil {
status = c.StatusError
}

metrics.S3RequestsTotal.WithLabelValues(method, status).Inc()
metrics.S3RequestDuration.WithLabelValues(method, status).Observe(duration)
return err
}
10 changes: 8 additions & 2 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ const (

// Service initialization constants
const (
MetricsPath = "/metrics"
MetricsAddress = ":8080"
MetricsPath = "/metrics" // Path to expose Prometheus metrics
MetricsAddress = ":8080" // Default address to bind the metrics server
)

// Prometheus metrics status values
const (
StatusSuccess = "success" // Status value for successful requests
StatusError = "error" // Status value for failed requests
)

0 comments on commit b046d9e

Please sign in to comment.