Fix some inputs panic will lead to the telegraf exit

This commit is contained in:
Wu Taizeng 2016-01-26 16:19:34 +08:00
parent 47ea2d5fb4
commit 449c436e5d
1 changed files with 19 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import (
"math/big" "math/big"
"math/rand" "math/rand"
"os" "os"
"runtime"
"sync" "sync"
"time" "time"
@ -103,6 +104,15 @@ func (a *Agent) gatherParallel(pointChan chan *client.Point) error {
wg.Add(1) wg.Add(1)
counter++ counter++
go func(input *models.RunningInput) { go func(input *models.RunningInput) {
defer func() {
if err := recover(); err != nil {
trace := make([]byte, 2048)
count := runtime.Stack(trace, true)
log.Printf("Input [%s] recover from panic: %s\n", input.Name, err)
log.Printf("Input [%s] stack of %d bytes: %s\n", input.Name, count, trace)
}
}()
defer wg.Done() defer wg.Done()
acc := NewAccumulator(input.Config, pointChan) acc := NewAccumulator(input.Config, pointChan)
@ -148,6 +158,15 @@ func (a *Agent) gatherSeparate(
input *models.RunningInput, input *models.RunningInput,
pointChan chan *client.Point, pointChan chan *client.Point,
) error { ) error {
defer func() {
if err := recover(); err != nil {
trace := make([]byte, 2048)
count := runtime.Stack(trace, true)
log.Printf("Input [%s] recover from panic: %s\n", input.Name, err)
log.Printf("Input [%s] stack of %d bytes: %s\n", input.Name, count, trace)
}
}()
ticker := time.NewTicker(input.Config.Interval) ticker := time.NewTicker(input.Config.Interval)
for { for {