Skip to content

Commit 26c78c9

Browse files
authored
Merge pull request #183 from flant/fix_docs
docs: metrics grouping explained
2 parents 059424b + a7700ce commit 26c78c9

File tree

1 file changed

+77
-3
lines changed

1 file changed

+77
-3
lines changed

METRICS.md

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ Shell-operator exports Prometheus metrics to the `/metrics` path. The default po
3434

3535
## Custom metrics
3636

37-
Hooks can export metrics by writing a set of operation on JSON format into $METRICS_PATH file.
37+
Hooks can export metrics by writing a set of operations in JSON format into $METRICS_PATH file.
3838

39-
Operation to increase a counter:
39+
Operation to register a counter and increase its value:
4040

4141
```json
4242
{"name":"metric_name","add":1,"labels":{"label1":"value1"}}
4343
```
4444

45-
Operation to set a value for a gauge:
45+
Operation to register a gauge and set its value:
4646

4747
```json
4848
{"name":"metric_name","set":33,"labels":{"label1":"value1"}}
@@ -56,3 +56,77 @@ Several metrics can be expored at once. For example, this script will create 2 m
5656
echo '{"name":"hook_metric_count","add":1,"labels":{"label1":"value1"}}' >> $METRICS_PATH
5757
echo '{"name":"hook_metrics_items","add":1,"labels":{"label1":"value1"}}' >> $METRICS_PATH
5858
```
59+
60+
The metric name is used as-is, so several hooks can export same metric name. It is responsibility of hooks‘ developer to maintain consistent label cardinality.
61+
62+
There is no operation to delete metrics and no ttl mechanism yet. If hook set value for metric, this value is exported until the process ends.
63+
64+
To expire metrics, you can use a "group" field. When Shell-operator receives operations with "group", it expires previous metrics with the same group and apply new values. This grouping works across hooks and label values.
65+
66+
For example:
67+
68+
`hook1.sh` returns these metrics:
69+
70+
```
71+
echo '{"group":"hook1", "name":"hook_metric","add":1,"labels":{"kind":"pod"}}' >> $METRICS_PATH
72+
echo '{"group":"hook1", "name":"hook_metric","add":1,"labels":{"kind":"replicaset"}}' >> $METRICS_PATH
73+
echo '{"group":"hook1", "name":"hook_metric","add":1,"labels":{"kind":"deployment"}}' >> $METRICS_PATH
74+
echo '{"group":"hook1", "name":"hook1_special_metric","set":12,"labels":{"label1":"value1"}}' >> $METRICS_PATH
75+
echo '{"group":"hook1", "name":"common_metric","set":300,"labels":{"source":"source3"}}' >> $METRICS_PATH
76+
echo '{"name":"common_metric","set":100,"labels":{"source":"source1"}}' >> $METRICS_PATH
77+
```
78+
79+
`hook2.sh` returns these metrics:
80+
81+
```
82+
echo '{"group":"hook2", "name":"hook_metric","add":1,"labels":{"kind":"configmap"}}' >> $METRICS_PATH
83+
echo '{"group":"hook2", "name":"hook_metric","add":1,"labels":{"kind":"secret"}}' >> $METRICS_PATH
84+
echo '{"group":"hook2", "name":"hook2_special_metric","set":42}' >> $METRICS_PATH
85+
echo '{"name":"common_metric","set":200,"labels":{"source":"source2"}}' >> $METRICS_PATH
86+
```
87+
88+
Prometheus scrapes these metrics:
89+
90+
```
91+
# HELP hook_metric hook_metric
92+
# TYPE hook_metric counter
93+
hook_metric{hook="hook1.sh", kind="pod"} 1
94+
hook_metric{hook="hook1.sh", kind="replicaset"} 1
95+
hook_metric{hook="hook1.sh", kind="deployment"} 1
96+
hook_metric{hook="hook2.sh", kind="configmap"} 1
97+
hook_metric{hook="hook2.sh", kind="secret"} 1
98+
# HELP hook2_special_metric hook2_special_metric
99+
# TYPE hook1_special_metric gauge
100+
hook1_special_metric{hook="hook1.sh", label1="value1"} 12
101+
# HELP hook2_special_metric hook2_special_metric
102+
# TYPE hook2_special_metric gauge
103+
hook2_special_metric{hook="hook2.sh"} 42
104+
# HELP common_metric common_metric
105+
# TYPE common_metric gauge
106+
common_metric{hook="hook1.sh", source="source1"} 100
107+
common_metric{hook="hook1.sh", source="source3"} 300
108+
common_metric{hook="hook2.sh", source="source2"} 200
109+
```
110+
111+
On next execution of `hook1.sh` values for `hook_metric{kind="replicaset"}`, `hook_metric{kind="deployment"}`, `common_metric{source="source3"}` and `hook1_special_metric` are expired and hook returns only one metric:
112+
113+
```
114+
echo '{"group":"hook1", "name":"hook_metric","add":1,"labels":{"kind":"pod"}}' >> $METRICS_PATH
115+
```
116+
117+
Shell-operator expires previous values for group "hook1" and updates value for `hook_metric{hook="hook1.sh", kind="pod"}`. Values for group `hook2` and `common_metric` without group are left intact. Now Prometheus scrapes these metrics:
118+
119+
```
120+
# HELP hook_metric hook_metric
121+
# TYPE hook_metric counter
122+
hook_metric{hook="hook1.sh", kind="pod"} 2
123+
hook_metric{hook="hook2.sh", kind="configmap"} 1
124+
hook_metric{hook="hook2.sh", kind="secret"} 1
125+
# HELP hook2_special_metric hook2_special_metric
126+
# TYPE hook2_special_metric gauge
127+
hook2_special_metric{hook="hook2.sh"} 42
128+
# HELP common_metric common_metric
129+
# TYPE common_metric gauge
130+
common_metric{hook="hook1.sh", source="source1"} 100
131+
common_metric{hook="hook2.sh", source="source2"} 200
132+
```

0 commit comments

Comments
 (0)