Skip to content

Commit

Permalink
Add filter go
Browse files Browse the repository at this point in the history
    Signed-off-by:  longhui.li <[email protected]>
  • Loading branch information
llhhbc committed Jan 9, 2021
1 parent 69a52b4 commit 0ecef46
Show file tree
Hide file tree
Showing 7 changed files with 482 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ option(FLB_FILTER_STDOUT "Enable stdout filter" Yes)
option(FLB_FILTER_PARSER "Enable parser filter" Yes)
option(FLB_FILTER_KUBERNETES "Enable kubernetes filter" Yes)
option(FLB_FILTER_REWRITE_TAG "Enable tag rewrite filter" Yes)
option(FLB_FILTER_GO "Enable Go filter" Yes)
option(FLB_FILTER_THROTTLE "Enable throttle filter" Yes)
option(FLB_FILTER_THROTTLE_SIZE "Enable throttle size filter" No)
option(FLB_FILTER_NEST "Enable nest filter" Yes)
Expand Down
1 change: 1 addition & 0 deletions plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ REGISTER_FILTER_PLUGIN("filter_tensorflow")

if(FLB_REGEX)
REGISTER_FILTER_PLUGIN("filter_kubernetes")
REGISTER_FILTER_PLUGIN("filter_go")
REGISTER_FILTER_PLUGIN("filter_modify")
REGISTER_FILTER_PLUGIN("filter_nest")
REGISTER_FILTER_PLUGIN("filter_parser")
Expand Down
5 changes: 5 additions & 0 deletions plugins/filter_go/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set(src
go.c
)

FLB_PLUGIN(filter_go "${src}" "")
113 changes: 113 additions & 0 deletions plugins/filter_go/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@


call golib from c

example go src:

```go

package main

import "C"
import (
"fmt"
)

const FLB_OK = 0

//export FLBPluginInit
func FLBPluginInit(name, value []string) int {

fmt.Printf("get name %#v. ", name)
fmt.Printf("get value %#v. ", value)

return FLB_OK
}

//export FLBPluginFilter
func FLBPluginFilter(srcName, srcValue []string) int {
// go will return the result in the slice which called in. so can't append item that max slice's cap.
// must use cgoAppend instead of append if you want append value.
// use cgoSetSlice if you want change value of slice or just append.

src := loadCallIn(srcName, srcValue)

src["myadd"] ="teat"

return unLoadCallIn(src, srcName, srcValue)
}

//export FLBPluginExit
func FLBPluginExit() int {

fmt.Println("go exit")

return FLB_OK
}

func loadCallIn(name, value[]string) map[string]string {
res := make(map[string]string)
for idx, n := range name {
res[n] = value[idx]
}
return res
}

func unLoadCallIn(src map[string]string, name, value []string) int {
index := 0
for k, v := range src {
name = cgoSetSlice(name, index, k)
value = cgoSetSlice(value, index, v)
index++
}
fmt.Println("unload ok: ", name, value)
return index;
}


// can't append parameters that will over slice's cap.
// if over slice's cap, then go will malloc new memory, the c can't get the results.
func cgoAppend(src []string, parameters... string) []string {
if cap(src) - len(src) < len(parameters) {
fmt.Println(" cann't set parameters. slice cap is full. ", len(src) , cap(src), len(parameters))
return src
}
src = append(src, parameters...)
return src
}

func cgoSetSlice(src []string, index int, value string) []string {
if index > cap(src) {
fmt.Println(" cann't set parameters. index overflow. ", index, cap(src))
return src
}

if index >= len(src) {
src = append(src, value)
} else {
src[index] = value
}

return src
}

func main() {}

```

build

```shell
go build -o cgolib.so -buildmode=c-shared cgolib.go
```

config

```ini
[FILTER]
Name go
Match *
golib_so /usr/local/libs/cgolib.so
url http://example1.com
url2 http://example2.com
```
51 changes: 51 additions & 0 deletions plugins/filter_go/cgo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* Created by "go tool cgo" - DO NOT EDIT. */

/* package command-line-arguments */


#line 1 "cgo-builtin-prolog"

#include <stddef.h> /* for ptrdiff_t below */

#ifndef GO_CGO_EXPORT_PROLOGUE_H
#define GO_CGO_EXPORT_PROLOGUE_H

typedef struct { const char *p; ptrdiff_t n; } _GoString_;

#endif

/* Start of boilerplate cgo prologue. */
#line 1 "cgo-gcc-export-header-prolog"

#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H

typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
//typedef __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
//typedef float _Complex GoComplex64;
//typedef double _Complex GoComplex128;

/*
static assertion to make sure the file is being used on architecture
at least with matching size of GoInt.
*/
//typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];

typedef _GoString_ GoString;
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;

#endif
Loading

0 comments on commit 0ecef46

Please sign in to comment.