Skip to content

Commit

Permalink
Log SubscribeResponse_Error message and code. #8482 (#8483)
Browse files Browse the repository at this point in the history
(cherry picked from commit 6be3bd8)
  • Loading branch information
mousedownmike authored and ssoroka committed Dec 1, 2020
1 parent a57a0d8 commit 211e3fc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
13 changes: 8 additions & 5 deletions plugins/inputs/gnmi/gnmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,17 @@ func (c *GNMI) subscribeGNMI(ctx context.Context, address string, tlscfg *tls.Co
return nil
}

// HandleSubscribeResponse message from gNMI and parse contained telemetry data
func (c *GNMI) handleSubscribeResponse(address string, reply *gnmi.SubscribeResponse) {
// Check if response is a gNMI Update and if we have a prefix to derive the measurement name
response, ok := reply.Response.(*gnmi.SubscribeResponse_Update)
if !ok {
return
switch response := reply.Response.(type) {
case *gnmi.SubscribeResponse_Update:
c.handleSubscribeResponseUpdate(address, response)
case *gnmi.SubscribeResponse_Error:
c.Log.Errorf("Subscribe error (%d), %q", response.Error.Code, response.Error.Message)
}
}

// Handle SubscribeResponse_Update message from gNMI and parse contained telemetry data
func (c *GNMI) handleSubscribeResponseUpdate(address string, response *gnmi.SubscribeResponse_Update) {
var prefix, prefixAliasPath string
grouper := metric.NewSeriesGrouper()
timestamp := time.Unix(0, response.Update.Timestamp)
Expand Down
24 changes: 24 additions & 0 deletions plugins/inputs/gnmi/gnmi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,30 @@ func TestNotification(t *testing.T) {
}
}

type MockLogger struct {
telegraf.Logger
lastFormat string
lastArgs []interface{}
}

func (l *MockLogger) Errorf(format string, args ...interface{}) {
l.lastFormat = format
l.lastArgs = args
}

func TestSubscribeResponseError(t *testing.T) {
me := "mock error message"
var mc uint32 = 7
ml := &MockLogger{}
plugin := &GNMI{Log: ml}
errorResponse := &gnmi.SubscribeResponse_Error{
Error: &gnmi.Error{Message: me, Code: mc}}
plugin.handleSubscribeResponse(
"127.0.0.1:0", &gnmi.SubscribeResponse{Response: errorResponse})
require.NotEmpty(t, ml.lastFormat)
require.Equal(t, ml.lastArgs, []interface{}{mc, me})
}

func TestRedial(t *testing.T) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
Expand Down

0 comments on commit 211e3fc

Please sign in to comment.