Skip to content
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

91 changes: 91 additions & 0 deletions pkg/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,94 @@ var _ = Describe("Metrics Server Functions", func() {
})
})
})

var _ = Describe("S3 and IAM metrics", func() {
var (
server *http.Server
listener net.Listener
addr string
)

BeforeEach(func() {
var err error
listener, err = net.Listen("tcp", "127.0.0.1:0")
Expect(err).NotTo(HaveOccurred())

server, err = metrics.StartMetricsServerWithListener(listener)
Expect(err).NotTo(HaveOccurred())

addr = listener.Addr().String()
})

AfterEach(func() {
if server != nil {
_ = server.Close()
}
if listener != nil {
_ = listener.Close()
}
})

It("should correctly increment S3 metrics", func() {
metrics.S3RequestsTotal.WithLabelValues("CreateBucket", "success").Inc()
metrics.S3RequestsTotal.WithLabelValues("CreateBucket", "error").Add(2)

resp, err := http.Get(fmt.Sprintf("http://%s%s", addr, constants.MetricsPath))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW the prometheus module contains test utilities for this kind of thing, here this function could be used: https://pkg.go.dev/github.com/prometheus/[email protected]/prometheus/testutil#ScrapeAndCompare.

It's totally up to you if you'd like to use them or prefer custom test code though (and in case you need an example, some of the metrics PRs for metadata migration use some of those test functions).

Expect(err).NotTo(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusOK))

body, err := io.ReadAll(resp.Body)
Expect(err).NotTo(HaveOccurred())
Expect(resp.Body.Close()).To(Succeed())
Expect(string(body)).To(ContainSubstring(`s3_requests_total{method="CreateBucket",status="success"} 1`))
Expect(string(body)).To(ContainSubstring(`s3_requests_total{method="CreateBucket",status="error"} 2`))
})

It("should correctly record IAM request durations", func() {
timer := prometheus.NewTimer(metrics.IAMRequestDuration.WithLabelValues("CreateUser", "success"))
time.Sleep(50 * time.Millisecond)
timer.ObserveDuration()

resp, err := http.Get(fmt.Sprintf("http://%s%s", addr, constants.MetricsPath))
Expect(err).NotTo(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusOK))

body, err := io.ReadAll(resp.Body)
Expect(err).NotTo(HaveOccurred())
Expect(resp.Body.Close()).To(Succeed())
Expect(string(body)).To(ContainSubstring("iam_request_duration_seconds_bucket"))
Expect(string(body)).To(ContainSubstring("iam_request_duration_seconds_sum"))
})

It("should handle invalid metric labels gracefully", func() {
Expect(func() {
metrics.S3RequestsTotal.WithLabelValues("InvalidMethod", "").Inc()
}).NotTo(Panic())
})

It("should return 404 for invalid metrics paths", func() {
resp, err := http.Get(fmt.Sprintf("http://%s/invalid-path", addr))
Expect(err).NotTo(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusNotFound))
Expect(resp.Body.Close()).To(Succeed())
})

It("should support concurrent requests to the metrics endpoint", func() {
const numRequests = 10
done := make(chan bool, numRequests)

for i := 0; i < numRequests; i++ {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use a sync.WaitGroup to wait for the 10 goroutines a bit more simply (https://pkg.go.dev/sync#WaitGroup).

Also, it's good practice to call defer GinkgoRecover() at the beginning of each goroutine to help ginkgo recover if some goroutines are stuck or panic.

go func() {
resp, err := http.Get(fmt.Sprintf("http://%s%s", addr, constants.MetricsPath))
Expect(err).NotTo(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusOK))
Expect(resp.Body.Close()).To(Succeed())
done <- true
}()
}

for i := 0; i < numRequests; i++ {
<-done
}
})
})
Loading