-
Notifications
You must be signed in to change notification settings - Fork 1
/
collector.go
390 lines (344 loc) · 10.8 KB
/
collector.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package main
import (
"context"
"fmt"
"sync"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
dto "github.com/prometheus/client_model/go"
)
// Collector is a self-contained group of http queries and metric families to collect from results. It is
// conceptually similar to a prometheus.Collector.
type Collector interface {
// Collect is the equivalent of prometheus.Collector.Collect() but takes a context to run in and a database to run on.
Collect(context.Context, chan<- Metric, chan<- int)
SetClient(*Client)
GetClient() *Client
GetName() (id string)
GetId() (id string)
GetStatus() int
SetLogger(log.Logger)
SetSetStats(Target)
}
// collector implements Collector. It wraps a collection of queries, metrics and the database to collect them from.
type collector struct {
config *CollectorConfig
client *Client
// queries []*Query
logContext []interface{}
collect_script []*YAMLScript
// metricFamilies []*MetricFamily
status int
// to protect the data during exchange
content_mutex *sync.Mutex
logger log.Logger
}
// NewCollector returns a new Collector with the given configuration and database. The metrics it creates will all have
// the provided const labels applied.
func NewCollector(
logContext []interface{},
logger log.Logger,
cc *CollectorConfig,
constLabels []*dto.LabelPair,
collect_script []*YAMLScript) (Collector, error) {
// var mfs []*MetricFamily
logContext = append(logContext, "collector", cc.Name)
// mfs := make([]*MetricFamily,)
for _, scr := range collect_script {
for _, ma := range scr.metricsActions {
for _, act := range ma.Actions {
if act.Type() == metric_action {
mc := act.GetMetric()
if mc == nil {
return nil, fmt.Errorf("MetricAction nil received")
}
mf, err := NewMetricFamily(logContext, mc, constLabels, cc.customTemplate)
if err != nil {
return nil, err
}
// ma.metricFamilies = append(ma.metricFamilies, mf)
// mfs = append(mfs, mf)
act.SetMetricFamily(mf)
}
}
// for _, mc := range ma.GetMetrics() {
// }
}
}
// Instantiate metric families.
// for _, mc := range cc.Metrics {
// mf, err := NewMetricFamily(logContext, mc, constLabels, cc.customTemplate)
// if err != nil {
// return nil, err
// }
// mfs, found := queryMFs[mc.Query()]
// if !found {
// mfs = make([]*MetricFamily, 0, 2)
// }
// queryMFs[mc.Query()] = append(mfs, mf)
// }
// Instantiate queries.
// queries := make([]*Query, 0, len(cc.Metrics))
// for qc, mfs := range queryMFs {
// q, err := NewQuery(logContext, logger, qc, mfs...)
// if err != nil {
// return nil, err
// }
// queries = append(queries, q)
// }
c := collector{
config: cc,
// queries: queries,
logContext: logContext,
logger: logger,
// metricFamilies: mfs,
collect_script: collect_script,
content_mutex: &sync.Mutex{},
}
if c.config.MinInterval > 0 {
var logCtx []interface{}
logCtx = append(logCtx, logContext...)
logCtx = append(logCtx, "msg", fmt.Sprintf("NewCollector(): Non-zero min_interval (%s), using cached collector.", c.config.MinInterval))
level.Debug(logger).Log(logCtx...)
return newCachingCollector(&c), nil
}
return &c, nil
}
// GetClient implement GetClient for collector
// obtain pointer to client
func (c *collector) GetClient() (client *Client) {
return c.client
}
// SetClient implement SetClient for collector
// obtain pointer to client
func (c *collector) SetClient(client *Client) {
c.client = client
}
// GetId implement GetId for collector
// obtain collector id for log purpose
func (c *collector) GetId() string {
return c.config.id
}
// GetName implement GetName for collector
// obtain collector name for collector_status metric
func (c *collector) GetName() string {
return c.config.Name
}
// GetStatus implement GetStatus for collector
// obtain the status of collector scripts execution
func (c *collector) GetStatus() int {
return c.status
}
func (c *collector) SetLogger(logger log.Logger) {
c.content_mutex.Lock()
c.logger = logger
// c.client.logger = logger
c.content_mutex.Unlock()
}
// SetSetStats implements SetSetStats for collector.
// Set vars from collector symbols table into target symtab.
// Lock target during action
func (c *collector) SetSetStats(target Target) {
if len(c.collect_script) > 0 {
for _, sc := range c.collect_script {
if len(sc.setStatsActions) > 0 {
if r_setstats, ok := c.client.symtab["set_stats"]; ok {
if set_stats, ok := r_setstats.(map[string]any); ok {
target.Lock()
for key, value := range set_stats {
target.SetSymbol(key, value)
}
target.Unlock()
}
}
}
}
}
}
// type CollectContext struct {
// method func(*CallClientExecuteParams, map[string]any) error
// // ctx context.Context
// metric_ch chan<- Metric
// metricfamilies []*MetricFamily
// wake_cond *sync.Cond
// // logcontext []any
// }
// Collect implements Collector.
func (c *collector) Collect(ctx context.Context, metric_ch chan<- Metric, coll_ch chan<- int) {
var (
reset_coll_id bool = false
status int = 0
)
// var wg sync.WaitGroup
// wg.Add(len(c.queries))
// for _, q := range c.queries {
// go func(q *Query) {
// defer wg.Done()
// q.Collect(ctx, client, ch)
// }(q)
// }
// // Only return once all queries have been processed
// wg.Wait()
// use a collect context object
// cctx := &CollectContext{
// method: c.client.callClientExecute,
// // ctx: ctx,
// metric_ch: ch,
// metricfamilies: c.metricFamilies,
// wake_cond: wake_cond,
// // logcontext: c.logContext,
// }
c.client.symtab["__method"] = c.client.callClientExecute
// c.client.symtab["__context"] = ctx
c.client.symtab["__metric_channel"] = metric_ch
c.client.symtab["__coll_channel"] = coll_ch
// c.client.symtab["__metricfamilies"] = c.metricFamilies
// c.client.symtab["__wake_cond"] = wake_cond
// c.client.symtab["__logcontext"] = c.logContext
// c.client.symtab["__collect_context"] = cctx
cid := GetMapValueString(c.client.symtab, "__collector_id")
if cid == "" {
c.client.symtab["__collector_id"] = "--"
reset_coll_id = true
}
c.status = 0
status = 1
for _, scr := range c.collect_script {
level.Debug(c.logger).Log(
"collid", CollectorId(c.client.symtab, c.logger),
"msg", fmt.Sprintf("starting script '%s/%s'", c.config.Name, scr.name))
if err := scr.Play(c.client.symtab, false, c.logger); err != nil {
switch err {
case ErrInvalidLogin:
status = 2
coll_ch <- MsgLogin
case ErrContextDeadLineExceeded:
status = 3
coll_ch <- MsgTimeout
default:
level.Warn(c.logger).Log(
"collid", CollectorId(c.client.symtab, c.logger),
"script", ScriptName(c.client.symtab, c.logger),
"errmsg", err)
coll_ch <- MsgQuit
status = 0
}
break
}
}
// set collector execution status
c.status = status
// tell calling target that this collector is over.
if status != 0 {
coll_ch <- MsgDone
level.Debug(c.logger).Log(
"collid", CollectorId(c.client.symtab, c.logger),
"script", ScriptName(c.client.symtab, c.logger),
"msg", "MsgDone sent to channel.")
}
// clean up
level.Debug(c.logger).Log(
"collid", CollectorId(c.client.symtab, c.logger),
"msg", fmt.Sprintf("removing metric channel for '%s'", c.config.Name))
delete(c.client.symtab, "__metric_channel")
delete(c.client.symtab, "__coll_channel")
if reset_coll_id {
delete(c.client.symtab, "__collector_id")
}
}
// newCachingCollector returns a new Collector wrapping the provided raw Collector.
func newCachingCollector(rawColl *collector) Collector {
cc := &cachingCollector{
rawColl: rawColl,
minInterval: time.Duration(rawColl.config.MinInterval),
cacheSem: make(chan time.Time, 1),
}
cc.cacheSem <- time.Time{}
return cc
}
// Collector with a cache for collected metrics. Only used when min_interval is non-zero.
type cachingCollector struct {
// Underlying collector, which is being cached.
rawColl *collector
// Convenience copy of rawColl.config.MinInterval.
minInterval time.Duration
// Used as a non=blocking semaphore protecting the cache. The value in the channel is the time of the cached metrics.
cacheSem chan time.Time
// Metrics saved from the last Collect() call.
cache []Metric
}
// SetClient implement SetClient()for cachingCollector
func (cc *cachingCollector) SetClient(client *Client) {
cc.rawColl.client = client
}
// SetClient implement SetClient for cachingCollector
func (cc *cachingCollector) GetClient() (client *Client) {
return cc.rawColl.client
}
// GetName implement GetName for cachingCollector
// obtain collector name for collector_status metric
func (cc *cachingCollector) GetName() (id string) {
return cc.rawColl.config.Name
}
// GetId implement GetId for cachingCollector
func (cc *cachingCollector) GetId() (id string) {
return cc.rawColl.config.id
}
// GetStatus implement GetStatus for collector
// obtain the status of collector scripts execution
func (cc *cachingCollector) GetStatus() int {
return cc.rawColl.status
}
func (cc *cachingCollector) SetLogger(logger log.Logger) {
cc.rawColl.SetLogger(logger)
}
func (cc *cachingCollector) SetSetStats(target Target) {
cc.rawColl.SetSetStats(target)
}
// Collect implements Collector.
func (cc *cachingCollector) Collect(ctx context.Context, ch chan<- Metric, coll_ch chan<- int) {
if ctx.Err() != nil {
ch <- NewInvalidMetric(cc.rawColl.logContext, ctx.Err())
return
}
collTime := time.Now()
select {
case cacheTime := <-cc.cacheSem:
// Have the lock.
if age := collTime.Sub(cacheTime); age > cc.minInterval {
// Cache contents are older than minInterval, collect fresh metrics, cache them and pipe them through.
var logCtx []interface{}
logCtx = append(logCtx, cc.rawColl.logContext...)
logCtx = append(logCtx, "msg", fmt.Sprintf("Collecting fresh metrics: min_interval=%.3fs cache_age=%.3fs",
cc.minInterval.Seconds(), age.Seconds()))
level.Debug(cc.rawColl.logger).Log(logCtx...)
cacheChan := make(chan Metric, capMetricChan)
cc.cache = make([]Metric, 0, len(cc.cache))
go func() {
cc.rawColl.Collect(ctx, cacheChan, coll_ch)
close(cacheChan)
}()
for metric := range cacheChan {
cc.cache = append(cc.cache, metric)
ch <- metric
}
cacheTime = collTime
} else {
var logCtx []interface{}
logCtx = append(logCtx, cc.rawColl.logContext...)
logCtx = append(logCtx, "msg", fmt.Sprintf("Returning cached metrics: min_interval=%.3fs cache_age=%.3fs",
cc.minInterval.Seconds(), age.Seconds()))
level.Debug(cc.rawColl.logger).Log(logCtx...)
for _, metric := range cc.cache {
ch <- metric
}
}
// Always replace the value in the semaphore channel.
cc.cacheSem <- cacheTime
case <-ctx.Done():
// Context closed, record an error and return
// TODO: increment an error counter
ch <- NewInvalidMetric(cc.rawColl.logContext, ctx.Err())
}
}