-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoutput.go
114 lines (98 loc) · 3.43 KB
/
output.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
// Fluent Bit Go!
// ==============
// Copyright (C) 2015-2017 Treasure Data Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package output
/*
#include <stdlib.h>
#include "flb_plugin.h"
#include "flb_output.h"
*/
import "C"
import (
"sync"
"unsafe"
cmetrics "github.com/calyptia/cmetrics-go"
)
// Define constants matching Fluent Bit core
const (
FLB_ERROR = C.FLB_ERROR
FLB_OK = C.FLB_OK
FLB_RETRY = C.FLB_RETRY
FLB_PROXY_OUTPUT_PLUGIN = C.FLB_PROXY_OUTPUT_PLUGIN
FLB_PROXY_GOLANG = C.FLB_PROXY_GOLANG
FLB_LOG_ERROR = C.FLB_LOG_ERROR
FLB_LOG_WARN = C.FLB_LOG_WARN
FLB_LOG_INFO = C.FLB_LOG_INFO
FLB_LOG_DEBUG = C.FLB_LOG_DEBUG
)
type (
// FLBPluginProxyDef local type.
FLBPluginProxyDef C.struct_flb_plugin_proxy_def
FLBOutPlugin C.struct_flbgo_output_plugin
)
// FLBPluginRegister when the FLBPluginInit is triggered by Fluent Bit, a plugin context
// is passed and the next step is to invoke this FLBPluginRegister() function
// to fill the required information: type, proxy type, flags name and
// description.
func FLBPluginRegister(def unsafe.Pointer, name, desc string) int {
p := (*FLBPluginProxyDef)(def)
p._type = FLB_PROXY_OUTPUT_PLUGIN
p.proxy = FLB_PROXY_GOLANG
p.flags = 0
p.name = C.CString(name)
p.description = C.CString(desc)
return 0
}
// FLBPluginUnregister release resources allocated by the plugin initialization
func FLBPluginUnregister(def unsafe.Pointer) {
p := (*FLBPluginProxyDef)(def)
C.free(unsafe.Pointer(p.name))
C.free(unsafe.Pointer(p.description))
}
func FLBPluginConfigKey(plugin unsafe.Pointer, key string) string {
_key := C.CString(key)
value := C.GoString(C.output_get_property(_key, plugin))
C.free(unsafe.Pointer(_key))
return value
}
var contexts sync.Map
// FLBPluginSetContext sets the context for plugin to ctx.
//
// Limit FLBPluginSetContext calls to once per plugin instance for best performance.
func FLBPluginSetContext(plugin unsafe.Pointer, ctx interface{}) {
// Allocate a byte of memory in the C heap and fill it with '\0',
// then convert its pointer into the C type void*, represented by unsafe.Pointer.
// The C string is not managed by Go GC, so it will not be freed automatically.
i := unsafe.Pointer(C.CString(""))
// uintptr(i) returns the memory address of i, which is unique in the heap.
contexts.Store(uintptr(i), ctx)
p := (*FLBOutPlugin)(plugin)
p.context.remote_context = i
}
// FLBPluginGetContext reads the context associated with proxyCtx.
func FLBPluginGetContext(proxyCtx unsafe.Pointer) interface{} {
v, _ := contexts.Load(uintptr(proxyCtx))
return v
}
func FLBPluginGetCMetricsContext(plugin unsafe.Pointer) (*cmetrics.Context, error) {
cmt := C.output_get_cmt_instance(plugin)
return cmetrics.NewContextFromCMTPointer(cmt)
}
func FLBPluginLogPrint(plugin unsafe.Pointer, log_level C.int, message string) {
_message := C.CString(message)
C.output_log_print_novar(plugin, log_level, _message)
C.free(unsafe.Pointer(_message))
}