plugins/

directory
v0.0.0-...-8393a28 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2026 License: Apache-2.0

README

Google Cloud Service Extension Plugins Samples

Code samples and tools for developing Google Cloud Service Extensions WebAssembly (wasm) plugins.

Each sample/recipe has an example plugin written in Rust and C++, and an accompanying unit test that verifies both.

Getting started

We recommend the following process:

  1. Write a wasm plugin using the samples and SDKs as a starting point: C++, Go, Rust. See also the best practices.
  2. Build the plugin.
  3. Test and benchmark the plugin.

Building

All sample plugins can be built using Bazel. If you prefer to use language-native toolchains, see the SDK-specific instructions:

All languages also support Bazel; we recommend the Bazelisk wrapper which provides support for multiple Bazel versions:

# A target is defined using BUILD files. Dependencies are in WORKSPACE.
$ bazelisk build <target>

# For example, to build a sample in C++ and Rust, from the plugins/ directory:
$ bazelisk build //samples/add_header:plugin_cpp.wasm
$ bazelisk build //samples/add_header:plugin_rust.wasm

C++ builds may require a specific toolchain: --config=clang or --config=gcc.

Testing and benchmarking

  1. Write a plugin test file to specify the plugin's functional expectations. This file can either be in text proto format (example) or YAML (example). Consult the plugin tester proto API as needed.
  2. Add benchmark: true to tests that exemplify common wasm operations (example).
  3. Run + Test + Benchmark your wasm plugin as follows!
docker run -it -v $(pwd):/mnt \
    us-docker.pkg.dev/service-extensions-samples/plugins/wasm-tester:main \
    --proto /mnt/local/path/to/tests.textpb \
    --plugin /mnt/local/path/to/plugin.wasm

Tips:

  • When benchmarking and publishing, compile a release (optimized) wasm build.
  • Try sending empty or invalid input. Verify your plugin doesn't crash.
  • To see plugin-emitted logs on the console, add --logfile=/dev/stdout.
  • To see a trace of logs and wasm ABI calls, add --loglevel=TRACE.
  • To disable benchmarking for faster iteration, add --nobench.
  • To disable unit testing for cleaner output, add --notest.
  • To optionally specify plugin config data, add --config=<path>.
  • To test memory with high concurrency, add --num_additional_streams=500.

You can also run tests using Bazel. This is much slower the first time, because this builds both the tester and the V8 runtime from scratch. Use the Docker command above for a better experience.

bazelisk test --config=bench --test_output=all //samples/...

Samples & Recipes

The samples folder contains Samples & Recipes to use as a reference for your own plugin. Extend them to fit your particular use case.

Feature set / ABI

Service Extension plugins are compiled against the ProxyWasm ABI, described here: https://github.com/proxy-wasm/spec/tree/master

Service Extension plugins currently support a subset of the ProxyWasm spec. Support will grow over time. The current feature set includes:

  • Root context lifecycle callbacks (host -> wasm)
    • on_context_create
    • on_vm_start
    • on_configure
    • on_done
    • on_delete
  • Stream context lifecycle callbacks (host -> wasm)
    • on_context_create
    • on_done
    • on_delete
  • Stream context HTTP callbacks (host -> wasm)
    • on_request_headers
    • on_request_body
    • on_request_trailers
    • on_response_headers
    • on_response_body
    • on_response_trailers
  • Stream context HTTP hostcalls (wasm -> host)
    • send_local_response
    • get_header_map_value, add_header_map_value, replace_header_map_value, remove_header_map_value
    • get_header_map_pairs, set_header_map_pairs
    • get_header_map_size
  • Other hostcalls (wasm -> host)
    • log
    • get_current_time_nanoseconds (frozen per stream)
    • get_property ("plugin_root_id" only)
    • get_buffer_status, get_buffer_bytes, set_buffer_bytes (PluginConfiguration, HttpRequestBody, HttpResponseBody)

Implementation details

Fixture

In support of unit testing, this repo contains an HttpTest fixture with a TestWasm host implementation and TestHttpContext stream handler. These minimal implementations loosely match the GCP Service Extension execution environment. The contexts implement the ABI / feature set described above (mainly HTTP headers and logging), but often in a simple way (behaviors may not match GCP exactly).

Rust and Cargo

This project leverages crate_universe to integrate Cargo with Bazel. In order to add new Rust library dependencies:

  • Edit dependencies in Cargo.toml
  • Regenerate Bazel targets: $ CARGO_BAZEL_REPIN=1 bazelisk sync --only=crate_index
  • Reference libraries as @crate_index//:<target>

Go and Gazelle

This project leverages gazelle to integrate Go mod with Bazel. In order to add new Go dependencies:

  • Instruct Gazelle to generate a repo rule for your dependency using the Go import URL, for example: $ bazel run //:gazelle -- update-repos github.com/proxy-wasm/proxy-wasm-go-sdk
  • Add a import statement to the Go source file import "github.com/proxy-wasm/proxy-wasm-go-sdk/types".
  • Run $ bazel run //:gazelle to add the autogenerated repo rule dependency to the Go file's build file

Directories

Path Synopsis
samples
ab_testing command
[START serviceextensions_plugin_ab_testing]
[START serviceextensions_plugin_ab_testing]
add_custom_response command
[START serviceextensions_plugin_add_custom_response]
[START serviceextensions_plugin_add_custom_response]
add_device_type command
[START serviceextensions_plugin_device_type]
[START serviceextensions_plugin_device_type]
add_request_header command
[START serviceextensions_plugin_add_request_header]
[START serviceextensions_plugin_add_request_header]
add_response_header command
[START serviceextensions_plugin_add_repoonse_header]
[START serviceextensions_plugin_add_repoonse_header]
check_pii command
[START serviceextensions_plugin_check_pii]
[START serviceextensions_plugin_check_pii]
docs_first_plugin command
[START serviceextensions_plugin_docs_first_plugin]
[START serviceextensions_plugin_docs_first_plugin]
docs_plugin_config command
[START serviceextensions_plugin_docs_plugin_config]
[START serviceextensions_plugin_docs_plugin_config]
jwt_auth command
[START serviceextensions_plugin_jwt_auth]
[START serviceextensions_plugin_jwt_auth]
local_reply command
log_query command
[START serviceextensions_plugin_log_query]
[START serviceextensions_plugin_log_query]
normalize_header command
[START serviceextensions_plugin_normalize_header]
[START serviceextensions_plugin_normalize_header]
overwrite_errcode command
[START serviceextensions_plugin_overwrite_errcode]
[START serviceextensions_plugin_overwrite_errcode]
overwrite_header command
[START serviceextensions_plugin_overwrite_header]
[START serviceextensions_plugin_overwrite_header]
redirect command
[START serviceextensions_plugin_redirect]
[START serviceextensions_plugin_redirect]
redirect_bulk command
[START serviceextensions_plugin_redirect_bulk]
[START serviceextensions_plugin_redirect_bulk]
regex_rewrite command
[START serviceextensions_plugin_regex_rewrite]
[START serviceextensions_plugin_regex_rewrite]
remove_cookie command
[START serviceextensions_plugin_remove_setcookie]
[START serviceextensions_plugin_remove_setcookie]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL