Skip to content

Commit

Permalink
[Improvement] Improve metric handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ajanikow committed Sep 19, 2024
1 parent ae663ea commit 541006c
Show file tree
Hide file tree
Showing 83 changed files with 6,045 additions and 345 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- (Bugfix) Versioning Alignment
- (Feature) (Scheduler) Merge Strategy
- (Feature) (Networking) Endpoints Destination
- (Improvement) Improve Metrics Handling

## [1.2.42](https://github.com/arangodb/kube-arangodb/tree/1.2.42) (2024-07-23)
- (Maintenance) Go 1.22.4 & Kubernetes 1.29.6 libraries
Expand Down
70 changes: 36 additions & 34 deletions docs/generated/metrics/README.md

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions docs/generated/metrics/arangodb_operator_objects_processed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
layout: page
title: arangodb_operator_objects_processed
parent: List of available metrics
---

# arangodb_operator_objects_processed (Counter)

## Description

Number of the processed objects

## Labels

| Label | Description |
|:-------------:|:--------------|
| operator_name | Operator Name |
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
layout: page
title: arangodb_resources_deployment_config_map_duration
parent: List of available metrics
---

# arangodb_resources_deployment_config_map_duration (Gauge)

## Description

Duration of inspected ConfigMaps by Deployment in seconds

## Labels

| Label | Description |
|:----------:|:----------------|
| deployment | Deployment Name |
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
layout: page
title: arangodb_resources_deployment_config_map_inspected
parent: List of available metrics
---

# arangodb_resources_deployment_config_map_inspected (Counter)

## Description

Number of inspected ConfigMaps by Deployment

## Labels

