feat: Prometheus monitors in console and API

Closes #25

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 11:49:07 +01:00
1 parent 5e7db9c054
commit 94b519b421
8 files changed
+919 -580

No files matched your search

+8
View File
@@ -7,6 +7,7 @@ import (
"fmt"
"net/http"
"slices"
"strconv"
"strings"
"time"
@@ -476,6 +477,7 @@ func (s *Server) monitorEditor(c *gin.Context, p *principal, page web.Page, me w
Type: "http", IntervalSeconds: 60, TimeoutMS: 10000, Confirmations: 2, Enabled: true,
MinInterval: s.minInterval(), Method: "GET", ExpectStatus: "2xx, 3xx", GraceSeconds: check.DefaultHeartbeatGrace,
JSONExpect: "2xx", JSONVerify: true, Assertions: assertionRows(nil),
PromOp: ">=", PromThreshold: "1", PromMode: "all", PromVerify: true,
FollowRedirects: true, VerifyTLS: true, DNSRecord: "A", TLSMinDays: 14,
}
var (
@@ -543,6 +545,7 @@ func fillForm(f *web.MonitorForm, m store.Monitor) {
}
// Defaults for the other types, in case the type is switched.
f.JSONExpect, f.JSONVerify, f.Assertions, f.GraceSeconds = "2xx", true, assertionRows(nil), check.DefaultHeartbeatGrace
f.PromOp, f.PromThreshold, f.PromMode, f.PromVerify = ">=", "1", "all", true
switch check.Type(m.Type) {
case check.HTTP:
var c check.HTTPConfig
@@ -564,6 +567,11 @@ func fillForm(f *web.MonitorForm, m store.Monitor) {
var c check.TLSConfig
_ = json.Unmarshal(m.Config, &c)
f.TLSMinDays, f.TLSServerName = c.MinDaysValid, c.ServerName
case check.Prometheus:
var c check.PrometheusConfig
_ = json.Unmarshal(m.Config, &c)
f.PromQuery, f.PromOp, f.PromThreshold, f.PromMode = c.Query, c.Op, strconv.FormatFloat(c.Threshold, 'g', -1, 64), c.Mode
f.PromEmptyOK, f.PromVerify, f.Headers = c.EmptyOK, c.VerifyTLS, headerLines(c.Headers)
case check.Heartbeat:
var c check.HeartbeatConfig
_ = json.Unmarshal(m.Config, &c)
+30
View File
@@ -86,3 +86,33 @@ func asString(v any) string {
s, _ := v.(string)
return s
}
func TestPrometheusMonitor(t *testing.T) {
e := newEnv(t)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
go e.api.RunScheduler(ctx)
prom := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"api"},"value":[1,"0"]}]}}`))
}))
defer prom.Close()
tok, region := e.platformChecker(t, uniq("eu-"))
e.startChecker(t, tok)
owner := e.browser(t)
owner.signup("o@"+uniq("pm")+".test", uniq("pm-"))
st, body := owner.do("POST", "/api/v1/org/monitors", map[string]any{
"name": "API targets", "type": "prometheus", "target": prom.URL, "interval_seconds": 10, "timeout_ms": 2000,
"confirmations": 1, "regions": []string{region}, "config": map[string]any{"query": `up{job="api"}`, "op": "==", "threshold": 1},
})
if st != http.StatusCreated {
t.Fatalf("create prometheus monitor = %d %v", st, body)
}
waitFor(t, 15*time.Second, "an incident for the down target", func() bool {
_, b := owner.do("GET", "/api/v1/org/incidents", nil)
return len(b["incidents"].([]any)) == 1
})
_, list := owner.do("GET", "/api/v1/org/monitors", nil)
if !strings.Contains(mustJSON(list), `up{job=\"api\"} = 0, expected == 1`) {
t.Errorf("result lacks the failing series: %s", mustJSON(list))
}
}