|
| 1 | +# Custom Resource State Metrics |
| 2 | + |
| 3 | +This section describes how to add metrics based on the state of a custom resource without writing a custom resource |
| 4 | +registry and running your own build of KSM. |
| 5 | + |
| 6 | +## Configuration |
| 7 | + |
| 8 | +The `KSM_CUSTOM_RESOURCE_METRICS` environment variable can be used to configure the metrics for custom resources: |
| 9 | + |
| 10 | +```yaml |
| 11 | +apiVersion: apps/v1 |
| 12 | +kind: Deployment |
| 13 | +metadata: |
| 14 | + name: kube-state-metrics |
| 15 | + namespace: kube-system |
| 16 | +spec: |
| 17 | + template: |
| 18 | + spec: |
| 19 | + containers: |
| 20 | + - name: kube-state-metrics |
| 21 | + env: |
| 22 | + - name: KSM_CUSTOM_RESOURCE_METRICS |
| 23 | + value: | |
| 24 | + spec: |
| 25 | + resources: |
| 26 | + - groupVersionKind: |
| 27 | + group: myteam.io |
| 28 | + version: "v1" |
| 29 | + kind: Foo |
| 30 | + metrics: |
| 31 | + - name: active_count |
| 32 | + help: "Count of active Foo" |
| 33 | + ... |
| 34 | +``` |
| 35 | +
|
| 36 | +### Examples |
| 37 | +
|
| 38 | +The examples in this section will use the following custom resource: |
| 39 | +
|
| 40 | +```yaml |
| 41 | +kind: Foo |
| 42 | +apiVersion: myteam.io/vl |
| 43 | +metadata: |
| 44 | + annotations: |
| 45 | + bar: baz |
| 46 | + qux: quxx |
| 47 | + labels: |
| 48 | + foo: bar |
| 49 | + name: foo |
| 50 | +spec: |
| 51 | + order: |
| 52 | + - id: 1 |
| 53 | + value: true |
| 54 | + - id: 3 |
| 55 | + value: false |
| 56 | + replicas: 1 |
| 57 | +status: |
| 58 | + active: |
| 59 | + type-a: 1 |
| 60 | + type-b: 3 |
| 61 | + conditions: |
| 62 | + - name: a |
| 63 | + value: 45 |
| 64 | + - name: b |
| 65 | + value: 66 |
| 66 | + sub: |
| 67 | + type-a: |
| 68 | + active: 1 |
| 69 | + ready: 2 |
| 70 | + type-b: |
| 71 | + active: 3 |
| 72 | + ready: 4 |
| 73 | + uptime: 43.21 |
| 74 | +``` |
| 75 | +
|
| 76 | +#### Single Values |
| 77 | +
|
| 78 | +```yaml |
| 79 | +kind: CustomResourceStateMetrics |
| 80 | +spec: |
| 81 | + resources: |
| 82 | + - groupVersionKind: |
| 83 | + group: myteam.io |
| 84 | + kind: "Foo" |
| 85 | + version: "v1" |
| 86 | + metrics: |
| 87 | + - name: "uptime" |
| 88 | + help: "Foo uptime" |
| 89 | + each: |
| 90 | + path: [status, uptime] |
| 91 | +``` |
| 92 | +
|
| 93 | +Produces the metric: |
| 94 | +
|
| 95 | +```prometheus |
| 96 | +kube_myteam_io_v1_Foo_uptime 43.21 |
| 97 | +``` |
| 98 | + |
| 99 | +#### Multiple Metrics/Kitchen Sink |
| 100 | + |
| 101 | +```yaml |
| 102 | +kind: CustomResourceStateMetrics |
| 103 | +spec: |
| 104 | + resources: |
| 105 | + - groupVersionKind: |
| 106 | + group: myteam.io |
| 107 | + kind: "Foo" |
| 108 | + version: "v1" |
| 109 | + # labels can be added to all metrics from a resource |
| 110 | + commonLabels: |
| 111 | + crd_type: "foo" |
| 112 | + labelsFromPath: |
| 113 | + name: [metadata, name] |
| 114 | + metrics: |
| 115 | + - name: "ready_count" |
| 116 | + help: "Number Foo Bars ready" |
| 117 | + each: |
| 118 | + # targeting an object or array will produce a metric for each element |
| 119 | + # labelsFromPath and value are relative to this path |
| 120 | + path: [status, sub] |
| 121 | + |
| 122 | + # if path targets an object, the object key will be used as label value |
| 123 | + labelFromKey: type |
| 124 | + # label values can be resolved specific to this path |
| 125 | + labelsFromPath: |
| 126 | + active: [active] |
| 127 | + # The actual field to use as metric value. Should be a number. |
| 128 | + value: [ready] |
| 129 | + commonLabels: |
| 130 | + custom_metric: "yes" |
| 131 | + labelsFromPath: |
| 132 | + # whole objects may be copied into labels by prefixing with "*" |
| 133 | + # *anything will be copied into labels, with the highest sorted * strings first |
| 134 | + "*": [metadata, labels] |
| 135 | + "**": [metadata, annotations] |
| 136 | + |
| 137 | + # or specific fields may be copied. these fields will always override values from *s |
| 138 | + name: [metadata, name] |
| 139 | + foo: [metadata, labels, foo] |
| 140 | +``` |
| 141 | +
|
| 142 | +Produces the following metrics: |
| 143 | +
|
| 144 | +```prometheus |
| 145 | +kube_myteam_io_v1_Foo_active_count{active="1",custom_metric="yes",foo="bar",name="foo",bar="baz",qux="quxx",type="type-a"} 1 |
| 146 | +kube_myteam_io_v1_Foo_active_count{active="3",custom_metric="yes",foo="bar",name="foo",bar="baz",qux="quxx",type="type-b"} 3 |
| 147 | +``` |
| 148 | + |
| 149 | +### Naming |
| 150 | + |
| 151 | +The default metric names are prefixed to avoid collisions with other metrics. |
| 152 | +By default a namespace of `kube` and a subsystem based on your custom resource's GVK is used. |
| 153 | +You can override these with the namespace and subsystem fields. |
| 154 | + |
| 155 | +```yaml |
| 156 | +kind: CustomResourceStateMetrics |
| 157 | +spec: |
| 158 | + resources: |
| 159 | + - groupVersionKind: ... |
| 160 | + namespace: myteam |
| 161 | + subsystem: foos |
| 162 | + metrics: |
| 163 | + - name: uptime |
| 164 | + ... |
| 165 | +``` |
| 166 | +
|
| 167 | +Produces: |
| 168 | +```prometheus |
| 169 | +myteam_foos_uptime 43.21 |
| 170 | +``` |
| 171 | + |
| 172 | +To omit namespace and/or subsystem altogether, set them to `_`. |
| 173 | + |
| 174 | +### Logging |
| 175 | + |
| 176 | +If a metric path is registered but not found on a custom resource, an error will be logged. For some resources, |
| 177 | +this may produce a lot of noise. The error log [verbosity][vlog] for a metric or resource can be set with `errorLogV` on |
| 178 | +the resource or metric: |
| 179 | + |
| 180 | +```yaml |
| 181 | +kind: CustomResourceStateMetrics |
| 182 | +spec: |
| 183 | + resources: |
| 184 | + - groupVersionKind: ... |
| 185 | + errorLogV: 0 # 0 = default for errors |
| 186 | + metrics: |
| 187 | + - name: uptime |
| 188 | + errorLogV: 10 # only log at high verbosity |
| 189 | +``` |
| 190 | +
|
| 191 | +[vlog]: https://github.com/go-logr/logr#why-v-levels |
| 192 | +
|
| 193 | +### Path Syntax |
| 194 | +
|
| 195 | +Paths are specified as a list of strings. Each string is a path segment, resolved dynamically against the data of the custom resource. |
| 196 | +If any part of a path is missing, the result is nil. |
| 197 | +
|
| 198 | +Examples: |
| 199 | +
|
| 200 | +```yaml |
| 201 | +# simple path lookup |
| 202 | +[spec, replicas] # spec.replicas == 1 |
| 203 | + |
| 204 | +# indexing an array |
| 205 | +[spec, order, "0", value] # spec.order[0].value = true |
| 206 | + |
| 207 | +# finding an element in a list by key=value |
| 208 | +[status, conditions, "[name=a]", value] # status.conditions[0].value = 45 |
| 209 | + |
| 210 | +# if the value to be matched is a number or boolean, the value is compared as a number or boolean |
| 211 | +[status, conditions, "[value=66]", name] # status.conditions[1].name = "b" |
| 212 | +``` |
0 commit comments