forked from bukalapak/prometheus-aggregator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.go
107 lines (82 loc) · 2.79 KB
/
model.go
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
package main
import (
"time"
"github.com/prometheus/client_golang/prometheus"
)
type sampleHasherFunc func(*sample) []byte
// sampleHasher is a hashing function used on samples.
var sampleHasher sampleHasherFunc
type sampleKind string
const (
sampleUnknown sampleKind = ""
// sampleCounter represents a counter
sampleCounter sampleKind = "c"
// sampleGauge represents a gauge
sampleGauge sampleKind = "g"
// sampleHistogram represents histogram
sampleHistogram sampleKind = "h"
// sampleHistogramLinear represents histogram with linearly spaced buckets.
// See Prometheus Go client LinearBuckets for details.
sampleHistogramLinear sampleKind = "hl"
)
// sample represents single measurement submitted to the system.
// Samples are converted to metrics by collector.
type sample struct {
// name is used to represent sample. It's used as metric name in export to prometheus.
name string
// kind of the sample wen mapped to prometheus metric type
kind sampleKind
// labels is a set of string pairs mapped to prometheus LabelPairs type
labels map[string]string
// value of the sample
value float64
// histogramDef is a set of values used in mapping for the histogram types
histogramDef []string
}
// hash calculates a hash of the sample so it can be recognized.
// Should take all elements other than value under consideration.
func (s *sample) hash() []byte {
return sampleHasher(s)
}
// UpdatingCounter wraps prometheus.Counter, adding last update time.
type UpdatingCounter struct {
Counter prometheus.Counter
UpdatedAt time.Time
}
// NewUpdatingCounter creates new instance of UpdatingCounter, with UpdatedAt
// set to creation time.
func NewUpdatingCounter(c prometheus.Counter) *UpdatingCounter {
return &UpdatingCounter{c, time.Now()}
}
// Touch updates UpdatedAt field to current time.
func (u *UpdatingCounter) Touch() {
u.UpdatedAt = time.Now()
}
// UpdatingGauge wraps prometheus.Gauge, adding last update time.
type UpdatingGauge struct {
Gauge prometheus.Gauge
UpdatedAt time.Time
}
// NewUpdatingGauge creates new instance of UpdatingGauge, with UpdatedAt
// set to creation time.
func NewUpdatingGauge(c prometheus.Gauge) *UpdatingGauge {
return &UpdatingGauge{c, time.Now()}
}
// Touch updates UpdatedAt field to current time.
func (u *UpdatingGauge) Touch() {
u.UpdatedAt = time.Now()
}
// UpdatingHistogram wraps prometheus.Histogram, adding last update time.
type UpdatingHistogram struct {
Histogram prometheus.Histogram
UpdatedAt time.Time
}
// NewUpdatingHistogram creates new instance of UpdatingHistogram, with UpdatedAt
// set to creation time.
func NewUpdatingHistogram(c prometheus.Histogram) *UpdatingHistogram {
return &UpdatingHistogram{c, time.Now()}
}
// Touch updates UpdatedAt field to current time.
func (u *UpdatingHistogram) Touch() {
u.UpdatedAt = time.Now()
}