From 0af4ba18fc8949128fd42e0ce83af0f269793a11 Mon Sep 17 00:00:00 2001 From: Anurag Mittal Date: Tue, 24 Dec 2024 12:13:46 +0100 Subject: [PATCH] updated s3 create bucket with prom observe --- pkg/clients/s3/s3_client.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/pkg/clients/s3/s3_client.go b/pkg/clients/s3/s3_client.go index abfb9db8..381cf440 100644 --- a/pkg/clients/s3/s3_client.go +++ b/pkg/clients/s3/s3_client.go @@ -13,6 +13,7 @@ import ( "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" + "github.com/prometheus/client_golang/prometheus" "github.com/scality/cosi-driver/pkg/metrics" "github.com/scality/cosi-driver/pkg/util" ) @@ -65,7 +66,13 @@ var InitS3Client = func(ctx context.Context, params util.StorageClientParameters } func (client *S3Client) CreateBucket(ctx context.Context, bucketName string, params util.StorageClientParameters) error { - start := time.Now() + metricStatus := "success" + + timer := prometheus.NewTimer(prometheus.ObserverFunc(func(duration float64) { + metrics.S3RequestDuration.WithLabelValues("CreateBucket", metricStatus).Observe(duration) + })) + defer timer.ObserveDuration() + input := &s3.CreateBucketInput{Bucket: &bucketName} if params.Region != util.DefaultRegion { input.CreateBucketConfiguration = &types.CreateBucketConfiguration{ @@ -74,15 +81,11 @@ func (client *S3Client) CreateBucket(ctx context.Context, bucketName string, par } _, err := client.S3Service.CreateBucket(ctx, input) - duration := time.Since(start).Seconds() - status := "success" if err != nil { - status = "error" + metricStatus = "error" } - metrics.S3RequestsTotal.WithLabelValues("CreateBucket", status).Inc() - metrics.S3RequestDuration.WithLabelValues("CreateBucket", status).Observe(duration) - + metrics.S3RequestsTotal.WithLabelValues("CreateBucket", metricStatus).Inc() return err }