check / check (push) Waiting to run
metrics.New takes a Params holding every part at once, each behind a small interface declared in metrics beside the metrics it reads. A part that is off is left nil and adds no metric. The rule-match and reputation-hit counters are made in New, so no metric field is nil and no call order is needed. metrics no longer imports remotelog or rules. Model: opus-5-5
161 lines
3.9 KiB
Go
161 lines
3.9 KiB
Go
package metrics_test
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
|
"sneak.berlin/go/smallwebwaf/internal/metrics"
|
|
)
|
|
|
|
const (
|
|
listURL = "https://lists.example/block.txt"
|
|
zone = "dnsbl.example"
|
|
destination = "webhook"
|
|
)
|
|
|
|
// Each stand-in answers with a number of its own, so that each metric
|
|
// shows which part it was read from.
|
|
|
|
type ledgerStandIn struct{}
|
|
|
|
func (ledgerStandIn) Made(string) int { return 1 }
|
|
|
|
func (ledgerStandIn) Count(time.Time) (int, int) { return 2, 1 }
|
|
|
|
type clientTableStandIn struct{}
|
|
|
|
func (clientTableStandIn) Len() int { return 3 }
|
|
|
|
type ruleFilesStandIn struct{}
|
|
|
|
func (ruleFilesStandIn) Len() int { return 4 }
|
|
|
|
type listsStandIn struct{}
|
|
|
|
func (listsStandIn) URLs() []string { return []string{listURL} }
|
|
|
|
func (listsStandIn) Failures(string) int { return 5 }
|
|
|
|
func (listsStandIn) Fetched(string) time.Time { return time.Time{} }
|
|
|
|
type dnsblStandIn struct{}
|
|
|
|
func (dnsblStandIn) Zones() []string { return []string{zone} }
|
|
|
|
func (dnsblStandIn) Queries(string) int { return 6 }
|
|
|
|
func (dnsblStandIn) Failures(string) int { return 0 }
|
|
|
|
type abuseIPDBStandIn struct{}
|
|
|
|
func (abuseIPDBStandIn) Checked() int { return 0 }
|
|
|
|
func (abuseIPDBStandIn) Failures() int { return 0 }
|
|
|
|
func (abuseIPDBStandIn) BudgetLeft() int { return 7 }
|
|
|
|
type alertQueueStandIn struct{}
|
|
|
|
func (alertQueueStandIn) DestinationsSet() []string {
|
|
return []string{destination}
|
|
}
|
|
|
|
func (alertQueueStandIn) Counts(string) alerts.Counts {
|
|
return alerts.Counts{Sent: 8}
|
|
}
|
|
|
|
func (alertQueueStandIn) Suppressed() int64 { return 0 }
|
|
|
|
type lookupDatabaseStandIn struct{}
|
|
|
|
func (lookupDatabaseStandIn) LastRead() time.Time { return time.Time{} }
|
|
|
|
func (lookupDatabaseStandIn) ReadFailures() int { return 9 }
|
|
|
|
type remoteLogStandIn struct{}
|
|
|
|
func (remoteLogStandIn) Sent() int64 { return 10 }
|
|
|
|
func (remoteLogStandIn) Dropped() int64 { return 0 }
|
|
|
|
func (remoteLogStandIn) Depth() int { return 0 }
|
|
|
|
func TestMetricsAreReadFromEachPart(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
m := metrics.New(metrics.Params{
|
|
BusiestCount: 1,
|
|
InstanceName: "app",
|
|
Now: func() time.Time { return time.Time{} },
|
|
Bans: ledgerStandIn{},
|
|
Clients: clientTableStandIn{},
|
|
Rules: ruleFilesStandIn{},
|
|
Lists: listsStandIn{},
|
|
DNSBL: dnsblStandIn{},
|
|
AbuseIPDB: abuseIPDBStandIn{},
|
|
Alerts: alertQueueStandIn{},
|
|
LookupFile: lookupDatabaseStandIn{},
|
|
RemoteLog: remoteLogStandIn{},
|
|
})
|
|
scraped := scrape(t, m)
|
|
|
|
for _, line := range []string{
|
|
`sww_bans_made_total{cause="limit",instance="app"} 1`,
|
|
`sww_active_bans{instance="app"} 2`,
|
|
`sww_tracked_clients{instance="app"} 3`,
|
|
`sww_rules_loaded{instance="app"} 4`,
|
|
`sww_reputation_failures_total{instance="app",source="` + listURL + `"} 5`,
|
|
`sww_reputation_queries_total{instance="app",source="` + zone + `"} 6`,
|
|
`sww_reputation_daily_budget_remaining{instance="app",source="abuseipdb"} 7`,
|
|
`sww_alerts_sent_total{destination="` + destination + `",instance="app"} 8`,
|
|
`sww_lookup_database_read_failures_total{instance="app"} 9`,
|
|
`sww_remote_log_lines_sent_total{instance="app"} 10`,
|
|
} {
|
|
if !strings.Contains(scraped, "\n"+line+"\n") {
|
|
t.Errorf("metrics\n%s\nwant\n%s", scraped, line)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPartThatIsOffAddsNoMetric(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
scraped := scrape(t, metrics.New(metrics.Params{
|
|
BusiestCount: 1,
|
|
InstanceName: "app",
|
|
}))
|
|
|
|
for _, name := range []string{
|
|
"sww_bans_made_total",
|
|
"sww_tracked_clients",
|
|
"sww_rules_loaded",
|
|
"sww_reputation_",
|
|
"sww_alert",
|
|
"sww_lookup_database_",
|
|
"sww_remote_log_",
|
|
} {
|
|
if strings.Contains(scraped, name) {
|
|
t.Errorf("metrics\n%s\nshow %s", scraped, name)
|
|
}
|
|
}
|
|
}
|
|
|
|
// scrape returns the metrics m serves.
|
|
func scrape(t *testing.T, m *metrics.Metrics) string {
|
|
t.Helper()
|
|
|
|
scraped := httptest.NewRecorder()
|
|
m.ServeHTTP(
|
|
scraped,
|
|
httptest.NewRequestWithContext(
|
|
t.Context(), http.MethodGet, "/", http.NoBody,
|
|
),
|
|
)
|
|
|
|
return scraped.Body.String()
|
|
}
|