-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvkcloudlogs-fluent-bit.go
129 lines (111 loc) · 3.47 KB
/
vkcloudlogs-fluent-bit.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
package main
import (
"fmt"
"runtime"
"unsafe"
"C"
"github.com/vk-cs/cloudlogs-fluent-bit/gen"
"github.com/vk-cs/cloudlogs-fluent-bit/vkcloudlogs"
fluent "github.com/fluent/fluent-bit-go/output"
)
var (
BuildGitVersion string
BuildTime string
buildVersion = fmt.Sprintf("VK Cloudlog Fluent Bit Plugin %s compiled at %s on %s",
BuildGitVersion,
BuildTime,
runtime.Version())
)
//export FLBPluginRegister
func FLBPluginRegister(def unsafe.Pointer) int {
return fluent.FLBPluginRegister(def, "vkcloudlogs", buildVersion)
}
//export FLBPluginInit
func FLBPluginInit(plugin unsafe.Pointer) int {
getCompatKey := func(keys ...string) (result string) {
for _, key := range keys {
result = fluent.FLBPluginConfigKey(plugin, key)
if result != "" {
break
}
}
return
}
cfg := vkcloudlogs.Config{
// Auth
IdentityEndpoint: getCompatKey("auth_url", "auth-url"),
KeyFile: fluent.FLBPluginConfigKey(plugin, "key_file"),
UserID: getCompatKey("user_id", "user-id"),
Username: getCompatKey("user_name", "user-name"),
Password: fluent.FLBPluginConfigKey(plugin, "password"),
ProjectID: getCompatKey("project_id", "project-id"),
// Server
ServerHostPort: getCompatKey("server_host_port", "serverhostport"),
Tls: fluent.FLBPluginConfigKey(plugin, "tls_on"),
TlsVerify: fluent.FLBPluginConfigKey(plugin, "tls_verify"),
// Tagging
ServiceID: fluent.FLBPluginConfigKey(plugin, "service_id"),
GroupID: getCompatKey("groupid", "group_id"),
StreamID: getCompatKey("streamid", "stream_id"),
DefaultPayload: fluent.FLBPluginConfigKey(plugin, "default_payload"),
InternalRaw: fluent.FLBPluginConfigKey(plugin, "internal"),
// Parsing
MessageKey: fluent.FLBPluginConfigKey(plugin, "message_key"),
LevelKey: fluent.FLBPluginConfigKey(plugin, "level_key"),
DefaultLevel: fluent.FLBPluginConfigKey(plugin, "default_level"),
GroupIDKey: fluent.FLBPluginConfigKey(plugin, "group_id_key"),
StreamIDKey: fluent.FLBPluginConfigKey(plugin, "stream_id_key"),
}
instance, err := vkcloudlogs.NewVKCloudLogs(&cfg, buildVersion)
if err != nil {
return fluent.FLB_ERROR
}
err = instance.Init()
if err != nil {
return fluent.FLB_ERROR
}
fluent.FLBPluginSetContext(plugin, instance)
return fluent.FLB_OK
}
//export FLBPluginFlushCtx
func FLBPluginFlushCtx(ctx, data unsafe.Pointer, length C.int, tag *C.char) int {
// get plugin instance
instance := fluent.FLBPluginGetContext(ctx).(*vkcloudlogs.VKCloudLogs)
return flush(instance, data, int(length), C.GoString(tag))
}
func flush(instance *vkcloudlogs.VKCloudLogs, data unsafe.Pointer, length int, fluentTag string) int {
var tagEntries = make(map[vkcloudlogs.Tag][]*gen.LogEntry)
const (
decoderOk = 0
decoderDone = -1
)
decoder := fluent.NewDecoder(data, length)
for {
// Extract data from MessagePack
decoderCode, recordTimestamp, record := fluent.GetRecord(decoder)
if decoderCode != decoderOk {
if decoderCode != decoderDone {
instance.Logger.Warn("Cannot decode record")
}
break
}
entry, tag := instance.Parse(recordTimestamp, record)
if tag.GroupID == "" {
tag.GroupID = fluentTag
}
tagEntries[tag] = append(tagEntries[tag], entry)
}
for tag, entries := range tagEntries {
result := instance.Write(tag, entries)
if result != fluent.FLB_OK {
return result
}
}
return fluent.FLB_OK
}
//export FLBPluginExit
func FLBPluginExit() int {
return fluent.FLB_OK
}
func main() {
}