This repository has been archived by the owner on Nov 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
rfc5424.go
128 lines (108 loc) · 3.81 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
package syslog
import (
"fmt"
"strconv"
"time"
"code.cloudfoundry.org/go-loggregator/rpc/loggregator_v2"
)
const RFC5424TimeOffsetNum = "2006-01-02T15:04:05.999999-07:00"
func ToRFC5424(env *loggregator_v2.Envelope, hostname, appID string) ([][]byte, error) {
if len(hostname) > 255 {
return nil, invalidValue("Hostname", hostname)
}
if len(appID) > 48 {
return nil, invalidValue("AppName", appID)
}
if len(env.InstanceId) > 128 {
return nil, invalidValue("AppName", appID)
}
switch env.GetMessage().(type) {
case *loggregator_v2.Envelope_Log:
return [][]byte{
toRFC5424LogMessage(env, hostname, appID),
}, nil
case *loggregator_v2.Envelope_Gauge:
return toRFC5424GaugeMessage(env, hostname, appID), nil
case *loggregator_v2.Envelope_Counter:
return [][]byte{
toRFC5424CounterMessage(env, hostname, appID),
}, nil
default:
return nil, nil
}
}
func invalidValue(property, value string) error {
return fmt.Errorf("Invalid value \"%s\" for property %s \n", value, property)
}
func toRFC5424CounterMessage(env *loggregator_v2.Envelope, hostname, appID string) []byte {
priority := "14"
ts := time.Unix(0, env.GetTimestamp()).UTC().Format(RFC5424TimeOffsetNum)
hostname = nilify(hostname)
appID = nilify(appID)
pid := "[" + env.InstanceId + "]"
sd := `[counter@47450 name="` + env.GetCounter().GetName() + `" total="` + strconv.FormatUint(env.GetCounter().GetTotal(), 10) + `" delta="` + strconv.FormatUint(env.GetCounter().GetDelta(), 10) + `"]`
counter := make([]byte, 0, 20+len(priority)+len(ts)+len(hostname)+len(appID)+len(pid)+len(sd))
counter = append(counter, []byte("<"+priority+">1 ")...)
counter = append(counter, []byte(ts+" ")...)
counter = append(counter, []byte(hostname+" ")...)
counter = append(counter, []byte(appID+" ")...)
counter = append(counter, []byte(pid+" - ")...)
counter = append(counter, []byte(sd+" \n")...)
return counter
}
func toRFC5424GaugeMessage(env *loggregator_v2.Envelope, hostname, appID string) [][]byte {
gauges := make([][]byte, 0, 5)
ts := time.Unix(0, env.GetTimestamp()).UTC().Format(RFC5424TimeOffsetNum)
pid := "[" + env.InstanceId + "]"
priority := "14"
hostname = nilify(hostname)
appID = nilify(appID)
for name, g := range env.GetGauge().GetMetrics() {
sd := `[gauge@47450 name="` + name + `" value="` + strconv.FormatFloat(g.GetValue(), 'g', -1, 64) + `" unit="` + g.GetUnit() + `"]`
gauge := make([]byte, 0, 20+len(priority)+len(ts)+len(hostname)+len(appID)+len(pid)+len(sd))
gauge = append(gauge, []byte("<"+priority+">1 ")...)
gauge = append(gauge, []byte(ts+" ")...)
gauge = append(gauge, []byte(hostname+" ")...)
gauge = append(gauge, []byte(appID+" ")...)
gauge = append(gauge, []byte(pid+" - ")...)
gauge = append(gauge, []byte(sd+" \n")...)
gauges = append(gauges, gauge)
}
return gauges
}
func toRFC5424LogMessage(env *loggregator_v2.Envelope, hostname, appID string) []byte {
priority := genPriority(env.GetLog().Type)
ts := time.Unix(0, env.GetTimestamp()).UTC().Format(RFC5424TimeOffsetNum)
hostname = nilify(hostname)
appID = nilify(appID)
pid := nilify(generateProcessID(
env.Tags["source_type"],
env.InstanceId,
))
msg := appendNewline(removeNulls(env.GetLog().Payload))
tmp := make([]byte, 0, 20+len(priority)+len(ts)+len(hostname)+len(appID)+len(pid)+len(msg))
tmp = append(tmp, []byte("<"+priority+">1 ")...)
tmp = append(tmp, []byte(ts+" ")...)
tmp = append(tmp, []byte(hostname+" ")...)
tmp = append(tmp, []byte(appID+" ")...)
tmp = append(tmp, []byte(pid+" ")...)
tmp = append(tmp, []byte("- - ")...)
tmp = append(tmp, msg...)
return tmp
}
func genPriority(logType loggregator_v2.Log_Type) string {
switch logType {
case loggregator_v2.Log_OUT:
return "14"
case loggregator_v2.Log_ERR:
return "11"
default:
return "-1"
}
}
func nilify(x string) string {
if x == "" {
return "-"
}
return x
}