diff options
Diffstat (limited to 'fluent-bit/examples/wasi_serde_json')
-rw-r--r-- | fluent-bit/examples/wasi_serde_json/Cargo.lock | 123 | ||||
-rw-r--r-- | fluent-bit/examples/wasi_serde_json/Cargo.toml | 10 | ||||
-rw-r--r-- | fluent-bit/examples/wasi_serde_json/README.md | 65 | ||||
-rw-r--r-- | fluent-bit/examples/wasi_serde_json/src/main.rs | 32 |
4 files changed, 230 insertions, 0 deletions
diff --git a/fluent-bit/examples/wasi_serde_json/Cargo.lock b/fluent-bit/examples/wasi_serde_json/Cargo.lock new file mode 100644 index 000000000..e20066914 --- /dev/null +++ b/fluent-bit/examples/wasi_serde_json/Cargo.lock @@ -0,0 +1,123 @@ +# 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 = "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 = "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 = "ryu" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695" + +[[package]] +name = "serde" +version = "1.0.137" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1" + +[[package]] +name = "serde_json" +version = "1.0.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b7ce2b32a1aed03c558dc61a5cd328f15aff2dbc17daad8fb8af04d2100e15c" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[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 = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + +[[package]] +name = "wasi_serde_json" +version = "0.1.0" +dependencies = [ + "chrono", + "serde_json", +] + +[[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/wasi_serde_json/Cargo.toml b/fluent-bit/examples/wasi_serde_json/Cargo.toml new file mode 100644 index 000000000..c9778b46d --- /dev/null +++ b/fluent-bit/examples/wasi_serde_json/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "wasi_serde_json" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serde_json = "1.0" +chrono = "0.4"
\ No newline at end of file diff --git a/fluent-bit/examples/wasi_serde_json/README.md b/fluent-bit/examples/wasi_serde_json/README.md new file mode 100644 index 000000000..9df44189d --- /dev/null +++ b/fluent-bit/examples/wasi_serde_json/README.md @@ -0,0 +1,65 @@ +# Fluent Bit / wasi_serde_json + +This source source tree provides an example of WASM program with WASI mode. + +## Prerequisites + +* Rust + * rustc 1.61.0 (fe5b13d68 2022-05-18) +* [rustup](https://rustup.rs/) (For preparing rust compiler and toolchains) + +## How to build + +Add `wasm32-wasi` target for Rust toolchain: + +```console +$ rustup target add wasm32-wasi +``` + +Then, execute _cargo build_ as follows: + +```console +$ cargo build --target wasm32-wasi --release +``` + +Finally, under target/wasm32-wasi/release directory, `*.wasm` file will be created: + +```console +$ ls target/wasm32-wasi/release/*.wasm +target/wasm32-wasi/release/wasi_serde_json.wasm +``` + +## How to confirm WASI integration + +Create parsers.conf as follows: + +```ini +[PARSER] + Name wasi + Format json + Time_Key time + Time_Format %Y-%m-%dT%H:%M:%S.%L %z +``` + +And Create fluent-bit configuration file as follows: + +```ini +[SERVICE] + Flush 1 + Daemon Off + Parsers_File parsers.conf + Log_Level info + HTTP_Server Off + HTTP_Listen 0.0.0.0 + HTTP_Port 2020 + +[INPUT] + Name exec_wasi + Tag exec.wasi.local + WASI_Path /path/to/wasi_serde_json.wasm + Parser wasi + +[OUTPUT] + Name stdout + Match * +``` diff --git a/fluent-bit/examples/wasi_serde_json/src/main.rs b/fluent-bit/examples/wasi_serde_json/src/main.rs new file mode 100644 index 000000000..29ee25f4f --- /dev/null +++ b/fluent-bit/examples/wasi_serde_json/src/main.rs @@ -0,0 +1,32 @@ +// Import rust's io and filesystem module +use std::io::prelude::*; +use std::fs; +// Import pure and fast JSON library written in Rust +use serde_json::json; +// Import chrono library to handle time related operation conveniently +use chrono::Utc; + +// Entry point to our WASI applications +fn main() { + // Note that fractional second must be handled by `.%9f` not `%L` like as fluent-bit. + let time = Utc::now().format("%Y-%m-%dT%H:%M:%S.%9f %z").to_string(); + // The type of `john` is `serde_json::Value` + let john = json!({ + "name": "John Doe", + "age": 43, + "phones": [ + "+44 1234567", + "+44 2345678" + ], + "time": format!("{}", time), + }); + // Print out serialized JSON data for john + // This will handle writing to stdout for us using the WASI APIs (e.g fd_write) + println!("{}", john.to_string()); + + // Create a file (Testing for exposed wasi API) + let mut file = fs::File::create("helloworld.txt").expect("Unable to create file"); + + // Write the text to the file we created (Testing for exposed wasi API) + write!(file, "Hello world!\n").unwrap(); +}
\ No newline at end of file |