Skip to content

Commit d34fe69

Browse files
committed
[Feature] Global Metrics
1 parent 3803488 commit d34fe69

File tree

43 files changed

+440
-50
lines changed

Some content is hidden

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

43 files changed

+440
-50
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- (Feature) (ML) Unify Integration Sidecar
1818
- (Feature) (Analytics) Metadata
1919
- (Feature) (Analytics) StatefulSet
20+
- (Feature) Global Metrics
2021

2122
## [1.2.40](https://github.com/arangodb/kube-arangodb/tree/1.2.40) (2024-04-10)
2223
- (Feature) Add Core fields to the Scheduler Container Spec

docs/generated/metrics/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ has_toc: false
1414

1515
| Name | Namespace | Group | Type | Description |
1616
|:-------------------------------------------------------------------------------------------------------------------------------------:|:-----------------:|:-----------------:|:-------:|:--------------------------------------------------------------------------------------|
17+
| [arango_operator_objects_processed](./arango_operator_objects_processed.md) | arango_operator | objects | Counter | Number of the processed objects |
1718
| [arangodb_operator_agency_errors](./arangodb_operator_agency_errors.md) | arangodb_operator | agency | Counter | Current count of agency cache fetch errors |
1819
| [arangodb_operator_agency_fetches](./arangodb_operator_agency_fetches.md) | arangodb_operator | agency | Counter | Current count of agency cache fetches |
1920
| [arangodb_operator_agency_index](./arangodb_operator_agency_index.md) | arangodb_operator | agency | Gauge | Current index of the agency cache |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
layout: page
3+
title: arango_operator_objects_processed
4+
parent: List of available metrics
5+
---
6+
7+
# arango_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 |

