forked from newrelic/newrelic-fluent-bit-output
-
Notifications
You must be signed in to change notification settings - Fork 0
/
out_newrelic.go
96 lines (82 loc) · 2.43 KB
/
out_newrelic.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
package main
import (
"C"
"github.com/fluent/fluent-bit-go/output"
"github.com/newrelic/newrelic-fluent-bit-output/config"
"github.com/newrelic/newrelic-fluent-bit-output/nrclient"
"github.com/newrelic/newrelic-fluent-bit-output/record"
log "github.com/sirupsen/logrus"
"os"
"unsafe"
)
var (
nrClientRepo = make(map[string]*nrclient.NRClient)
dataFormatConfigRepo = make(map[string]config.DataFormatConfig)
)
//export FLBPluginRegister
func FLBPluginRegister(ctx unsafe.Pointer) int {
return output.FLBPluginRegister(ctx, "newrelic", "New relic output plugin")
}
//export FLBPluginInit
func FLBPluginInit(ctx unsafe.Pointer) int {
cfg, err := config.NewPluginConfig(ctx)
if err != nil {
log.WithField("error", err).Error("Error creating NewPluginConfig")
return output.FLB_ERROR
}
var nrClient *nrclient.NRClient
nrClient, err = nrclient.NewNRClient(cfg.NRClientConfig, cfg.ProxyConfig)
if err != nil {
log.WithField("error", err).Error("Error creating NewNRClient")
}
id := cfg.NRClientConfig.GetNewRelicKey()
nrClientRepo[id] = nrClient
dataFormatConfigRepo[id] = cfg.DataFormatConfig
output.FLBPluginSetContext(ctx, id)
return output.FLB_OK
}
//export FLBPluginFlushCtx
func FLBPluginFlushCtx(ctx, data unsafe.Pointer, length C.int, tag *C.char) int {
// Create Fluent Bit decoder
dec := output.NewDecoder(data, int(length))
// Get New Relic Client
id := output.FLBPluginGetContext(ctx).(string)
nrClient := nrClientRepo[id]
dataFormatConfig := dataFormatConfigRepo[id]
// Iterate, parse and accumulate records to be sent
var buffer []record.LogRecord
for {
// Extract Record
ret, ts, fbRecord := output.GetRecord(dec)
if ret != 0 {
break
}
buffer = append(buffer, record.RemapRecord(fbRecord, ts, VERSION, dataFormatConfig))
}
// 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.
retry, err := nrClient.Send(buffer)
if err != nil {
log.WithField("error", err).Error("Non-retryable error received. Retry:false")
return output.FLB_ERROR
}
if retry {
log.Debug("Retryable error received. Retry:true")
return output.FLB_RETRY
}
return output.FLB_OK
}
//export FLBPluginExit
func FLBPluginExit() int {
return output.FLB_OK
}
func main() {
logLevel, err := log.ParseLevel(os.Getenv("LOG_LEVEL"))
if err != nil {
logLevel = log.InfoLevel
}
log.SetLevel(logLevel)
}