|
| 1 | +package check |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "gjallar/internal/config" |
| 13 | +) |
| 14 | + |
| 15 | +// esCheck measures the freshness of an Elasticsearch index: the number of hours |
| 16 | +// between now and max(timestamp_field), evaluated against the rule (e.g. "< 3"). |
| 17 | +// This is the end-of-chain freshness signal — when an indexing worker stalls, |
| 18 | +// max(timestamp_field) stops advancing and the lag grows without bound. |
| 19 | +type esCheck struct { |
| 20 | + baseURL string |
| 21 | + index string |
| 22 | + tsField string |
| 23 | + rule *Rule |
| 24 | + client *http.Client |
| 25 | +} |
| 26 | + |
| 27 | +func newESCheck(m config.Monitor) (*esCheck, error) { |
| 28 | + rule, err := ParseRule(m.Rule) |
| 29 | + if err != nil { |
| 30 | + return nil, err |
| 31 | + } |
| 32 | + if rule.TargetsRows() { |
| 33 | + return nil, fmt.Errorf("rule %q: row count rules do not apply to elasticsearch", m.Rule) |
| 34 | + } |
| 35 | + return &esCheck{ |
| 36 | + baseURL: strings.TrimRight(m.URL, "/"), |
| 37 | + index: m.Index, |
| 38 | + tsField: m.TimestampField, |
| 39 | + rule: rule, |
| 40 | + client: &http.Client{}, |
| 41 | + }, nil |
| 42 | +} |
| 43 | + |
| 44 | +func (c *esCheck) Check(ctx context.Context) (bool, string) { |
| 45 | + body := fmt.Sprintf(`{"size":0,"aggs":{"max_ts":{"max":{"field":%q}}}}`, c.tsField) |
| 46 | + url := c.baseURL + "/" + c.index + "/_search" |
| 47 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, strings.NewReader(body)) |
| 48 | + if err != nil { |
| 49 | + return false, err.Error() |
| 50 | + } |
| 51 | + req.Header.Set("Content-Type", "application/json") |
| 52 | + |
| 53 | + resp, err := c.client.Do(req) |
| 54 | + if err != nil { |
| 55 | + return false, err.Error() |
| 56 | + } |
| 57 | + defer resp.Body.Close() |
| 58 | + if resp.StatusCode < 200 || resp.StatusCode > 299 { |
| 59 | + return false, fmt.Sprintf("status %d querying %s", resp.StatusCode, c.index) |
| 60 | + } |
| 61 | + |
| 62 | + var parsed struct { |
| 63 | + Aggregations struct { |
| 64 | + MaxTS struct { |
| 65 | + Value float64 `json:"value"` // epoch millis |
| 66 | + } `json:"max_ts"` |
| 67 | + } `json:"aggregations"` |
| 68 | + } |
| 69 | + if err := json.NewDecoder(resp.Body).Decode(&parsed); err != nil { |
| 70 | + return false, fmt.Sprintf("parsing response: %v", err) |
| 71 | + } |
| 72 | + ms := parsed.Aggregations.MaxTS.Value |
| 73 | + if ms <= 0 { |
| 74 | + return false, fmt.Sprintf("no %q value (empty index?)", c.tsField) |
| 75 | + } |
| 76 | + |
| 77 | + last := time.UnixMilli(int64(ms)) |
| 78 | + lagHours := time.Since(last).Hours() |
| 79 | + if err := c.rule.EvalValue(strconv.FormatFloat(lagHours, 'f', 2, 64)); err != nil { |
| 80 | + return false, fmt.Sprintf("freshness lag %.1fh (last %s): %v", |
| 81 | + lagHours, last.UTC().Format("2006-01-02 15:04 MST"), err) |
| 82 | + } |
| 83 | + return true, "" |
| 84 | +} |
0 commit comments