fix(processors.snmp_lookup): Return empty tag-map on error to avoid panic (#15466)
This commit is contained in:
parent
f0c72586cc
commit
45e9ae4658
|
|
@ -126,25 +126,25 @@ func (l *Lookup) Add(m telegraf.Metric, acc telegraf.Accumulator) error {
|
||||||
|
|
||||||
// Default update function
|
// Default update function
|
||||||
func (l *Lookup) updateAgent(agent string) *tagMap {
|
func (l *Lookup) updateAgent(agent string) *tagMap {
|
||||||
|
tm := &tagMap{created: time.Now()}
|
||||||
|
|
||||||
// Initialize connection to agent
|
// Initialize connection to agent
|
||||||
conn, err := l.getConnectionFunc(agent)
|
conn, err := l.getConnectionFunc(agent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Log.Errorf("Getting connection for %q failed: %v", agent, err)
|
l.Log.Errorf("Getting connection for %q failed: %v", agent, err)
|
||||||
return nil
|
return tm
|
||||||
}
|
}
|
||||||
|
|
||||||
// Query table including translation
|
// Query table including translation
|
||||||
table, err := l.table.Build(conn, true)
|
table, err := l.table.Build(conn, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Log.Errorf("Building table for %q failed: %v", agent, err)
|
l.Log.Errorf("Building table for %q failed: %v", agent, err)
|
||||||
return nil
|
return tm
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy tags for all rows
|
// Copy tags for all rows
|
||||||
tm := &tagMap{
|
tm.created = table.Time
|
||||||
created: table.Time,
|
tm.rows = make(tagMapRows, len(table.Rows))
|
||||||
rows: make(tagMapRows, len(table.Rows)),
|
|
||||||
}
|
|
||||||
for _, row := range table.Rows {
|
for _, row := range table.Rows {
|
||||||
index := row.Tags["index"]
|
index := row.Tags["index"]
|
||||||
delete(row.Tags, "index")
|
delete(row.Tags, "index")
|
||||||
|
|
|
||||||
|
|
@ -231,7 +231,12 @@ func TestUpdateAgent(t *testing.T) {
|
||||||
t.Run("table build fail", func(t *testing.T) {
|
t.Run("table build fail", func(t *testing.T) {
|
||||||
tsc = &testSNMPConnection{}
|
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())
|
require.EqualValues(t, 1, tsc.calls.Load())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -240,7 +245,12 @@ func TestUpdateAgent(t *testing.T) {
|
||||||
return nil, errors.New("Random connection error")
|
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)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue