add targetError struct
@@ -21,6 +21,11 @@ import (
"google.golang.org/grpc/metadata"
)
+type TargetError struct {
+ SubscriptionName string
+ Err error
+}
+
// Target represents a gNMI enabled box
type Target struct {
Config *TargetConfig
@@ -32,7 +37,7 @@ type Target struct {
SubscribeClients map[string]gnmi.GNMI_SubscribeClient // subscription name to subscribeClient
PollChan chan string // subscription name to be polled
SubscribeResponses chan *SubscribeResponse
- Errors chan error
+ Errors chan *TargetError
}
// TargetConfig //
@@ -70,7 +75,7 @@ func NewTarget(c *TargetConfig) *Target {
SubscribeClients: make(map[string]gnmi.GNMI_SubscribeClient),
PollChan: make(chan string),
SubscribeResponses: make(chan *SubscribeResponse, c.BufferSize),
- Errors: make(chan error),
+ Errors: make(chan *TargetError),
}
return t
@@ -156,7 +161,10 @@ func (t *Target) Subscribe(ctx context.Context, req *gnmi.SubscribeRequest, subs
nctx = metadata.AppendToOutgoingContext(nctx, "username", *t.Config.Username, "password", *t.Config.Password)
subscribeClient, err := t.Client.Subscribe(nctx)
if err != nil {
- t.Errors <- fmt.Errorf("failed to create a subscribe client, target='%s': %v", t.Config.Name, err)
+ t.Errors <- &TargetError{
+ SubscriptionName: subscriptionName,
+ Err: fmt.Errorf("failed to create a subscribe client, target='%s': %v", t.Config.Name, err),
+ }
return
}
t.m.Lock()
@@ -164,7 +172,10 @@ func (t *Target) Subscribe(ctx context.Context, req *gnmi.SubscribeRequest, subs
t.m.Unlock()
err = subscribeClient.Send(req)
if err != nil {
- t.Errors <- fmt.Errorf("target '%s' send error: %v", t.Config.Name, err)
+ t.Errors <- &TargetError{
+ SubscriptionName: subscriptionName,
+ Err: fmt.Errorf("target '%s' send error: %v", t.Config.Name, err),
+ }
return
}
switch req.GetSubscribe().Mode {
@@ -172,11 +183,17 @@ func (t *Target) Subscribe(ctx context.Context, req *gnmi.SubscribeRequest, subs
for {
response, err := subscribeClient.Recv()
if err == io.EOF {
- t.Errors <- err
+ t.Errors <- &TargetError{
+ SubscriptionName: subscriptionName,
+ Err: err,
+ }
return
}
if err != nil {
- t.Errors <- fmt.Errorf("receive error: %v", err)
+ t.Errors <- &TargetError{
+ SubscriptionName: subscriptionName,
+ Err: fmt.Errorf("receive error: %v", err),
+ }
return
}
t.SubscribeResponses <- &SubscribeResponse{Response: response, SubscriptionName: subscriptionName}
@@ -197,12 +214,18 @@ func (t *Target) Subscribe(ctx context.Context, req *gnmi.SubscribeRequest, subs
},
})
if err != nil {
- t.Errors <- fmt.Errorf("failed to send PollRequest: %v", err)
+ t.Errors <- &TargetError{
+ SubscriptionName: subscriptionName,
+ Err: fmt.Errorf("failed to send PollRequest: %v", err),
+ }
continue
}
response, err := subscribeClient.Recv()
if err != nil {
- t.Errors <- fmt.Errorf("rcv error: %v", err)
+ t.Errors <- &TargetError{
+ SubscriptionName: subscriptionName,
+ Err: fmt.Errorf("rcv error: %v", err),
+ }
continue
}
t.SubscribeResponses <- &SubscribeResponse{Response: response, SubscriptionName: subscriptionName}
