-
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
COSI-19, COSI-21: Enhance Scality COSI Driver with S3 and IAM Metrics, Logging, and IAM Client Improvements #85
Open
anurag4DSB
wants to merge
5
commits into
feature/COSI-65-instrument-cosi-drover-with-gprc-metrics
Choose a base branch
from
feature/COSI-19-add-s3-iam-metrics
base: feature/COSI-65-instrument-cosi-drover-with-gprc-metrics
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+495
−60
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e8dec1d
COSI-19: refactor; use shared ctx in init clients
anurag4DSB 51f0045
COSI-19: add S3 and IAM metrics
anurag4DSB 18c2f28
COSI-19: add e2e tests for s3 and iam metrics
anurag4DSB 8f30ff1
COSI-19: renamed inline policy method for clarity
anurag4DSB 8880371
COSI-21: Add docs for s3 and iam metrics
anurag4DSB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package metrics | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/smithy-go/middleware" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
var AttachPrometheusMiddleware = attachPrometheusMiddlewareMetrics | ||
|
||
// AttachPrometheusMiddleware attaches a Prometheus middleware for metrics tracking. | ||
func attachPrometheusMiddlewareMetrics(stack *middleware.Stack, requestDuration *prometheus.HistogramVec, requestsTotal *prometheus.CounterVec) error { | ||
middlewareFunc := middleware.FinalizeMiddlewareFunc("PrometheusMetrics", func( | ||
ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler, | ||
) (out middleware.FinalizeOutput, metadata middleware.Metadata, err error) { | ||
operationName := middleware.GetOperationName(ctx) | ||
|
||
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(duration float64) { | ||
status := "success" | ||
if err != nil { | ||
status = "error" | ||
} | ||
requestDuration.WithLabelValues(operationName, status).Observe(duration) | ||
requestsTotal.WithLabelValues(operationName, status).Inc() | ||
})) | ||
defer timer.ObserveDuration() | ||
|
||
out, metadata, err = next.HandleFinalize(ctx, in) | ||
if err != nil { | ||
klog.ErrorS(err, "AWS SDK operation failed", "operation", operationName) | ||
} | ||
return out, metadata, err | ||
}) | ||
|
||
// Add the middleware to the Finalize step | ||
return stack.Finalize.Add(middlewareFunc, middleware.After) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package metrics_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/aws/smithy-go/middleware" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/testutil" | ||
"github.com/scality/cosi-driver/pkg/metrics" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
// MockFinalizeMiddleware satisfies the FinalizeMiddleware interface | ||
type MockFinalizeMiddleware struct { | ||
HandleFunc func(ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler) (middleware.FinalizeOutput, middleware.Metadata, error) | ||
IDValue string | ||
} | ||
|
||
func (m MockFinalizeMiddleware) HandleFinalize(ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler) (middleware.FinalizeOutput, middleware.Metadata, error) { | ||
if next == nil { | ||
return middleware.FinalizeOutput{}, middleware.Metadata{}, errors.New("next handler is nil") | ||
} | ||
|
||
out, metadata, err := m.HandleFunc(ctx, in, next) | ||
return out, metadata, err | ||
} | ||
|
||
// ID returns the unique identifier for the middleware | ||
func (m MockFinalizeMiddleware) ID() string { | ||
return m.IDValue | ||
} | ||
|
||
// TerminalHandler represents the final handler in the middleware chain | ||
type TerminalHandler struct{} | ||
|
||
// HandleFinalize simulates the terminal handler in the chain | ||
func (t TerminalHandler) HandleFinalize(ctx context.Context, in middleware.FinalizeInput) (middleware.FinalizeOutput, middleware.Metadata, error) { | ||
return middleware.FinalizeOutput{}, middleware.Metadata{}, nil | ||
} | ||
|
||
var _ = Describe("AttachPrometheusMiddleware", func() { | ||
var ( | ||
stack *middleware.Stack | ||
requestDuration *prometheus.HistogramVec | ||
requestsTotal *prometheus.CounterVec | ||
) | ||
|
||
BeforeEach(func() { | ||
// Initialize Prometheus metric vectors | ||
requestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ | ||
Name: "request_duration_seconds", | ||
Help: "Duration of requests", | ||
}, []string{"operation", "status"}) | ||
|
||
requestsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
Name: "requests_total", | ||
Help: "Total number of requests", | ||
}, []string{"operation", "status"}) | ||
|
||
stack = middleware.NewStack("testStack", nil) | ||
|
||
}) | ||
|
||
It("should attach the middleware to the stack", func() { | ||
// Attach the Prometheus middleware | ||
err := metrics.AttachPrometheusMiddleware(stack, requestDuration, requestsTotal) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
// Verify middleware is in the stack | ||
Expect(stack.Finalize.List()).To(HaveLen(1)) | ||
Expect(stack.Finalize.List()[0]).To(Equal("PrometheusMetrics")) | ||
}) | ||
|
||
It("should safely execute the middleware chain", func(ctx SpecContext) { | ||
err := metrics.AttachPrometheusMiddleware(stack, requestDuration, requestsTotal) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
// Add mock middleware to simulate behavior | ||
mockMiddleware := MockFinalizeMiddleware{ | ||
HandleFunc: func(ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler) (middleware.FinalizeOutput, middleware.Metadata, error) { | ||
klog.InfoS("Mock middleware executed", "operation", middleware.GetOperationName(ctx)) | ||
if next == nil { | ||
return middleware.FinalizeOutput{}, middleware.Metadata{}, errors.New("next handler is nil") | ||
} | ||
return next.HandleFinalize(ctx, in) | ||
}, | ||
IDValue: "MockMiddleware", | ||
} | ||
err = stack.Finalize.Add(mockMiddleware, middleware.Before) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
// Ensure the chain is correctly constructed | ||
var handler middleware.FinalizeHandler = TerminalHandler{} | ||
for i := len(stack.Finalize.List()) - 1; i >= 0; i-- { | ||
middlewareID := stack.Finalize.List()[i] | ||
m, _ := stack.Finalize.Get(middlewareID) | ||
|
||
// Wrap the handler in a way that prevents infinite recursion | ||
previousHandler := handler | ||
handler = middleware.FinalizeHandlerFunc(func(ctx context.Context, in middleware.FinalizeInput) (middleware.FinalizeOutput, middleware.Metadata, error) { | ||
return m.HandleFinalize(ctx, in, previousHandler) | ||
}) | ||
} | ||
|
||
// Execute the middleware chain | ||
_, _, err = handler.HandleFinalize(ctx, middleware.FinalizeInput{}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
It("should record metrics with error status when next handler fails", func(ctx SpecContext) { | ||
err := metrics.AttachPrometheusMiddleware(stack, requestDuration, requestsTotal) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
// Add mock middleware to simulate behavior | ||
mockMiddleware := MockFinalizeMiddleware{ | ||
HandleFunc: func(ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler) (middleware.FinalizeOutput, middleware.Metadata, error) { | ||
klog.InfoS("Mock middleware executed", "operation", middleware.GetOperationName(ctx)) | ||
return next.HandleFinalize(ctx, in) | ||
}, | ||
IDValue: "MockMiddleware", | ||
} | ||
err = stack.Finalize.Add(mockMiddleware, middleware.Before) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
failingHandler := middleware.FinalizeHandlerFunc(func(ctx context.Context, in middleware.FinalizeInput) (middleware.FinalizeOutput, middleware.Metadata, error) { | ||
return middleware.FinalizeOutput{}, middleware.Metadata{}, errors.New("simulated error") | ||
}) | ||
|
||
var handler middleware.FinalizeHandler = failingHandler | ||
for i := len(stack.Finalize.List()) - 1; i >= 0; i-- { | ||
middlewareID := stack.Finalize.List()[i] | ||
m, _ := stack.Finalize.Get(middlewareID) | ||
|
||
previousHandler := handler | ||
handler = middleware.FinalizeHandlerFunc(func(ctx context.Context, in middleware.FinalizeInput) (middleware.FinalizeOutput, middleware.Metadata, error) { | ||
return m.HandleFinalize(ctx, in, previousHandler) | ||
}) | ||
} | ||
|
||
_, _, err = handler.HandleFinalize(ctx, middleware.FinalizeInput{}) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(Equal("simulated error")) | ||
|
||
metrics := testutil.CollectAndCount(requestDuration) | ||
Expect(metrics).To(BeNumerically(">", 0)) // Ensure metrics are collected | ||
Expect(requestDuration.WithLabelValues("TestOperation", "error")).NotTo(BeNil()) | ||
Expect(requestsTotal.WithLabelValues("TestOperation", "error")).NotTo(BeNil()) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think the S3 operation type is commonly called
action
throughout the product metrics.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.
Okay, I will change that.
Thanks
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.
same for iam