Skip to content

Commit

Permalink
Adds --statsd.passthrough-udp to allow forwarding statsd to elsewhere…
Browse files Browse the repository at this point in the history
… via UDP
  • Loading branch information
macabu committed Oct 19, 2023
1 parent 5a947bd commit 8868f74
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ NOTE: Version 0.7.0 switched to the [kingpin](https://github.com/alecthomas/king
metric lines in datagram. "" disables it.
--statsd.unixsocket-mode="755"
The permission mode of the unix socket.
--statsd.passthrough-udp=""
The UDP address to pass metrics through.
--statsd.mapping-config=STATSD.MAPPING-CONFIG
Metric mapping configuration file name.
--statsd.read-buffer=STATSD.READ-BUFFER
Expand Down
18 changes: 18 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ func main() {
statsdListenUnixgram = kingpin.Flag("statsd.listen-unixgram", "The Unixgram socket path to receive statsd metric lines in datagram. \"\" disables it.").Default("").String()
// not using Int here because flag displays default in decimal, 0755 will show as 493
statsdUnixSocketMode = kingpin.Flag("statsd.unixsocket-mode", "The permission mode of the unix socket.").Default("755").String()
statsdPassthroughUDP = kingpin.Flag("statsd.passthrough-udp", "The UDP address to pass metrics through").String()
mappingConfig = kingpin.Flag("statsd.mapping-config", "Metric mapping configuration file name.").String()
readBuffer = kingpin.Flag("statsd.read-buffer", "Size (in bytes) of the operating system's transmit read buffer associated with the UDP or Unixgram connection. Please make sure the kernel parameters net.core.rmem_max is set to a value greater than the value specified.").Int()
cacheSize = kingpin.Flag("statsd.cache-size", "Maximum size of your metric mapping cache. Relies on least recently used replacement policy if max size is reached.").Default("1000").Int()
Expand Down Expand Up @@ -360,6 +361,22 @@ func main() {
os.Exit(1)
}

var udpPassthroughConn *net.UDPConn

if *statsdPassthroughUDP != "" {
udpPassthroughAddr, err := address.UDPAddrFromString(*statsdPassthroughUDP)
if err != nil {
level.Error(logger).Log("msg", "invalid UDP passthrough address", "address", *statsdPassthroughUDP, "error", err)
os.Exit(1)
}

udpPassthroughConn, err = net.ListenUDP("udp", udpPassthroughAddr)
if err != nil {
level.Error(logger).Log("msg", "failed to start UDP passthrough", "error", err)
os.Exit(1)
}
}

if *readBuffer != 0 {
err = uconn.SetReadBuffer(*readBuffer)
if err != nil {
Expand All @@ -381,6 +398,7 @@ func main() {
SamplesReceived: samplesReceived,
TagErrors: tagErrors,
TagsReceived: tagsReceived,
Passthrough: udpPassthroughConn,
}

go ul.Listen()
Expand Down
10 changes: 10 additions & 0 deletions pkg/listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Parser interface {

type StatsDUDPListener struct {
Conn *net.UDPConn
Passthrough *net.UDPConn
EventHandler event.EventHandler
Logger log.Logger
LineParser Parser
Expand Down Expand Up @@ -64,6 +65,15 @@ func (l *StatsDUDPListener) Listen() {
level.Error(l.Logger).Log("error", err)
return
}

if l.Passthrough != nil {
_, err := l.Passthrough.Write(buf[0:n])
if err != nil {
level.Error(l.Logger).Log("msg", "Failed to write to UDP passthrough", "error", err)
return
}
}

l.HandlePacket(buf[0:n])
}
}
Expand Down

0 comments on commit 8868f74

Please sign in to comment.