5
5
[id="osdk-monitoring-prometheus-metrics-helper-{context}"]
6
6
= Metrics helper
7
7
8
- In Go-based Operators generated using the Operator SDK, the following helper
8
+ In Go-based Operators generated using the Operator SDK, the following
9
9
function exposes general metrics about the running program:
10
10
11
11
[source,go]
12
12
----
13
13
func ExposeMetricsPort(ctx context.Context, port int32) (*v1.Service, error)
14
14
----
15
15
16
- These metrics are inherited from the `controller-runtime` library API. The
17
- metrics are by default served on `:8383/metrics`.
16
+ These metrics are inherited from the `controller-runtime` library API. By default, the metrics are served on `0.0.0.0:8383/metrics`.
18
17
19
18
A Service object is created with the metrics port exposed, which can be then
20
19
accessed by Prometheus. The Service object is garbage collected when the leader
@@ -25,29 +24,34 @@ Operators generated using the Operator SDK:
25
24
26
25
[source,go]
27
26
----
28
- import(
29
- "github.com/operator-framework/operator-sdk/pkg/metrics"
30
- "sigs.k8s.io/controller-runtime/pkg/manager"
31
- )
32
-
33
- // Change the below variables to serve metrics on different host or port.
34
- var metricsHost = "0.0.0.0" <1>
35
- var metricsPort int32 = 8383 <2>
36
-
27
+ import(
28
+ "github.com/operator-framework/operator-sdk/pkg/metrics"
29
+ "sigs.k8s.io/controller-runtime/pkg/manager"
30
+ )
31
+
32
+ var (
33
+ // Change the below variables to serve metrics on a different host or port.
34
+ metricsHost = "0.0.0.0" <1>
35
+ metricsPort int32 = 8383 <2>
36
+ )
37
+ ...
38
+ func main() {
39
+ ...
37
40
// Pass metrics address to controller-runtime manager
38
- mgr, err := manager.New(cfg, manager.Options{
39
- Namespace: namespace,
40
- MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
41
- })
41
+ mgr, err := manager.New(cfg, manager.Options{
42
+ Namespace: namespace,
43
+ MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
44
+ })
42
45
43
46
...
44
-
45
- // Create Service object to expose the metrics port.
46
- _, err = metrics.ExposeMetricsPort(ctx, metricsPort)
47
- if err != nil {
47
+ // Create Service object to expose the metrics port.
48
+ _, err = metrics.ExposeMetricsPort(ctx, metricsPort)
49
+ if err != nil {
48
50
// handle error
49
- log.Info(err.Error())
50
- }
51
+ log.Info(err.Error())
52
+ }
53
+ ...
54
+ }
51
55
----
52
- <1> Host the metrics are exposed on.
53
- <2> Port the metrics are exposed on.
56
+ <1> The host that the metrics are exposed on.
57
+ <2> The port that the metrics are exposed on.
0 commit comments