Add cron schedules and sync docs
This commit is contained in:
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/monlet/agent/internal/config"
|
||||
"github.com/monlet/agent/internal/metrics"
|
||||
"github.com/monlet/agent/internal/spool"
|
||||
dto "github.com/prometheus/client_model/go"
|
||||
)
|
||||
|
||||
func TestPrometheusOnlyDoesNotPush(t *testing.T) {
|
||||
@@ -257,6 +258,92 @@ func TestReloadAcceptsLabelsAndChecks(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReloadUpdatesIntervalMetricForCron(t *testing.T) {
|
||||
oldCfg := &config.Config{
|
||||
AgentID: "a1",
|
||||
Hostname: "h",
|
||||
StateDir: "/tmp/old",
|
||||
Server: config.ServerConfig{
|
||||
Enabled: boolPtr(true),
|
||||
URL: "http://server",
|
||||
HeartbeatInterval: config.Duration{Duration: 10 * time.Second},
|
||||
BatchInterval: config.Duration{Duration: 10 * time.Second},
|
||||
},
|
||||
Metrics: config.MetricsConfig{Enabled: false},
|
||||
Checks: []config.CheckConfig{{
|
||||
ID: "c1",
|
||||
Command: "true",
|
||||
Interval: config.Duration{Duration: time.Second},
|
||||
Timeout: config.Duration{Duration: time.Second},
|
||||
}},
|
||||
}
|
||||
a := &App{
|
||||
Cfg: oldCfg,
|
||||
AgentID: "a1",
|
||||
Metrics: metrics.New(),
|
||||
}
|
||||
a.recordCheckScheduleMetrics(oldCfg.Checks)
|
||||
if got, ok := checkIntervalMetric(t, a, "c1"); !ok || got != 1 {
|
||||
t.Fatalf("interval metric missing before reload: value=%v ok=%v", got, ok)
|
||||
}
|
||||
|
||||
cronCfg := *oldCfg
|
||||
cronCfg.Checks = []config.CheckConfig{{
|
||||
ID: "c1",
|
||||
Command: "true",
|
||||
Cron: "0 * * * *",
|
||||
Timeout: config.Duration{Duration: time.Second},
|
||||
}}
|
||||
if err := a.Reload(&cronCfg, "a1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, ok := checkIntervalMetric(t, a, "c1"); ok {
|
||||
t.Fatalf("interval metric must be deleted for cron check, got %v", got)
|
||||
}
|
||||
|
||||
intervalCfg := cronCfg
|
||||
intervalCfg.Checks = []config.CheckConfig{{
|
||||
ID: "c1",
|
||||
Command: "true",
|
||||
Interval: config.Duration{Duration: 2 * time.Second},
|
||||
Timeout: config.Duration{Duration: time.Second},
|
||||
}}
|
||||
if err := a.Reload(&intervalCfg, "a1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, ok := checkIntervalMetric(t, a, "c1"); !ok || got != 2 {
|
||||
t.Fatalf("interval metric missing after interval reload: value=%v ok=%v", got, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func checkIntervalMetric(t *testing.T, a *App, checkID string) (float64, bool) {
|
||||
t.Helper()
|
||||
families, err := a.Metrics.Registry.Gather()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, family := range families {
|
||||
if family.GetName() != "monlet_agent_check_interval_seconds" {
|
||||
continue
|
||||
}
|
||||
for _, metric := range family.GetMetric() {
|
||||
if labelValue(metric.GetLabel(), "check_id") == checkID && metric.GetGauge() != nil {
|
||||
return metric.GetGauge().GetValue(), true
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func labelValue(labels []*dto.LabelPair, name string) string {
|
||||
for _, label := range labels {
|
||||
if label.GetName() == name {
|
||||
return label.GetValue()
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func boolPtr(v bool) *bool {
|
||||
return &v
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user