Skip to content

Commit

Permalink
Fix for issue intelsdi-x#1167, added handling of errors which are ret…
Browse files Browse the repository at this point in the history
…urned by scanner
  • Loading branch information
katarzyna-z committed Oct 3, 2016
1 parent e621183 commit 0fc5130
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
44 changes: 43 additions & 1 deletion control/plugin/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func (e *ExecutablePlugin) Run(timeout time.Duration) (Response, error) {
e.cmd.Start()
e.captureStderr()
go func() {
OK:
for stdOutScanner.Scan() {
// The first chunk from the scanner is the plugin's response to the
// handshake. Once we've received that, we can begin to forward
Expand All @@ -130,7 +131,28 @@ func (e *ExecutablePlugin) Run(timeout time.Duration) (Response, error) {
"io": "stdout",
}).Debug(stdOutScanner.Text())
}

}

if err := stdOutScanner.Err(); err != nil {
if err == bufio.ErrTooLong {
reader := bufio.NewReader(e.stdout)
log, _, _ := reader.ReadLine()

execLogger.
WithField("plugin", path.Base(e.cmd.Path())).
WithField("io", "stdout").
WithField("scanner_err", stdOutScanner.Err()).
Debug(string(log))
goto OK
}

execLogger.
WithField("plugin", path.Base(e.cmd.Path())).
WithField("io", "stdout").
Error(stdOutScanner.Err())
}

}()

// Wait until:
Expand Down Expand Up @@ -167,12 +189,32 @@ func (e *ExecutablePlugin) Kill() error {
func (e *ExecutablePlugin) captureStderr() {
stdErrScanner := bufio.NewScanner(e.stderr)
go func() {
OK:
for stdErrScanner.Scan() {
execLogger.
WithField("io", "stderr").
WithField("plugin", path.Base(e.cmd.Path())).
WithField("io", "stderr").
Debug(stdErrScanner.Text())

}

if err := stdErrScanner.Err(); err != nil {
if err == bufio.ErrTooLong {
reader := bufio.NewReader(e.stderr)
log, _, _ := reader.ReadLine()

execLogger.
WithField("plugin", path.Base(e.cmd.Path())).
WithField("io", "stderr").
WithField("scanner_err", stdErrScanner.Err()).
Debug(string(log))
goto OK
}

execLogger.
WithField("plugin", path.Base(e.cmd.Path())).
WithField("io", "stderr").
Error(stdErrScanner.Err())
}
}()
}
18 changes: 15 additions & 3 deletions plugin/collector/snap-plugin-collector-mock2/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ limitations under the License.
package mock

import (
"bufio"
"fmt"
"log"
"math/rand"
"os"
"time"

"github.com/intelsdi-x/snap/control/plugin"
Expand Down Expand Up @@ -56,14 +58,24 @@ func (f *Mock) CollectMetrics(mts []plugin.MetricType) ([]plugin.MetricType, err
rand.Seed(time.Now().UTC().UnixNano())
metrics := []plugin.MetricType{}
for i := range mts {
if c, ok := mts[i].Config().Table()["long_print"]; ok && c.(ctypes.ConfigValueBool).Value {
if c, ok := mts[i].Config().Table()["long_stdout_log"]; ok && c.(ctypes.ConfigValueBool).Value {
letterBytes := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
longLine := []byte{}
for i := 0; i < 8193; i++ {
for i := 0; i < bufio.MaxScanTokenSize; i++ {
longLine = append(longLine, letterBytes[rand.Intn(len(letterBytes))])
}
fmt.Println(string(longLine))
fmt.Fprintln(os.Stdout, string(longLine))
}

if c, ok := mts[i].Config().Table()["long_stderr_log"]; ok && c.(ctypes.ConfigValueBool).Value {
letterBytes := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
longLine := []byte{}
for i := 0; i < bufio.MaxScanTokenSize; i++ {
longLine = append(longLine, letterBytes[rand.Intn(len(letterBytes))])
}
fmt.Fprintln(os.Stderr, string(longLine))
}

if c, ok := mts[i].Config().Table()["panic"]; ok && c.(ctypes.ConfigValueBool).Value {
panic("Oops!")
}
Expand Down

0 comments on commit 0fc5130

Please sign in to comment.