forked from jesperfj/heroku-log-drain-sample
/
process_logs.go
68 lines (62 loc) · 1.81 KB
/
process_logs.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
package main
import (
"fmt"
"bufio"
"time"
"net/http"
"github.com/bmizerany/lpx"
"github.com/kr/logfmt"
)
// This struct and the method below takes care of capturing the data we need
// from each log line. We pass it to Keith Rarick's logfmt parser and it
// handles parsing for us.
type routerLog struct {
host string
}
func (r *routerLog) HandleLogfmt(key, val []byte) error {
if string(key) == "host" {
r.host = string(val)
}
return nil
}
// This is called every time we receive log lines from an app
func processLogs(w http.ResponseWriter, r *http.Request) {
c := redisPool.Get()
defer c.Close()
lp := lpx.NewReader(bufio.NewReader(r.Body))
// a single request may contain multiple log lines. Loop over each of them
for lp.Next() {
// we only care about logs from the heroku router
if string(lp.Header().Procid) == "router" {
rl := new(routerLog)
if err := logfmt.Unmarshal(lp.Bytes(), rl); err != nil {
fmt.Printf("Error parsing log line: %v\n", err)
} else {
timeBucket, err := timestamp2Bucket(lp.Header().Time)
if err != nil {
fmt.Printf("Error parsing time: %v", err)
continue
}
_, err = c.Do("INCR", fmt.Sprintf("%v:%v", rl.host, timeBucket))
if err != nil {
fmt.Printf("Error running INCR on Redis: %v\n", err)
}
_, err = c.Do("INCR", "host:"+rl.host)
if err != nil {
fmt.Printf("Error running INCR on Redis: %v\n", err)
}
fmt.Printf("%v @ %v: +1\n", rl.host, timeBucket)
}
}
}
}
// Heroku log lines are formatted according to RFC5424 which is a subset
// of RFC3339 (RFC5424 is more restrictive).
// Reference: https://devcenter.heroku.com/articles/logging#log-format
func timestamp2Bucket(b []byte) (int64, error) {
t, err := time.Parse(time.RFC3339, string(b))
if err != nil {
return 0, err
}
return (t.Unix() / 60) * 60, nil
}