-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter.go
65 lines (49 loc) · 1.14 KB
/
exporter.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
package main
import (
"context"
"log"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
)
// Exporter of webhook metrics.
// Build
type Exporter struct {
bots []botExporter
}
// NewExporter creates exporter for provided bots
func NewExporter(ctx context.Context, tokens []string) *Exporter {
exporter := &Exporter{
bots: make([]botExporter, len(tokens)),
}
for i, token := range tokens {
exporter.bots[i] = newBotExporter(token)
}
return exporter
}
// Describe implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- metricaUp
ch <- metricaPendingUpdates
ch <- metricaMaxConns
ch <- metricaLastDeliveryError
}
// Collect implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
wg := &sync.WaitGroup{}
ctx := context.Background()
for i, bot := range e.bots {
bot := bot
i := i
wg.Add(1)
go func() {
defer wg.Done()
ctx, cancel := context.WithTimeout(ctx, time.Second*30)
defer cancel()
started := time.Now()
bot(ctx, ch)
log.Printf("stats of bot #%d collect in %s", i, time.Since(started))
}()
}
wg.Wait()
}