forked from vojtad/heroku-logs-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (63 loc) · 2.19 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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
herokuLog "heroku-logs-exporter/heroku_log"
"heroku-logs-exporter/metrics"
)
var (
listenAddress = flag.String("web.listen-address", ":9841", "Address to listen on for telemetry")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics")
logsPath = flag.String("web.logs-path", "/logs", "Path under which to accept Heroku Log Drain")
logsTokenParamName = flag.String("web.logs-token-param-name", "token", "Parameter name to check against token parameter value in Heroku Log Drain requests")
logsTokenParamValue = flag.String("web.logs-token-param-value", "", "Token to check against token parameter in Heroku Log Drain requests")
)
var (
exportedMetrics = []metrics.HerokuMetricGroup{
metrics.NewHerokuSystemMetrics(),
metrics.NewHerokuRuntimeMetrics(),
metrics.NewHerokuPostgresMetrics(),
metrics.NewHerokuPgbouncerMetrics(),
metrics.NewHerokuRouterMetrics(),
metrics.NewRackTimeoutMetrics(),
}
)
func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "heroku-logs-exporter")
}
func logsHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
if *logsTokenParamName != "" && *logsTokenParamValue != "" {
if *logsTokenParamValue != r.URL.Query().Get(*logsTokenParamName) {
log.Printf("Token mismatch: %s\n", r.URL)
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
}
appName := r.URL.Query().Get("app_name")
count := 0
scanner := bufio.NewScanner(r.Body)
for scanner.Scan() {
hLog := herokuLog.ParseHerokuLog(appName, scanner.Text())
for _, metric := range exportedMetrics {
metric.UpdateFromLog(hLog)
}
count = count + 1
}
log.Printf("Processed %d log lines from %s\n", count, appName)
}
func main() {
flag.Parse()
http.HandleFunc("/", helloHandler)
http.HandleFunc(*logsPath, logsHandler)
http.Handle(*metricsPath, promhttp.Handler())
log.Printf("Starting heroku-logs-exporter on %s\n", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}