Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable the ability to specify IPv4/6 expilcitly when listening on UDP #3344

Merged
merged 2 commits into from
Oct 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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