Skip to content

Commit

Permalink
add filter_cgolib
Browse files Browse the repository at this point in the history
1. add filter call go lib filter

Signed-off-by: longhui.li <[email protected]>
  • Loading branch information
llhhbc committed Apr 15, 2019
1 parent de308c7 commit 9bfaff4
Show file tree
Hide file tree
Showing 7 changed files with 468 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ option(FLB_FILTER_MODIFY "Enable modify filter" Yes)
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_CGOLIB "Enable cgolib filter" Yes)
option(FLB_FILTER_THROTTLE "Enable throttle filter" Yes)
option(FLB_FILTER_NEST "Enable nest filter" Yes)
option(FLB_FILTER_LUA "Enable Lua scripting 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 @@ -157,6 +157,7 @@ REGISTER_FILTER_PLUGIN("filter_throttle")

if(FLB_REGEX)
REGISTER_FILTER_PLUGIN("filter_kubernetes")
REGISTER_FILTER_PLUGIN("filter_cgolib")
REGISTER_FILTER_PLUGIN("filter_parser")
endif()

Expand Down
5 changes: 5 additions & 0 deletions plugins/filter_cgolib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set(src
cgolib.c
)

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


call golib from c

example go src:

```go

package main

import "C"
import (
"fmt"
)

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

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

return 0
}

//export Golib_filter
func Golib_filter(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 Golib_exit
func Golib_exit() int {

fmt.Println("go exit")

return 0
}

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 cgolib
Match *
golib_so /usr/local/libs/cgolib.so
url http://12321
url2 http://12321
```
51 changes: 51 additions & 0 deletions plugins/filter_cgolib/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 9bfaff4

Please sign in to comment.