diff --git a/pkg/util/prometheus_middleware.go b/pkg/util/prometheus_middleware.go index c7bf0da0..2c1ee855 100644 --- a/pkg/util/prometheus_middleware.go +++ b/pkg/util/prometheus_middleware.go @@ -8,8 +8,10 @@ import ( "k8s.io/klog/v2" ) +var AttachPrometheusMiddleware = attachPrometheusMiddlewareMetrics + // AttachPrometheusMiddleware attaches a Prometheus middleware for metrics tracking. -var AttachPrometheusMiddleware = func(stack *middleware.Stack, requestDuration *prometheus.HistogramVec, requestsTotal *prometheus.CounterVec) error { +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) { diff --git a/pkg/util/prometheus_middleware_test.go b/pkg/util/prometheus_middleware_test.go index 354b77e2..e0dd6879 100644 --- a/pkg/util/prometheus_middleware_test.go +++ b/pkg/util/prometheus_middleware_test.go @@ -159,71 +159,4 @@ var _ = Describe("AttachPrometheusMiddleware", func() { Expect(requestDuration.WithLabelValues("TestOperation", "error")).NotTo(BeNil()) Expect(requestsTotal.WithLabelValues("TestOperation", "error")).NotTo(BeNil()) }) - // It("should record metrics with status 'success'", func() { - // // Attach the Prometheus middleware - // err := u.AttachPrometheusMiddleware(stack, requestDuration, requestsTotal) - // Expect(err).NotTo(HaveOccurred()) - - // // Mock middleware simulating a successful operation - // mockMiddleware := MockFinalizeMiddleware{ - // HandleFunc: func(ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler) (middleware.FinalizeOutput, middleware.Metadata, error) { - // return next.HandleFinalize(ctx, in) // Call the next handler - // }, - // IDValue: "MockSuccessMiddleware", - // } - // err = stack.Finalize.Add(mockMiddleware, middleware.Before) // Add mock middleware - // Expect(err).NotTo(HaveOccurred()) - - // // Build the middleware chain - // 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) - // handler = middleware.FinalizeHandlerFunc(func(ctx context.Context, in middleware.FinalizeInput) (middleware.FinalizeOutput, middleware.Metadata, error) { - // return m.HandleFinalize(ctx, in, handler) - // }) - // } - - // // Execute the middleware - // _, _, err = handler.HandleFinalize(ctx, middleware.FinalizeInput{}) - // Expect(err).NotTo(HaveOccurred()) - - // // Verify Prometheus metrics - // Expect(testutil.CollectAndCount(requestDuration)).To(Equal(1)) - // Expect(testutil.ToFloat64(requestsTotal.WithLabelValues("TestOperation", "success"))).To(Equal(1.0)) - // }) - - // It("should record metrics with status 'error'", func() { - // // Attach the Prometheus middleware - // err := u.AttachPrometheusMiddleware(stack, requestDuration, requestsTotal) - // Expect(err).NotTo(HaveOccurred()) - - // // Mock middleware simulating an error - // mockMiddleware := MockFinalizeMiddleware{ - // HandleFunc: func(ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler) (middleware.FinalizeOutput, middleware.Metadata, error) { - // return middleware.FinalizeOutput{}, middleware.Metadata{}, errors.New("mock error") // Simulate an error - // }, - // IDValue: "MockErrorMiddleware", - // } - // err = stack.Finalize.Add(mockMiddleware, middleware.Before) // Add mock middleware - // Expect(err).NotTo(HaveOccurred()) - - // // Build the middleware chain - // 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) - // handler = middleware.FinalizeHandlerFunc(func(ctx context.Context, in middleware.FinalizeInput) (middleware.FinalizeOutput, middleware.Metadata, error) { - // return m.HandleFinalize(ctx, in, handler) - // }) - // } - - // // Execute the middleware - // _, _, err = handler.HandleFinalize(ctx, middleware.FinalizeInput{}) - // Expect(err).To(HaveOccurred()) - - // // Verify Prometheus metrics - // Expect(testutil.CollectAndCount(requestDuration)).To(Equal(1)) - // Expect(testutil.ToFloat64(requestsTotal.WithLabelValues("TestOperation", "error"))).To(Equal(1.0)) - // }) })