Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filter:cgolib #1277

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,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_MULTILINE "Enable multiline 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 @@ -263,6 +263,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_multiline")
REGISTER_FILTER_PLUGIN("filter_nest")
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. */
llhhbc marked this conversation as resolved.
Show resolved Hide resolved

/* 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