Repository navigation
Expand file tree
/
Copy pathplugin.go
More file actions
90 lines (75 loc) · 2.25 KB
/
Copy pathplugin.go
File metadata and controls
90 lines (75 loc) · 2.25 KB
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
// Package main implements the Fluent Bit ↔ Go plugin boundary.
// All Site24x7 logic lives in pkg/site24x7 (pure Go, unit-testable without cgo).
package main
import (
"C"
"log"
"time"
"unsafe"
"github.com/fluent/fluent-bit-go/output"
"github.com/site24x7/fluent-bit-plugin-site24x7/pkg/site24x7"
)
//export FLBPluginRegister
func FLBPluginRegister(def unsafe.Pointer) int {
return output.FLBPluginRegister(def, "site24x7", "Site24x7 AppLogs output plugin")
}
//export FLBPluginInit
func FLBPluginInit(plugin unsafe.Pointer) int {
cfg := site24x7.PluginConfig{
LogTypeConfig: output.FLBPluginConfigKey(plugin, "log_type_config"),
HTTPProxy: output.FLBPluginConfigKey(plugin, "http_proxy"),
HTTPReadTimeout: output.FLBPluginConfigKey(plugin, "http_read_timeout"),
SkipSSLVerify: output.FLBPluginConfigKey(plugin, "skip_ssl_verify"),
}
client, err := site24x7.NewClient(cfg)
if err != nil {
log.Printf("[out_site24x7] initialization failed: %v", err)
return output.FLB_ERROR
}
// Support multiple [OUTPUT] instances, each with its own log_type_config.
output.FLBPluginSetContext(plugin, client)
log.Printf("[out_site24x7] initialized. uploading log type %q to %s", client.LogType(), client.UploadURL())
return output.FLB_OK
}
//export FLBPluginFlushCtx
func FLBPluginFlushCtx(ctx, data unsafe.Pointer, length C.int, tag *C.char) int {
client, ok := output.FLBPluginGetContext(ctx).(*site24x7.Client)
if !ok || client == nil {
log.Printf("[out_site24x7] flush called with no plugin context")
return output.FLB_ERROR
}
dec := output.NewDecoder(data, int(length))
var events []site24x7.Event
for {
ret, ts, record := output.GetRecord(dec)
if ret != 0 {
break
}
var timestamp time.Time
switch t := ts.(type) {
case output.FLBTime:
timestamp = t.Time
case uint64:
timestamp = time.Unix(int64(t), 0)
default:
timestamp = time.Now()
}
if event := client.BuildEvent(record, timestamp); event != nil {
events = append(events, event)
}
}
switch client.SendEvents(events) {
case site24x7.ResultRetry:
return output.FLB_RETRY
case site24x7.ResultDrop:
return output.FLB_ERROR
default:
return output.FLB_OK
}
}
//export FLBPluginExit
func FLBPluginExit() int {
return output.FLB_OK
}
func main() {
}