|
| 1 | +// Copyright (c) 2020 Red Hat, Inc. |
| 2 | +// Copyright Contributors to the Open Cluster Management project |
| 3 | + |
| 4 | +package e2e |
| 5 | + |
| 6 | +import ( |
| 7 | + "os/exec" |
| 8 | + "strings" |
| 9 | + |
| 10 | + . "github.com/onsi/ginkgo/v2" |
| 11 | + . "github.com/onsi/gomega" |
| 12 | +) |
| 13 | + |
| 14 | +// getMetrics curls the metrics endpoint, filters the response with the given patterns, |
| 15 | +// and returns the value(s) for the matching metric(s). |
| 16 | +func getMetrics(metricPatterns ...string) []string { |
| 17 | + metricFilter := " | grep " + strings.Join(metricPatterns, " | grep ") |
| 18 | + metricsCmd := `curl localhost:8383/metrics` + metricFilter |
| 19 | + cmd := exec.Command("bash", "-c", metricsCmd) |
| 20 | + |
| 21 | + matchingMetricsRaw, err := cmd.Output() |
| 22 | + if err != nil { |
| 23 | + if err.Error() == "exit status 1" { |
| 24 | + return []string{} // exit 1 indicates that grep couldn't find a match. |
| 25 | + } |
| 26 | + |
| 27 | + return []string{err.Error()} |
| 28 | + } |
| 29 | + |
| 30 | + matchingMetrics := strings.Split(strings.TrimSpace(string(matchingMetricsRaw)), "\n") |
| 31 | + values := make([]string, len(matchingMetrics)) |
| 32 | + |
| 33 | + for i, metric := range matchingMetrics { |
| 34 | + fields := strings.Fields(metric) |
| 35 | + if len(fields) > 0 { |
| 36 | + values[i] = fields[len(fields)-1] |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return values |
| 41 | +} |
| 42 | + |
| 43 | +var _ = Describe("Test metrics are exposed", Ordered, func() { |
| 44 | + It("Should expose the controller runtime reconcile total for each controller", func() { |
| 45 | + controllers := []string{ |
| 46 | + "policy-spec-sync", |
| 47 | + "policy-status-sync", |
| 48 | + "policy-template-sync", |
| 49 | + "secret-sync", |
| 50 | + } |
| 51 | + |
| 52 | + for _, ctrl := range controllers { |
| 53 | + By("Checking for the " + ctrl + " controller metric") |
| 54 | + matches := getMetrics("controller_runtime_reconcile_total", "success", ctrl) |
| 55 | + Expect(matches).To(HaveLen(1)) |
| 56 | + } |
| 57 | + }) |
| 58 | +}) |
0 commit comments