fix(processors.snmp_lookup): Return empty tag-map on error to avoid panic (#15466)

This commit is contained in:
Thomas Casteleyn 2024-06-06 15:46:22 +02:00 committed by GitHub
parent f0c72586cc
commit 45e9ae4658
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 8 deletions

View File

@ -126,25 +126,25 @@ func (l *Lookup) Add(m telegraf.Metric, acc telegraf.Accumulator) error {
// Default update function
func (l *Lookup) updateAgent(agent string) *tagMap {
tm := &tagMap{created: time.Now()}
// Initialize connection to agent
conn, err := l.getConnectionFunc(agent)
if err != nil {
l.Log.Errorf("Getting connection for %q failed: %v", agent, err)
return nil
return tm
}
// Query table including translation
table, err := l.table.Build(conn, true)
if err != nil {
l.Log.Errorf("Building table for %q failed: %v", agent, err)
return nil
return tm
}
// Copy tags for all rows
tm := &tagMap{
created: table.Time,
rows: make(tagMapRows, len(table.Rows)),
}
tm.created = table.Time
tm.rows = make(tagMapRows, len(table.Rows))
for _, row := range table.Rows {
index := row.Tags["index"]
delete(row.Tags, "index")

View File

@ -231,7 +231,12 @@ func TestUpdateAgent(t *testing.T) {
t.Run("table build fail", func(t *testing.T) {
tsc = &testSNMPConnection{}
require.Nil(t, p.updateAgent("127.0.0.1"))
start := time.Now()
tm := p.updateAgent("127.0.0.1")
end := time.Now()
require.Nil(t, tm.rows)
require.WithinRange(t, tm.created, start, end)
require.EqualValues(t, 1, tsc.calls.Load())
})
@ -240,7 +245,12 @@ func TestUpdateAgent(t *testing.T) {
return nil, errors.New("Random connection error")
}
require.Nil(t, p.updateAgent("127.0.0.1"))
start := time.Now()
tm := p.updateAgent("127.0.0.1")
end := time.Now()
require.Nil(t, tm.rows)
require.WithinRange(t, tm.created, start, end)
})
}