-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Prometheus metrics for S3 and IAM requests #71
Changes from 1 commit
4d96040
08e8c70
1990d8a
9f352dc
0ab5bd9
a162a1a
aa85ea0
3433918
b046d9e
ad6a043
8973af0
6701bdc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
) | ||
|
||
|
@@ -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, | ||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this metrics pattern seems to be repeated a lot, it may be worth having a wrapper around all request handlers that want metrics. I think it could be done using function closures, so the function signature is identical to the wrapper in all cases. But your call, just though it could help readability and maintenance and limit the risk of mistakes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could it be done with some kind of middleware around the aws sdk client: https://aws.github.io/aws-sdk-go-v2/docs/middleware/ ? |
||
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since those are global constants, it can be better to prefix them like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about have a specific small pkg |
||
StatusError = "error" // Status value for failed requests | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case it helps, the prometheus module provides a helper to observe durations that can make the code a tiny bit simpler: https://pkg.go.dev/github.com/prometheus/client_golang/prometheus#Timer