forked from newrelic/newrelic-fluent-bit-output
-
Notifications
You must be signed in to change notification settings - Fork 0
/
out_newrelic.go
102 lines (86 loc) · 2.77 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
97
98
99
100
101
102
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/metrics"
"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
}
metricsClient, err := metrics.NewClient(cfg.NRClientConfig)
if err != nil {
log.WithField("error", err).Error("Error creating Metrics client")
}
nrClient, err := nrclient.NewNRClient(cfg.NRClientConfig, cfg.ProxyConfig, metricsClient)
if err != nil {
log.WithField("error", err).Error("Error creating NewNRClient")
}
licenseKey := cfg.NRClientConfig.GetNewRelicKey()
nrClientRepo[licenseKey] = nrClient
dataFormatConfigRepo[licenseKey] = cfg.DataFormatConfig
output.FLBPluginSetContext(ctx, licenseKey)
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 retry {
log.WithField("error", err).Info("Retryable error received. Will retry to send the logs (if there are attempts remaining, check Retry_Limit option)")
return output.FLB_RETRY
}
if err != nil {
log.WithField("error", err).Error("Unexpected non-retryable error received. Logs were discarded.")
return output.FLB_ERROR
}
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)
}