Skip to content

Commit

Permalink
feat(inputs.gnmi): Add keepalive settings (#15171)
Browse files Browse the repository at this point in the history
Co-authored-by: Hunter Kelly <retnuh@gmail.com>
  • Loading branch information
powersj and retnuh authored Apr 17, 2024
1 parent 1214de6 commit c443b76
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
12 changes: 12 additions & 0 deletions plugins/inputs/gnmi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
## redial in case of failures after
# redial = "10s"

## gRPC Keepalive settings
## See https://pkg.go.dev/google.golang.org/grpc/keepalive
## The client will ping the server to see if the transport is still alive if it has
## not see any activity for the given time.
## If not set, none of the keep-alive setting (including those below) will be applied.
## If set and set below 10 seconds, the gRPC library will apply a minimum value of 10s will be used instead.
# keepalive_time = ""

## Timeout for seeing any activity after the keep-alive probe was
## sent. If no activity is seen the connection is closed.
# keepalive_timeout = ""

## gRPC Maximum Message Size
# max_msg_size = "4MB"

Expand Down
8 changes: 8 additions & 0 deletions plugins/inputs/gnmi/gnmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/google/gnxi/utils/xpath"
gnmiLib "github.com/openconfig/gnmi/proto/gnmi"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/metadata"

"github.com/influxdata/telegraf"
Expand Down Expand Up @@ -57,6 +58,8 @@ type GNMI struct {
GuessPathTag bool `toml:"guess_path_tag" deprecated:"1.30.0;use 'path_guessing_strategy' instead"`
GuessPathStrategy string `toml:"path_guessing_strategy"`
EnableTLS bool `toml:"enable_tls" deprecated:"1.27.0;use 'tls_enable' instead"`
KeepaliveTime config.Duration `toml:"keepalive_time"`
KeepaliveTimeout config.Duration `toml:"keepalive_timeout"`
Log telegraf.Logger `toml:"-"`
internaltls.ClientConfig

Expand Down Expand Up @@ -233,6 +236,11 @@ func (c *GNMI) Start(acc telegraf.Accumulator) error {
trimSlash: c.TrimFieldNames,
guessPathStrategy: c.GuessPathStrategy,
log: c.Log,
ClientParameters: keepalive.ClientParameters{
Time: time.Duration(c.KeepaliveTime),
Timeout: time.Duration(c.KeepaliveTimeout),
PermitWithoutStream: false,
},
}
for ctx.Err() == nil {
if err := h.subscribeGNMI(ctx, acc, tlscfg, request); err != nil && ctx.Err() == nil {
Expand Down
6 changes: 6 additions & 0 deletions plugins/inputs/gnmi/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"

Expand All @@ -42,6 +43,7 @@ type handler struct {
trimSlash bool
guessPathStrategy string
log telegraf.Logger
keepalive.ClientParameters
}

// SubscribeGNMI and extract telemetry data
Expand All @@ -62,6 +64,10 @@ func (h *handler) subscribeGNMI(ctx context.Context, acc telegraf.Accumulator, t
))
}

if h.ClientParameters.Time > 0 {
opts = append(opts, grpc.WithKeepaliveParams(h.ClientParameters))
}

client, err := grpc.DialContext(ctx, h.address, opts...)
if err != nil {
return fmt.Errorf("failed to dial: %w", err)
Expand Down
12 changes: 12 additions & 0 deletions plugins/inputs/gnmi/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
## redial in case of failures after
# redial = "10s"

## gRPC Keepalive settings
## See https://pkg.go.dev/google.golang.org/grpc/keepalive
## The client will ping the server to see if the transport is still alive if it has
## not see any activity for the given time.
## If not set, none of the keep-alive setting (including those below) will be applied.
## If set and set below 10 seconds, the gRPC library will apply a minimum value of 10s will be used instead.
# keepalive_time = ""

## Timeout for seeing any activity after the keep-alive probe was
## sent. If no activity is seen the connection is closed.
# keepalive_timeout = ""

## gRPC Maximum Message Size
# max_msg_size = "4MB"

Expand Down

0 comments on commit c443b76

Please sign in to comment.