| Label | Description |
|:----------:|:----------------|
| deployment | Deployment Name |
2 changes: 1 addition & 1 deletion internal/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,12 @@ func generateMetricsGO(root string, in MetricsDoc) error {
"fname": strings.Join(fnameParts, ""),
"ename": strings.Join(tparts, ""),
"shortDescription": details.ShortDescription,
"global": details.Global,
"labels": generateLabels(details.Labels),
"type": details.Type,
"mapTypes": mapTypes,
"mapKeys": mapKeys,
"mapIKeys": mapIKeys,
"global": details.Global,
"args": strings.Join(params[1:], ", "),
"fparams": strings.Join(params, ", "),
"fkeys": strings.Join(keys, ", "),
Expand Down
141 changes: 24 additions & 117 deletions internal/metrics.item.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,154 +23,61 @@ package metric_descriptions

import (
"github.com/arangodb/kube-arangodb/pkg/util/metrics"
{{- if .global }}

"sync"
{{- end }}
)

var (
{{ .fname }} = metrics.NewDescription("{{ .name }}", "{{ .shortDescription }}", {{ .labels }}, nil)
{{- if .global }}

// Global Fields
global{{ .ename }}{{ .type }} = New{{ .ename }}{{ .type }}Factory()
{{- end }}
)

func init() {
registerDescription({{ .fname }})
{{- if .global }}
registerCollector({{ .fname }}Global)
registerCollector(global{{ .ename }}{{ .type }})
{{- end }}
}

func {{ .ename }}() metrics.Description {
return {{ .fname }}
}

{{- if .global }}

func {{ .ename }}Get({{ .args }}) float64 {
return {{ .fname }}Global.Get({{ .ename }}Item{
{{- range $i, $field := .mapKeys }}
{{ $field }}: {{ index $root.mapIKeys $field }},
{{- end }}
})
}

func {{ .ename }}Add({{ .fparams }}) {
{{ .fname }}Global.Add(value, {{ .ename }}Item{
{{- range $i, $field := .mapKeys }}
{{ $field }}: {{ index $root.mapIKeys $field }},
{{- end }}
})
}
{{- if eq .type "Counter" }}

func {{ .ename }}Inc({{ .args }}) {
{{ .fname }}Global.Inc({{ .ename }}Item{
{{- range $i, $field := .mapKeys }}
{{ $field }}: {{ index $root.mapIKeys $field }},
{{- end }}
})
func Global{{ .ename }}{{ .type }}() metrics.Factory{{ .type }}[{{ .ename }}Input] {
return global{{ .ename }}{{ .type }}
}
{{- end }}

func Get{{ .ename }}Factory() {{ .ename }}Factory {
return {{ .fname }}Global
}

var {{ .fname }}Global = &{{ .fname }}Factory{
items: {{ .fname }}Items{},
func New{{ .ename }}{{ .type }}Factory() metrics.Factory{{ .type }}[{{ .ename }}Input] {
return metrics.NewFactory{{ .type }}[{{ .ename }}Input]()
}

type {{ .ename }}Factory interface {
Get(object {{ .ename }}Item) float64
Add(value float64, object {{ .ename }}Item)
Remove(object {{ .ename }}Item)
Items() []{{ .ename }}Item
{{- if eq .type "Counter" }}

Inc(object {{ .ename }}Item)
func New{{ .ename }}Input({{- range $i, $e := .mapKeys }}{{ if $i }}, {{ end }}{{ index $root.mapIKeys . }} {{ index $root.mapTypes . }}{{- end }}) {{ .ename }}Input {
return {{ .ename }}Input{
{{- range .mapKeys }}
{{ . }}: {{ index $root.mapIKeys . }},
{{- end }}
}

type {{ .fname }}Factory struct {
lock sync.RWMutex

items {{ .fname }}Items
}

func (a *{{ .fname }}Factory) Get(object {{ .ename }}Item) float64 {
a.lock.Lock()
defer a.lock.Unlock()

v, ok := a.items[object]
if !ok {
return 0
}

return v
}

func (a *{{ .fname }}Factory) Add(value float64, object {{ .ename }}Item) {
a.lock.Lock()
defer a.lock.Unlock()

v, ok := a.items[object]
if !ok {
a.items[object] = value
return
}

a.items[object] = value + v
}

func (a *{{ .fname }}Factory) Remove(obj {{ .ename }}Item) {
a.lock.Lock()
defer a.lock.Unlock()

delete(a.items, obj)
}

func (a *{{ .fname }}Factory) Items() []{{ .ename }}Item {
a.lock.Lock()
defer a.lock.Unlock()

var r = make([]{{ .ename }}Item, 0, len(a.items))

for k := range a.items {
r = append(r, k)
}

return r
}
{{- if eq .type "Counter" }}

func (a *{{ .fname }}Factory) Inc(object {{ .ename }}Item) {
a.Add(1, object)
}
type {{ .ename }}Input struct {
{{- range .mapKeys }}
{{ . }} {{ index $root.mapTypes . }} `json:"{{ index $root.mapIKeys . }}"`
{{- end }}

func (a *{{ .fname }}Factory) CollectMetrics(in metrics.PushMetric) {
a.lock.RLock()
defer a.lock.RUnlock()

for k, v := range a.items {
in.Push({{ .fname }}.{{ .type }}(v{{- range .mapKeys }}, k.{{ . }}{{- end }}))
}
}

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

type {{ .fname }}Items map[{{ .ename }}Item]float64
func (i {{ .ename }}Input) Desc() metrics.Description {
return {{ .ename }}()
}

type {{ .ename }}Item struct {
{{- range .mapKeys }}
{{ . }} {{ index $root.mapTypes . }}
{{- end }}
func {{ .ename }}() metrics.Description {
return {{ .fname }}
}
{{- else }}

func {{ .ename }}{{ .type }}({{ .fparams }}) metrics.Metric {
return {{ .ename }}().{{ .type }}({{ .fkeys }})
}
{{- end }}
77 changes: 61 additions & 16 deletions internal/metrics.item.go_test.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,24 @@ package metric_descriptions

import (
"testing"
{{- if .global }}

"github.com/stretchr/testify/require"
{{- end }}
)

func Test_{{ .ename }}_Descriptor(t *testing.T) {
{{ .ename }}()
}

{{- if .global }}

func Test_{{ .ename }}_Global(t *testing.T) {
global := Get{{ .ename }}Factory()
func Test_{{ .ename }}_Factory(t *testing.T) {
global := New{{ .ename }}{{ .type }}Factory()

object1 := {{ .ename }}Item{
object1 := {{ .ename }}Input{
{{- range $i, $field := .mapKeys }}
{{ $field }}: "1",
{{- end }}
}

object2 := {{ .ename }}Item{
object2 := {{ .ename }}Input{
{{- range $i, $field := .mapKeys }}
{{ $field }}: "2",
{{- end }}
Expand All @@ -60,7 +56,7 @@ func Test_{{ .ename }}_Global(t *testing.T) {
})

t.Run("Add", func(t *testing.T) {
global.Add(10, object1)
global.Add(object1, 10)

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

t.Run("Add", func(t *testing.T) {
global.Add(3, object2)
global.Add(object2, 3)

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

t.Run("Dec", func(t *testing.T) {
global.Add(-1, object1)
global.Add(object1, -1)

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

func Test_{{ .ename }}_Global_Counter(t *testing.T) {
global := Get{{ .ename }}Factory()
func Test_{{ .ename }}_Factory_Counter(t *testing.T) {
global := New{{ .ename }}{{ .type }}Factory()

object1 := {{ .ename }}Item{
object1 := {{ .ename }}Input{
{{- range $i, $field := .mapKeys }}
{{ $field }}: "1",
{{- end }}
}

object2 := {{ .ename }}Item{
object2 := {{ .ename }}Input{
{{- range $i, $field := .mapKeys }}
{{ $field }}: "2",
{{- end }}
Expand All @@ -152,7 +148,7 @@ func Test_{{ .ename }}_Global_Counter(t *testing.T) {
})

t.Run("Add", func(t *testing.T) {
global.Add(10, object1)
global.Add(object1, 10)

require.EqualValues(t, 10, global.Get(object1))
require.EqualValues(t, 0, global.Get(object2))
Expand All @@ -175,5 +171,54 @@ func Test_{{ .ename }}_Global_Counter(t *testing.T) {
})
}
{{- end }}
{{- if eq .type "Gauge" }}

func Test_{{ .ename }}_Factory_Gauge(t *testing.T) {
global := New{{ .ename }}{{ .type }}Factory()

object1 := {{ .ename }}Input{
{{- range $i, $field := .mapKeys }}
{{ $field }}: "1",
{{- end }}
}

object2 := {{ .ename }}Input{
{{- range $i, $field := .mapKeys }}
{{ $field }}: "2",
{{- end }}
}

t.Run("List", func(t *testing.T) {
require.Len(t, global.Items(), 0)
})

t.Run("Precheck", func(t *testing.T) {
require.EqualValues(t, 0, global.Get(object1))
require.EqualValues(t, 0, global.Get(object2))
})

t.Run("Add", func(t *testing.T) {
global.Add(object1, 10)

require.EqualValues(t, 10, global.Get(object1))
require.EqualValues(t, 0, global.Get(object2))
})

t.Run("List", func(t *testing.T) {
require.Len(t, global.Items(), 1)
})

t.Run("Set", func(t *testing.T) {
global.Set(object1, 3)
global.Set(object2, 1)

require.EqualValues(t, 3, global.Get(object1))
require.EqualValues(t, 1, global.Get(object2))
})

t.Run("List", func(t *testing.T) {
require.Len(t, global.Items(), 2)
})
}
{{- end }}

Loading

0 comments on commit 541006c

Please sign in to comment.