Skip to content

Commit bda9985

Browse files
authored
[Improvement] Improve metric handling (#1728)
1 parent ae663ea commit bda9985

File tree

83 files changed

+6045
-345
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+6045
-345
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
- (Bugfix) Versioning Alignment
3333
- (Feature) (Scheduler) Merge Strategy
3434
- (Feature) (Networking) Endpoints Destination
35+
- (Improvement) Improve Metrics Handling
3536

3637
## [1.2.42](https://github.com/arangodb/kube-arangodb/tree/1.2.42) (2024-07-23)
3738
- (Maintenance) Go 1.22.4 & Kubernetes 1.29.6 libraries

docs/generated/metrics/README.md

+36-34
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
layout: page
3+
title: arangodb_operator_objects_processed
4+
parent: List of available metrics
5+
---
6+
7+
# arangodb_operator_objects_processed (Counter)
8+
9+
## Description
10+
11+
Number of the processed objects
12+
13+
## Labels
14+
15+
| Label | Description |
16+
|:-------------:|:--------------|
17+
| operator_name | Operator Name |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
layout: page
3+
title: arangodb_resources_deployment_config_map_duration
4+
parent: List of available metrics
5+
---
6+
7+
# arangodb_resources_deployment_config_map_duration (Gauge)
8+
9+
## Description
10+
11+
Duration of inspected ConfigMaps by Deployment in seconds
12+
13+
## Labels
14+
15+
| Label | Description |
16+
|:----------:|:----------------|
17+
| deployment | Deployment Name |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
layout: page
3+
title: arangodb_resources_deployment_config_map_inspected
4+
parent: List of available metrics
5+
---
6+
7+
# arangodb_resources_deployment_config_map_inspected (Counter)
8+
9+
## Description
10+
11+
Number of inspected ConfigMaps by Deployment
12+
13+
## Labels
14+
15+
| Label | Description |
16+
|:----------:|:----------------|
17+
| deployment | Deployment Name |

internal/metrics.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,12 @@ func generateMetricsGO(root string, in MetricsDoc) error {
361361
"fname": strings.Join(fnameParts, ""),
362362
"ename": strings.Join(tparts, ""),
363363
"shortDescription": details.ShortDescription,
364-
"global": details.Global,
365364
"labels": generateLabels(details.Labels),
366365
"type": details.Type,
367366
"mapTypes": mapTypes,
368367
"mapKeys": mapKeys,
369368
"mapIKeys": mapIKeys,
369+
"global": details.Global,
370370
"args": strings.Join(params[1:], ", "),
371371
"fparams": strings.Join(params, ", "),
372372
"fkeys": strings.Join(keys, ", "),

internal/metrics.item.go.tmpl

+24-117
Original file line numberDiff line numberDiff line change
@@ -23,154 +23,61 @@ package metric_descriptions
2323

2424
import (
2525
"github.com/arangodb/kube-arangodb/pkg/util/metrics"
26-
{{- if .global }}
27-
28-
"sync"
29-
{{- end }}
3026
)
3127

3228
var (
3329
{{ .fname }} = metrics.NewDescription("{{ .name }}", "{{ .shortDescription }}", {{ .labels }}, nil)
30+
{{- if .global }}
31+
32+
// Global Fields
33+
global{{ .ename }}{{ .type }} = New{{ .ename }}{{ .type }}Factory()
34+
{{- end }}
3435
)
3536

3637
func init() {
3738
registerDescription({{ .fname }})
3839
{{- if .global }}
39-
registerCollector({{ .fname }}Global)
40+
registerCollector(global{{ .ename }}{{ .type }})
4041
{{- end }}
4142
}
42-
43-
func {{ .ename }}() metrics.Description {
44-
return {{ .fname }}
45-
}
46-
4743
{{- if .global }}
4844

49-
func {{ .ename }}Get({{ .args }}) float64 {
50-
return {{ .fname }}Global.Get({{ .ename }}Item{
51-
{{- range $i, $field := .mapKeys }}
52-
{{ $field }}: {{ index $root.mapIKeys $field }},
53-
{{- end }}
54-
})
55-
}
56-
57-
func {{ .ename }}Add({{ .fparams }}) {
58-
{{ .fname }}Global.Add(value, {{ .ename }}Item{
59-
{{- range $i, $field := .mapKeys }}
60-
{{ $field }}: {{ index $root.mapIKeys $field }},
61-
{{- end }}
62-
})
63-
}
64-
{{- if eq .type "Counter" }}
65-
66-
func {{ .ename }}Inc({{ .args }}) {
67-
{{ .fname }}Global.Inc({{ .ename }}Item{
68-
{{- range $i, $field := .mapKeys }}
69-
{{ $field }}: {{ index $root.mapIKeys $field }},
70-
{{- end }}
71-
})
45+
func Global{{ .ename }}{{ .type }}() metrics.Factory{{ .type }}[{{ .ename }}Input] {
46+
return global{{ .ename }}{{ .type }}
7247
}
7348
{{- end }}
7449

75-
func Get{{ .ename }}Factory() {{ .ename }}Factory {
76-
return {{ .fname }}Global
77-
}
7850

79-
var {{ .fname }}Global = &{{ .fname }}Factory{
80-
items: {{ .fname }}Items{},
51+
func New{{ .ename }}{{ .type }}Factory() metrics.Factory{{ .type }}[{{ .ename }}Input] {
52+
return metrics.NewFactory{{ .type }}[{{ .ename }}Input]()
8153
}
8254

83-
type {{ .ename }}Factory interface {
84-
Get(object {{ .ename }}Item) float64
85-
Add(value float64, object {{ .ename }}Item)
86-
Remove(object {{ .ename }}Item)
87-
Items() []{{ .ename }}Item
88-
{{- if eq .type "Counter" }}
89-
90-
Inc(object {{ .ename }}Item)
55+
func New{{ .ename }}Input({{- range $i, $e := .mapKeys }}{{ if $i }}, {{ end }}{{ index $root.mapIKeys . }} {{ index $root.mapTypes . }}{{- end }}) {{ .ename }}Input {
56+
return {{ .ename }}Input{
57+
{{- range .mapKeys }}
58+
{{ . }}: {{ index $root.mapIKeys . }},
9159
{{- end }}
92-
}
93-
94-
type {{ .fname }}Factory struct {
95-
lock sync.RWMutex
96-
97-
items {{ .fname }}Items
98-
}
99-
100-
func (a *{{ .fname }}Factory) Get(object {{ .ename }}Item) float64 {
101-
a.lock.Lock()
102-
defer a.lock.Unlock()
103-
104-
v, ok := a.items[object]
105-
if !ok {
106-
return 0
107-
}
108-
109-
return v
110-
}
111-
112-
func (a *{{ .fname }}Factory) Add(value float64, object {{ .ename }}Item) {
113-
a.lock.Lock()
114-
defer a.lock.Unlock()
115-
116-
v, ok := a.items[object]
117-
if !ok {
118-
a.items[object] = value
119-
return
120-
}
121-
122-
a.items[object] = value + v
123-
}
124-
125-
func (a *{{ .fname }}Factory) Remove(obj {{ .ename }}Item) {
126-
a.lock.Lock()
127-
defer a.lock.Unlock()
128-
129-
delete(a.items, obj)
130-
}
131-
132-
func (a *{{ .fname }}Factory) Items() []{{ .ename }}Item {
133-
a.lock.Lock()
134-
defer a.lock.Unlock()
135-
136-
var r = make([]{{ .ename }}Item, 0, len(a.items))
137-
138-
for k := range a.items {
139-
r = append(r, k)
14060
}
141-
142-
return r
14361
}
144-
{{- if eq .type "Counter" }}
14562

146-
func (a *{{ .fname }}Factory) Inc(object {{ .ename }}Item) {
147-
a.Add(1, object)
148-
}
63+
type {{ .ename }}Input struct {
64+
{{- range .mapKeys }}
65+
{{ . }} {{ index $root.mapTypes . }} `json:"{{ index $root.mapIKeys . }}"`
14966
{{- end }}
150-
151-
func (a *{{ .fname }}Factory) CollectMetrics(in metrics.PushMetric) {
152-
a.lock.RLock()
153-
defer a.lock.RUnlock()
154-
155-
for k, v := range a.items {
156-
in.Push({{ .fname }}.{{ .type }}(v{{- range .mapKeys }}, k.{{ . }}{{- end }}))
157-
}
15867
}
15968

160-
func (a *{{ .fname }}Factory) CollectDescriptions(in metrics.PushDescription) {
161-
in.Push({{ .fname }})
69+
func (i {{ .ename }}Input) {{ .type }}(value float64) metrics.Metric {
70+
return {{ .ename }}{{ .type }}(value {{- range .mapKeys }}, i.{{ . }}{{- end }})
16271
}
16372

164-
type {{ .fname }}Items map[{{ .ename }}Item]float64
73+
func (i {{ .ename }}Input) Desc() metrics.Description {
74+
return {{ .ename }}()
75+
}
16576

166-
type {{ .ename }}Item struct {
167-
{{- range .mapKeys }}
168-
{{ . }} {{ index $root.mapTypes . }}
169-
{{- end }}
77+
func {{ .ename }}() metrics.Description {
78+
return {{ .fname }}
17079
}
171-
{{- else }}
17280

17381
func {{ .ename }}{{ .type }}({{ .fparams }}) metrics.Metric {
17482
return {{ .ename }}().{{ .type }}({{ .fkeys }})
17583
}
176-
{{- end }}

internal/metrics.item.go_test.tmpl

+61-16
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,24 @@ package metric_descriptions
2323

2424
import (
2525
"testing"
26-
{{- if .global }}
2726

2827
"github.com/stretchr/testify/require"
29-
{{- end }}
3028
)
3129

3230
func Test_{{ .ename }}_Descriptor(t *testing.T) {
3331
{{ .ename }}()
3432
}
3533

36-
{{- if .global }}
37-
38-
func Test_{{ .ename }}_Global(t *testing.T) {
39-
global := Get{{ .ename }}Factory()
34+
func Test_{{ .ename }}_Factory(t *testing.T) {
35+
global := New{{ .ename }}{{ .type }}Factory()
4036

41-
object1 := {{ .ename }}Item{
37+
object1 := {{ .ename }}Input{
4238
{{- range $i, $field := .mapKeys }}
4339
{{ $field }}: "1",
4440
{{- end }}
4541
}
4642

47-
object2 := {{ .ename }}Item{
43+
object2 := {{ .ename }}Input{
4844
{{- range $i, $field := .mapKeys }}
4945
{{ $field }}: "2",
5046
{{- end }}
@@ -60,7 +56,7 @@ func Test_{{ .ename }}_Global(t *testing.T) {
6056
})
6157

6258
t.Run("Add", func(t *testing.T) {
63-
global.Add(10, object1)
59+
global.Add(object1, 10)
6460

6561
require.EqualValues(t, 10, global.Get(object1))
6662
require.EqualValues(t, 0, global.Get(object2))
@@ -71,7 +67,7 @@ func Test_{{ .ename }}_Global(t *testing.T) {
7167
})
7268

7369
t.Run("Add", func(t *testing.T) {
74-
global.Add(3, object2)
70+
global.Add(object2, 3)
7571

7672
require.EqualValues(t, 10, global.Get(object1))
7773
require.EqualValues(t, 3, global.Get(object2))
@@ -82,7 +78,7 @@ func Test_{{ .ename }}_Global(t *testing.T) {
8278
})
8379

8480
t.Run("Dec", func(t *testing.T) {
85-
global.Add(-1, object1)
81+
global.Add(object1, -1)
8682

8783
require.EqualValues(t, 9, global.Get(object1))
8884
require.EqualValues(t, 3, global.Get(object2))
@@ -127,16 +123,16 @@ func Test_{{ .ename }}_Global(t *testing.T) {
127123
}
128124
{{- if eq .type "Counter" }}
129125

130-
func Test_{{ .ename }}_Global_Counter(t *testing.T) {
131-
global := Get{{ .ename }}Factory()
126+
func Test_{{ .ename }}_Factory_Counter(t *testing.T) {
127+
global := New{{ .ename }}{{ .type }}Factory()
132128

133-
object1 := {{ .ename }}Item{
129+
object1 := {{ .ename }}Input{
134130
{{- range $i, $field := .mapKeys }}
135131
{{ $field }}: "1",
136132
{{- end }}
137133
}
138134

139-
object2 := {{ .ename }}Item{
135+
object2 := {{ .ename }}Input{
140136
{{- range $i, $field := .mapKeys }}
141137
{{ $field }}: "2",
142138
{{- end }}
@@ -152,7 +148,7 @@ func Test_{{ .ename }}_Global_Counter(t *testing.T) {
152148
})
153149

154150
t.Run("Add", func(t *testing.T) {
155-
global.Add(10, object1)
151+
global.Add(object1, 10)
156152

157153
require.EqualValues(t, 10, global.Get(object1))
158154
require.EqualValues(t, 0, global.Get(object2))
@@ -175,5 +171,54 @@ func Test_{{ .ename }}_Global_Counter(t *testing.T) {
175171
})
176172
}
177173
{{- end }}
174+
{{- if eq .type "Gauge" }}
175+
176+
func Test_{{ .ename }}_Factory_Gauge(t *testing.T) {
177+
global := New{{ .ename }}{{ .type }}Factory()
178178

179+
object1 := {{ .ename }}Input{
180+
{{- range $i, $field := .mapKeys }}
181+
{{ $field }}: "1",
179182
{{- end }}
183+
}
184+
185+
object2 := {{ .ename }}Input{
186+
{{- range $i, $field := .mapKeys }}
187+
{{ $field }}: "2",
188+
{{- end }}
189+
}
190+
191+
t.Run("List", func(t *testing.T) {
192+
require.Len(t, global.Items(), 0)
193+
})
194+
195+
t.Run("Precheck", func(t *testing.T) {
196+
require.EqualValues(t, 0, global.Get(object1))
197+
require.EqualValues(t, 0, global.Get(object2))
198+
})
199+
200+
t.Run("Add", func(t *testing.T) {
201+
global.Add(object1, 10)
202+
203+
require.EqualValues(t, 10, global.Get(object1))
204+
require.EqualValues(t, 0, global.Get(object2))
205+
})
206+
207+
t.Run("List", func(t *testing.T) {
208+
require.Len(t, global.Items(), 1)
209+
})
210+
211+
t.Run("Set", func(t *testing.T) {
212+
global.Set(object1, 3)
213+
global.Set(object2, 1)
214+
215+
require.EqualValues(t, 3, global.Get(object1))
216+
require.EqualValues(t, 1, global.Get(object2))
217+
})
218+
219+
t.Run("List", func(t *testing.T) {
220+
require.Len(t, global.Items(), 2)
221+
})
222+
}
223+
{{- end }}
224+

0 commit comments

Comments
 (0)