internal/metrics.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2016-2024 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -98,6 +98,8 @@ type Metric struct {
9898
Type string `json:"type" yaml:"type"`
9999
ShortDescription string `json:"shortDescription" yaml:"shortDescription"`
100100

101+
Global bool `json:"global" yaml:"global"`
102+
101103
Labels []Label `json:"labels" yaml:"labels"`
102104
AlertingRules []Alerting `json:"alertingRules" yaml:"alertingRules"`
103105
}
@@ -311,6 +313,10 @@ func generateMetricsGO(root string, in MetricsDoc) error {
311313
params = append(params, "value float64")
312314
keys = append(keys, "value")
313315

316+
var mapTypes = map[string]string{}
317+
var mapKeys []string
318+
var mapIKeys = map[string]string{}
319+
314320
for _, label := range details.Labels {
315321
v := strings.Split(strings.ToLower(label.Key), "_")
316322
for id := range v {
@@ -323,12 +329,21 @@ func generateMetricsGO(root string, in MetricsDoc) error {
323329

324330
k := strings.Join(v, "")
325331

332+
v[0] = strings.Title(v[0])
333+
334+
kPublic := strings.Join(v, "")
335+
326336
keys = append(keys, k)
337+
mapKeys = append(mapKeys, kPublic)
338+
339+
mapIKeys[kPublic] = k
327340

328341
if t := label.Type; t != nil {
329342
params = append(params, fmt.Sprintf("%s %s", k, *t))
343+
mapTypes[kPublic] = *t
330344
} else {
331345
params = append(params, fmt.Sprintf("%s string", k))
346+
mapTypes[kPublic] = "string"
332347
}
333348
}
334349

@@ -337,8 +352,13 @@ func generateMetricsGO(root string, in MetricsDoc) error {
337352
"fname": strings.Join(fnameParts, ""),
338353
"ename": strings.Join(tparts, ""),
339354
"shortDescription": details.ShortDescription,
355+
"global": details.Global,
340356
"labels": generateLabels(details.Labels),
341357
"type": details.Type,
358+
"mapTypes": mapTypes,
359+
"mapKeys": mapKeys,
360+
"mapIKeys": mapIKeys,
361+
"args": strings.Join(params[1:], ", "),
342362
"fparams": strings.Join(params, ", "),
343363
"fkeys": strings.Join(keys, ", "),
344364
}); err != nil {

internal/metrics.go.tmpl

+23
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import (
2828
var (
2929
descriptions []metrics.Description
3030
descriptionsLock sync.Mutex
31+
32+
collectors []metrics.Collector
33+
collectorsLock sync.Mutex
3134
)
3235

3336
func registerDescription( d ... metrics.Description) {
@@ -41,9 +44,29 @@ func registerDescription( d ... metrics.Description) {
4144
descriptions = append(descriptions, d...)
4245
}
4346

47+
func registerCollector( d ... metrics.Collector) {
48+
if len(d) == 0 {
49+
return
50+
}
51+
52+
collectorsLock.Lock()
53+
defer collectorsLock.Unlock()
54+
55+
collectors = append(collectors, d...)
56+
}
57+
4458
func Descriptions (c metrics.PushDescription) {
4559
descriptionsLock.Lock()
4660
defer descriptionsLock.Unlock()
4761

4862
c.Push(descriptions...)
63+
}
64+
65+
func Collectors (c metrics.PushMetric) {
66+
collectorsLock.Lock()
67+
defer collectorsLock.Unlock()
68+
69+
for _, collector := range collectors {
70+
collector.CollectMetrics(c)
71+
}
4972
}

internal/metrics.item.go.tmpl

+118-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{{- $root := . -}}
12
//
23
// DISCLAIMER
34
//
@@ -20,20 +21,135 @@
2021

2122
package metric_descriptions
2223

23-
import "github.com/arangodb/kube-arangodb/pkg/util/metrics"
24+
import (
25+
"github.com/arangodb/kube-arangodb/pkg/util/metrics"
26+
{{- if .global }}
27+
28+
"sync"
29+
{{- end }}
30+
)
2431

2532
var (
2633
{{ .fname }} = metrics.NewDescription("{{ .name }}", "{{ .shortDescription }}", {{ .labels }}, nil)
2734
)
2835

2936
func init() {
3037
registerDescription({{ .fname }})
38+
{{- if .global }}
39+
registerCollector({{ .fname }}Global)
40+
{{- end }}
41+
}
42+
43+
{{- if .global }}
44+
45+
func {{ .ename }}Add({{ .fparams }}) {
46+
{{ .fname }}Global.Add(value, {{ .ename }}Item{
47+
{{- range $i, $field := .mapKeys }}
48+
{{ $field }}: {{ index $root.mapIKeys $field }},
49+
{{- end }}
50+
})
51+
}
52+
{{- if eq .type "Counter" }}
53+
54+
func {{ .ename }}Inc({{ .args }}) {
55+
{{ .fname }}Global.Inc({{ .ename }}Item{
56+
{{- range $i, $field := .mapKeys }}
57+
{{ $field }}: {{ index $root.mapIKeys $field }},
58+
{{- end }}
59+
})
60+
}
61+
{{- end }}
62+
63+
func Get{{ .ename }}Factory() {{ .ename }}Factory {
64+
return {{ .fname }}Global
65+
}
66+
67+
var {{ .fname }}Global = &{{ .fname }}Factory{
68+
items: {{ .fname }}Items{},
69+
}
70+
71+
type {{ .ename }}Factory interface {
72+
Add(value float64, object {{ .ename }}Item)
73+
Remove(object {{ .ename }}Item)
74+
Items() []{{ .ename }}Item
75+
{{- if eq .type "Counter" }}
76+
77+
Inc(object {{ .ename }}Item)
78+
{{- end }}
79+
}
80+
81+
type {{ .fname }}Factory struct {
82+
lock sync.RWMutex
83+
84+
items {{ .fname }}Items
85+
}
86+
87+
func (a *{{ .fname }}Factory) Add(value float64, object {{ .ename }}Item) {
88+
a.lock.Lock()
89+
defer a.lock.Unlock()
90+
91+
v, ok := a.items[object]
92+
if !ok {
93+
a.items[object] = value
94+
return
95+
}
96+
97+
a.items[object] = value + v
98+
}
99+
100+
func (a *{{ .fname }}Factory) Remove(obj {{ .ename }}Item) {
101+
a.lock.Lock()
102+
defer a.lock.Unlock()
103+
104+
delete(a.items, obj)
105+
}
106+
107+
func (a *{{ .fname }}Factory) Items() []{{ .ename }}Item {
108+
a.lock.Lock()
109+
defer a.lock.Unlock()
110+
111+
var r = make([]{{ .ename }}Item, len(a.items))
112+
113+
for k := range a.items {
114+
r = append(r, k)
115+
}
116+
117+
return r
118+
}
119+
{{- if eq .type "Counter" }}
120+
121+
func (a *{{ .fname }}Factory) Inc(object {{ .ename }}Item) {
122+
a.Add(1, object)
123+
}
124+
{{- end }}
125+
126+
func (a *{{ .fname }}Factory) CollectMetrics(in metrics.PushMetric) {
127+
a.lock.RLock()
128+
defer a.lock.RUnlock()
129+
130+
for k, v := range a.items {
131+
in.Push({{ .fname }}.{{ .type }}(v{{- range .mapKeys }}, k.{{ . }}{{- end }}))
132+
}
133+
}
134+
135+
func (a *{{ .fname }}Factory) CollectDescriptions(in metrics.PushDescription) {
136+
in.Push({{ .fname }})
31137
}
32138

139+
type {{ .fname }}Items map[{{ .ename }}Item]float64
140+
141+
type {{ .ename }}Item struct {
142+
{{- range .mapKeys }}
143+
{{ . }} {{ index $root.mapTypes . }}
144+
{{- end }}
145+
}
146+
{{- else }}
147+
33148
func {{ .ename }}() metrics.Description {
34149
return {{ .fname }}
35150
}
36151

37152
func {{ .ename }}{{ .type }}({{ .fparams }}) metrics.Metric {
38153
return {{ .ename }}().{{ .type }}({{ .fkeys }})
39-
}
154+
}
155+
{{- end }}

internal/metrics.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
documentation: docs/generated/metrics
22
destination: pkg/generated/metric_descriptions
33
namespaces:
4+
arango_operator:
5+
objects:
6+
processed:
7+
shortDescription: "Number of the processed objects"
8+
description: "Number of the processed objects"
9+
type: "Counter"
10+
global: true
11+
labels:
12+
- key: operator_name
13+
description: "Operator Name"
414
arangodb_operator:
515
agency_cache:
616
present:

pkg/deployment/old_metrics.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2016-2024 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -71,6 +71,8 @@ func (i *inventory) CollectDescriptions(in metrics.PushDescription) {
7171
}
7272

7373
func (i *inventory) CollectMetrics(in metrics.PushMetric) {
74+
metric_descriptions.Collectors(in)
75+
7476
for _, deployments := range i.deployments {
7577
for _, deployment := range deployments {
7678
in.Push(i.deploymentsMetric.Gauge(1, deployment.GetNamespace(), deployment.GetName()))

0 commit comments

Comments
 (0)