-
Notifications
You must be signed in to change notification settings - Fork 28
/
rfc5424.go
244 lines (202 loc) · 7.72 KB
/
rfc5424.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package syslog
import (
"fmt"
"regexp"
"sort"
"strconv"
"time"
"code.cloudfoundry.org/go-loggregator/v9/rpc/loggregator_v2"
"code.cloudfoundry.org/go-loggregator/v9/rfc5424"
)
var findSpaces, findInvalidCharactersProcID, findInvalidCharactersHostname, findTrailingDashes *regexp.Regexp
func init() {
findSpaces = regexp.MustCompile(`\s+`)
findInvalidCharactersProcID = regexp.MustCompile("[^[:graph:]]+")
findInvalidCharactersHostname = regexp.MustCompile("[^-a-zA-Z0-9]+")
findTrailingDashes = regexp.MustCompile("-+$")
}
const RFC5424TimeOffsetNum = "2006-01-02T15:04:05.999999-07:00"
// gaugeStructuredDataID contains the registered enterprise ID for the Cloud
// Foundry Foundation.
// See: https://www.iana.org/assignments/enterprise-numbers/enterprise-numbers
const (
gaugeStructuredDataID = "gauge@47450"
timerStructuredDataID = "timer@47450"
counterStructuredDataID = "counter@47450"
eventStructuredDataID = "event@47450"
tagsStructuredDataID = "tags@47450"
)
type ConverterOption func(*Converter)
func WithoutSyslogMetadata() ConverterOption {
return func(c *Converter) {
c.omitTags = true
}
}
type Converter struct {
omitTags bool
}
func NewConverter(opts ...ConverterOption) *Converter {
c := &Converter{}
for _, o := range opts {
o(c)
}
return c
}
func (c *Converter) ToRFC5424(env *loggregator_v2.Envelope, defaultHostname string) ([][]byte, error) {
hostname := c.BuildHostname(env, defaultHostname)
appID := env.GetSourceId()
switch env.GetMessage().(type) {
case *loggregator_v2.Envelope_Log:
return c.toRFC5424LogMessage(env, hostname, appID)
case *loggregator_v2.Envelope_Gauge:
return c.toRFC5424GaugeMessage(env, hostname, appID)
case *loggregator_v2.Envelope_Timer:
return c.toRFC5424TimerMessage(env, hostname, appID)
case *loggregator_v2.Envelope_Counter:
return c.toRFC5424CounterMessage(env, hostname, appID)
case *loggregator_v2.Envelope_Event:
return c.toRFC5424EventMessage(env, hostname, appID)
default:
return nil, nil
}
}
func (c *Converter) BuildHostname(env *loggregator_v2.Envelope, defaultHostname string) string {
hostname := defaultHostname
envTags := env.GetTags()
orgName, orgOk := envTags["organization_name"]
spaceName, spaceOk := envTags["space_name"]
appName, appOk := envTags["app_name"]
if orgOk || spaceOk || appOk {
sanitizedOrgName := c.sanitizeHostname(orgName)
sanitizedSpaceName := c.sanitizeHostname(spaceName)
sanitizedAppName := c.sanitizeHostname(appName)
hostname = fmt.Sprintf("%s.%s.%s", c.truncate(sanitizedOrgName, 63), c.truncate(sanitizedSpaceName, 63), c.truncate(sanitizedAppName, 63))
}
return hostname
}
func (c *Converter) truncate(s string, num int) string {
if len(s) <= num {
return s
}
return s[:num]
}
func (c *Converter) sanitizeHostname(originalName string) string {
return findTrailingDashes.ReplaceAllString(findInvalidCharactersHostname.ReplaceAllString(findSpaces.ReplaceAllString(originalName, "-"), ""), "")
}
func (c *Converter) sanitizeProcID(originalName string) string {
return findTrailingDashes.ReplaceAllString(findInvalidCharactersProcID.ReplaceAllString(findSpaces.ReplaceAllString(originalName, "-"), ""), "")
}
func (c *Converter) toRFC5424CounterMessage(env *loggregator_v2.Envelope, hostname, appID string) ([][]byte, error) {
counter := env.GetCounter()
sd := rfc5424.StructuredData{ID: counterStructuredDataID, Parameters: []rfc5424.SDParam{{Name: "name", Value: counter.GetName()}, {Name: "total", Value: strconv.FormatUint(counter.GetTotal(), 10)}, {Name: "delta", Value: strconv.FormatUint(counter.GetDelta(), 10)}}}
messageBinary, err := c.toRFC5424MetricMessage(env, hostname, appID, sd)
return [][]byte{messageBinary}, err
}
func (c *Converter) toRFC5424GaugeMessage(env *loggregator_v2.Envelope, hostname, appID string) ([][]byte, error) {
gauges := make([][]byte, 0, 5)
for name, g := range env.GetGauge().GetMetrics() {
sd := rfc5424.StructuredData{ID: gaugeStructuredDataID, Parameters: []rfc5424.SDParam{{Name: "name", Value: name}, {Name: "value", Value: strconv.FormatFloat(g.GetValue(), 'g', -1, 64)}, {Name: "unit", Value: g.GetUnit()}}}
guage, err := c.toRFC5424MetricMessage(env, hostname, appID, sd)
if err != nil {
return gauges, err
}
gauges = append(gauges, guage)
}
return gauges, nil
}
func (c *Converter) toRFC5424TimerMessage(env *loggregator_v2.Envelope, hostname, appID string) ([][]byte, error) {
timer := env.GetTimer()
sd := rfc5424.StructuredData{ID: timerStructuredDataID, Parameters: []rfc5424.SDParam{{Name: "name", Value: timer.GetName()}, {Name: "start", Value: strconv.FormatInt(timer.GetStart(), 10)}, {Name: "stop", Value: strconv.FormatInt(timer.GetStop(), 10)}}}
messageBinary, err := c.toRFC5424MetricMessage(env, hostname, appID, sd)
return [][]byte{messageBinary}, err
}
func (c *Converter) toRFC5424EventMessage(env *loggregator_v2.Envelope, hostname, appID string) ([][]byte, error) {
event := env.GetEvent()
sd := rfc5424.StructuredData{ID: eventStructuredDataID, Parameters: []rfc5424.SDParam{{Name: "title", Value: event.GetTitle()}, {Name: "body", Value: event.GetBody()}}}
messageBinary, err := c.toRFC5424MetricMessage(env, hostname, appID, sd)
return [][]byte{messageBinary}, err
}
func (c *Converter) toRFC5424LogMessage(env *loggregator_v2.Envelope, hostname, appID string) ([][]byte, error) {
priority := c.genPriority(env.GetLog().Type)
ts := time.Unix(0, env.GetTimestamp()).UTC()
hostname = c.nilify(hostname)
appID = c.nilify(appID)
pid := c.nilify(generateProcessID(
c.sanitizeProcID(env.Tags["source_type"]),
env.InstanceId,
))
msg := appendNewline(removeNulls(env.GetLog().Payload))
structuredDatas := []rfc5424.StructuredData{}
baseSD := c.buildTagsStructuredData(env.GetTags())
if baseSD.ID != "" {
structuredDatas = append(structuredDatas, baseSD)
}
message := rfc5424.Message{
Priority: rfc5424.Priority(priority),
Timestamp: ts,
Hostname: hostname,
AppName: appID,
ProcessID: pid,
Message: msg,
StructuredData: structuredDatas,
}
messageBinary, err := message.MarshalBinary()
return [][]byte{messageBinary}, err
}
func (c *Converter) buildTagsStructuredData(tags map[string]string) rfc5424.StructuredData {
if c.omitTags {
return rfc5424.StructuredData{}
}
var tagKeys []string
var tagsData []rfc5424.SDParam
for k := range tags {
tagKeys = append(tagKeys, k)
}
if len(tagKeys) == 0 {
return rfc5424.StructuredData{}
}
sort.Strings(tagKeys)
for _, k := range tagKeys {
tagsData = append(tagsData, rfc5424.SDParam{Name: k, Value: tags[k]})
}
return rfc5424.StructuredData{ID: tagsStructuredDataID, Parameters: tagsData}
}
func (c *Converter) toRFC5424MetricMessage(env *loggregator_v2.Envelope, hostname, appID string, structuredData rfc5424.StructuredData) ([]byte, error) {
ts := time.Unix(0, env.GetTimestamp()).UTC()
hostname = c.nilify(hostname)
appID = c.nilify(appID)
pid := "[" + env.InstanceId + "]"
priority := 14
structuredDatas := []rfc5424.StructuredData{structuredData}
baseSD := c.buildTagsStructuredData(env.GetTags())
if baseSD.ID != "" {
structuredDatas = append(structuredDatas, baseSD)
}
message := rfc5424.Message{
Priority: rfc5424.Priority(priority),
Timestamp: ts,
Hostname: hostname,
AppName: appID,
ProcessID: pid,
Message: []byte(""),
StructuredData: structuredDatas, //TODO: Fix this to get both structured datas
}
messageBinary, err := message.MarshalBinary()
return append(messageBinary, []byte(" \n")...), err
}
func (c *Converter) genPriority(logType loggregator_v2.Log_Type) int {
switch logType {
case loggregator_v2.Log_OUT:
return 14
case loggregator_v2.Log_ERR:
return 11
default:
return -1
}
}
func (c *Converter) nilify(x string) string {
if x == "" {
return "-"
}
return x
}