Skip to content

Commit

Permalink
Add UDP IPv6 support to statsd input (#3344)
Browse files Browse the repository at this point in the history
  • Loading branch information
mindscratch authored and danielnelson committed Oct 16, 2017
1 parent b73f493 commit cc47382
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion plugins/inputs/statsd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
```toml
# Statsd Server
[[inputs.statsd]]
## Protocol, must be "tcp" or "udp" (default=udp)
## Protocol, must be "tcp", "udp4", "udp6" or "udp" (default=udp)
protocol = "udp"

## MaxTCPConnection - applicable when protocol is set to tcp (default=250)
Expand Down
23 changes: 12 additions & 11 deletions plugins/inputs/statsd/statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (_ *Statsd) Description() string {
}

const sampleConfig = `
## Protocol, must be "tcp" or "udp" (default=udp)
## Protocol, must be "tcp", "udp", "udp4" or "udp6" (default=udp)
protocol = "udp"
## MaxTCPConnection - applicable when protocol is set to tcp (default=250)
Expand Down Expand Up @@ -327,10 +327,9 @@ func (s *Statsd) Start(_ telegraf.Accumulator) error {

s.wg.Add(2)
// Start the UDP listener
switch s.Protocol {
case "udp":
if s.isUDP() {
go s.udpListen()
case "tcp":
} else {
go s.tcpListen()
}
// Start the line parser
Expand Down Expand Up @@ -382,8 +381,8 @@ func (s *Statsd) tcpListen() error {
func (s *Statsd) udpListen() error {
defer s.wg.Done()
var err error
address, _ := net.ResolveUDPAddr("udp", s.ServiceAddress)
s.UDPlistener, err = net.ListenUDP("udp", address)
address, _ := net.ResolveUDPAddr(s.Protocol, s.ServiceAddress)
s.UDPlistener, err = net.ListenUDP(s.Protocol, address)
if err != nil {
log.Fatalf("ERROR: ListenUDP - %s", err)
}
Expand Down Expand Up @@ -825,10 +824,9 @@ func (s *Statsd) Stop() {
s.Lock()
log.Println("I! Stopping the statsd service")
close(s.done)
switch s.Protocol {
case "udp":
if s.isUDP() {
s.UDPlistener.Close()
case "tcp":
} else {
s.TCPlistener.Close()
// Close all open TCP connections
// - get all conns from the s.conns map and put into slice
Expand All @@ -843,8 +841,6 @@ func (s *Statsd) Stop() {
for _, conn := range conns {
conn.Close()
}
default:
s.UDPlistener.Close()
}
s.Unlock()

Expand All @@ -856,6 +852,11 @@ func (s *Statsd) Stop() {
s.Unlock()
}

// IsUDP returns true if the protocol is UDP, false otherwise.
func (s *Statsd) isUDP() bool {
return strings.HasPrefix(s.Protocol, "udp")
}

func init() {
inputs.Add("statsd", func() telegraf.Input {
return &Statsd{
Expand Down

0 comments on commit cc47382

Please sign in to comment.