Migration files restructured with shared helpers; verified byte-identical SQLite schema vs previous commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Lx9FNLG2CqeBbVGGd9bFsP
129 lines
3.2 KiB
Go
129 lines
3.2 KiB
Go
package alert
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"math"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// query runs a rule and returns one reading per active, trusted entity.
|
|
func (ev *Evaluator) query(ctx context.Context, r *compiledRule, now time.Time) ([]reading, error) {
|
|
qctx, cancel := context.WithTimeout(ctx, queryTimeout)
|
|
defer cancel()
|
|
raw, err := ev.o.Querier.Query(qctx, ev.o.StoreName, r.Expr, now)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
series, err := parseVector(raw)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
best := map[string]reading{}
|
|
var order []string
|
|
for _, s := range series {
|
|
ent, ok := ev.entityOfSeries(r, s)
|
|
if !ok {
|
|
continue
|
|
}
|
|
k := ent.Key()
|
|
cur, seen := best[k]
|
|
if !seen {
|
|
order = append(order, k)
|
|
}
|
|
// Duplicate series for one entity: keep the one nearer to alerting.
|
|
if !seen || r.nearer(s.value, cur.value) {
|
|
best[k] = reading{ent, s.value}
|
|
}
|
|
}
|
|
out := make([]reading, 0, len(order))
|
|
for _, k := range order {
|
|
out = append(out, best[k])
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// nearer reports whether a is closer to breaching than b.
|
|
func (r *compiledRule) nearer(a, b float64) bool {
|
|
if r.Op == OpLess {
|
|
return a < b
|
|
}
|
|
return a > b
|
|
}
|
|
|
|
// entityOfSeries maps a series to its entity. It reports false for series that
|
|
// lack a label the rule needs, are on unreliable interfaces, or are inactive.
|
|
func (ev *Evaluator) entityOfSeries(r *compiledRule, s sample) (Entity, bool) {
|
|
ent := Entity{Rule: r.Name}
|
|
for _, l := range r.Labels {
|
|
v := s.labels[l]
|
|
if v == "" {
|
|
ev.log.Debug("series without label", "rule", r.Name, "label", l)
|
|
return Entity{}, false
|
|
}
|
|
switch l {
|
|
case "device":
|
|
ent.Device = v
|
|
case "iface":
|
|
ent.Iface = v
|
|
case "component":
|
|
ent.Component = v
|
|
}
|
|
}
|
|
if r.SkipUnreliable && ent.Iface != "" && ev.o.Unreliable != nil && ev.o.Unreliable(ent.Iface) {
|
|
return Entity{}, false
|
|
}
|
|
if ev.o.Active != nil && !ev.o.Active(ent) {
|
|
return Entity{}, false
|
|
}
|
|
return ent, true
|
|
}
|
|
|
|
type sample struct {
|
|
labels map[string]string
|
|
value float64
|
|
}
|
|
|
|
// parseVector reads an instant-query response. NaN samples are dropped (no data).
|
|
func parseVector(raw []byte) ([]sample, error) {
|
|
var resp struct {
|
|
Status string `json:"status"`
|
|
ErrorType string `json:"errorType"`
|
|
Error string `json:"error"`
|
|
Data struct {
|
|
ResultType string `json:"resultType"`
|
|
Result []struct {
|
|
Metric map[string]string `json:"metric"`
|
|
Value [2]json.RawMessage `json:"value"`
|
|
} `json:"result"`
|
|
} `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(raw, &resp); err != nil {
|
|
return nil, fmt.Errorf("alert: bad query response: %w", err)
|
|
}
|
|
if resp.Status != "success" {
|
|
return nil, fmt.Errorf("alert: query status %q: %s %s", resp.Status, resp.ErrorType, resp.Error)
|
|
}
|
|
if resp.Data.ResultType != "vector" {
|
|
return nil, fmt.Errorf("alert: query returned %q, want vector", resp.Data.ResultType)
|
|
}
|
|
var out []sample
|
|
for _, r := range resp.Data.Result {
|
|
var str string
|
|
if err := json.Unmarshal(r.Value[1], &str); err != nil {
|
|
return nil, fmt.Errorf("alert: bad sample value %s", r.Value[1])
|
|
}
|
|
v, err := strconv.ParseFloat(str, 64)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("alert: bad sample value %q", str)
|
|
}
|
|
if math.IsNaN(v) {
|
|
continue
|
|
}
|
|
out = append(out, sample{r.Metric, v})
|
|
}
|
|
return out, nil
|
|
}
|