Skip to content

Commit b314ccb

Browse files
authored
Merge pull request #1777 from chrischdi/poc-additional-metric-types-2
pkg/customresourcestate implement info and stateSet metric type and refactor configuration file
2 parents 4bb1b38 + 52d3ab6 commit b314ccb

8 files changed

+746
-353
lines changed

docs/customresourcestate-metrics.md

Lines changed: 133 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ spec:
4141
metrics:
4242
- name: active_count
4343
help: "Count of active Foo"
44+
type: Gauge
4445
...
4546
- --resources=certificatesigningrequests,configmaps,cronjobs,daemonsets,deployments,endpoints,foos,horizontalpodautoscalers,ingresses,jobs,limitranges,mutatingwebhookconfigurations,namespaces,networkpolicies,nodes,persistentvolumeclaims,persistentvolumes,poddisruptionbudgets,pods,replicasets,replicationcontrollers,resourcequotas,secrets,services,statefulsets,storageclasses,validatingwebhookconfigurations,volumeattachments,verticalpodautoscalers
4647
```
@@ -60,13 +61,15 @@ metadata:
6061
foo: bar
6162
name: foo
6263
spec:
64+
version: v1.2.3
6365
order:
6466
- id: 1
6567
value: true
6668
- id: 3
6769
value: false
6870
replicas: 1
6971
status:
72+
phase: Pending
7073
active:
7174
type-a: 1
7275
type-b: 3
@@ -101,7 +104,9 @@ spec:
101104
- name: "uptime"
102105
help: "Foo uptime"
103106
each:
104-
path: [status, uptime]
107+
type: Gauge
108+
gauge:
109+
path: [status, uptime]
105110
```
106111
107112
Produces the metric:
@@ -129,17 +134,19 @@ spec:
129134
- name: "ready_count"
130135
help: "Number Foo Bars ready"
131136
each:
132-
# targeting an object or array will produce a metric for each element
133-
# labelsFromPath and value are relative to this path
134-
path: [status, sub]
135-
136-
# if path targets an object, the object key will be used as label value
137-
labelFromKey: type
138-
# label values can be resolved specific to this path
139-
labelsFromPath:
140-
active: [active]
141-
# The actual field to use as metric value. Should be a number.
142-
value: [ready]
137+
type: Gauge
138+
gauge:
139+
# targeting an object or array will produce a metric for each element
140+
# labelsFromPath and value are relative to this path
141+
path: [status, sub]
142+
143+
# if path targets an object, the object key will be used as label value
144+
labelFromKey: type
145+
# label values can be resolved specific to this path
146+
labelsFromPath:
147+
active: [active]
148+
# The actual field to use as metric value. Should be a number.
149+
value: [ready]
143150
commonLabels:
144151
custom_metric: "yes"
145152
labelsFromPath:
@@ -160,19 +167,117 @@ kube_myteam_io_v1_Foo_active_count{active="1",custom_metric="yes",foo="bar",name
160167
kube_myteam_io_v1_Foo_active_count{active="3",custom_metric="yes",foo="bar",name="foo",bar="baz",qux="quxx",type="type-b"} 3
161168
```
162169

170+
### Metric types
171+
172+
The configuration supports three kind of metrics from the [OpenMetrics specification](https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md).
173+
174+
The metric type is specified by the `type` field and its specific configuration at the types specific struct.
175+
176+
#### Gauge
177+
178+
> Gauges are current measurements, such as bytes of memory currently used or the number of items in a queue. For gauges the absolute value is what is of interest to a user. [[0]](https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#gauge)
179+
180+
Example:
181+
182+
```yaml
183+
kind: CustomResourceStateMetrics
184+
spec:
185+
resources:
186+
- groupVersionKind:
187+
group: myteam.io
188+
kind: "Foo"
189+
version: "v1"
190+
metrics:
191+
- name: "uptime"
192+
help: "Foo uptime"
193+
each:
194+
type: Gauge
195+
gauge:
196+
path: [status, uptime]
197+
```
198+
199+
Produces the metric:
200+
201+
```prometheus
202+
kube_myteam_io_v1_Foo_uptime 43.21
203+
```
204+
205+
#### StateSet
206+
207+
> StateSets represent a series of related boolean values, also called a bitset. If ENUMs need to be encoded this MAY be done via StateSet. [[1]](https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#stateset)
208+
209+
```yaml
210+
kind: CustomResourceStateMetrics
211+
spec:
212+
resources:
213+
- groupVersionKind:
214+
group: myteam.io
215+
kind: "Foo"
216+
version: "v1"
217+
metrics:
218+
- name: "status_phase"
219+
help: "Foo status_phase"
220+
each:
221+
type: StateSet
222+
stateSet:
223+
labelName: phase
224+
path: [status, phase]
225+
list: [Pending, Bar, Baz]
226+
```
227+
228+
Metrics of type `SateSet` will generate a metric for each value defined in `list` for each resource.
229+
The value will be 1, if the value matches the one in list.
230+
231+
Produces the metric:
232+
233+
```prometheus
234+
kube_myteam_io_v1_Foo_status_phase{phase="Pending"} 1
235+
kube_myteam_io_v1_Foo_status_phase{phase="Bar"} 0
236+
kube_myteam_io_v1_Foo_status_phase{phase="Baz"} 0
237+
```
238+
239+
#### Info
240+
241+
> Info metrics are used to expose textual information which SHOULD NOT change during process lifetime. Common examples are an application's version, revision control commit, and the version of a compiler. [[2]](https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#info)
242+
243+
Metrics of type `Info` will always have a value of 1.
244+
245+
```yaml
246+
kind: CustomResourceStateMetrics
247+
spec:
248+
resources:
249+
- groupVersionKind:
250+
group: myteam.io
251+
kind: "Foo"
252+
version: "v1"
253+
metrics:
254+
- name: "version"
255+
help: "Foo version"
256+
each:
257+
type: Info
258+
info:
259+
labelsFromPath:
260+
version: [spec, version]
261+
```
262+
263+
Produces the metric:
264+
265+
```prometheus
266+
kube_myteam_io_v1_Foo_version{version="v1.2.3"} 1
267+
```
268+
163269
### Naming
164270

165271
The default metric names are prefixed to avoid collisions with other metrics.
166-
By default, a namespace of `kube` and a subsystem based on your custom resource's group+version+kind is used.
167-
You can override these with the namespace and subsystem fields.
272+
By default, a metric prefix of `kube_` concatenated with your custom resource's group+version+kind is used.
273+
You can override this behavior with the `metricNamePrefix` field.
168274

169275
```yaml
170276
kind: CustomResourceStateMetrics
171277
spec:
172278
resources:
173279
- groupVersionKind: ...
174-
namespace: myteam
175-
subsystem: foos
280+
metricNamePrefix: myteam_foos
176281
metrics:
177282
- name: uptime
178283
...
@@ -183,7 +288,18 @@ Produces:
183288
myteam_foos_uptime 43.21
184289
```
185290

186-
To omit namespace and/or subsystem altogether, set them to `_`.
291+
To omit namespace and/or subsystem altogether, set them to the empty string:
292+
293+
```yaml
294+
kind: CustomResourceStateMetrics
295+
spec:
296+
resources:
297+
- groupVersionKind: ...
298+
metricNamePrefix: ""
299+
metrics:
300+
- name: uptime
301+
...
302+
```
187303

188304
### Logging
189305

0 commit comments

Comments
 (0)