Skip to content

Commit f27c1ef

Browse files
authored
Merge pull request #1964 from jabdoa2/patch-1
Document how gauges convert types to float
2 parents 77d0935 + 8e3adcd commit f27c1ef

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

docs/customresourcestate-metrics.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,61 @@ Produces the metric:
242242
kube_customresource_uptime{customresource_group="myteam.io", customresource_kind="Foo", customresource_version="v1"} 43.21
243243
```
244244

245+
##### Type conversion and special handling
246+
247+
Gauges produce values of type float64 but custom resources can be of all kinds of types.
248+
Kube-state-metrics performs implicity type conversions for a lot of type.
249+
Supported types are:
250+
251+
* (u)int32/64, int, float32 and byte are cast to float64
252+
* `nil` is generally mapped to `0.0` if NilIsZero is `true`. Otherwise it yields an error
253+
* for bool `true` is mapped to `1.0` and `false` is mapped to `0.0`
254+
* for string the following logic applies
255+
* `"true"` and `"yes"` are mapped to `1.0` and `"false"` and `"no"` are mapped to `0.0` (all case insensitive)
256+
* RFC3339 times are parsed to float timestamp
257+
* finally the string is parsed to float using https://pkg.go.dev/strconv#ParseFloat which should support all common number formats. If that fails an error is yielded
258+
259+
##### Example for status conditions on Kubernetes Controllers
260+
261+
```yaml
262+
kind: CustomResourceStateMetrics
263+
spec:
264+
resources:
265+
- groupVersionKind:
266+
group: myteam.io
267+
kind: "Foo"
268+
version: "v1"
269+
labelsFromPath:
270+
name:
271+
- metadata
272+
- name
273+
namespace:
274+
- metadata
275+
- namespace
276+
metrics:
277+
- name: "foo_status"
278+
help: "status condition "
279+
each:
280+
type: Gauge
281+
gauge:
282+
path: [status, conditions]
283+
labelsFromPath:
284+
type: ["type"]
285+
valueFrom: ["status"]
286+
```
287+
288+
This will work for kubernetes controller CRs which expose status conditions according to the kubernetes api (https://pkg.go.dev/k8s.io/apimachinery/pkg/apis/meta/v1#Condition):
289+
290+
```yaml
291+
status:
292+
conditions:
293+
- lastTransitionTime: "2019-10-22T16:29:31Z"
294+
status: "True"
295+
type: Ready
296+
```
297+
298+
kube_customresource_foo_status{customresource_group="myteam.io", customresource_kind="Foo", customresource_version="v1", type="Ready"} 1.0
299+
245300
#### StateSet
246301

247302
> 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)

0 commit comments

Comments
 (0)