-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
275 lines (233 loc) · 7.56 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
package main
import (
"errors"
"flag"
"fmt"
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"github.com/hpcloud/tail"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/satyrius/gonx"
"github.com/tdevelioglu/prometheus-nginx-log-exporter/logging"
)
const version = "1.1.0"
var logger *logging.Logger
var reqParamsRegex = regexp.MustCompile(`\?.*`)
var staticLabels = []string{"method", "path", "status"}
func main() {
if err := do_main(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func do_main() error {
configFile := flag.String("config-file", "prometheus-nginx-log-exporter.yaml",
"Path to a YAML file to read configuration from")
showVersion := flag.Bool("version", false, "Prints the version and exits")
flag.Parse()
if *showVersion {
fmt.Println(version)
return nil
}
if *configFile == "" {
return errors.New("Must specify config using -config-file")
}
var gc *globalConfig
var err error
if gc, err = newConfig(*configFile); err != nil {
return err
}
if logger, err = logging.NewLogger(gc.LogLevel); err != nil {
return err
}
for n := range gc.Applications {
monitorApp(n, gc.Applications[n])
}
listenAddr := fmt.Sprintf("%s:%d", gc.Listen.Address, gc.Listen.Port)
fmt.Printf("running HTTP server on %s\n", listenAddr)
http.Handle("/metrics", promhttp.Handler())
err = http.ListenAndServe(listenAddr, nil)
if err != nil {
return err
}
return nil
}
// monitorApp sets up the parsers and metrics for the log files
// that belong to a single application
func monitorApp(name string, ac *appConfig) {
ln := append(staticLabels, ac.orderedLabelNames...)
metrics := newMetrics(name, ln, ac.HistogramBuckets)
parser := gonx.NewParser(ac.Format)
for _, file := range ac.LogFiles {
go monitorFile(file, parser, metrics, ac.orderedLabelValues, ac.Exclude, ac.Include, ac.Replace, ac.FromBeginning)
}
}
// monitorFile tracks and collects metrics for a single log file.
func monitorFile(file string, parser *gonx.Parser, metrics *metrics, extraLabelValues []string,
exclude []filterConfig, include []filterConfig, replace []replaceConfig, fromBeginning bool) {
logger.Info("(%s): starting log file monitoring\n", file)
// tail silently quits if the file exists but is inaccessible, so we test
// here ourselves.
f, err := os.Open(file)
if err != nil {
logger.Err("(%s): %s\n", file, err)
os.Exit(1)
} else {
f.Close()
}
// We start from either the beginning or the end of the log file.
var location *tail.SeekInfo
if fromBeginning {
location = nil
} else {
location = &tail.SeekInfo{Offset: 0, Whence: 2}
}
t, err := tail.TailFile(file, tail.Config{
Follow: true,
ReOpen: true,
Location: location,
})
if err != nil {
logger.Err("(%s): %s\n", file, err)
os.Exit(1)
}
labelValues := make([]string, len(extraLabelValues)+len(staticLabels))
for i := range extraLabelValues {
labelValues[i+len(staticLabels)] = extraLabelValues[i]
}
LINES:
for line := range t.Lines {
logger.Debug("(%s): parsing line: '%s'", file, line.Text)
entry, err := parser.ParseString(line.Text)
if err != nil {
logger.Warn("(%s): failed to parse line: %s\n", file, err)
continue
}
labelValues[0] = "" // method
labelValues[1] = "" // path
labelValues[2] = "" // status
if request, err := entry.Field("request"); err == nil {
method, path, err := parseRequest(request)
if err != nil {
logger.Warn("(%s): Failed to parse request field: %s", err)
continue
}
logger.Debug("(%s): method and path are: %s and %s", file, method, path)
if len(exclude) > 0 {
if fc := filterMatch(method, path, exclude); fc != nil {
logger.Debug("(%s): found matching exclude rule: '%s' %v",
file, fc.pathRe.String(), fc.Methods)
continue LINES
} else {
logger.Debug("(%s): no matching exclude rule found", file)
}
}
if len(include) > 0 {
if fc := filterMatch(method, path, include); fc != nil {
logger.Debug("(%s): found matching include rule: '%s' %v",
file, fc.pathRe.String(), fc.Methods)
} else {
logger.Debug("(%s): no matching include rule found", file)
continue LINES
}
}
if len(replace) > 0 {
if rc := replaceMatch(method, path, replace); rc != nil {
logger.Debug("(%s): found matching replace rule, %s %v -> %s",
file, rc.pathRe.String(), rc.Methods, rc.With)
if rc.useTemplate() {
// use template to update path
path, err = rc.replaceWithTemplate(path)
if err != nil {
logger.Warn("(%s): replace path (%s) with template (%s) error, %v", file, path, rc.With, err)
continue
}
} else {
// use regex to update path
path = rc.replace(path)
}
} else {
logger.Debug("(%s): no matching replace rule found", file)
}
}
labelValues[0] = method
labelValues[1] = path
logger.Debug("(%s): matched path to %s", file, path)
}
if status, err := entry.Field("status"); err == nil {
logger.Debug("(%s): matched status to %s", file, status)
labelValues[2] = status
}
if bodyBytes, err := entry.FloatField("body_bytes_sent"); err == nil {
logger.Debug("(%s): matched body_bytes_sent to %.f", file, bodyBytes)
metrics.bodyBytes.WithLabelValues(labelValues...).Observe(bodyBytes)
}
if upstreamTime, err := entry.Field("upstream_response_time"); err == nil {
if totalTime, err := parseUpstreamTime(upstreamTime); err == nil {
logger.Debug("(%s): matched upstream_response_time to %.3f", file, totalTime)
metrics.upstreamSeconds.WithLabelValues(labelValues...).Observe(totalTime)
} else {
logger.Warn("(%s): failed to parse upstream_response_time field, line: %s", file, line, err)
}
}
if headerTime, err := entry.Field("upstream_header_time"); err == nil {
if totalTime, err := parseUpstreamTime(headerTime); err == nil {
logger.Debug("(%s): matched upstream_header_time to %.3f", file, totalTime)
metrics.upstreamHeaderSeconds.WithLabelValues(labelValues...).Observe(totalTime)
} else {
logger.Warn("(%s): failed to parse upstream_header_time field, line: %s", file, line, err)
}
}
if responseTime, err := entry.FloatField("request_time"); err == nil {
logger.Debug("(%s): matched request_time to %.3f", file, responseTime)
metrics.requestSeconds.WithLabelValues(labelValues...).Observe(responseTime)
}
}
}
// filterMatch checks if a method/path combination matches a list of filters.
func filterMatch(method string, path string, f []filterConfig) *filterConfig {
for i := range f {
if f[i].match(method, path) {
return &f[i]
}
}
return nil
}
// replaceMatch checks if a method/path combination matches a list of filters.
func replaceMatch(method string, path string, f []replaceConfig) *replaceConfig {
for i := range f {
if f[i].match(method, path) {
return &f[i]
}
}
return nil
}
// parseRequest parses an nginx $request value into method and path.
func parseRequest(request string) (string, string, error) {
fields := strings.Split(request, " ")
if len(fields) < 2 {
return "", "", requestParseError(request)
}
path, err := url.PathUnescape(fields[1])
if err != nil {
return "", "", err
}
path = reqParamsRegex.ReplaceAllLiteralString(path, "")
return fields[0], path, nil
}
// parseUpstreamTime sums an nginx $upstream_response_time value into a single float.
func parseUpstreamTime(upstreamTime string) (float64, error) {
var totalTime float64
for _, timeString := range strings.Split(upstreamTime, ", ") {
time, err := strconv.ParseFloat(timeString, 32)
if err != nil {
return -1, err
}
totalTime = totalTime + time
}
return totalTime, nil
}