/
gelf.go
219 lines (176 loc) · 4.19 KB
/
gelf.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 main
import (
"bytes"
"compress/gzip"
"crypto/rand"
"encoding/binary"
"encoding/json"
"errors"
"io/ioutil"
"log"
"math"
"net"
"time"
)
const (
defaultConnection = "lan"
defaultMaxChunkSizeWan = 1420
defaultMaxChunkSizeLan = 8154
)
type gelfMessage struct {
Version string
Host string
Level int
ShortMessage string
FullMessage string
Timestamp float64
Facility string
LoggerName string
CustomFields map[string]interface{}
}
func newGelfMessage(host string, appName string, loggerName string, level int) *gelfMessage {
return &gelfMessage{
Version: "1.1",
LoggerName: loggerName,
Host: host,
Facility: appName,
Timestamp: float64(time.Now().UnixNano()) / float64(time.Second),
Level: level,
CustomFields: make(map[string]interface{}),
}
}
func (m *gelfMessage) toByte() []byte {
items := make(map[string]interface{})
items["version"] = m.Version
items["host"] = m.Host
items["level"] = m.Level
items["short_message"] = m.ShortMessage
items["full_message"] = m.FullMessage
items["timestamp"] = m.Timestamp
items["_app_id"] = m.Facility
items["_logger_name"] = m.LoggerName
for k, v := range m.CustomFields {
items["_"+k] = v
}
payload, _ := json.Marshal(items)
return payload
}
type gelfConfig struct {
ConnectionString string
Connection string
MaxChunkSizeWan int
MaxChunkSizeLan int
}
type gelf struct {
conn net.Conn
writer *gzip.Writer
gelfConfig
}
func newGelf(config gelfConfig) *gelf {
gz, err := gzip.NewWriterLevel(ioutil.Discard, gzip.BestSpeed)
if err != nil {
panic(err)
}
if len(config.ConnectionString) == 0 {
config.ConnectionString = "127.0.0.1:12201"
}
if config.Connection == "" {
config.Connection = defaultConnection
}
if config.MaxChunkSizeWan == 0 {
config.MaxChunkSizeWan = defaultMaxChunkSizeWan
}
if config.MaxChunkSizeLan == 0 {
config.MaxChunkSizeLan = defaultMaxChunkSizeLan
}
udpConn, err := net.Dial("udp", config.ConnectionString)
if err != nil {
panic(err)
}
g := &gelf{
conn: udpConn,
writer: gz,
gelfConfig: config,
}
return g
}
func (g *gelf) log(data []byte) {
/*
msgJson := g.parseJson(message)
err := g.testForForbiddenValues(msgJson)
if err != nil {
log.Printf("Uh oh! %s", err)
return
}
*/
compressed := g.compress(data)
/*
compressed := []byte(message)
_logger.debug(compressed)
g.send(compressed)
*/
chunksize := g.gelfConfig.MaxChunkSizeLan
length := compressed.Len()
if length > chunksize {
chunkCountInt := int(math.Ceil(float64(length) / float64(chunksize)))
id := make([]byte, 8)
rand.Read(id)
for i, index := 0, 0; i < length; i, index = i+chunksize, index+1 {
packet := g.createChunkedMessage(index, chunkCountInt, id, &compressed)
g.send(packet.Bytes())
}
} else {
g.send(compressed.Bytes())
}
}
func (g *gelf) createChunkedMessage(index int, chunkCountInt int, id []byte, compressed *bytes.Buffer) bytes.Buffer {
var packet bytes.Buffer
chunksize := g.getChunksize()
packet.Write(g.intToBytes(30))
packet.Write(g.intToBytes(15))
packet.Write(id)
packet.Write(g.intToBytes(index))
packet.Write(g.intToBytes(chunkCountInt))
packet.Write(compressed.Next(chunksize))
return packet
}
func (g *gelf) getChunksize() int {
if g.gelfConfig.Connection == "wan" {
return g.gelfConfig.MaxChunkSizeWan
}
if g.gelfConfig.Connection == "lan" {
return g.gelfConfig.MaxChunkSizeLan
}
return g.gelfConfig.MaxChunkSizeWan
}
func (g *gelf) intToBytes(i int) []byte {
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.LittleEndian, int8(i))
if err != nil {
log.Printf("Uh oh! %s", err)
}
return buf.Bytes()
}
func (g *gelf) compress(b []byte) bytes.Buffer {
var buf bytes.Buffer
comp := gzip.NewWriter(&buf)
comp.Write(b)
comp.Close()
return buf
}
func (g *gelf) parseJson(msg string) map[string]interface{} {
var i map[string]interface{}
c := []byte(msg)
json.Unmarshal(c, &i)
return i
}
func (g *gelf) testForForbiddenValues(gmap map[string]interface{}) error {
if _, err := gmap["_id"]; err {
return errors.New("Key _id is forbidden")
}
return nil
}
func (g *gelf) send(b []byte) {
//time.Sleep(1 * time.Second)
g.conn.Write(b)
}