-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathout.go
223 lines (191 loc) · 4.69 KB
/
out.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
package main
import (
"C"
"fmt"
"log"
"strings"
"time"
"unsafe"
"github.com/fluent/fluent-bit-go/output"
)
var (
msgKey = "message"
tsLayout = "20060102T15:04:05Z"
tsLoc *time.Location
optKeys []string
lastMsgByTag = map[string]string{}
lastMsgTsByTag = map[string]time.Time{}
skipDupMsg bool
skipDupMsgDur time.Duration
floorFloat bool
pluginVersion = "dev"
)
//export FLBPluginRegister
func FLBPluginRegister(def unsafe.Pointer) int {
return output.FLBPluginRegister(def, "telegram", "Telegram Output Plugin.")
}
// (fluentbit will call this)
// plugin (context) pointer to fluentbit context (state/ c code)
//
//export FLBPluginInit
func FLBPluginInit(plugin unsafe.Pointer) int {
getParam := func(key string) string {
p := output.FLBPluginConfigKey(plugin, key)
// Trim inline comment
if i := strings.Index(p, "#"); i != -1 {
p = p[:i]
}
return strings.TrimSpace(p)
}
isParamTrue := func(key string) bool {
switch p := strings.ToLower(getParam(key)); p {
case "yes", "on", "true":
return true
}
return false
}
tgApiToken := getParam("api_token")
tgRoomIDs := getParam("room_ids")
if err := initTgBot(tgApiToken, tgRoomIDs); err != nil {
log.Printf("fail to init telegram bot: %v", err)
return output.FLB_ERROR
}
if getParam("message_key") != "" {
msgKey = getParam("message_key")
}
if getParam("timestamp_layout") != "" {
tsLayout = getParam("timestamp_layout")
}
if getParam("timestamp_location") != "" {
var err error
tsLoc, err = time.LoadLocation(getParam("timestamp_location"))
if err != nil {
log.Printf("fail to load location: %v", err)
return output.FLB_ERROR
}
} else {
tsLoc, _ = time.LoadLocation("UTC")
}
if getParam("optional_keys") != "" {
optKeys = strings.Split(getParam("optional_keys"), ",")
for i, v := range optKeys {
optKeys[i] = strings.TrimSpace(v)
}
}
skipDupMsg = isParamTrue("suppress_duplication")
floorFloat = isParamTrue("floor_float")
var err error
skipDupMsgDur, err = time.ParseDuration(getParam("suppress_timeout"))
if err != nil {
log.Printf("fail to parse suppress_timeout: %v", err)
// return output.FLB_ERROR // not fatal
}
fmt.Printf("telegram output plugin %s initialized", pluginVersion)
return output.FLB_OK
}
//export FLBPluginFlush
func FLBPluginFlush(data unsafe.Pointer, length C.int, tag *C.char) int {
var ret int
var ts interface{}
var record map[interface{}]interface{}
dec := output.NewDecoder(data, int(length)) // Create Fluent Bit decoder
// count := 0 // batch out count
for {
ret, ts, record = output.GetRecord(dec)
if ret != 0 { // all record have been flushed
break
}
valueByKey := map[string]string{}
for k, v := range record {
valueByKey[str(k)] = str(v)
}
var msg string
var ok bool
if msg, ok = valueByKey[msgKey]; !ok {
log.Printf("message key not found: %v", msgKey)
return output.FLB_ERROR
}
tagStr := str(tag)
if lastMsg, ok := lastMsgByTag[tagStr]; ok && skipDupMsg {
if lastMsg == msg && time.Since(lastMsgTsByTag[tagStr]) < skipDupMsgDur {
continue
}
}
tsTime := getTime(ts)
lastMsgByTag[tagStr] = msg
lastMsgTsByTag[tagStr] = tsTime
tsStr := tsTime.In(tsLoc).Format(tsLayout)
var optMsg string
for _, k := range optKeys {
if v, ok := valueByKey[k]; ok {
optMsg += fmt.Sprintf("- %s: %s\n", k, v)
}
}
if optMsg != "" {
msg = fmt.Sprintf(
"%s\n---\n%s---\n%s",
msg, optMsg, tsStr,
)
} else {
msg = fmt.Sprintf(
"%s\n---\n%s",
msg, tsStr,
)
}
if err := sendMsgToTelegram(msg); err != nil {
log.Printf("fail to send msg to telegram: %v", err)
return output.FLB_ERROR
}
// count++
}
// Return options:
//
// output.FLB_OK = data have been processed.
// output.FLB_ERROR = unrecoverable error, do not try this again.
// output.FLB_RETRY = retry to flush later.
return output.FLB_OK
}
//export FLBPluginExit
func FLBPluginExit() int {
return output.FLB_OK
}
// ---
func str(v interface{}) string {
switch v := v.(type) {
case string:
return v
case []byte:
return string(v)
case *C.char:
return C.GoString(v)
case float64:
if floorFloat {
return fmt.Sprintf("%d", int(v+0.5))
} else {
return fmt.Sprintf("%f", v)
}
case float32:
if floorFloat {
return fmt.Sprintf("%d", int(v+0.5))
} else {
return fmt.Sprintf("%f", v)
}
default:
return fmt.Sprintf("%v", v)
}
}
func getTime(ts any) time.Time {
var timestamp time.Time
switch t := ts.(type) {
case output.FLBTime:
timestamp = ts.(output.FLBTime).Time
case uint64:
timestamp = time.Unix(int64(t), 0)
default:
fmt.Println("time provided invalid, defaulting to now.")
timestamp = time.Now()
}
return timestamp
}
func main() {
}