From b8a4eae5835e50c8699d9da104c357fe3395ca86 Mon Sep 17 00:00:00 2001 From: Jorge Canha Date: Fri, 16 Feb 2018 04:04:49 +0000 Subject: [PATCH] Add keep alive support to the TCP mode of statsd (#3781) --- 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 438a32771..b09ff3912 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 9562b9362..648fa72ac 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 9f711d913..3e5a73aa3 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,