Skip to content

Commit a981f3c

Browse files
JustinKuliopenshift-merge-robot
authored andcommitted
Expose metrics
By default, the metrics are exposed at localhost:8383. Note that this means in a pod, workloads outside the pod *can not* access them. Refs: - https://issues.redhat.com/browse/ACM-2179 Signed-off-by: Justin Kulikauskas <[email protected]>
1 parent 14c2ce3 commit a981f3c

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func main() {
162162
mgrOptionsBase := manager.Options{
163163
LeaderElection: tool.Options.EnableLeaderElection,
164164
// Disable the metrics endpoint
165-
MetricsBindAddress: "0",
165+
MetricsBindAddress: tool.Options.MetricsAddr,
166166
Scheme: scheme,
167167
// Override the EventBroadcaster so that the spam filter will not ignore events for the policy but with
168168
// different messages if a large amount of events for that policy are sent in a short time.
@@ -446,6 +446,10 @@ func getHubManager(
446446
options.Namespace = tool.Options.ClusterNamespaceOnHub
447447
options.NewCache = newCacheFunc
448448

449+
// Disable the metrics endpoint for this manager. Note that since they both use the global
450+
// metrics registry, metrics for this manager are still exposed by the other manager.
451+
options.MetricsBindAddress = "0"
452+
449453
// Create a new manager to provide shared dependencies and start components
450454
mgr, err := ctrl.NewManager(hubCfg, options)
451455
if err != nil {

test/e2e/case13_metrics_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
})

tool/options.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type SyncerOptions struct {
2020
EnableLeaderElection bool
2121
LegacyLeaderElection bool
2222
ProbeAddr string
23+
MetricsAddr string
2324
// The namespace that the replicated policies should be synced to. This defaults to the same namespace as on the
2425
// Hub.
2526
ClusterNamespace string
@@ -95,4 +96,11 @@ func ProcessFlags() {
9596
":8080",
9697
"The address the first probe endpoint binds to.",
9798
)
99+
100+
flag.StringVar(
101+
&Options.MetricsAddr,
102+
"metrics-bind-address",
103+
"localhost:8383",
104+
"The address the metrics endpoint binds to.",
105+
)
98106
}

0 commit comments

Comments
 (0)