Skip to content

Commit a36b225

Browse files
authored
plugin_proxy: enable event_type specification for proxy plugins
1 parent 0915933 commit a36b225

10 files changed

Lines changed: 249 additions & 2 deletions

File tree

include/fluent-bit/flb_plugin_proxy.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct flb_plugin_proxy_def {
4040
int flags;
4141
char *name; /* plugin short name */
4242
char *description; /* plugin description */
43+
int event_type; /* event type (logs/metrics/traces) */
4344
};
4445

4546
/* Proxy context */

src/flb_plugin_proxy.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,14 @@ static int flb_proxy_register_output(struct flb_plugin_proxy *proxy,
362362
out->flags = def->flags;
363363
out->name = flb_strdup(def->name);
364364

365+
/* If event_type is unset (0) then default to logs (this is the current behavior) */
366+
if (def->event_type == 0) {
367+
out->event_type = FLB_OUTPUT_LOGS;
368+
}
369+
else {
370+
out->event_type = def->event_type;
371+
}
372+
365373
out->description = def->description;
366374
mk_list_add(&out->_head, &config->out_plugins);
367375

@@ -396,6 +404,7 @@ static int flb_proxy_register_input(struct flb_plugin_proxy *proxy,
396404
in->flags = def->flags | FLB_INPUT_THREADED;
397405
in->name = flb_strdup(def->name);
398406
in->description = def->description;
407+
399408
mk_list_add(&in->_head, &config->in_plugins);
400409

401410
/*
@@ -612,7 +621,7 @@ struct flb_plugin_proxy *flb_plugin_proxy_create(const char *dso_path, int type,
612621
return NULL;
613622
}
614623

615-
proxy->def = flb_malloc(sizeof(struct flb_plugin_proxy_def));
624+
proxy->def = flb_calloc(1, sizeof(struct flb_plugin_proxy_def));
616625
if (!proxy->def) {
617626
flb_errno();
618627
dlclose(handle);

tests/runtime_shell/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ set(UNIT_TESTS_SH
1717
processor_invalid.sh
1818
)
1919

20+
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
21+
list(APPEND UNIT_TESTS_SH proxy_logs_expect.sh)
22+
endif()
23+
2024
# Prepare list of unit tests
2125
foreach(script ${UNIT_TESTS_SH})
2226
add_test(NAME ${script}
@@ -27,6 +31,7 @@ foreach(script ${UNIT_TESTS_SH})
2731
"FLB_ROOT=${PROJECT_SOURCE_DIR};\
2832
FLB_RUNTIME_SHELL_PATH=${CMAKE_CURRENT_SOURCE_DIR};\
2933
FLB_RUNTIME_SHELL_CONF=${CMAKE_CURRENT_SOURCE_DIR}/conf;\
30-
FLB_BIN=${CMAKE_BINARY_DIR}/bin/fluent-bit"
34+
FLB_BIN=${CMAKE_BINARY_DIR}/bin/fluent-bit;\
35+
FLB_BUILD=${CMAKE_BINARY_DIR}"
3136
)
3237
endforeach()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[SERVICE]
2+
Flush 1
3+
Grace 2
4+
Log_Level info
5+
Daemon Off
6+
7+
[INPUT]
8+
Name dummy
9+
Dummy {"message": "test log entry", "level": "info"}
10+
Samples 3
11+
Tag test.logs
12+
13+
[OUTPUT]
14+
Name test_logs_go
15+
Match test.logs
16+
17+
[OUTPUT]
18+
Name file
19+
Match test.logs
20+
File ${SIGNAL_FILE_PATH}
21+
mkdir on

tests/runtime_shell/conf/proxy_metrics_test.conf

Whitespace-only changes.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#!/bin/bash
2+
3+
# Build script for Go test plugins
4+
set -e
5+
6+
GO_PLUGIN_DIR="${FLB_ROOT}/tests/runtime_shell/go_plugins"
7+
BUILD_DIR="${FLB_ROOT}/build"
8+
9+
install_go_if_needed() {
10+
if ! command -v go &> /dev/null; then
11+
echo "Go not found, installing Go..."
12+
13+
ARCH=$(uname -m)
14+
case $ARCH in
15+
x86_64) GO_ARCH="amd64" ;;
16+
aarch64|arm64) GO_ARCH="arm64" ;;
17+
*) echo "Unsupported architecture: $ARCH"; exit 1 ;;
18+
esac
19+
20+
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
21+
GO_VERSION="1.25.4"
22+
GO_TARBALL="go${GO_VERSION}.${OS}-${GO_ARCH}.tar.gz"
23+
GO_URL="https://golang.org/dl/${GO_TARBALL}"
24+
25+
echo "Downloading Go from $GO_URL..."
26+
27+
TEMP_DIR=$(mktemp -d)
28+
cd "$TEMP_DIR"
29+
30+
if command -v curl > /dev/null 2>&1; then
31+
curl -L -O "$GO_URL"
32+
else
33+
echo "Neither wget nor curl is available to download Go."
34+
exit 1
35+
fi
36+
37+
echo "Extracting Go tarball..."
38+
ls -la
39+
40+
if [ ! -f "$GO_TARBALL" ]; then
41+
echo "Failed to download Go tarball."
42+
exit 1
43+
fi
44+
45+
tar -xzf "$GO_TARBALL"
46+
47+
if [ -w "/usr/local" ]; then
48+
if [ -d /usr/local/go ]; then
49+
sudo rm -rf /usr/local/go
50+
fi
51+
sudo mv go /usr/local/go
52+
export PATH="/usr/local/go/bin:$PATH"
53+
else
54+
echo "No write permission to /usr/local. Installing Go to $HOME/.local/go"
55+
mkdir -p "$HOME/.local"
56+
rm -rf "$HOME/.local/go"
57+
mv go "$HOME/.local/go"
58+
export PATH="$HOME/.local/go/bin:$PATH"
59+
fi
60+
cd - > /dev/null
61+
rm -rf "$TEMP_DIR"
62+
echo "Go installed successfully."
63+
go version
64+
else
65+
echo "Go is already installed."
66+
fi
67+
}
68+
69+
verify_go_cgo() {
70+
echo "Verifying Go CGO support..."
71+
if ! go env CGO_ENABLED | grep -q "1"; then
72+
echo "Warning: CGO is not enabled. Attempting to enable CGO..."
73+
export CGO_ENABLED=1
74+
fi
75+
76+
TEMP_GO_FILE=$(mktemp --suffix=.go)
77+
cat > "$TEMP_GO_FILE" << 'EOF'
78+
package main
79+
import "C"
80+
//export TestFunc
81+
func TestFunc() {}
82+
func main() {}
83+
EOF
84+
TEMP_SO_FILE=$(mktemp --suffix=.so)
85+
if go build -buildmode=c-shared -o "$TEMP_SO_FILE" "$TEMP_GO_FILE" 2> /dev/null; then
86+
echo "CGO is enabled and working."
87+
rm -f "$TEMP_GO_FILE" "$TEMP_SO_FILE"
88+
else
89+
echo "Error: CGO is not enabled or not working properly. Please ensure you have a C compiler installed."
90+
rm -f "$TEMP_GO_FILE" "$TEMP_SO_FILE"
91+
exit 1
92+
fi
93+
}
94+
95+
build_go_plugins() {
96+
echo "Building Go test plugins..."
97+
98+
echo "Building logs output plugin..."
99+
cd "$GO_PLUGIN_DIR"
100+
CGO_ENABLED=1 GO111MODULE=on go build -buildmode=c-shared -v -ldflags="-s -w" -o $BUILD_DIR/test_logs_go.so logs_output.go
101+
if [ $? -eq 0 ]; then
102+
echo "Go test plugins built successfully!"
103+
echo "Logs plugin: $BUILD_DIR/test_logs_go.so"
104+
else
105+
echo "Failed to build Go test plugins."
106+
exit 1
107+
fi
108+
}
109+
110+
echo "Setting up Go build environment..."
111+
install_go_if_needed
112+
verify_go_cgo
113+
build_go_plugins
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/fluent/fluent-bit/tests/runtime_shell/go_plugins
2+
3+
go 1.25.1
4+
5+
require (
6+
github.com/fluent/fluent-bit-go v0.0.0-20230731091245-a7a013e2473c
7+
github.com/ugorji/go/codec v1.1.7 // indirect
8+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
github.com/fluent/fluent-bit-go v0.0.0-20230731091245-a7a013e2473c h1:yKN46XJHYC/gvgH2UsisJ31+n4K3S7QYZSfU2uAWjuI=
2+
github.com/fluent/fluent-bit-go v0.0.0-20230731091245-a7a013e2473c/go.mod h1:L92h+dgwElEyUuShEwjbiHjseW410WIcNz+Bjutc8YQ=
3+
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
4+
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
5+
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"C"
5+
"fmt"
6+
"unsafe"
7+
8+
"github.com/fluent/fluent-bit-go/output"
9+
)
10+
11+
//export FLBPluginRegister
12+
func FLBPluginRegister(def unsafe.Pointer) int {
13+
// Register as logs-only output plugin
14+
return output.FLBPluginRegister(def, "test_logs_go", "Test Go Output Plugin for Logs")
15+
}
16+
17+
//export FLBPluginInit
18+
func FLBPluginInit(plugin unsafe.Pointer) int {
19+
return output.FLB_OK
20+
}
21+
22+
//export FLBPluginFlushCtx
23+
func FLBPluginFlushCtx(ctx, data unsafe.Pointer, length C.int, tag *C.char) int {
24+
// Write to a stdout to verify it received data
25+
dec := output.NewDecoder(data, int(length))
26+
var logrecords []string
27+
for {
28+
ret, _, record := output.GetRecord(dec)
29+
if ret != 0 {
30+
break
31+
}
32+
logrecords = append(logrecords, fmt.Sprintf("%v", record))
33+
}
34+
for _, record := range logrecords {
35+
fmt.Printf("%s\n", record)
36+
}
37+
38+
return output.FLB_OK
39+
}
40+
41+
//export FLBPluginExit
42+
func FLBPluginExit() int {
43+
return output.FLB_OK
44+
}
45+
46+
func main() {
47+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/sh
2+
3+
# Setup environment if not already set
4+
if [ -z "$FLB_BIN" ]; then
5+
FLB_ROOT=${FLB_ROOT:-$(cd $(dirname $0)/../.. && pwd)}
6+
FLB_BIN=${FLB_BIN:-$FLB_ROOT/build/bin/fluent-bit}
7+
fi
8+
9+
echo "Using Fluent Bit at: $FLB_BIN"
10+
11+
. $FLB_RUNTIME_SHELL_PATH/go_plugins/build_test_plugins.sh
12+
13+
test_proxy_logs_compatibility() {
14+
export SIGNAL_FILE_PATH="/tmp/flb_signal_logs_$$.txt"
15+
STDOUT_OUTPUT_FILE="/tmp/test_logs_stdout_$$.txt"
16+
17+
rm -f "$STDOUT_OUTPUT_FILE" "$SIGNAL_FILE_PATH"
18+
19+
$FLB_BIN -e $FLB_ROOT/build/test_logs_go.so -c $FLB_RUNTIME_SHELL_CONF/proxy_logs_test.conf > "$STDOUT_OUTPUT_FILE" 2>&1 &
20+
FLB_PID=$!
21+
22+
sleep 3
23+
24+
if [ -f "$STDOUT_OUTPUT_FILE" ]; then
25+
echo "SUCCESS: Captured Fluent Bit output"
26+
echo "Output contents:"
27+
cat "$STDOUT_OUTPUT_FILE"
28+
else
29+
echo "FAIL: No stdout output captured"
30+
return 1
31+
fi
32+
33+
# Clean up
34+
rm -f "$STDOUT_OUTPUT_FILE" "$SIGNAL_FILE_PATH"
35+
}
36+
37+
# Load the runtime shell environment
38+
. $FLB_RUNTIME_SHELL_PATH/runtime_shell.env

0 commit comments

Comments
 (0)