/
reader.go
161 lines (137 loc) · 4.32 KB
/
reader.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
/*
bilies-go - Bulk Insert Logs Into ElasticSearch
Copyright (C) 2016 Adirelle <adirelle@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Message input
bilies-go waits for JSON messages on its standard input. They should have the following format:
{"date:"YYYY.MM.DD", "id":"some unique idd", "log":{"foo":"bar"}}
The "id" is optional; if missing, a time-based UUID is generated. It is used to identify the document in ElasticSearch. Invalid messages are ignored and logged.
bilies-go expects UTF-8 messages (as JSON). In case the input is not a valid UTF-8 strings, a charset conversion is tried.
The following switch controls input reading:
-c --input-charset=STRING [default: ISO-8859-1]
Alternative charset, in case the input is a not UTF-8 string.
*/
package main
import (
"bufio"
"bytes"
"encoding/base64"
"io"
"os"
"unicode/utf8"
"github.com/landjur/golibrary/uuid"
"github.com/rcrowley/go-metrics"
"github.com/spf13/pflag"
"github.com/ugorji/go/codec"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/htmlindex"
"github.com/Adirelle/bilies-go/data"
)
var (
mReader = metrics.NewPrefixedChildRegistry(mRoot, "reader.")
mInRecords = metrics.GetOrRegisterMeter("in.records", mReader)
mInBytes = metrics.GetOrRegisterMeter("in.bytes", mReader)
mInErrors = metrics.GetOrRegisterMeter("in.errors", mReader)
reader = os.Stdin
lines = make(chan []byte)
linesReq = make(chan bool)
// This is used for the synchronisation with the batcher
readerDone = make(chan bool)
// Charset to try to convert from when given invalid UTF-8.
inputCharset = "ISO-8859-1"
)
func init() {
AddBackgroundTask("Line reader", LineReader)
AddMainTask("Record parser", RecordParser)
pflag.StringVarP(&inputCharset, "input-charset", "c", inputCharset, "Expected charset for invalid UTF-8 input")
}
// LineReader reads lines from input on demand.
func LineReader() {
var decoder *encoding.Decoder
if encoding, err := htmlindex.Get(inputCharset); err == nil {
decoder = encoding.NewDecoder()
} else {
logger.Fatalf("Cannot create converter for %s: %s", inputCharset, err)
}
buf := bufio.NewReader(reader)
defer close(lines)
for range linesReq {
for {
line, err := buf.ReadBytes('\n')
if err == io.EOF {
logger.Notice("End of input reached")
return
} else if err != nil {
logger.Errorf("Cannot read input: %s", err)
continue
}
l := len(line)
mInBytes.Mark(int64(l))
if l > 1 {
if !utf8.Valid(line) {
if fixedLine, err := decoder.Bytes(line); err == nil {
line = fixedLine
} else {
logger.Warningf("Could not convert from %s to UTF-8: %s, input: %q", inputCharset, err, line)
}
}
lines <- bytes.TrimRight(line[:l-1], " \t\n\r")
break
}
}
}
}
// RecordParser requests Line from LineReader, converts them to InputRecords, and send them to the RecordQueuer.
func RecordParser() {
dec := codec.NewDecoderBytes(nil, &codec.JsonHandle{})
defer close(linesReq)
defer close(readerDone)
ever := true
req := linesReq
for ever {
var buf []byte
select {
case buf, ever = <-lines:
if buf == nil {
break
}
req = linesReq
dec.ResetBytes(buf)
var inRec data.InputRecord
if err := dec.Decode(&inRec); err != nil {
logger.Errorf("Invalid JSON, %s: %q", err, buf)
mInErrors.Mark(1)
break
}
if inRec.Suffix == "" || len(inRec.Document) == 0 {
logger.Errorf("Malformed record: %q", buf)
mInErrors.Mark(1)
break
}
if inRec.ID == "" {
if uuid, err := uuid.NewTimeBased(); err == nil {
inRec.ID = base64.RawURLEncoding.EncodeToString(uuid[:])
} else {
logger.Errorf("Could not generate an UUID: %s", err)
}
}
mInRecords.Mark(1)
queue.WriteC <- inRec.Record()
case <-done:
return
case req <- true:
req = nil
}
}
}