diff options
Diffstat (limited to 'fluent-bit/examples/filter_rust_clib')
-rw-r--r-- | fluent-bit/examples/filter_rust_clib/Cargo.lock | 219 | ||||
-rw-r--r-- | fluent-bit/examples/filter_rust_clib/Cargo.toml | 15 | ||||
-rw-r--r-- | fluent-bit/examples/filter_rust_clib/Makefile | 25 | ||||
-rw-r--r-- | fluent-bit/examples/filter_rust_clib/README.md | 77 | ||||
-rw-r--r-- | fluent-bit/examples/filter_rust_clib/rust_clib_filter.c | 9 | ||||
-rw-r--r-- | fluent-bit/examples/filter_rust_clib/src/lib.rs | 32 |
6 files changed, 377 insertions, 0 deletions
diff --git a/fluent-bit/examples/filter_rust_clib/Cargo.lock b/fluent-bit/examples/filter_rust_clib/Cargo.lock new file mode 100644 index 000000000..577860a85 --- /dev/null +++ b/fluent-bit/examples/filter_rust_clib/Cargo.lock @@ -0,0 +1,219 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + +[[package]] +name = "chrono" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" +dependencies = [ + "libc", + "num-integer", + "num-traits", + "time", + "winapi", +] + +[[package]] +name = "filter_rust_clib" +version = "0.1.0" +dependencies = [ + "chrono", + "libc", + "rmp-serde", + "serde", + "serde_bytes", + "serde_json", +] + +[[package]] +name = "itoa" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d" + +[[package]] +name = "libc" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + +[[package]] +name = "paste" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c520e05135d6e763148b6426a837e239041653ba7becd2e538c076c738025fc" + +[[package]] +name = "proc-macro2" +version = "1.0.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c278e965f1d8cf32d6e0e96de3d3e79712178ae67986d9cf9151f51e95aac89b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bcdf212e9776fbcb2d23ab029360416bb1706b1aea2d1a5ba002727cbcab804" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rmp" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44519172358fd6d58656c86ab8e7fbc9e1490c3e8f14d35ed78ca0dd07403c9f" +dependencies = [ + "byteorder", + "num-traits", + "paste", +] + +[[package]] +name = "rmp-serde" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25786b0d276110195fa3d6f3f31299900cf71dfbd6c28450f3f58a0e7f7a347e" +dependencies = [ + "byteorder", + "rmp", + "serde", +] + +[[package]] +name = "ryu" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695" + +[[package]] +name = "serde" +version = "1.0.140" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc855a42c7967b7c369eb5860f7164ef1f6f81c20c7cc1141f2a604e18723b03" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_bytes" +version = "0.11.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "212e73464ebcde48d723aa02eb270ba62eff38a9b732df31f33f1b4e145f3a54" +dependencies = [ + "serde", +] + +[[package]] +name = "serde_derive" +version = "1.0.140" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f2122636b9fe3b81f1cb25099fcf2d3f542cdb1d45940d56c713158884a05da" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82c2c1fdcd807d1098552c5b9a36e425e42e9fbd7c6a37a8425f390f781f7fa7" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "1.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c50aef8a904de4c23c788f104b7dddc7d6f79c647c7c8ce4cc8f73eb0ca773dd" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "time" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +dependencies = [ + "libc", + "wasi", + "winapi", +] + +[[package]] +name = "unicode-ident" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15c61ba63f9235225a22310255a29b806b907c9b8c964bcbd0a2c70f3f2deea7" + +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/fluent-bit/examples/filter_rust_clib/Cargo.toml b/fluent-bit/examples/filter_rust_clib/Cargo.toml new file mode 100644 index 000000000..400508d23 --- /dev/null +++ b/fluent-bit/examples/filter_rust_clib/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "filter_rust_clib" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib", "staticlib", "rlib"] + +[dependencies] +serde = { version = "*", features = ["derive"] } +serde_json = "1.0" +serde_bytes = "0.11" +rmp-serde = "1.1" +chrono = "0.4" +libc = "0.2" diff --git a/fluent-bit/examples/filter_rust_clib/Makefile b/fluent-bit/examples/filter_rust_clib/Makefile new file mode 100644 index 000000000..c8d7f4292 --- /dev/null +++ b/fluent-bit/examples/filter_rust_clib/Makefile @@ -0,0 +1,25 @@ +WASI_SDK_ROOT ?= /opt/wasi-sdk +STACK_SIZE ?= 8192 +INITIAL_MEMORY_SIZE ?= 65536 + +rustclib: + cargo build --target wasm32-unknown-unknown --release + +build: wasm rustclib + +wasm: rustclib rust_clib_filter.h + ${WASI_SDK_ROOT}/bin/clang -O3 -nostdlib \ + -z stack-size=${STACK_SIZE} -Wl,--initial-memory=${INITIAL_MEMORY_SIZE} \ + -o rust_clib_filter.wasm rust_clib_filter.c \ + -L ./target/wasm32-unknown-unknown/release -lfilter_rust_clib \ + -Wl,--export=__heap_base -Wl,--export=__data_end -Wl,--export=rust_clib_filter \ + -Wl,--no-entry -Wl,--strip-all -Wl,--allow-undefined + +rust_clib_filter.h: + cbindgen --crate filter_rust_clib --output filter_rust_clib.h --lang c + + +clean: + rm -f *.wasm + rm -f *.h + cargo clean diff --git a/fluent-bit/examples/filter_rust_clib/README.md b/fluent-bit/examples/filter_rust_clib/README.md new file mode 100644 index 000000000..9be89598b --- /dev/null +++ b/fluent-bit/examples/filter_rust_clib/README.md @@ -0,0 +1,77 @@ +# Fluent Bit / filter_rust_clib + +This source source tree provides an example of WASM program with WASI mode mainly written in Rust. + +## Prerequisites + +* Rust + * rustc 1.61.0 (fe5b13d68 2022-05-18) +* [rustup](https://rustup.rs/) (For preparing rust compiler and toolchains) +* [cbindgen](https://github.com/eqrion/cbindgen) (For preparing C headers to include exported Rust's c style function(s)) +* [WASI SDK](https://github.com/WebAssembly/wasi-sdk) + +For Ubuntu, it's easy to install with: + +```console +$ export WASI_VERSION=14 +$ export WASI_VERSION_FULL=${WASI_VERSION}.0 +$ wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_VERSION}/wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz +$ sudo mkdir -p /opt/wasi-sdk/ +$ sudo tar xvf wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz --strip-components=1 -C /opt/wasi-sdk +``` + +## How to build + +Add `wasm32-unknown-unknown` target for Rust toolchain: + +```console +$ rustup target add wasm32-unknown-unknown +``` + +Install `cbindgen` command as follows: + +```console +$ cargo install --force cbindgen +``` + +Then, execute _make build_ as follows: + +```console +$ make build +``` + +Finally, `*.wasm` file will be created: + +```console +$ ls *.wasm +rust_clib_filter.wasm +``` + +## How to confirm WASI integration + +Create fluent-bit configuration file as follows: + +```ini +[SERVICE] + Flush 1 + Daemon Off + Log_Level info + HTTP_Server Off + HTTP_Listen 0.0.0.0 + HTTP_Port 2020 + +[INPUT] + Name dummy + Tag dummy.local + +[FILTER] + Name wasm + match dummy.* + WASM_Path /path/to/rust_clib_filter.wasm + Function_Name rust_clib_filter + accessible_paths .,/path/to/fluent-bit + +[OUTPUT] + Name stdout + Match * +``` diff --git a/fluent-bit/examples/filter_rust_clib/rust_clib_filter.c b/fluent-bit/examples/filter_rust_clib/rust_clib_filter.c new file mode 100644 index 000000000..ca19d516a --- /dev/null +++ b/fluent-bit/examples/filter_rust_clib/rust_clib_filter.c @@ -0,0 +1,9 @@ +#include <stdio.h> +#include <string.h> +#include <time.h> +#include "filter_rust_clib.h" + +char* rust_clib_filter(char* tag, int len, uint32_t sec, uint32_t nsec, char* record, int record_len) +{ + return (char *)rust_filter(tag, strlen(tag), sec, nsec, record, strlen(record)); +} diff --git a/fluent-bit/examples/filter_rust_clib/src/lib.rs b/fluent-bit/examples/filter_rust_clib/src/lib.rs new file mode 100644 index 000000000..c409ba53a --- /dev/null +++ b/fluent-bit/examples/filter_rust_clib/src/lib.rs @@ -0,0 +1,32 @@ +// Import pure and fast JSON library written in Rust +use serde_json::json; +use serde_json::Value; +// Import chrono library to handle time related operation conveniently +use chrono::{Utc, TimeZone}; +use std::slice; +use std::str; +use std::os::raw::c_char; +use std::io::Write; + +#[no_mangle] +pub extern "C" fn rust_filter(tag: *const c_char, tag_len: u32, time_sec: u32, time_nsec: u32, record: *const c_char, record_len: u32) -> *const u8 { + let slice_tag: &[u8] = unsafe { slice::from_raw_parts(tag as *const u8, tag_len as usize) }; + let slice_record: &[u8] = unsafe { slice::from_raw_parts(record as *const u8, record_len as usize) }; + let mut vt: Vec<u8> = Vec::new(); + vt.write(slice_tag).expect("Unable to write"); + let vtag = str::from_utf8(&vt).unwrap(); + let v: Value = serde_json::from_slice(slice_record).unwrap(); + let dt = Utc.timestamp(time_sec as i64, time_nsec); + let time = dt.format("%Y-%m-%dT%H:%M:%S.%9f %z").to_string(); + + let message = json!({ + "message": v["message"], + "time": format!("{}", time), + "tag": vtag, + "original": v.to_string(), + "lang": "Rust", + }); + + let buf: String = message.to_string(); + buf.as_ptr() +} |