|
| 1 | +{{- $root := . -}} |
| 2 | +// |
| 3 | +// DISCLAIMER |
| 4 | +// |
| 5 | +// Copyright 2016-2024 ArangoDB GmbH, Cologne, Germany |
| 6 | +// |
| 7 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +// you may not use this file except in compliance with the License. |
| 9 | +// You may obtain a copy of the License at |
| 10 | +// |
| 11 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +// |
| 13 | +// Unless required by applicable law or agreed to in writing, software |
| 14 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +// See the License for the specific language governing permissions and |
| 17 | +// limitations under the License. |
| 18 | +// |
| 19 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 20 | +// |
| 21 | + |
| 22 | +package metric_descriptions |
| 23 | + |
| 24 | +import ( |
| 25 | + "github.com/arangodb/kube-arangodb/pkg/util/metrics" |
| 26 | +{{- if .global }} |
| 27 | + |
| 28 | + "sync" |
| 29 | +{{- end }} |
| 30 | +) |
| 31 | + |
| 32 | +var ( |
| 33 | + {{ .fname }} = metrics.NewDescription("{{ .name }}", "{{ .shortDescription }}", {{ .labels }}, nil) |
| 34 | +) |
| 35 | + |
| 36 | +func init() { |
| 37 | + 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 }}) |
| 137 | +} |
| 138 | + |
| 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 | + |
| 148 | +func {{ .ename }}() metrics.Description { |
| 149 | + return {{ .fname }} |
| 150 | +} |
| 151 | + |
| 152 | +func {{ .ename }}{{ .type }}({{ .fparams }}) metrics.Metric { |
| 153 | + return {{ .ename }}().{{ .type }}({{ .fkeys }}) |
| 154 | +} |
| 155 | +{{- end }} |
0 commit comments