-
Notifications
You must be signed in to change notification settings - Fork 2
/
out_mongo.go
175 lines (136 loc) · 3.84 KB
/
out_mongo.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"C"
"context"
"errors"
"fmt"
"time"
"unsafe"
"github.com/fluent/fluent-bit-go/output"
"github.com/saagie/fluent-bit-mongo/pkg/config"
flbcontext "github.com/saagie/fluent-bit-mongo/pkg/context"
"github.com/saagie/fluent-bit-mongo/pkg/entry"
"github.com/saagie/fluent-bit-mongo/pkg/entry/mongo"
"github.com/saagie/fluent-bit-mongo/pkg/log"
mgo "gopkg.in/mgo.v2"
)
const PluginID = "mongo"
//export FLBPluginRegister
func FLBPluginRegister(ctxPointer unsafe.Pointer) int {
logger, err := log.New(log.OutputPlugin, PluginID)
if err != nil {
fmt.Printf("error initializing logger: %s\n", err)
return output.FLB_ERROR
}
logger.Info("Registering plugin", nil)
result := output.FLBPluginRegister(ctxPointer, PluginID, "Go mongo go")
switch result {
case output.FLB_OK:
flbcontext.Set(ctxPointer, &flbcontext.Value{
Logger: logger,
})
default:
// nothing to do
}
return result
}
//export FLBPluginInit
// (fluentbit will call this)
// ctx (context) pointer to fluentbit context (state/ c code)
func FLBPluginInit(ctxPointer unsafe.Pointer) int {
value, err := flbcontext.Get(ctxPointer)
if err != nil {
logger, err := log.New(log.OutputPlugin, PluginID)
if err != nil {
fmt.Printf("error initializing logger: %s\n", err)
return output.FLB_ERROR
}
logger.Info("New logger initialized", nil)
value.Logger = logger
}
value.Logger.Info("Initializing plugin", nil)
value.Config = config.GetConfig(ctxPointer)
flbcontext.Set(ctxPointer, value)
return output.FLB_OK
}
//export FLBPluginFlush
func FLBPluginFlush(data unsafe.Pointer, length C.int, tag *C.char) int {
panic(errors.New("not supported call"))
}
//export FLBPluginFlushCtx
func FLBPluginFlushCtx(ctxPointer, data unsafe.Pointer, length C.int, tag *C.char) (result int) {
value, err := flbcontext.Get(ctxPointer)
if err != nil {
fmt.Printf("error getting value: %s\n", err)
return output.FLB_ERROR
}
logger := value.Logger
ctx := log.WithLogger(context.TODO(), logger)
// Open mongo session
config := value.Config.(*mgo.DialInfo)
logger.Info("Connecting to mongodb", map[string]interface{}{
"hosts": config.Addrs,
"user": config.Username,
"source": config.Source,
"database": config.Database,
"with_password": config.Password != "",
})
session, err := mgo.DialWithInfo(config)
if err != nil {
logger.Error("Failed to connect to mongodb", map[string]interface{}{
"error": err,
})
return output.FLB_RETRY
}
defer session.Close()
dec := output.NewDecoder(data, int(length)) // Create Fluent Bit decoder
processor := mongo.New(session)
if err := ProcessAll(ctx, dec, processor); err != nil {
logger.Error("Failed to process logs", map[string]interface{}{
"error": err,
})
if errors.Is(err, &entry.ErrRetry{}) {
return output.FLB_RETRY
}
return output.FLB_ERROR
}
// 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.
return output.FLB_OK
}
func ProcessAll(ctx context.Context, dec *output.FLBDecoder, processor entry.Processor) error {
// For log purpose
startTime := time.Now()
total := 0
logger, err := log.GetLogger(ctx)
if err != nil {
return fmt.Errorf("get logger: %w", err)
}
// Iterate Records
for {
// Extract Record
ts, record, err := entry.GetRecord(dec)
if err != nil {
if errors.Is(err, entry.ErrNoRecord) {
logger.Debug("Records flushed", map[string]interface{}{
"count": total,
"duration": time.Since(startTime),
})
break
}
return fmt.Errorf("get record: %w", err)
}
total++
if err := processor.ProcessRecord(ctx, ts, record); err != nil {
return fmt.Errorf("process record: %w", err)
}
}
return nil
}
//export FLBPluginExit
func FLBPluginExit() int {
return output.FLB_OK
}