-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
257 lines (219 loc) · 6.35 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/signal"
"path"
"syscall"
"time"
kingpin "github.com/alecthomas/kingpin/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
yaml "gopkg.in/yaml.v3"
)
type queryJob struct {
Id string
Timespan string
Repo string
MetricName string
MetricLabels []MetricLabel
}
type queryJobData struct {
Done bool
Events []map[string]interface{}
FieldOrder []string
MetaData map[string]interface{}
ExtraData map[string]string
ProcessedEvents int
}
type MetricMap struct {
Gauges map[string]*prometheus.GaugeVec
}
type YamlConfig struct {
Queries []struct {
Query string `yaml:"query"`
Repo string `yaml:"repo"`
Interval string `yaml:"interval"`
MetricName string `yaml:"metric_name"`
MetricLabels []MetricLabel `yaml:"metric_labels"`
} `yaml:"queries"`
}
type MetricLabel struct {
Key string `yaml:"key"`
Value string `yaml:"value"`
}
var (
version = ""
supportedFunctions = []string{"_count", "_min", "_max", "_avg", "_rate", "_range", "_stddev", "_sum"}
)
const (
repoLabel = "repo"
intervalLabel = "interval"
)
func main() {
logger, _ := zap.NewProduction()
zap.ReplaceGlobals(logger)
defer logger.Sync()
flags := kingpin.New("humio_exporter", "Humio exporter for Prometheus. Provide your Humio API token and configuration file with queries to expose as Prometheus metrics.")
configFile := flags.Flag("config", "The humio_exporter configuration file to be used").Required().String()
baseURL := flags.Flag("humio.url", "Humio base API url").Required().String()
apiToken := flags.Flag("humio.api-token", "Humio API token").Required().String()
requestTimeout := flags.Flag("humio.timeout", "Timeout for requests against the Humio API").Default("10").Int()
listenAddress := flags.Flag("web.listen-address", "Address on which to expose metrics.").Default(":9534").String()
flags.HelpFlag.Short('h')
flags.Version(version)
kingpin.MustParse(flags.Parse(os.Args[1:]))
// Parse YAML queries file
yamlConfig := YamlConfig{}
currentDir, err := os.Getwd()
if err != nil {
zap.L().Sugar().Fatal(err)
}
yamlFile, err := ioutil.ReadFile(path.Join(currentDir, *configFile))
if err != nil {
zap.L().Sugar().Infof("yamlFile.Get err #%v ", err)
}
err = yaml.Unmarshal([]byte(yamlFile), &yamlConfig)
if err != nil {
zap.L().Sugar().Fatalf("error: %v", err)
}
// Register the prometheus metrics
metricMap := MetricMap{
Gauges: make(map[string]*prometheus.GaugeVec),
}
for _, q := range yamlConfig.Queries {
metricMap.AddGauge(q.MetricName, q.MetricLabels)
}
err = metricMap.Register()
if err != nil {
zap.L().Sugar().Fatalf("error: %v", err)
}
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "healthy")
})
// TODO: Add more logic on when the exporter is actually ready
// e.g. connection to humio is succesful
http.HandleFunc("/ready", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "healthy")
})
done := make(chan error, 1)
go func() {
zap.L().Sugar().Infof("Listening on %s", *listenAddress)
err := http.ListenAndServe(*listenAddress, nil)
if err != nil {
done <- err
}
}()
go runAPIPolling(done, *baseURL, *apiToken, yamlConfig, secondDuration(*requestTimeout), metricMap)
reason := <-done
if reason != nil {
zap.L().Sugar().Errorf("Humio_exporter exited due to error: %v", reason)
os.Exit(1)
}
zap.L().Sugar().Info("Humio_exporter exited with exit 0")
}
func runAPIPolling(done chan error, url, token string, yamlConfig YamlConfig, requestTimeout time.Duration, metricMap MetricMap) {
client := client{
httpClient: &http.Client{
Timeout: requestTimeout,
},
token: token,
baseURL: url,
}
var jobs []queryJob
for _, q := range yamlConfig.Queries {
job, err := client.startQueryJob(q.Query, q.Repo, q.MetricName, q.Interval, "now", q.MetricLabels)
if err != nil {
done <- err
return
}
jobs = append(jobs, job)
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
for _, job := range jobs {
client.stopQueryJob(job.Id, job.Repo)
}
done <- fmt.Errorf("received os signal '%s'", sig)
}()
for {
for _, job := range jobs {
poll, err := client.pollQueryJob(job.Id, job.Repo)
if err != nil {
done <- err
return
}
// Handle cases where the metric may be missing for the given time range
if len(poll.Events) < 1 {
zap.L().Sugar().Debugf("No Events returned by query. Timespan: %v, MetricName: %s", job.Timespan, job.MetricName)
continue
}
var floatValue float64
for _, f := range supportedFunctions {
value, ok := poll.Events[0][f]
if !ok {
continue
}
floatValue, err = parseFloat(value)
if err != nil {
done <- err
return
}
break
}
if poll.Done {
metricMap.UpdateMetricValue(job.MetricName, job.Timespan, job.Repo, floatValue, job.MetricLabels)
if err != nil {
done <- err
return
}
} else {
zap.L().Sugar().Debugf("Skipped value because query isn't done. Timespan: %v, Value: %v", job.Timespan, floatValue)
}
}
time.Sleep(5000 * time.Millisecond)
}
}
func secondDuration(seconds int) time.Duration {
return time.Duration(seconds) * time.Second
}
func (m *MetricMap) Register() error {
for _, v := range m.Gauges {
err := prometheus.Register(v)
if err != nil {
return err
}
}
return nil
}
func (m *MetricMap) UpdateMetricValue(metricName, timespan, repo string, value float64, staticLabels []MetricLabel) error {
labels := make(map[string]string)
labels[intervalLabel] = timespan
labels[repoLabel] = repo
for _, l := range staticLabels {
labels[l.Key] = l.Value
}
gauge := m.Gauges[metricName]
gauge.With(labels).Set(value)
return nil
}
func (m *MetricMap) AddGauge(metricName string, staticLabels []MetricLabel) error {
var labelKeys []string
labelKeys = append(labelKeys, intervalLabel)
labelKeys = append(labelKeys, repoLabel)
for _, l := range staticLabels {
labelKeys = append(labelKeys, l.Key)
}
m.Gauges[metricName] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: metricName,
Help: "Gauge for humio query",
}, labelKeys)
return nil
}