forked from rebuy-de/exporter-merger
/
handler.go
78 lines (70 loc) · 1.73 KB
/
handler.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
package cmd
import (
"io"
"net/http"
"sync"
"time"
prom "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
log "github.com/sirupsen/logrus"
)
type Handler struct {
Exporters []Exporter
ExportersHTTPTimeout int
}
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.WithFields(log.Fields{
"RequestURI": r.RequestURI,
"UserAgent": r.UserAgent(),
}).Debug("handling new request")
h.Merge(w)
}
func (h Handler) Merge(w io.Writer) {
mfs := map[string]*prom.MetricFamily{}
mfMutex := sync.Mutex{}
httpClientTimeout := time.Second * time.Duration(h.ExportersHTTPTimeout)
wg := sync.WaitGroup{}
for _, exporter := range h.Exporters {
wg.Add(1)
go func(exporter Exporter) {
defer wg.Done()
url := exporter.URL
log.WithField("url", url).Debug("getting remote metrics")
httpClient := http.Client{Timeout: httpClientTimeout}
resp, err := httpClient.Get(url)
if err != nil {
log.WithField("url", url).Errorf("HTTP connection failed: %v", err)
return
}
defer resp.Body.Close()
tp := new(expfmt.TextParser)
part, err := tp.TextToMetricFamilies(resp.Body)
if err != nil {
log.WithField("url", url).Errorf("Parse response body to metrics: %v", err)
return
}
for n, mf := range part {
for i, metric := range mf.Metric {
mf.Metric[i].Label = append(metric.Label, exporter.AddLabels...)
}
mfMutex.Lock()
mfo, ok := mfs[n]
if ok {
mfo.Metric = append(mfo.Metric, mf.Metric...)
} else {
mfs[n] = mf
}
mfMutex.Unlock()
}
}(exporter)
}
wg.Wait()
enc := expfmt.NewEncoder(w, expfmt.FmtText)
for mf := range mfs {
err := enc.Encode(mfs[mf])
if err != nil {
log.Error(err)
return
}
}
}