Skip to content

Commit

Permalink
done with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anurag4DSB committed Dec 24, 2024
1 parent b1adf7f commit 4871ebd
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 68 deletions.
4 changes: 3 additions & 1 deletion pkg/util/prometheus_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
67 changes: 0 additions & 67 deletions pkg/util/prometheus_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
// })
})

0 comments on commit 4871ebd

Please sign in to comment.