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

wasm binary version enhanced traces #53

Merged
merged 1 commit into from
Jun 10, 2024
Merged
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
4 changes: 3 additions & 1 deletion .github/workflows/build-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Add latest tag
if: ${{ github.ref_name == env.MAIN_BRANCH_NAME }}
id: add-latest-tag
Expand All @@ -30,6 +30,8 @@ jobs:
with:
image: wasm-shim
tags: ${{ env.IMG_TAGS }}
build-args: |
GITHUB_SHA=${{ github.sha }}
dockerfiles: |
./Dockerfile
- name: Push Image
Expand Down
23 changes: 22 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
[package]
name = "wasm-shim"
version = "0.1.0"
version = "0.5.0-dev"
edition = "2021"
authors = ["Rahul Anand <[email protected]>"]
description = "shim connecting envoy and authorino/limitador"
authors = ["Alex Snaps <[email protected]>", "Eguzki Astiz Lezaun <[email protected]>", "Rahul Anand <[email protected]>"]
description = "Wasm module connecting envoy and authorino/limitador"
license = "Apache-2.0"
keywords = ["rate-limiting", "rate", "limiter", "envoy", "rls"]
categories = ["web-programming"]
homepage = "https://kuadrant.io"
repository = "https://github.com/Kuadrant/wasm-shim"
documentation = "https://kuadrant.io"
readme = "README.md"

[lib]
crate-type = ["cdylib"]
Expand All @@ -24,6 +30,7 @@ protobuf = { version = "2.27", features = ["with-serde"] }
thiserror = "1.0"
regex = "1"
radix_trie = "0.2.1"
const_format = "0.2.31"

[dev-dependencies]
proxy-wasm-test-framework = { git = "https://github.com/Kuadrant/wasm-test-framework.git", branch = "kuadrant" }
Expand Down
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

FROM alpine:3.16 as wasm-shim-build

ARG GITHUB_SHA
ENV GITHUB_SHA=${GITHUB_SHA:-unknown}

ARG RUSTC_VERSION=1.69.0
RUN apk update \
&& apk upgrade \
Expand Down
48 changes: 48 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,57 @@
use std::error::Error;
use std::process::Command;

fn main() -> Result<(), Box<dyn Error>> {
set_git_hash("WASM_SHIM_GIT_HASH");
set_profile("WASM_SHIM_PROFILE");
set_features("WASM_SHIM_FEATURES");
generate_protobuf()
}

fn set_profile(env: &str) {
if let Ok(profile) = std::env::var("PROFILE") {
println!("cargo:rustc-env={env}={profile}");
}
}

fn set_features(env: &str) {
let mut features = vec![];
if cfg!(feature = "with-serde") {
features.push("+with-serde");
}
println!("cargo:rustc-env={env}={features:?}");
}

fn set_git_hash(env: &str) {
let git_sha = Command::new("/usr/bin/git")
.args(["rev-parse", "HEAD"])
.output()
.ok()
.filter(|output| output.status.success())
.and_then(|x| String::from_utf8(x.stdout).ok())
.map(|sha| sha[..8].to_owned());

if let Some(sha) = git_sha {
let dirty = Command::new("/usr/bin/git")
.args(["diff", "--stat"])
.output()
.ok()
.filter(|output| output.status.success())
.map(|output| !matches!(output.stdout.len(), 0));

match dirty {
Some(true) => println!("cargo:rustc-env={env}={sha}-dirty"),
Some(false) => println!("cargo:rustc-env={env}={sha}"),
_ => unreachable!("How can we have a git hash, yet not know if the tree is dirty?"),
}
} else {
let fallback = option_env!("GITHUB_SHA")
.map(|sha| if sha.len() > 8 { &sha[..8] } else { sha })
.unwrap_or("NO_SHA");
println!("cargo:rustc-env={env}={fallback}");
}
}

fn generate_protobuf() -> Result<(), Box<dyn Error>> {
let custom = protoc_rust::Customize {
serde_derive: Some(true),
Expand Down
20 changes: 19 additions & 1 deletion src/filter/root_context.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
use crate::configuration::{FilterConfig, PluginConfiguration};
use crate::filter::http_context::Filter;
use const_format::formatcp;
use log::{info, warn};
use proxy_wasm::traits::{Context, HttpContext, RootContext};
use proxy_wasm::types::ContextType;
use std::rc::Rc;

const WASM_SHIM_VERSION: &str = env!("CARGO_PKG_VERSION");
const WASM_SHIM_PROFILE: &str = env!("WASM_SHIM_PROFILE");
const WASM_SHIM_FEATURES: &str = env!("WASM_SHIM_FEATURES");
const WASM_SHIM_GIT_HASH: &str = env!("WASM_SHIM_GIT_HASH");
const WASM_SHIM_HEADER: &str = "Kuadrant wasm module";

pub struct FilterRoot {
pub context_id: u32,
pub config: Rc<FilterConfig>,
}

impl RootContext for FilterRoot {
fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool {
info!("root-context #{}: VM started", self.context_id);
let full_version: &'static str = formatcp!(
"v{} ({}) {} {}",
WASM_SHIM_VERSION,
WASM_SHIM_GIT_HASH,
WASM_SHIM_FEATURES,
WASM_SHIM_PROFILE,
);

info!(
"{} {} root-context #{}: VM started",
WASM_SHIM_HEADER, full_version, self.context_id
);
true
}

Expand Down
Loading