go-graphite / carbonapi

Implementation of graphite API (graphite-web) in golang
Other
309 stars 140 forks source link

Implement aliasQuery function #766

Closed leizor closed 1 year ago

leizor commented 1 year ago

This PR adds an implementation for the aliasQuery function.

Graphite doc: https://graphite.readthedocs.io/en/latest/functions.html#graphite.render.functions.aliasQuery Graphite-web implementation: https://github.com/graphite-project/graphite-web/blob/02bc0c836269e07b38175fbfe3b5689afc87b4fa/webapp/graphite/render/functions.py#L2587-L2610

As part of this implementation, this PR changes the interface of interfaces.Evaluator from this:

type Evaluator interface {
    Eval(ctx context.Context, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error)
}

to this:

type Evaluator interface {
        // Fetch populates the values map being passed into it by translating input expressions into a series of
    // parser.MetricRequest and fetching the raw data from the configured backend.
    //
    // It returns a map of only the data fetched in the current invocation.
    Fetch(ctx context.Context, e []parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) (map[parser.MetricRequest][]*types.MetricData, error)
    // Eval uses the raw data within the values map being passed into it to in order to evaluate the input expression.
    Eval(ctx context.Context, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error)
}

This decouples the fetching of raw data from the evaluation of expressions and allows implementations of interface.Evaluator to implement fetching from alternate backends with greater flexibility.

leizor commented 1 year ago

@Civil Would love if you could take a look! The refactor to interfaces.Evaluator might be a pretty opinionated change but @carrieedwards and I have done quite a bit of acceptance testing around it and we think it looks pretty good.