|
| 1 | +// Module included in the following assemblies: |
| 2 | +// |
| 3 | +// * operators/operator_sdk/osdk-monitoring-prometheus.adoc |
| 4 | + |
| 5 | +[id="osdk-monitoring-custom-metrics_{context}"] |
| 6 | += Exposing custom metrics |
| 7 | + |
| 8 | +As an Operator author, you can publish custom metrics by using the global Prometheus registry from the `controller-runtime/pkg/metrics` library. |
| 9 | + |
| 10 | +.Prerequisites |
| 11 | + |
| 12 | +* Go-based Operator generated using the Operator SDK |
| 13 | +* Prometheus Operator (deployed by default on {product-title} clusters) |
| 14 | + |
| 15 | +.Procedure |
| 16 | + |
| 17 | +. In your Operator SDK project, uncomment the following line in the `config/default/kustomization.yaml` file: |
| 18 | ++ |
| 19 | +[source,yaml] |
| 20 | +---- |
| 21 | +../prometheus |
| 22 | +---- |
| 23 | + |
| 24 | +. Create a custom controller class to publish additional metrics from the Operator. The following example declares the `widgets` and `widgetFailures` collectors as global variables, and then registers them with the `init()` function in the controller's package: |
| 25 | ++ |
| 26 | +.`controllers/memcached_controller_test_metrics.go` file |
| 27 | +[%collapsible] |
| 28 | +==== |
| 29 | +[source,go] |
| 30 | +---- |
| 31 | +package controllers |
| 32 | +
|
| 33 | +import ( |
| 34 | + "github.com/prometheus/client_golang/prometheus" |
| 35 | + "sigs.k8s.io/controller-runtime/pkg/metrics" |
| 36 | +) |
| 37 | +
|
| 38 | +
|
| 39 | +var ( |
| 40 | + widgets = prometheus.NewCounter( |
| 41 | + prometheus.CounterOpts{ |
| 42 | + Name: "widgets_total", |
| 43 | + Help: "Number of widgets processed", |
| 44 | + }, |
| 45 | + ) |
| 46 | + widgetFailures = prometheus.NewCounter( |
| 47 | + prometheus.CounterOpts{ |
| 48 | + Name: "widget_failures_total", |
| 49 | + Help: "Number of failed widgets", |
| 50 | + }, |
| 51 | + ) |
| 52 | +) |
| 53 | +
|
| 54 | +func init() { |
| 55 | + // Register custom metrics with the global prometheus registry |
| 56 | + metrics.Registry.MustRegister(widgets, widgetFailures) |
| 57 | +} |
| 58 | +---- |
| 59 | +==== |
| 60 | + |
| 61 | +. Record to these collectors from any part of the reconcile loop in the `main` controller class, which determines the business logic for the metric: |
| 62 | ++ |
| 63 | +.`controllers/memcached_controller.go` file |
| 64 | +[%collapsible] |
| 65 | +==== |
| 66 | +[source,go] |
| 67 | +---- |
| 68 | +func (r *MemcachedReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 69 | + ... |
| 70 | + ... |
| 71 | + // Add metrics |
| 72 | + widgets.Inc() |
| 73 | + widgetFailures.Inc() |
| 74 | +
|
| 75 | + return ctrl.Result{}, nil |
| 76 | +} |
| 77 | +---- |
| 78 | +==== |
| 79 | + |
| 80 | +. Build and push the Operator: |
| 81 | ++ |
| 82 | +[source,terminal] |
| 83 | +---- |
| 84 | +$ make docker-build docker-push IMG=<registry>/<user>/<image_name>:<tag> |
| 85 | +---- |
| 86 | + |
| 87 | +. Deploy the Operator: |
| 88 | ++ |
| 89 | +[source,terminal] |
| 90 | +---- |
| 91 | +$ make deploy IMG=<registry>/<user>/<image_name>:<tag> |
| 92 | +---- |
| 93 | + |
| 94 | +. Create role and role binding definitions to allow the service monitor of the Operator to be scraped by the Prometheus instance of the {product-title} cluster. |
| 95 | ++ |
| 96 | +Roles must be assigned so that service accounts have the permissions to scrape the metrics of the namespace: |
| 97 | ++ |
| 98 | +.`config/prometheus/role.yaml` role |
| 99 | +[%collapsible] |
| 100 | +==== |
| 101 | +[source,yaml] |
| 102 | +---- |
| 103 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 104 | +kind: ClusterRole |
| 105 | +metadata: |
| 106 | + name: prometheus-k8s-role |
| 107 | + namespace: <operator_namespace> |
| 108 | +rules: |
| 109 | + - apiGroups: |
| 110 | + - "" |
| 111 | + resources: |
| 112 | + - endpoints |
| 113 | + - pods |
| 114 | + - services |
| 115 | + - nodes |
| 116 | + - secrets |
| 117 | + verbs: |
| 118 | + - get |
| 119 | + - list |
| 120 | + - watch |
| 121 | +---- |
| 122 | +==== |
| 123 | ++ |
| 124 | +.`config/prometheus/rolebinding.yaml` role binding |
| 125 | +[%collapsible] |
| 126 | +==== |
| 127 | +[source,yaml] |
| 128 | +---- |
| 129 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 130 | +kind: ClusterRoleBinding |
| 131 | +metadata: |
| 132 | + name: prometheus-k8s-rolebinding |
| 133 | + namespace: memcached-operator-system |
| 134 | +roleRef: |
| 135 | + apiGroup: rbac.authorization.k8s.io |
| 136 | + kind: ClusterRole |
| 137 | + name: prometheus-k8s-role |
| 138 | +subjects: |
| 139 | + - kind: ServiceAccount |
| 140 | + name: prometheus-k8s |
| 141 | + namespace: openshift-monitoring |
| 142 | +---- |
| 143 | +==== |
| 144 | + |
| 145 | +. Apply the roles and role bindings for the deployed Operator: |
| 146 | ++ |
| 147 | +[source,terminal] |
| 148 | ++ |
| 149 | +---- |
| 150 | +$ oc apply -f config/prometheus/role.yaml |
| 151 | +---- |
| 152 | ++ |
| 153 | +[source,terminal] |
| 154 | +---- |
| 155 | +$ oc apply -f config/prometheus/rolebinding.yaml |
| 156 | +---- |
| 157 | + |
| 158 | +. Set the labels for the namespace that you want to scrape, which enables OpenShift cluster monitoring for that namespace: |
| 159 | ++ |
| 160 | +[source,terminal] |
| 161 | +---- |
| 162 | +$ oc label namespace <operator_namespace> openshift.io/cluster-monitoring="true" |
| 163 | +---- |
| 164 | + |
| 165 | +.Verification |
| 166 | + |
| 167 | +* Query and view the metrics in the {product-title} web console. You can use the names that were set in the custom controller class, for example `widgets_total` and `widget_failures_total`. |
0 commit comments