EngineQueryFunc returns a new query function that executes instant queries against the given engine. It converts scalar into vector results.
(engine promql.QueryEngine, q storage.Queryable)
| 48 | // the given engine. |
| 49 | // It converts scalar into vector results. |
| 50 | func EngineQueryFunc(engine promql.QueryEngine, q storage.Queryable) QueryFunc { |
| 51 | return func(ctx context.Context, qs string, t time.Time) (promql.Vector, error) { |
| 52 | q, err := engine.NewInstantQuery(ctx, q, nil, qs, t) |
| 53 | if err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | defer q.Close() |
| 57 | |
| 58 | res := q.Exec(ctx) |
| 59 | if res.Err != nil { |
| 60 | return nil, res.Err |
| 61 | } |
| 62 | switch v := res.Value.(type) { |
| 63 | case promql.Vector: |
| 64 | return v, nil |
| 65 | case promql.Scalar: |
| 66 | return promql.Vector{promql.Sample{ |
| 67 | T: v.T, |
| 68 | F: v.V, |
| 69 | Metric: labels.Labels{}, |
| 70 | }}, nil |
| 71 | default: |
| 72 | return nil, errors.New("rule result is not a vector or scalar") |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // DefaultEvalIterationFunc is the default implementation of |
| 78 | // GroupEvalIterationFunc that is periodically invoked to evaluate the rules |
searching dependent graphs…