A funny thing about Golang packages and adding new functions in a parser: Go package import allows any importer to mutate an exported package variable, e.g., the supported function list, so one can easily add a new function to an existing PromQL parser.
Rust, on the other hand, defaults to immutable variables for importers, so extending this Rust parser with a custom function requires either forking it or the maintainers adding an explicit registration API.
Follow-up thought: We should add an explicit registration API for ASAPController for extending query types / sketch types / primitive types / or even data types.
Example registration API using the promql-parser crate as a reference (using the inventory crate for distributed, compile-time registration):
// in promql-parser
pub struct FunctionRegistration {
pub name: &'static str,
pub build: fn() -> Function,
}
inventory::collect!(FunctionRegistration);
pub(crate) fn get_function(name: &str) -> Option<Function> {
if let Some(f) = BUILTIN_FUNCTIONS.get(name) {
return Some(f.clone());
}
inventory::iter::<FunctionRegistration>
.into_iter()
.find(|r| r.name == name)
.map(|r| (r.build)())
}
// in a downstream crate that depends on promql-parser
inventory::submit! {
promql_parser::FunctionRegistration {
name: "my_func",
build: || Function { /* ... */ },
}
}
A funny thing about Golang packages and adding new functions in a parser: Go package import allows any importer to mutate an exported package variable, e.g., the supported function list, so one can easily add a new function to an existing PromQL parser.
Rust, on the other hand, defaults to immutable variables for importers, so extending this Rust parser with a custom function requires either forking it or the maintainers adding an explicit registration API.
Follow-up thought: We should add an explicit registration API for ASAPController for extending query types / sketch types / primitive types / or even data types.
Example registration API using the
promql-parsercrate as a reference (using theinventorycrate for distributed, compile-time registration):