Skip to content

Commit 363b420

Browse files
committed
Refactor config files discovery scheduling
1 parent fda9106 commit 363b420

11 files changed

Lines changed: 771 additions & 439 deletions

File tree

comp/core/configfilesdiscovery/impl/collectors/kafka.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ func NewKafka() configfilesdiscoveryimpl.ConfigCollector {
2626
return kafkaConfigCollector{}
2727
}
2828

29-
// MatchesCommandline returns whether the command line contains an explicit
30-
// Kafka broker properties argument. Path resolution is deferred until
31-
// collection because workloadmeta process events may not include the working
32-
// directory.
33-
func (kafkaConfigCollector) MatchesCommandline(args []string) bool {
34-
_, ok := kafkaGetConfigArgFromCommandline(args)
35-
return ok
29+
// CanCollectFromProcess returns whether the command line contains an explicit,
30+
// resolvable Kafka broker properties path.
31+
func (kafkaConfigCollector) CanCollectFromProcess(commandline configfilesdiscoveryimpl.TargetCommandline) bool {
32+
configArg, ok := kafkaGetConfigArgFromCommandline(commandline.Args)
33+
if !ok {
34+
return false
35+
}
36+
_, resolved := resolveConfigPath(configArg, commandline.WorkingDir)
37+
return resolved
3638
}
3739

3840
func (c kafkaConfigCollector) Collect(ctx context.Context, reader configfilesdiscoveryimpl.ConfigReader) (configfilesdiscoveryimpl.CollectedConfig, error) {

comp/core/configfilesdiscovery/impl/collectors/kafka_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,26 +154,28 @@ func TestKafkaGetConfigPath(t *testing.T) {
154154
}
155155
}
156156

157-
func TestKafkaCollectorMatchesAndReadsRelativeProcessConfig(t *testing.T) {
157+
func TestKafkaCollectorResolvesAndReadsRelativeProcessConfig(t *testing.T) {
158158
eventArgs := []string{"kafka-server-start.sh", "config/server.properties"}
159+
eventCommandline := configfilesdiscoveryimpl.TargetCommandline{
160+
Args: eventArgs,
161+
WorkingDir: "/opt/kafka",
162+
}
159163
reader := &kafkaCollectorTestReader{
160164
runtimeCommandline: configfilesdiscoveryimpl.TargetCommandline{
161165
Args: []string{"/bin/bash", "/mnt/kafka-wrapper/start-kafka.sh"},
162166
},
163-
liveProcessCommandlines: []configfilesdiscoveryimpl.TargetCommandline{{
164-
Args: eventArgs,
165-
WorkingDir: "/opt/kafka",
166-
}},
167-
file: configfilesdiscoveryimpl.ConfigFile{Path: "/opt/kafka/config/server.properties"},
167+
liveProcessCommandlines: []configfilesdiscoveryimpl.TargetCommandline{eventCommandline},
168+
file: configfilesdiscoveryimpl.ConfigFile{Path: "/opt/kafka/config/server.properties"},
168169
}
169170

170-
configArg, matched := kafkaGetConfigArgFromCommandline(eventArgs)
171-
require.True(t, matched)
172-
_, resolved := resolveConfigPath(configArg, "")
173-
assert.False(t, resolved)
174-
require.True(t, kafkaConfigCollector{}.MatchesCommandline(eventArgs))
171+
collector := kafkaConfigCollector{}
172+
assert.False(t, collector.CanCollectFromProcess(configfilesdiscoveryimpl.TargetCommandline{Args: eventArgs}))
173+
assert.True(t, collector.CanCollectFromProcess(eventCommandline))
174+
assert.True(t, collector.CanCollectFromProcess(configfilesdiscoveryimpl.TargetCommandline{
175+
Args: []string{"kafka-server-start.sh", "/etc/kafka/server.properties"},
176+
}))
175177

176-
collected, err := kafkaConfigCollector{}.Collect(context.Background(), reader)
178+
collected, err := collector.Collect(context.Background(), reader)
177179

178180
require.NoError(t, err)
179181
assert.Equal(t, []string{"/opt/kafka/config/server.properties"}, reader.readFileCalls)

comp/core/configfilesdiscovery/impl/collectors/redis.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ func NewRedis() configfilesdiscoveryimpl.ConfigCollector {
2727
return redisConfigCollector{}
2828
}
2929

30-
// MatchesCommandline returns whether the command line contains an explicit
31-
// Redis config argument. Path resolution is deferred until collection because
32-
// workloadmeta process events may not include the working directory.
33-
func (redisConfigCollector) MatchesCommandline(args []string) bool {
34-
_, ok := redisGetConfigArgFromCommandline(args)
35-
return ok
30+
// CanCollectFromProcess returns whether the command line contains an explicit,
31+
// resolvable Redis config path.
32+
func (redisConfigCollector) CanCollectFromProcess(commandline configfilesdiscoveryimpl.TargetCommandline) bool {
33+
configArg, ok := redisGetConfigArgFromCommandline(commandline.Args)
34+
if !ok {
35+
return false
36+
}
37+
_, resolved := resolveConfigPath(configArg, commandline.WorkingDir)
38+
return resolved
3639
}
3740

3841
func (c redisConfigCollector) Collect(ctx context.Context, reader configfilesdiscoveryimpl.ConfigReader) (configfilesdiscoveryimpl.CollectedConfig, error) {

comp/core/configfilesdiscovery/impl/collectors/redis_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,26 +130,28 @@ func TestRedisGetConfigPath(t *testing.T) {
130130
}
131131
}
132132

133-
func TestRedisCollectorMatchesAndReadsRelativeProcessConfig(t *testing.T) {
133+
func TestRedisCollectorResolvesAndReadsRelativeProcessConfig(t *testing.T) {
134134
eventArgs := []string{"redis-server", "redis.conf"}
135+
eventCommandline := configfilesdiscoveryimpl.TargetCommandline{
136+
Args: eventArgs,
137+
WorkingDir: "/etc/redis",
138+
}
135139
reader := &redisCollectorTestReader{
136140
runtimeCommandline: configfilesdiscoveryimpl.TargetCommandline{
137141
Args: []string{"/usr/local/bin/tini", "--", "/etc/scripts/start_redis.sh"},
138142
},
139-
liveProcessCommandlines: []configfilesdiscoveryimpl.TargetCommandline{{
140-
Args: eventArgs,
141-
WorkingDir: "/etc/redis",
142-
}},
143-
file: configfilesdiscoveryimpl.ConfigFile{Path: "/etc/redis/redis.conf"},
143+
liveProcessCommandlines: []configfilesdiscoveryimpl.TargetCommandline{eventCommandline},
144+
file: configfilesdiscoveryimpl.ConfigFile{Path: "/etc/redis/redis.conf"},
144145
}
145146

146-
configArg, matched := redisGetConfigArgFromCommandline(eventArgs)
147-
require.True(t, matched)
148-
_, resolved := resolveConfigPath(configArg, "")
149-
assert.False(t, resolved)
150-
require.True(t, redisConfigCollector{}.MatchesCommandline(eventArgs))
147+
collector := redisConfigCollector{}
148+
assert.False(t, collector.CanCollectFromProcess(configfilesdiscoveryimpl.TargetCommandline{Args: eventArgs}))
149+
assert.True(t, collector.CanCollectFromProcess(eventCommandline))
150+
assert.True(t, collector.CanCollectFromProcess(configfilesdiscoveryimpl.TargetCommandline{
151+
Args: []string{"redis-server", "/etc/redis/redis.conf"},
152+
}))
151153

152-
collected, err := redisConfigCollector{}.Collect(context.Background(), reader)
154+
collected, err := collector.Collect(context.Background(), reader)
153155

154156
require.NoError(t, err)
155157
assert.Equal(t, []string{"/etc/redis/redis.conf"}, reader.readFileCalls)

0 commit comments

Comments
 (0)