From 982d121929b8b012227be34e4d90c0a691967241 Mon Sep 17 00:00:00 2001 From: Jorge Canha Date: Sat, 10 Feb 2018 02:50:31 +0000 Subject: [PATCH] Add keep alive support to the TCP mode of statsd --- etc/telegraf.conf | 7 +++++++ plugins/inputs/statsd/README.md | 10 ++++++++++ plugins/inputs/statsd/statsd.go | 24 ++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/etc/telegraf.conf b/etc/telegraf.conf index 438a327714f7b..b09ff3912e63f 100644 --- a/etc/telegraf.conf +++ b/etc/telegraf.conf @@ -3045,6 +3045,13 @@ # # ## MaxTCPConnection - applicable when protocol is set to tcp (default=250) # max_tcp_connections = 250 +# ## Enable TCP keep alive probes (default=false) +# tcp_keep_alive = false +# +# ## Specifies the keep-alive period for an active network connection. +# ## Only applies to TCP sockets and will be ignored if tcp_keep_alive is false. +# ## Defaults to the OS configuration. +# # tcp_keep_alive_period = "2h" # # ## Address and port to host UDP listener on # service_address = ":8125" diff --git a/plugins/inputs/statsd/README.md b/plugins/inputs/statsd/README.md index 9562b93626e31..648fa72ac2577 100644 --- a/plugins/inputs/statsd/README.md +++ b/plugins/inputs/statsd/README.md @@ -10,6 +10,14 @@ ## MaxTCPConnection - applicable when protocol is set to tcp (default=250) max_tcp_connections = 250 + + ## Enable TCP keep alive probes (default=false) + tcp_keep_alive = false + + ## Specifies the keep-alive period for an active network connection. + ## Only applies to TCP sockets and will be ignored if tcp_keep_alive is false. + ## Defaults to the OS configuration. + # tcp_keep_alive_period = "2h" ## Address and port to host UDP listener on service_address = ":8125" @@ -157,6 +165,8 @@ metric type: - **protocol** string: Protocol used in listener - tcp or udp options - **max_tcp_connections** []int: Maximum number of concurrent TCP connections to allow. Used when protocol is set to tcp. +- **tcp_keep_alive** boolean: Enable TCP keep alive probes +- **tcp_keep_alive_period** internal.Duration: Specifies the keep-alive period for an active network connection - **service_address** string: Address to listen for statsd UDP packets on - **delete_gauges** boolean: Delete gauges on every collection interval - **delete_counters** boolean: Delete counters on every collection interval diff --git a/plugins/inputs/statsd/statsd.go b/plugins/inputs/statsd/statsd.go index 9f711d91319ec..3e5a73aa3e998 100644 --- a/plugins/inputs/statsd/statsd.go +++ b/plugins/inputs/statsd/statsd.go @@ -113,6 +113,9 @@ type Statsd struct { MaxTCPConnections int `toml:"max_tcp_connections"` + TCPKeepAlive bool `toml:"tcp_keep_alive"` + TCPKeepAlivePeriod *internal.Duration `toml:"tcp_keep_alive_period"` + graphiteParser *graphite.GraphiteParser acc telegraf.Accumulator @@ -177,6 +180,14 @@ const sampleConfig = ` ## MaxTCPConnection - applicable when protocol is set to tcp (default=250) max_tcp_connections = 250 + ## Enable TCP keep alive probes (default=false) + tcp_keep_alive = false + + ## Specifies the keep-alive period for an active network connection. + ## Only applies to TCP sockets and will be ignored if tcp_keep_alive is false. + ## Defaults to the OS configuration. + # tcp_keep_alive_period = "2h" + ## Address and port to host UDP listener on service_address = ":8125" @@ -361,6 +372,18 @@ func (s *Statsd) tcpListen() error { return err } + if s.TCPKeepAlive { + if err = conn.SetKeepAlive(true); err != nil { + return err + } + + if s.TCPKeepAlivePeriod != nil { + if err = conn.SetKeepAlivePeriod(s.TCPKeepAlivePeriod.Duration); err != nil { + return err + } + } + } + select { case <-s.accept: // not over connection limit, handle the connection properly. @@ -863,6 +886,7 @@ func init() { Protocol: defaultProtocol, ServiceAddress: ":8125", MaxTCPConnections: 250, + TCPKeepAlive: false, MetricSeparator: "_", AllowedPendingMessages: defaultAllowPendingMessage, DeleteCounters: true,