Skip to content

Commit a262d94

Browse files
authored
Merge pull request #253 from newrelic/fix/warning-stops-mbeans-processing
fix: warning stops mbeans processing
2 parents 1601955 + 22f38a2 commit a262d94

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

jmx/jmx.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ func doQuery(ctx context.Context, out chan []byte, queryErrC chan error, querySt
323323
queryErrC <- fmt.Errorf("reading nrjmx stdout: %s", err.Error())
324324
}
325325
out <- b
326+
return
326327
}
327328
}
328329

@@ -341,6 +342,7 @@ func Query(objectPattern string, timeoutMillis int) (result map[string]interface
341342

342343
// receiveResult checks for channels to receive result from nrjmx command.
343344
func receiveResult(lineC chan []byte, queryErrC chan error, cancelFn context.CancelFunc, objectPattern string, timeout time.Duration) (result map[string]interface{}, err error) {
345+
defer logAvailableWarnings(cmdWarnC)
344346
var warn string
345347
for {
346348
select {
@@ -361,12 +363,11 @@ func receiveResult(lineC chan []byte, queryErrC chan error, cancelFn context.Can
361363
for k, v := range r {
362364
result[k] = v
363365
}
366+
364367
return
365368

366369
case warn = <-cmdWarnC:
367-
// change on the API is required to return warnings
368370
log.Warn(warn)
369-
return
370371

371372
case err = <-cmdErrC:
372373
return
@@ -383,3 +384,15 @@ func receiveResult(lineC chan []byte, queryErrC chan error, cancelFn context.Can
383384
}
384385
}
385386
}
387+
388+
func logAvailableWarnings(channel chan string) {
389+
var warn string
390+
for {
391+
select {
392+
case warn = <-channel:
393+
log.Warn(warn)
394+
default:
395+
return
396+
}
397+
}
398+
}

jmx/jmx_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package jmx
22

33
import (
44
"bufio"
5+
"bytes"
56
"context"
67
"flag"
78
"fmt"
@@ -10,6 +11,7 @@ import (
1011
"testing"
1112
"time"
1213

14+
"github.com/newrelic/infra-integrations-sdk/v4/log"
1315
"github.com/stretchr/testify/assert"
1416
"github.com/stretchr/testify/require"
1517
)
@@ -179,17 +181,25 @@ func openWaitWithSSL(hostname, port, username, password, keyStore, keyStorePassw
179181
}
180182

181183
func Test_receiveResult_warningsDoNotBreakResultReception(t *testing.T) {
184+
185+
var buf bytes.Buffer
186+
log.SetOutput(&buf)
187+
182188
_, cancelFn := context.WithCancel(context.Background())
183189

184190
resultCh := make(chan []byte, 1)
185191
queryErrCh := make(chan error)
186192
outTimeout := time.Duration(timeoutMillis) * time.Millisecond
193+
warningMessage := fmt.Sprint("WARNING foo bar")
194+
cmdWarnC <- warningMessage
187195

188-
_, _ = receiveResult(resultCh, queryErrCh, cancelFn, "empty", outTimeout)
196+
resultCh <- []byte("{\"foo\":1}")
189197

190-
cmdErrC <- fmt.Errorf("WARNING foo bar")
191-
assert.Equal(t, <-cmdErrC, fmt.Errorf("WARNING foo bar"))
198+
result, err := receiveResult(resultCh, queryErrCh, cancelFn, "foo", outTimeout)
192199

193-
resultCh <- []byte("{foo}")
194-
assert.Equal(t, string(<-resultCh), "{foo}")
200+
assert.NoError(t, err)
201+
assert.Equal(t, map[string]interface{}{
202+
"foo": 1.,
203+
}, result)
204+
assert.Equal(t, fmt.Sprintf("[WARN] %s\n", warningMessage), buf.String())
195205
}

log/log.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ func SetupLogging(verbose bool) {
8181
}
8282
}
8383

84+
// SetOutput sets output stream
85+
func SetOutput(w io.Writer) {
86+
globalLogger.logger.SetOutput(w)
87+
}
88+
8489
// Debug logs a formatted message at level Debug.
8590
func Debug(format string, args ...interface{}) {
8691
globalLogger.Debugf(format, args...)

0 commit comments

Comments
 (0)