This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathout_rabbitmq.go
executable file
·188 lines (157 loc) · 4.71 KB
/
out_rabbitmq.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
package main
import (
"C"
"encoding/json"
"log"
"strconv"
"unsafe"
"github.com/fluent/fluent-bit-go/output"
"github.com/streadway/amqp"
)
var (
connection *amqp.Connection
channel *amqp.Channel
exchangeName string
routingKey string
routingKeyDelimiter string
removeRkValuesFromRecord bool
addTagToRecord bool
addTimestampToRecord bool
)
//export FLBPluginRegister
func FLBPluginRegister(def unsafe.Pointer) int {
// Gets called only once when the plugin.so is loaded
return output.FLBPluginRegister(def, "rabbitmq", "Stdout GO!")
}
//export FLBPluginInit
func FLBPluginInit(plugin unsafe.Pointer) int {
// Gets called only once for each instance you have configured.
var err error
host := output.FLBPluginConfigKey(plugin, "RabbitHost")
port := output.FLBPluginConfigKey(plugin, "RabbitPort")
user := output.FLBPluginConfigKey(plugin, "RabbitUser")
password := output.FLBPluginConfigKey(plugin, "RabbitPassword")
exchangeName = output.FLBPluginConfigKey(plugin, "ExchangeName")
exchangeType := output.FLBPluginConfigKey(plugin, "ExchangeType")
routingKey = output.FLBPluginConfigKey(plugin, "RoutingKey")
routingKeyDelimiter = output.FLBPluginConfigKey(plugin, "RoutingKeyDelimiter")
removeRkValuesFromRecordStr := output.FLBPluginConfigKey(plugin, "RemoveRkValuesFromRecord")
addTagToRecordStr := output.FLBPluginConfigKey(plugin, "AddTagToRecord")
addTimestampToRecordStr := output.FLBPluginConfigKey(plugin, "AddTimestampToRecord")
if len(routingKeyDelimiter) < 1 {
routingKeyDelimiter = "."
logInfo("The routing-key-delimiter is set to the default value '" + routingKeyDelimiter + "' ")
}
removeRkValuesFromRecord, err = strconv.ParseBool(removeRkValuesFromRecordStr)
if err != nil {
logError("Couldn't parse RemoveRkValuesFromRecord to boolean: ", err)
return output.FLB_ERROR
}
addTagToRecord, err = strconv.ParseBool(addTagToRecordStr)
if err != nil {
logError("Couldn't parse AddTagToRecord to boolean: ", err)
return output.FLB_ERROR
}
addTimestampToRecord, err = strconv.ParseBool(addTimestampToRecordStr)
if err != nil {
logError("Couldn't parse AddTimestampToRecord to boolean: ", err)
return output.FLB_ERROR
}
err = RoutingKeyIsValid(routingKey, routingKeyDelimiter)
if err != nil {
logError("The Parsing of the Routing-Key failed: ", err)
return output.FLB_ERROR
}
connection, err = amqp.Dial("amqp://" + user + ":" + password + "@" + host + ":" + port + "/")
if err != nil {
logError("Failed to establish a connection to RabbitMQ: ", err)
return output.FLB_ERROR
}
channel, err = connection.Channel()
if err != nil {
logError("Failed to open a channel: ", err)
connection.Close()
return output.FLB_ERROR
}
logInfo("Established successfully a connection to the RabbitMQ-Server")
err = channel.ExchangeDeclare(
exchangeName, // name
exchangeType, // type
true, // durable
false, // auto-deleted
false, // internal
false, // no-wait
nil, // arguments
)
if err != nil {
logError("Failed to declare an exchange: ", err)
connection.Close()
return output.FLB_ERROR
}
return output.FLB_OK
}
//export FLBPluginFlushCtx
func FLBPluginFlushCtx(ctx, data unsafe.Pointer, length C.int, tag *C.char) int {
// Gets called with a batch of records to be written to an instance.
// Create Fluent Bit decoder
dec := output.NewDecoder(data, int(length))
// Iterate Records
for {
// Extract Record
ret, ts, record := output.GetRecord(dec)
if ret != 0 {
break
}
timestamp := ts.(output.FLBTime)
parsedRecord := ParseRecord(record)
if addTagToRecord {
parsedRecord["@tag"] = C.GoString(tag)
}
if addTimestampToRecord {
parsedRecord["@timestamp"] = timestamp.String()
}
rk, err := CreateRoutingKey(routingKey, &parsedRecord, routingKeyDelimiter)
if err != nil {
logError("Couldn't create the Routing-Key", err)
continue
}
jsonString, err := json.Marshal(parsedRecord)
if err != nil {
logError("Couldn't parse record: ", err)
continue
}
err = channel.Publish(
exchangeName, // exchange
rk, // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "application/json",
Body: jsonString,
})
if err != nil {
logError("Couldn't publish record: ", err)
}
}
return output.FLB_OK
}
//export FLBPluginExit
func FLBPluginExit() int {
return output.FLB_OK
}
func logInfo(msg string) {
log.Printf("%s", msg)
}
func logError(msg string, err error) {
log.Printf("%s: %s", msg, err)
}
func arrayContainsString(arr []string, str string) bool {
for _, item := range arr {
if item == str {
return true
}
}
return false
}
func main() {
}