-
Notifications
You must be signed in to change notification settings - Fork 2
/
interface.go
153 lines (135 loc) · 3.34 KB
/
interface.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
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
package collector
import (
"context"
"time"
"github.com/JulienBalestra/monitoring/pkg/datadog"
"github.com/JulienBalestra/monitoring/pkg/metrics"
"github.com/JulienBalestra/monitoring/pkg/tagger"
"go.uber.org/zap"
)
const (
collectorMetricPrefix = "collector."
)
type Config struct {
MetricsClient *datadog.Client
Tagger *tagger.Tagger
Host string
CollectInterval time.Duration
Options map[string]string
Tags []string
}
type Collector interface {
Config() *Config
Collect(context.Context) error
Name() string
IsDaemon() bool
DefaultOptions() map[string]string
DefaultCollectInterval() time.Duration
DefaultTags() []string
Tags() []string
SubmittedSeries() float64
}
func WithDefaults(c Collector) Collector {
config := c.Config()
if config.CollectInterval == 0 {
config.CollectInterval = c.DefaultCollectInterval()
}
if config.Options == nil {
config.Options = c.DefaultOptions()
} else {
d := c.DefaultOptions()
for k, v := range d {
_, ok := config.Options[k]
if ok {
continue
}
config.Options[k] = v
}
}
if config.Tags == nil {
config.Tags = c.DefaultTags()
} else {
m := make(map[string]struct{}, len(config.Tags))
for _, t := range config.Tags {
m[t] = struct{}{}
}
for _, d := range c.DefaultTags() {
_, ok := m[d]
if ok {
continue
}
config.Tags = append(config.Tags, d)
}
}
return c
}
func RunCollection(ctx context.Context, c Collector) error {
config := c.Config()
zctx := zap.L().With(
zap.String("co", c.Name()),
)
extCtx := zctx.With(
zap.Duration("collectionInterval", config.CollectInterval),
zap.Any("options", config.Options),
)
if c.IsDaemon() {
extCtx.Info("collecting metrics continuously")
err := c.Collect(ctx)
if err != nil {
return err
}
return nil
}
runCollection := time.NewTicker(config.CollectInterval)
defer runCollection.Stop()
extCtx.Info("collecting metrics periodically")
collectorTag := "collector:" + c.Name()
measures := metrics.NewMeasures(config.MetricsClient.ChanSeries)
collectorMetrics := time.NewTicker(time.Minute * 5)
defer collectorMetrics.Stop()
var series, runSuccess, runErr float64
for {
select {
case <-ctx.Done():
extCtx.Info("end of collection")
return ctx.Err()
case <-collectorMetrics.C:
now := time.Now()
tags := append(config.Tagger.GetUnstable(config.Host), collectorTag)
_ = measures.Count(&metrics.Sample{
Name: collectorMetricPrefix + "series",
Value: series,
Time: now,
Host: config.Host,
Tags: tags,
})
_ = measures.Count(&metrics.Sample{
Name: collectorMetricPrefix + "collections",
Value: runSuccess,
Time: now,
Host: config.Host,
Tags: append(tags, "success:true"),
})
_ = measures.Count(&metrics.Sample{
Name: collectorMetricPrefix + "collections",
Value: runErr,
Time: now,
Host: config.Host,
Tags: append(tags, "success:false"),
})
case <-runCollection.C:
beforeCollection := c.SubmittedSeries()
// TODO observe performance
err := c.Collect(ctx)
series = c.SubmittedSeries()
collectionSeries := series - beforeCollection
if err != nil {
runErr++
extCtx.Error("failed collection", zap.Error(err), zap.Float64("series", collectionSeries))
continue
}
runSuccess++
zctx.Info("ok", zap.Uint64("s", uint64(collectionSeries)))
}
}
}