From 4d436f40949f6a558f5d98af30fac619bbc96475 Mon Sep 17 00:00:00 2001 From: Saswata Mukherjee Date: Wed, 18 Dec 2024 17:37:22 +0000 Subject: [PATCH] Fix leaky goroutine on metrics-collector Signed-off-by: Saswata Mukherjee --- .../metrics/cmd/metrics-collector/main.go | 4 + .../cmd/metrics-collector/main_test.go | 97 +++++++++++++++++++ 2 files changed, 101 insertions(+) diff --git a/collectors/metrics/cmd/metrics-collector/main.go b/collectors/metrics/cmd/metrics-collector/main.go index f86a41c38..68ebea551 100644 --- a/collectors/metrics/cmd/metrics-collector/main.go +++ b/collectors/metrics/cmd/metrics-collector/main.go @@ -722,6 +722,10 @@ func initShardedConfigs(o *Options, agent Agent) ([]*forwarder.Config, error) { } func runMultiWorkers(o *Options, cfg *forwarder.Config) error { + if o.WorkerNum > 1 { + return nil + } + for i := 1; i < int(o.WorkerNum); i++ { opt := &Options{ From: o.From, diff --git a/collectors/metrics/cmd/metrics-collector/main_test.go b/collectors/metrics/cmd/metrics-collector/main_test.go index 5ee34be01..d8e8f4cda 100644 --- a/collectors/metrics/cmd/metrics-collector/main_test.go +++ b/collectors/metrics/cmd/metrics-collector/main_test.go @@ -5,6 +5,7 @@ package main import ( + "fmt" stdlog "log" "os" "testing" @@ -65,3 +66,99 @@ func TestMultiWorkers(t *testing.T) { time.Sleep(1 * time.Second) } + +func TestSplitMatchersIntoShards(t *testing.T) { + tests := []struct { + name string + matchers []string + shardCount int + want [][]string + }{ + { + name: "single shard", + matchers: []string{"match1", "match2", "match3"}, + shardCount: 1, + want: [][]string{{"match1", "match2", "match3"}}, + }, + { + name: "two shards", + matchers: []string{"match1", "match2", "match3", "match4"}, + shardCount: 2, + want: [][]string{ + {"match1", "match3"}, + {"match2", "match4"}, + }, + }, + { + name: "two shards", + matchers: []string{"match1", "match2", "match3", "match4"}, + shardCount: 6, + want: [][]string{ + {"match1"}, + {"match2"}, + {"match3"}, + {"match4"}, + }, + }, + { + name: "three shards", + matchers: []string{"match1", "match2", "match3", "match4", "match5"}, + shardCount: 3, + want: [][]string{ + {"match1", "match4"}, + {"match2", "match5"}, + {"match3"}, + }, + }, + { + name: "more shards than matchers", + matchers: []string{"match1", "match2"}, + shardCount: 3, + want: [][]string{ + {"match1"}, + {"match2"}, + {}, + }, + }, + { + name: "zero shards", + matchers: []string{"match1", "match2", "match3"}, + shardCount: 0, + want: [][]string{{"match1", "match2", "match3"}}, + }, + { + name: "negative shards", + matchers: []string{"match1", "match2", "match3"}, + shardCount: -1, + want: [][]string{{"match1", "match2", "match3"}}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := splitMatchersIntoShards(tt.matchers, tt.shardCount) + fmt.Println(got) + // Check if number of shards matches + if len(got) != len(tt.want) { + t.Errorf("splitMatchersIntoShards() got %d shards, want %d shards", + len(got), len(tt.want)) + return + } + + // Check if each shard contains the expected matchers + for i := 0; i < len(got); i++ { + if len(got[i]) != len(tt.want[i]) { + t.Errorf("shard %d: got %d matchers, want %d matchers", + i, len(got[i]), len(tt.want[i])) + continue + } + for j := 0; j < len(got[i]); j++ { + if got[i][j] != tt.want[i][j] { + t.Errorf("shard %d matcher %d: got %s, want %s", + i, j, got[i][j], tt.want[i][j]) + } + } + } + }) + } +}