-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.go
More file actions
221 lines (178 loc) · 5.12 KB
/
Copy pathvector.go
File metadata and controls
221 lines (178 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright (c) 2017 Roland Rifandi Utama
//
// This software may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// Package dynamicvector give flexibility to add any number of labels into
// prometheus Vector. It also give a feature to expire old metrics that
// never been updated.
package dynamicvector
import (
"fmt"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
)
// Metric is an interface that encapsulate prometheus.Metric interface
type Metric interface {
prometheus.Metric
// LastEdit return last time metric is edited
LastEdit() time.Time
}
// Vector is a dynamicvector that used to keep metrics.
type Vector struct {
opts Opts // vector options
constructor func(vec *Vector, labelValues []string) Metric // constructor to make new metric
mtx sync.RWMutex
labels *Labels // Labels contain information about metric labels.
pseudoLength int // it used when resetting vector that already exceed max length.
metrics map[uint64]Metric // vector metric
desc *prometheus.Desc
}
// NewVector will create new vector with specified option and metric constructor.
func NewVector(opts Opts, cons func(v *Vector, labelValues []string) Metric) *Vector {
vec := &Vector{
opts: opts,
constructor: cons,
}
vec.reset()
return vec
}
// GetMetricWith returns the Metric for the given Labels map (the label names must match those of
// the VariableLabels in Desc). If that label map is accessed for the first time, a new Metric is created.
// Return error if maxLen is exceeded.
func (v *Vector) GetMetricWith(labels prometheus.Labels) (prometheus.Metric, error) {
v.mtx.RLock()
metric := v.get(labels)
v.mtx.RUnlock()
if metric != nil {
return metric, nil
}
v.mtx.Lock()
defer v.mtx.Unlock()
metric = v.get(labels)
if metric != nil {
return metric, nil
}
if v.exceedMaxLength() {
return nil, fmt.Errorf("vector with %s exceed length limit", v.desc.String())
}
return v.create(labels), nil
}
// With behave like GetMetricWith except it will panic instead when there is an error.
func (v *Vector) With(l prometheus.Labels) prometheus.Metric {
m, err := v.GetMetricWith(l)
if err != nil {
panic(err)
}
return m
}
// Length will return number of metrics in this vector.
func (v *Vector) Length() int {
if v.pseudoLength > 0 {
return v.pseudoLength
} else {
return len(v.metrics)
}
}
// Reset will delete all metrics in vector.
func (v *Vector) Reset() {
v.mtx.Lock()
defer v.mtx.Unlock()
v.reset()
}
// Delete will delete metric that have exact match labels from vector.
func (v *Vector) Delete(l prometheus.Labels) bool {
v.mtx.Lock()
defer v.mtx.Unlock()
if !v.labels.Include(l) {
return false
}
h := v.labels.Hash(l)
_, found := v.metrics[h]
delete(v.metrics, h)
return found
}
// Collect implement prometheus.Collector.
func (v *Vector) Collect(ch chan<- prometheus.Metric) {
v.mtx.RLock()
defer v.mtx.RUnlock()
if v.exceedMaxLength() {
return
}
for _, m := range v.metrics {
if !v.isExpire(m.LastEdit()) {
ch <- m
}
}
}
// Describe implement prometheus.Collector.
func (v *Vector) Describe(ch chan<- *prometheus.Desc) {
v.mtx.RLock()
defer v.mtx.RUnlock()
ch <- v.desc
}
// GC will do housekeeping work related to this metrics and return
// number of metrics that is deleted. Currently there are two things that this method do.
// First, delete all expired metrics. Second, delete all metrics for vector that exceed MaxLength.
func (v *Vector) GC() GCStat {
var stat GCStat
v.mtx.Lock()
defer v.mtx.Unlock()
// delete expired metrics
for h, m := range v.metrics {
if v.isExpire(m.LastEdit()) {
delete(v.metrics, h)
stat.Deleted++
}
}
// delete all metrics for vector that exceed MaxLength
if v.exceedMaxLength() {
v.pseudoLength = v.Length()
v.reset()
stat.Deleted = stat.Deleted + v.pseudoLength
stat.LimitExceeded = true
}
return stat
}
func (v *Vector) get(l prometheus.Labels) prometheus.Metric {
if !v.labels.Include(l) {
return nil
}
return v.metrics[v.labels.Hash(l)]
}
func (v *Vector) create(l prometheus.Labels) prometheus.Metric {
oldLen := len(v.labels.Keys)
labelValues := v.labels.PromLabelsToValues(l)
if oldLen != len(v.labels.Keys) {
v.desc = v.newDesc()
}
metric := v.constructor(v, labelValues)
v.metrics[v.labels.Hash(l)] = metric
return metric
}
func (v *Vector) reset() {
v.metrics = make(map[uint64]Metric)
v.labels = NewLabels(v.opts.ConstLabels)
v.desc = v.newDesc()
}
func (v *Vector) exceedMaxLength() bool {
return v.opts.MaxLength > 0 && v.Length() > v.opts.MaxLength
}
func (v *Vector) isExpire(lastEdit time.Time) bool {
return v.opts.Expire != 0 && time.Since(lastEdit) > v.opts.Expire
}
func (v *Vector) newDesc() *prometheus.Desc {
return prometheus.NewDesc(
prometheus.BuildFQName(v.opts.Namespace, v.opts.Subsystem, v.opts.Name),
v.opts.Help,
v.labels.Keys,
v.opts.ConstLabels,
)
}
// GCStat is status for garbage collector.
type GCStat struct {
// Number of deleted metrics
Deleted int
// Whether metric exceed limit or not.
LimitExceeded bool
}