Skip to content

Add an explicit registration API for extending query/sketch/primitive/data types #131

Description

@zzylol

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 { /* ... */ },
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions