Skip to content

Commit d449bba

Browse files
committed
Initial Commit.
0 parents  commit d449bba

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
all:
2+
go build -buildmode=c-shared -o out_sls.so .
3+
4+
clean:
5+
rm -rf *.so *.h *~

out_sls.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package main
2+
3+
import "github.com/fluent/fluent-bit-go/output"
4+
import (
5+
"C"
6+
"fmt"
7+
sls "github.com/galaxydi/go-loghub"
8+
"github.com/gogo/protobuf/proto"
9+
"github.com/ugorji/go/codec"
10+
"reflect"
11+
"unsafe"
12+
)
13+
14+
var project *sls.LogProject
15+
var logstore *sls.LogStore
16+
17+
//export FLBPluginInit
18+
func FLBPluginInit(ctx unsafe.Pointer) int {
19+
project = &sls.LogProject{
20+
Name: "loghub-test",
21+
Endpoint: "cn-hangzhou.log.aliyuncs.com",
22+
AccessKeyID: "xxx",
23+
AccessKeySecret: "xxx",
24+
}
25+
logstore_name := "test"
26+
logstore, _ = project.GetLogStore(logstore_name)
27+
return output.FLBPluginRegister(ctx, "sls", "Aliyun SLS output")
28+
}
29+
30+
//export FLBPluginFlush
31+
func FLBPluginFlush(data unsafe.Pointer, length C.int, tag *C.char) int {
32+
var h codec.Handle = new(codec.MsgpackHandle)
33+
var b []byte
34+
var m interface{}
35+
var err error
36+
37+
b = C.GoBytes(data, length)
38+
dec := codec.NewDecoderBytes(b, h)
39+
40+
// Iterate the original MessagePack array
41+
logs := []*sls.Log{}
42+
for {
43+
// Decode the entry
44+
err = dec.Decode(&m)
45+
if err != nil {
46+
break
47+
}
48+
49+
// Get a slice and their two entries: timestamp and map
50+
slice := reflect.ValueOf(m)
51+
timestamp := slice.Index(0)
52+
data := slice.Index(1)
53+
54+
// Convert slice data to a real map and iterate
55+
mapData := data.Interface().(map[interface{}]interface{})
56+
content := []*sls.LogContent{}
57+
for k, v := range mapData {
58+
content = append(content, &sls.LogContent{
59+
Key: proto.String(fmt.Sprintf("%s", k)),
60+
Value: proto.String(fmt.Sprintf("%s", v)),
61+
})
62+
}
63+
log := &sls.Log{
64+
Time: proto.Uint32(uint32(timestamp.Uint())),
65+
Contents: content,
66+
}
67+
logs = append(logs, log)
68+
}
69+
loggroup := &sls.LogGroup{
70+
Topic: proto.String(""),
71+
Source: proto.String("10.230.201.117"),
72+
Logs: logs,
73+
}
74+
err = logstore.PutLogs(loggroup)
75+
if err != nil {
76+
return output.FLB_ERROR
77+
}
78+
79+
// Return options:
80+
//
81+
// output.FLB_OK = data have been processed.
82+
// output.FLB_ERROR = unrecoverable error, do not try this again.
83+
// output.FLB_RETRY = retry to flush later.
84+
return output.FLB_OK
85+
}
86+
87+
//export FLBPluginExit
88+
func FLBPluginExit() int {
89+
return 0
90+
}
91+
92+
func main() {
93+
}

0 commit comments

Comments
 (0)