forked from alvintzz/prometheus-nginxlog-exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
490 lines (404 loc) · 14.3 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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
* Copyright 2019 Martin Helmich <martin@helmich.me>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"flag"
"fmt"
"net/http"
"os"
"os/exec"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"github.com/DataDog/datadog-go/statsd"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/satyrius/gonx"
"github.com/tokopedia/prometheus-nginxlog-exporter/config"
"github.com/tokopedia/prometheus-nginxlog-exporter/discovery"
"github.com/tokopedia/prometheus-nginxlog-exporter/prof"
"github.com/tokopedia/prometheus-nginxlog-exporter/relabeling"
"github.com/tokopedia/prometheus-nginxlog-exporter/syslog"
"github.com/tokopedia/prometheus-nginxlog-exporter/tail"
)
type NSMetrics struct {
cfg *config.NamespaceConfig
registry *prometheus.Registry
Metrics
}
func NewNSMetrics(cfg *config.NamespaceConfig, ddog *statsd.Client) *NSMetrics {
m := &NSMetrics{
cfg: cfg,
registry: prometheus.NewRegistry(),
}
m.Init(cfg)
m.registry.MustRegister(m.countTotal)
m.registry.MustRegister(m.bytesTotal)
m.registry.MustRegister(m.upstreamSeconds)
m.registry.MustRegister(m.upstreamSecondsHist)
m.registry.MustRegister(m.responseSeconds)
m.registry.MustRegister(m.responseSecondsHist)
m.registry.MustRegister(m.parseErrorsTotal)
m.datadogClient = ddog
return m
}
// Metrics is a struct containing pointers to all metrics that should be
// exposed to Prometheus
type Metrics struct {
countTotal *prometheus.CounterVec
bytesTotal *prometheus.CounterVec
upstreamSeconds *prometheus.SummaryVec
upstreamSecondsHist *prometheus.HistogramVec
responseSeconds *prometheus.SummaryVec
responseSecondsHist *prometheus.HistogramVec
parseErrorsTotal prometheus.Counter
datadogClient *statsd.Client
}
func inLabels(label string, labels []string) bool {
for _, l := range labels {
if label == l {
return true
}
}
return false
}
// Init initializes a metrics struct
func (m *Metrics) Init(cfg *config.NamespaceConfig) {
cfg.MustCompile()
labels := cfg.OrderedLabelNames
for i := range cfg.RelabelConfigs {
labels = append(labels, cfg.RelabelConfigs[i].TargetLabel)
}
for _, r := range relabeling.DefaultRelabelings {
if !inLabels(r.TargetLabel, labels) {
labels = append(labels, r.TargetLabel)
}
}
m.countTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: cfg.NamespacePrefix,
ConstLabels: cfg.NamespaceLabels,
Name: "http_response_count_total",
Help: "Amount of processed HTTP requests",
}, labels)
m.bytesTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: cfg.NamespacePrefix,
ConstLabels: cfg.NamespaceLabels,
Name: "http_response_size_bytes",
Help: "Total amount of transferred bytes",
}, labels)
m.upstreamSeconds = prometheus.NewSummaryVec(prometheus.SummaryOpts{
Namespace: cfg.NamespacePrefix,
ConstLabels: cfg.NamespaceLabels,
Name: "http_upstream_time_seconds",
Help: "Time needed by upstream servers to handle requests",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}, labels)
m.upstreamSecondsHist = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: cfg.NamespacePrefix,
ConstLabels: cfg.NamespaceLabels,
Name: "http_upstream_time_seconds_hist",
Help: "Time needed by upstream servers to handle requests",
Buckets: cfg.HistogramBuckets,
}, labels)
m.responseSeconds = prometheus.NewSummaryVec(prometheus.SummaryOpts{
Namespace: cfg.NamespacePrefix,
ConstLabels: cfg.NamespaceLabels,
Name: "http_response_time_seconds",
Help: "Time needed by NGINX to handle requests",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}, labels)
m.responseSecondsHist = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: cfg.NamespacePrefix,
ConstLabels: cfg.NamespaceLabels,
Name: "http_response_time_seconds_hist",
Help: "Time needed by NGINX to handle requests",
Buckets: cfg.HistogramBuckets,
}, labels)
m.parseErrorsTotal = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: cfg.NamespacePrefix,
ConstLabels: cfg.NamespaceLabels,
Name: "parse_errors_total",
Help: "Total number of log file lines that could not be parsed",
})
}
//For Datadog START
var datadogTags map[string]bool
func (m *Metrics) IncrDD(name string, tags []string) {
if m.datadogClient == nil {
return
}
m.datadogClient.Incr(name, tags, 1)
}
func (m *Metrics) CountDD(name string, value int64, tags []string) {
if m.datadogClient == nil {
return
}
m.datadogClient.Count(name, value, tags, 1)
}
func (m *Metrics) HistogramDD(name string, value float64, tags []string) {
if m.datadogClient == nil {
return
}
m.datadogClient.Histogram(name, value, tags, 1)
}
func (m *Metrics) GaugeDD(name string, value float64, tags []string) {
if m.datadogClient == nil {
return
}
m.datadogClient.Gauge(name, value, tags, 1)
}
//For Datadog END
func main() {
var opts config.StartupFlags
var cfg = config.Config{
Listen: config.ListenConfig{
Port: 4040,
Address: "0.0.0.0",
MetricsEndpoint: "/metrics",
},
}
nsGatherers := make(prometheus.Gatherers, 0)
flag.IntVar(&opts.ListenPort, "listen-port", 4040, "HTTP port to listen on")
flag.StringVar(&opts.Format, "format", `$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"`, "NGINX access log format")
flag.StringVar(&opts.Namespace, "namespace", "nginx", "namespace to use for metric names")
flag.StringVar(&opts.ConfigFile, "config-file", "", "Configuration file to read from")
flag.BoolVar(&opts.EnableExperimentalFeatures, "enable-experimental", false, "Set this flag to enable experimental features")
flag.StringVar(&opts.CPUProfile, "cpuprofile", "", "write cpu profile to `file`")
flag.StringVar(&opts.MemProfile, "memprofile", "", "write memory profile to `file`")
flag.StringVar(&opts.DatadogUrl, "datadog-url", "datadog.tokopedia.local:8125", "Datadog URL")
flag.StringVar(&opts.MetricsEndpoint, "metrics-endpoint", cfg.Listen.MetricsEndpoint, "URL path at which to serve metrics")
flag.Parse()
opts.Filenames = flag.Args()
sigChan := make(chan os.Signal, 1)
stopChan := make(chan bool)
stopHandlers := sync.WaitGroup{}
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
signal.Notify(sigChan, os.Interrupt, syscall.SIGINT)
go func() {
sig := <-sigChan
fmt.Printf("caught term %s. exiting\n", sig)
close(stopChan)
stopHandlers.Wait()
os.Exit(0)
}()
defer func() {
close(stopChan)
stopHandlers.Wait()
}()
dd, err := statsd.New(opts.DatadogUrl)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to connect to datadog.")
os.Exit(1)
}
datadogTags = make(map[string]bool)
prof.SetupCPUProfiling(opts.CPUProfile, stopChan, &stopHandlers)
prof.SetupMemoryProfiling(opts.MemProfile, stopChan, &stopHandlers)
loadConfig(&opts, &cfg)
fmt.Printf("using configuration %+v\n", cfg)
if stabilityError := cfg.StabilityWarnings(); stabilityError != nil && !opts.EnableExperimentalFeatures {
fmt.Fprintf(os.Stderr, "Your configuration file contains an option that is explicitly labeled as experimental feature:\n\n %s\n\n", stabilityError.Error())
fmt.Fprintln(os.Stderr, "Use the -enable-experimental flag or the enable_experimental option to enable these features. Use them at your own peril.")
os.Exit(1)
}
if cfg.Consul.Enable {
setupConsul(&cfg, stopChan, &stopHandlers)
}
for _, ns := range cfg.Namespaces {
nsMetrics := NewNSMetrics(&ns, dd)
nsGatherers = append(nsGatherers, nsMetrics.registry)
fmt.Printf("starting listener for namespace %s\n", ns.Name)
go processNamespace(ns, &(nsMetrics.Metrics))
}
listenAddr := fmt.Sprintf("%s:%d", cfg.Listen.Address, cfg.Listen.Port)
endpoint := cfg.Listen.MetricsEndpointOrDefault()
fmt.Printf("running HTTP server on address %s, serving metrics at %s\n", listenAddr, endpoint)
nsHandler := promhttp.InstrumentMetricHandler(
prometheus.DefaultRegisterer, promhttp.HandlerFor(nsGatherers, promhttp.HandlerOpts{}),
)
http.Handle(endpoint, nsHandler)
if err := http.ListenAndServe(listenAddr, nil); err != nil {
fmt.Printf("error while starting HTTP server: %s", err.Error())
}
}
func loadConfig(opts *config.StartupFlags, cfg *config.Config) {
if opts.ConfigFile != "" {
fmt.Printf("loading configuration file %s\n", opts.ConfigFile)
if err := config.LoadConfigFromFile(cfg, opts.ConfigFile); err != nil {
panic(err)
}
} else if err := config.LoadConfigFromFlags(cfg, opts); err != nil {
panic(err)
}
}
func setupConsul(cfg *config.Config, stopChan <-chan bool, stopHandlers *sync.WaitGroup) {
registrator, err := discovery.NewConsulRegistrator(cfg)
if err != nil {
panic(err)
}
fmt.Printf("registering service in Consul\n")
if err := registrator.RegisterConsul(); err != nil {
panic(err)
}
go func() {
<-stopChan
fmt.Printf("unregistering service in Consul\n")
if err := registrator.UnregisterConsul(); err != nil {
fmt.Printf("error while unregistering from consul: %s\n", err.Error())
}
stopHandlers.Done()
}()
stopHandlers.Add(1)
}
func processNamespace(nsCfg config.NamespaceConfig, metrics *Metrics) {
var followers []tail.Follower
parser := gonx.NewParser(nsCfg.Format)
for _, f := range nsCfg.SourceData.Files {
t, err := tail.NewFileFollower(f)
if err != nil {
panic(err)
}
t.OnError(func(err error) {
panic(err)
})
followers = append(followers, t)
}
if nsCfg.SourceData.Syslog != nil {
slCfg := nsCfg.SourceData.Syslog
fmt.Printf("running Syslog server on address %s\n", slCfg.ListenAddress)
channel, server, err := syslog.Listen(slCfg.ListenAddress, slCfg.Format)
if err != nil {
panic(err)
}
for _, f := range slCfg.Tags {
t, err := tail.NewSyslogFollower(f, server, channel)
if err != nil {
panic(err)
}
t.OnError(func(err error) {
panic(err)
})
followers = append(followers, t)
}
}
for _, f := range followers {
go processSource(nsCfg, f, parser, metrics)
}
}
func getServerIP() (string, error) {
output, err := exec.Command("ip", "r").Output()
if err != nil {
return "0.0.0.0", nil
}
result := ""
arr := strings.Split(string(output), "\n")
for _, v := range arr {
v = strings.TrimSpace(v)
if strings.Contains(v, "proto kernel") && strings.Contains(v, "scope link") {
splited := strings.Split(v, " ")
result = splited[len(splited)-1]
}
}
return result, nil
}
func processSource(nsCfg config.NamespaceConfig, t tail.Follower, parser *gonx.Parser, metrics *Metrics) {
relabelings := relabeling.NewRelabelings(nsCfg.RelabelConfigs)
relabelings = append(relabelings, relabeling.DefaultRelabelings...)
relabelings = relabeling.UniqueRelabelings(relabelings)
staticLabelValues := nsCfg.OrderedLabelValues
staticLabels := nsCfg.Labels //For Datadog
staticName := nsCfg.Name //For Datadog
totalLabelCount := len(staticLabelValues) + len(relabelings)
relabelLabelOffset := len(staticLabelValues)
labelValues := make([]string, totalLabelCount)
datadogLabels := []string{} //For Datadog
for i := range staticLabelValues {
labelValues[i] = staticLabelValues[i]
}
//For Datadog START
for k, v := range staticLabels {
datadogLabels = append(datadogLabels, fmt.Sprintf("%s:%s", k, v))
}
hostname, _ := os.Hostname()
serverIP, _ := getServerIP()
datadogLabels = append(datadogLabels, fmt.Sprintf("%s_hostname:%s", staticName, hostname))
datadogLabels = append(datadogLabels, fmt.Sprintf("%s_ip:%s", staticName, serverIP))
//For Datadog END
for line := range t.Lines() {
if nsCfg.PrintLog {
fmt.Println(line)
}
entry, err := parser.ParseString(line)
if err != nil {
fmt.Printf("error while parsing line '%s': %s\n", line, err)
metrics.parseErrorsTotal.Inc()
continue
}
fields := entry.Fields()
tags := []string{}
for _, v := range datadogLabels {
tags = append(tags, v)
}
for i := range relabelings {
if str, ok := fields[relabelings[i].SourceValue]; ok {
mapped, err := relabelings[i].Map(str)
if err == nil {
labelValues[i+relabelLabelOffset] = mapped
tags = append(tags, fmt.Sprintf("%s:%s", relabelings[i].TargetLabel, mapped))
if relabelings[i].TargetLabel == "status" {
tags = append(tags, fmt.Sprintf("status_group:%sxx", mapped[0:1]))
}
}
}
}
metrics.countTotal.WithLabelValues(labelValues...).Inc()
metrics.IncrDD(staticName+".nginx.response.count_total", tags) //For Datadog
// // check datadog tags length
// for _, t := range tags {
// datadogTags[t] = true
// }
// if len(datadogTags) >= 400 {
// log.Printf("too many datadog tags beign created, please check, datadogTags: %v", datadogTags)
// os.Exit(0)
// }
if bytes, ok := floatFromFields(fields, "body_bytes_sent"); ok {
metrics.bytesTotal.WithLabelValues(labelValues...).Add(bytes)
metrics.CountDD(staticName+".nginx.response.size_bytes", int64(bytes), tags) //For Datadog
}
if upstreamTime, ok := floatFromFields(fields, "upstream_response_time"); ok {
metrics.upstreamSeconds.WithLabelValues(labelValues...).Observe(upstreamTime)
metrics.upstreamSecondsHist.WithLabelValues(labelValues...).Observe(upstreamTime)
metrics.HistogramDD(staticName+".nginx.upstream.time_seconds", upstreamTime, tags) //For Datadog
}
if responseTime, ok := floatFromFields(fields, "request_time"); ok {
metrics.responseSeconds.WithLabelValues(labelValues...).Observe(responseTime)
metrics.responseSecondsHist.WithLabelValues(labelValues...).Observe(responseTime)
metrics.HistogramDD(staticName+".nginx.response.time_seconds", responseTime, tags) //For Datadog
}
}
}
func floatFromFields(fields gonx.Fields, name string) (float64, bool) {
val, ok := fields[name]
if !ok {
return 0, false
}
f, err := strconv.ParseFloat(val, 64)
if err != nil {
return 0, false
}
return f, true
}