forked from heroku/log-shuttle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch.go
More file actions
188 lines (161 loc) · 4.55 KB
/
batch.go
File metadata and controls
188 lines (161 loc) · 4.55 KB
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
package main
//TODO(edwardam): refactor syslogPrefix bits
import (
"bytes"
"fmt"
"github.com/nu7hatch/gouuid"
"time"
)
const (
SYSLOG_TIME_LENGTH = 15 // locally this is always 15 AFAICT, but may not be if we decide to take input from elsewhere
LOGPLEX_MAX_LENGTH = 10000 // It's actually 10240, but leave enough space for headers
BATCH_TIME_FORMAT = "2006-01-02T15:04:05.000000+00:00"
)
var (
PRIVAL_END = []byte(">")
)
// A buffer suitable for posting with a http client
// keeps track of line's Write()n to the buffer
type Batch struct {
MsgCount int
config *ShuttleConfig
oldest *time.Time
newest *time.Time
UUID *uuid.UUID
Drops, Lost int
bytes.Buffer
}
// Create a new batch
func NewBatch(config *ShuttleConfig) (batch *Batch) {
batch = &Batch{config: config}
batch.Reset()
return
}
// Generates a new UUID for the batch
func (b *Batch) SetUUID() {
rid, err := uuid.NewV4()
if err != nil {
ErrLogger.Printf("at=generate_uuid err=%q\n", err)
}
b.UUID = rid
}
// Returns the time range of the messages in the batch in seconds
func (b *Batch) MsgAgeRange() float64 {
if b.oldest == nil || b.newest == nil {
return 0.0
}
newest := *b.newest
return newest.Sub(*b.oldest).Seconds()
}
func (b *Batch) writeError(code, codeMsg string) {
prefix := fmt.Sprintf("<172>%s %s heroku %s log-shuttle %s ",
b.config.Version,
time.Now().UTC().Format(BATCH_TIME_FORMAT),
b.config.Appname,
b.config.Msgid,
)
msg := fmt.Sprintf("Error %s: %s.", code, codeMsg)
b.writeMsg(prefix, []byte(msg))
}
func (b *Batch) WriteDrops(dropped int, since time.Time) {
b.Drops = dropped
b.writeError("L12", fmt.Sprintf("%d messages dropped since %s", dropped, since.UTC().Format(BATCH_TIME_FORMAT)))
}
func (b *Batch) WriteLost(lost int, since time.Time) {
b.Lost = lost
b.writeError("L13", fmt.Sprintf("%d messages lost since %s", lost, since.UTC().Format(BATCH_TIME_FORMAT)))
}
// Write a message into the buffer, incrementing MsgCount
// TODO(edwardam): Ensure that we can't recurse forever
func (b *Batch) writeMsg(prefix string, msg []byte) {
msgLen := len(msg)
if msgLen > LOGPLEX_MAX_LENGTH {
for i := 0; i < msgLen; i += LOGPLEX_MAX_LENGTH {
target := i + LOGPLEX_MAX_LENGTH
if target > msgLen {
target = msgLen
}
b.writeMsg(prefix, msg[i:target])
}
} else {
fmt.Fprintf(&b.Buffer, "%d %s%s", len(prefix)+msgLen, prefix, msg)
b.MsgCount++
}
}
// Write an RFC5424 msg to the buffer from the RFC3164 formatted msg
// TODO(edwardam): Punt on time manipulation for now, use received time
// TODO(edwardam): Punt on host/tag/pid for now, use value from config
func (b *Batch) writeRFC3164Msg(logLine LogLine) {
var msg []byte
b.UpdateTimes(logLine.when)
// Figure out the prival
pe := bytes.Index(logLine.line, PRIVAL_END)
prival := string(logLine.line[1:pe])
//Find the first ': ' after the syslog time, naive, but meh
logLineLength := len(logLine.line)
for msgStart := pe + SYSLOG_TIME_LENGTH; msgStart < logLineLength; msgStart++ {
if logLine.line[msgStart] == ':' && logLine.line[msgStart+1] == ' ' {
msg = logLine.line[msgStart+2 : len(logLine.line)]
break
}
}
syslogPrefix := "<" + prival + ">" + b.config.Version + " " +
logLine.when.UTC().Format(BATCH_TIME_FORMAT) + " " +
b.config.Hostname + " " +
b.config.Appname + " " +
b.config.Procid + " " +
b.config.Msgid + " "
b.writeMsg(syslogPrefix, msg)
}
// Write a line to the batch, increment it's line counter
func (b *Batch) Write(logLine LogLine) {
b.UpdateTimes(logLine.when)
if b.config.InputFormat == INPUT_FORMAT_RFC3164 {
b.writeRFC3164Msg(logLine)
} else {
var syslogPrefix string
if !b.config.SkipHeaders {
syslogPrefix = "<" + b.config.Prival + ">" + b.config.Version + " " +
logLine.when.UTC().Format(BATCH_TIME_FORMAT) + " " +
b.config.Hostname + " " +
b.config.Appname + " " +
b.config.Procid + " " +
b.config.Msgid + " "
}
b.writeMsg(syslogPrefix, logLine.line)
}
}
func (b *Batch) UpdateTimes(t time.Time) {
if b.oldest == nil || t.Before(*b.oldest) {
b.oldest = &t
}
if b.newest == nil || t.After(*b.newest) {
b.newest = &t
}
return
}
// Zero the line count and reset the internal buffer
func (b *Batch) Reset() {
b.MsgCount = 0
b.newest = nil
b.oldest = nil
b.Drops = 0
b.Lost = 0
b.SetUUID()
b.Buffer.Reset()
}
// Is the batch full?
func (b *Batch) Full() bool {
if b.MsgCount >= b.config.BatchSize {
return true
}
return false
}
// NoOpCloser
func (b *Batch) Close() error { return nil }
func SyslogFields(r rune) bool {
if r == '<' || r == '>' || r == ' ' {
return true
}
return false
}