Skip to content

Commit

Permalink
fix(inputs.nsq_consumer): Move config checks to Init method
Browse files Browse the repository at this point in the history
  • Loading branch information
Hipska committed Sep 12, 2024
1 parent 3da9e45 commit c4d27b6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
24 changes: 14 additions & 10 deletions plugins/inputs/nsq_consumer/nsq_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ func (*NSQConsumer) SampleConfig() string {
return sampleConfig
}

func (n *NSQConsumer) Init() error {
// For backward compatibility
if n.Server != "" {
n.Nsqd = append(n.Nsqd, n.Server)
}

// Check if we have anything to connect to
if len(n.Nsqlookupd) == 0 && len(n.Nsqd) == 0 {
return errors.New("either 'nsqd' or 'nsqlookupd' needs to be specified")
}

return nil
}

// SetParser takes the data_format from the config and finds the right parser for that format
func (n *NSQConsumer) SetParser(parser telegraf.Parser) {
n.parser = parser
Expand Down Expand Up @@ -104,16 +118,6 @@ func (n *NSQConsumer) Start(ac telegraf.Accumulator) error {
return nil
}))

// For backward compatibility
if n.Server != "" {
n.Nsqd = append(n.Nsqd, n.Server)
}

// Check if we have anything to connect to
if len(n.Nsqlookupd) == 0 && len(n.Nsqd) == 0 {
return errors.New("either 'nsqd' or 'nsqlookupd' needs to be specified")
}

if len(n.Nsqlookupd) > 0 {
err := n.consumer.ConnectToNSQLookupds(n.Nsqlookupd)
if err != nil && !errors.Is(err, nsq.ErrAlreadyConnected) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/nsq_consumer/nsq_consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ func TestReadsMetricsFromNSQ(t *testing.T) {

consumer := &NSQConsumer{
Log: testutil.Logger{},
Server: "127.0.0.1:4155",
Topic: "telegraf",
Channel: "consume",
MaxInFlight: 1,
MaxUndeliveredMessages: defaultMaxUndeliveredMessages,
Nsqd: []string{"127.0.0.1:4155"},
}
require.NoError(t, consumer.Init())

p := &influx.Parser{}
require.NoError(t, p.Init())
Expand Down

0 comments on commit c4d27b6

Please sign in to comment.