Josherich's Blog

HOME SHORTS PODCAST SOFTWARES DRAWING ABOUT RSS

Running Prometheus promtool on Web with WASM

24 Sep 2023

prometheus-wasm

WASM is great but the tooling looks terrifying for no reason. I went into this quick hack worrying about being overwhelmed by the tooling. It turned out to be surprisingly easy.

The main hurdle is of course knowing the build process itself, I always appreciate the help of –verbose to report what is going on underneath, which is important when a separate binary promu is used to build the go project. There is even a public RFC doc for it.

The next part is removing unsupported features and associated pkgs: fs.watcher in fsnotify, mmap, syscall.SIGUSR1 in go-metrics, and sockets in go-connections.

The final part is wasm file and glue code on both sides. None of these looks right:

const go = new Go();
WebAssembly.instantiateStreaming(fetch("promtool.wasm"), go.importObject).then((result) => {
    go.run(result.instance);
});
// it should really just be
const go = new Go("promtool.wasm");
go.run();
func main() {
	js.Global().Set("checkRules", CheckRulesWebWrapper())
	<-make(chan bool)
}
// makes no sense for app code to do this