fix(inputs.gnmi): Create selfstat to track connection state (#13149)

This commit is contained in:
Joshua Powers 2023-05-02 05:29:30 -06:00 committed by GitHub
parent ca13259989
commit f55d214082
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 0 deletions

View File

@ -15,6 +15,7 @@ import (
"github.com/influxdata/telegraf/internal/choice"
"github.com/influxdata/telegraf/metric"
jnprHeader "github.com/influxdata/telegraf/plugins/inputs/gnmi/extensions/jnpr_gnmi_extention"
"github.com/influxdata/telegraf/selfstat"
gnmiLib "github.com/openconfig/gnmi/proto/gnmi"
gnmiExt "github.com/openconfig/gnmi/proto/gnmi_ext"
"google.golang.org/grpc"
@ -97,11 +98,19 @@ func (h *handler) subscribeGNMI(ctx context.Context, acc telegraf.Accumulator, t
}
h.log.Debugf("Connection to gNMI device %s established", h.address)
// Used to report the status of the TCP connection to the device. If the
// GNMI connection goes down, but TCP is still up this will still report
// connected until the TCP connection times out.
connectStat := selfstat.Register("gnmi", "grpc_connection_status", map[string]string{"source": h.address})
connectStat.Set(1)
defer h.log.Debugf("Connection to gNMI device %s closed", h.address)
for ctx.Err() == nil {
var reply *gnmiLib.SubscribeResponse
if reply, err = subscribeClient.Recv(); err != nil {
if !errors.Is(err, io.EOF) && ctx.Err() == nil {
connectStat.Set(0)
return fmt.Errorf("aborted gNMI subscription: %w", err)
}
break
@ -120,6 +129,8 @@ func (h *handler) subscribeGNMI(ctx context.Context, acc telegraf.Accumulator, t
h.handleSubscribeResponseUpdate(acc, response, reply.GetExtension())
}
}
connectStat.Set(0)
return nil
}