From 0547a719053052b7ab55bab41c7bd14938886bbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Sch=C3=A4rmeli?= Date: Tue, 16 Apr 2024 12:47:22 +0200 Subject: [PATCH] fix: collectors ignore watches with missing title (#22) --- pkg/collectors/watch_collector_test.go | 13 +++++++++++++ pkg/data/api.go | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/pkg/collectors/watch_collector_test.go b/pkg/collectors/watch_collector_test.go index 7dc7356..0363d20 100644 --- a/pkg/collectors/watch_collector_test.go +++ b/pkg/collectors/watch_collector_test.go @@ -29,3 +29,16 @@ func TestWatchCollector(t *testing.T) { //testutil.ExpectMetricCount(t, c, 2, expectedWatchMetrics...) testutil.ExpectMetrics(t, c, "watch_metrics.prom", expectedWatchMetrics...) } + +func TestWatchCollector_IgnoresWatchesWithoutTitle(t *testing.T) { + _, watchDb := testutil.NewCollectorTestDb() + server := testutil.CreateTestApiServer(t, watchDb) + emptyUuid, emptyTitleItem := testutil.NewTestItem("", 100, "CHF", 20, 15, 10) + watchDb[emptyUuid] = emptyTitleItem + defer server.Close() + + client := cdio.NewTestApiClient(server.URL()) + c := NewWatchCollector(client) + + testutil.ExpectMetrics(t, c, "watch_metrics.prom", expectedWatchMetrics...) +} diff --git a/pkg/data/api.go b/pkg/data/api.go index fced668..c28f8b5 100644 --- a/pkg/data/api.go +++ b/pkg/data/api.go @@ -3,6 +3,7 @@ package data import ( + "fmt" "net/url" ) @@ -38,5 +39,8 @@ func (w *WatchItem) GetMetrics() ([]string, error) { if err != nil { return nil, err } + if w.Title == "" { + return nil, fmt.Errorf("title is empty") + } return []string{w.Title, url.Host}, nil }