|
| 1 | +// Module included in the following assemblies: |
| 2 | +// |
| 3 | +// * operators/operator_sdk/osdk-monitoring-prometheus.adoc |
| 4 | + |
| 5 | +:_content-type: PROCEDURE |
| 6 | +[id="osdk-ansible-metrics_{context}"] |
| 7 | += Exposing custom metrics for Ansible-based Operators |
| 8 | + |
| 9 | +As an Operator author creating Ansible-based Operators, you can use the Operator SDK's `osdk_metrics` module to expose custom Operator and Operand metrics, emit events, and support logging. |
| 10 | + |
| 11 | +.Prerequisites |
| 12 | + |
| 13 | +* Ansible-based Operator generated using the Operator SDK |
| 14 | +* Prometheus Operator, which is deployed by default on {product-title} clusters |
| 15 | +
|
| 16 | +.Procedure |
| 17 | + |
| 18 | +. Generate an Ansible-based Operator. This example uses a `testmetrics.com` domain: |
| 19 | ++ |
| 20 | +[source,terminal] |
| 21 | +---- |
| 22 | +$ operator-sdk init \ |
| 23 | + --plugins=ansible \ |
| 24 | + --domain=testmetrics.com |
| 25 | +---- |
| 26 | + |
| 27 | +. Create a `metrics` API. This example uses a `kind` named `Testmetrics`: |
| 28 | ++ |
| 29 | +[source,terminal] |
| 30 | +---- |
| 31 | +$ operator-sdk create api \ |
| 32 | + --group metrics \ |
| 33 | + --version v1 \ |
| 34 | + --kind Testmetrics \ |
| 35 | + --generate-role |
| 36 | +---- |
| 37 | + |
| 38 | +. Edit the `roles/testmetrics/tasks/main.yml` file and use the `osdk_metrics` module to create custom metrics for your Operator project: |
| 39 | ++ |
| 40 | +.Example `roles/testmetrics/tasks/main.yml` file |
| 41 | +[%collapsible] |
| 42 | +==== |
| 43 | +[source,yaml] |
| 44 | +---- |
| 45 | +--- |
| 46 | +# tasks file for Memcached |
| 47 | +- name: start k8sstatus |
| 48 | + k8s: |
| 49 | + definition: |
| 50 | + kind: Deployment |
| 51 | + apiVersion: apps/v1 |
| 52 | + metadata: |
| 53 | + name: '{{ ansible_operator_meta.name }}-memcached' |
| 54 | + namespace: '{{ ansible_operator_meta.namespace }}' |
| 55 | + spec: |
| 56 | + replicas: "{{size}}" |
| 57 | + selector: |
| 58 | + matchLabels: |
| 59 | + app: memcached |
| 60 | + template: |
| 61 | + metadata: |
| 62 | + labels: |
| 63 | + app: memcached |
| 64 | + spec: |
| 65 | + containers: |
| 66 | + - name: memcached |
| 67 | + command: |
| 68 | + - memcached |
| 69 | + - -m=64 |
| 70 | + - -o |
| 71 | + - modern |
| 72 | + - -v |
| 73 | + image: "docker.io/memcached:1.4.36-alpine" |
| 74 | + ports: |
| 75 | + - containerPort: 11211 |
| 76 | +
|
| 77 | +- osdk_metric: |
| 78 | + name: my_thing_counter |
| 79 | + description: This metric counts things |
| 80 | + counter: {} |
| 81 | +
|
| 82 | +- osdk_metric: |
| 83 | + name: my_counter_metric |
| 84 | + description: Add 3.14 to the counter |
| 85 | + counter: |
| 86 | + increment: yes |
| 87 | +
|
| 88 | +- osdk_metric: |
| 89 | + name: my_gauge_metric |
| 90 | + description: Create my gauge and set it to 2. |
| 91 | + gauge: |
| 92 | + set: 2 |
| 93 | +
|
| 94 | +- osdk_metric: |
| 95 | + name: my_histogram_metric |
| 96 | + description: Observe my histogram |
| 97 | + histogram: |
| 98 | + observe: 2 |
| 99 | +
|
| 100 | +- osdk_metric: |
| 101 | + name: my_summary_metric |
| 102 | + description: Observe my summary |
| 103 | + summary: |
| 104 | + observe: 2 |
| 105 | +---- |
| 106 | +==== |
| 107 | +
|
| 108 | +.Verification |
| 109 | +
|
| 110 | +. Run your Operator on a cluster. For example, to use the "run as a deployment" method: |
| 111 | +
|
| 112 | +
|
| 113 | +.. Build the Operator image and push it to a registry: |
| 114 | ++ |
| 115 | +[source,terminal] |
| 116 | +---- |
| 117 | +$ make docker-build docker-push IMG=<registry>/<user>/<image_name>:<tag> |
| 118 | +---- |
| 119 | +
|
| 120 | +.. Install the Operator on a cluster: |
| 121 | ++ |
| 122 | +[source,terminal] |
| 123 | +---- |
| 124 | +$ make install |
| 125 | +---- |
| 126 | +
|
| 127 | +.. Deploy the Operator: |
| 128 | ++ |
| 129 | +[source,terminal] |
| 130 | +---- |
| 131 | +$ make deploy IMG=<registry>/<user>/<image_name>:<tag> |
| 132 | +---- |
| 133 | +
|
| 134 | +. Create a `Testmetrics` custom resource (CR): |
| 135 | +
|
| 136 | +.. Define the CR spec: |
| 137 | ++ |
| 138 | +.Example `config/samples/metrics_v1_testmetrics.yaml` file |
| 139 | +[%collapsible] |
| 140 | +==== |
| 141 | +[source,yaml] |
| 142 | +---- |
| 143 | +apiVersion: metrics.testmetrics.com/v1 |
| 144 | +kind: Testmetrics |
| 145 | +metadata: |
| 146 | + name: testmetrics-sample |
| 147 | +spec: |
| 148 | + size: 1 |
| 149 | +---- |
| 150 | +==== |
| 151 | +
|
| 152 | +.. Create the object: |
| 153 | ++ |
| 154 | +[source,terminal] |
| 155 | +---- |
| 156 | +$ oc create -f config/samples/metrics_v1_testmetrics.yaml |
| 157 | +---- |
| 158 | +
|
| 159 | +. Get the pod details: |
| 160 | ++ |
| 161 | +[source,terminal] |
| 162 | +---- |
| 163 | +$ oc get pods |
| 164 | +---- |
| 165 | ++ |
| 166 | +.Example output |
| 167 | +[source,terminal] |
| 168 | +---- |
| 169 | +NAME READY STATUS RESTARTS AGE |
| 170 | +ansiblemetrics-controller-manager-<id> 2/2 Running 0 149m |
| 171 | +testmetrics-sample-memcached-<id> 1/1 Running 0 147m |
| 172 | +---- |
| 173 | +
|
| 174 | +. Get the endpoint details: |
| 175 | ++ |
| 176 | +[source,terminal] |
| 177 | +---- |
| 178 | +$ oc get ep |
| 179 | +---- |
| 180 | ++ |
| 181 | +.Example output |
| 182 | +[source,terminal] |
| 183 | +---- |
| 184 | +NAME ENDPOINTS AGE |
| 185 | +ansiblemetrics-controller-manager-metrics-service 10.129.2.70:8443 150m |
| 186 | +---- |
| 187 | +
|
| 188 | +. Get the custom metrics token: |
| 189 | ++ |
| 190 | +[source,terminal] |
| 191 | +---- |
| 192 | +$ token=`oc sa get-token prometheus-k8s -n openshift-monitoring` |
| 193 | +---- |
| 194 | +
|
| 195 | +. Check the metrics values: |
| 196 | +
|
| 197 | +.. Check the `my_counter_metric` value: |
| 198 | ++ |
| 199 | +[source,terminal] |
| 200 | +---- |
| 201 | +$ oc exec ansiblemetrics-controller-manager-<id> -- curl -k -H "Authoriza |
| 202 | +tion: Bearer $token" 'https://10.129.2.70:8443/metrics' | grep my_counter |
| 203 | +---- |
| 204 | ++ |
| 205 | +.Example output |
| 206 | +[source,terminal] |
| 207 | +---- |
| 208 | +HELP my_counter_metric Add 3.14 to the counter |
| 209 | +TYPE my_counter_metric counter |
| 210 | +my_counter_metric 2 |
| 211 | +---- |
| 212 | +
|
| 213 | +.. Check the `my_gauge_metric` value: |
| 214 | ++ |
| 215 | +[source,terminal] |
| 216 | +---- |
| 217 | +$ oc exec ansiblemetrics-controller-manager-<id> -- curl -k -H "Authoriza |
| 218 | +tion: Bearer $token" 'https://10.129.2.70:8443/metrics' | grep gauge |
| 219 | +---- |
| 220 | ++ |
| 221 | +.Example output |
| 222 | +[source,terminal] |
| 223 | +---- |
| 224 | +HELP my_gauge_metric Create my gauge and set it to 2. |
| 225 | +---- |
| 226 | +
|
| 227 | +.. Check the `my_histogram_metric` and `my_summary_metric` values: |
| 228 | ++ |
| 229 | +[source,terminal] |
| 230 | +---- |
| 231 | +$ oc exec ansiblemetrics-controller-manager-<id> -- curl -k -H "Authoriza |
| 232 | +tion: Bearer $token" 'https://10.129.2.70:8443/metrics' | grep Observe |
| 233 | +---- |
| 234 | ++ |
| 235 | +.Example output |
| 236 | +[source,terminal] |
| 237 | +---- |
| 238 | +HELP my_histogram_metric Observe my histogram |
| 239 | +HELP my_summary_metric Observe my summary |
| 240 | +---- |
0 commit comments