-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.go
219 lines (191 loc) · 5.37 KB
/
stream.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
package logslurp
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"io"
"regexp"
"sort"
"strings"
"time"
)
// StreamConfig represents an entry for one stream.
type StreamConfig struct {
Path string `json:"path"`
Labels map[string]string `json:"labels"`
RegexpString string `json:"regexp"`
TimestampLayout string `json:"timestamp_layout"`
}
// A PushRequestStreamEntry is a timestamp/log line value pair.
type PushRequestStreamEntry struct {
Timestamp time.Time
Line string
}
var _ fmt.Stringer = (*PushRequestStreamEntry)(nil)
var _ json.Marshaler = (*PushRequestStreamEntry)(nil)
func (e *PushRequestStreamEntry) String() string {
return fmt.Sprintf(
"{Timestamp:%s, Line: %q}",
e.Timestamp.UTC().Format("\"2006-01-02T15:04:05.000000-07:00\""),
e.Line,
)
}
// MarshalJSON returns the JSON encoding of the PushRequestStreamEntry.
func (e *PushRequestStreamEntry) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(
"[\"%d\", %q]",
e.Timestamp.UnixNano(),
e.Line,
)), nil
}
// A PushRequestStream is a stream of timestamp/log line values in ascending
// timestamp order, plus stream labels in key/value pair format.
type PushRequestStream struct {
Stream map[string]string `json:"stream"`
Values []*PushRequestStreamEntry `json:"values"`
}
var _ fmt.Stringer = (*PushRequestStreamEntry)(nil)
func (s *PushRequestStream) String() string {
return fmt.Sprintf(
"{Stream:%s, Values:%s}",
s.Stream,
s.Values,
)
}
// PushRequest is a message that can be sent to /loki/api/v1/push.
//
// This is documented at
// https://github.com/grafana/loki/blob/master/docs/api.md#post-lokiapiv1push
type PushRequest struct {
Streams []*PushRequestStream `json:"streams"`
}
// A LogStream is used to parse a log given a StreamConfig.
type LogStream struct {
rd io.RuneReader
regexp *regexp.Regexp
config *StreamConfig
slop string
}
// NewLogStream returns a new instance of LogStream.
func NewLogStream(rd io.RuneReader, config *StreamConfig) (*LogStream, error) {
s := &LogStream{
rd: rd,
config: config,
}
r, err := regexp.Compile(s.config.RegexpString)
if err != nil {
return nil, err
}
s.regexp = r
return s, nil
}
// A teeRuneReader is analogous to io.TeeReader but uses the io.RuneReader
// interface instead of io.Reader.
type teeRuneReader struct {
r io.RuneReader
w strings.Builder
}
var _ io.RuneReader = (*teeRuneReader)(nil)
func (t *teeRuneReader) ReadRune() (ch rune, size int, err error) {
ch, size, err = t.r.ReadRune()
if size > 0 {
if _, err := t.w.WriteRune(ch); err != nil {
return ch, size, err
}
}
return
}
// A multiRuneReader is analogous to io.MultiReader but uses the io.RuneReader
// interface instead of io.Reader.
type multiRuneReader struct {
r []io.RuneReader
}
var _ io.RuneReader = (*multiRuneReader)(nil)
func (m *multiRuneReader) ReadRune() (ch rune, size int, err error) {
for {
if len(m.r) == 0 {
return 0, 0, io.EOF
}
ch, size, err = m.r[0].ReadRune()
if err != io.EOF {
return
}
m.r = m.r[1:]
}
}
// Read returns the next log entry wrapped in a PushRequestStream so that it
// can retain the labels.
func (s *LogStream) Read() (*PushRequestStream, error) {
t := teeRuneReader{
r: &multiRuneReader{[]io.RuneReader{strings.NewReader(s.slop), s.rd}},
}
groupPairs := s.regexp.FindReaderSubmatchIndex(&t)
if groupPairs == nil {
return nil, io.EOF
}
entry := &PushRequestStreamEntry{}
labels := map[string]string{
"filename": s.config.Path,
}
for key, value := range s.config.Labels {
labels[key] = value
}
var finalLine []string
for i, label := range s.regexp.SubexpNames() {
if i == 0 {
// The first entry is always empty.
continue
}
group := t.w.String()[groupPairs[2*i]:groupPairs[2*i+1]]
if label == "ts" {
ts, err := time.Parse(s.config.TimestampLayout, group)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse timestamp %q", group)
}
entry.Timestamp = ts
} else if label != "" {
labels[label] = group
} else {
finalLine = append(finalLine, group)
}
}
entry.Line = strings.Join(finalLine, " ")
s.slop = t.w.String()[groupPairs[len(groupPairs)-1]:]
if time.Time(entry.Timestamp).IsZero() {
entry.Timestamp = time.Now()
}
return &PushRequestStream{
Values: []*PushRequestStreamEntry{entry},
Stream: labels,
}, nil
}
// NewPushRequest returns a PushRequest given a list of PushRequestStream
// objects. This groups the streams by their labels so that the final payload
// is smaller.
func NewPushRequest(streams []*PushRequestStream) *PushRequest {
mapping := map[string]*PushRequestStream{}
for _, logstream := range streams {
var serializedLabels []string
for k, v := range logstream.Stream {
serializedLabels = append(serializedLabels, fmt.Sprintf("%s=%q", k, v))
}
sort.Strings(serializedLabels)
mappingKey := strings.Join(serializedLabels, ",")
if mappedStream, ok := mapping[mappingKey]; ok {
mappedStream.Values = append(mappedStream.Values, logstream.Values...)
} else {
mapping[mappingKey] = &PushRequestStream{
Stream: logstream.Stream,
Values: logstream.Values,
}
}
}
request := &PushRequest{}
for _, stream := range mapping {
sort.Slice(stream.Values, func(i, j int) bool {
return stream.Values[i].Timestamp.Before(stream.Values[j].Timestamp)
})
request.Streams = append(request.Streams, stream)
}
return request
}