Skip to content

Instantly share code, notes, and snippets.

@faustind
Created August 23, 2023 00:10
Show Gist options
  • Save faustind/2b6caddb9789bda5a844d421bdf5f39c to your computer and use it in GitHub Desktop.
Save faustind/2b6caddb9789bda5a844d421bdf5f39c to your computer and use it in GitHub Desktop.
Listens for dogstatsd metrics on localhost:8125 and prints them to stdout
package main
import (
"fmt"
"net"
"time"
)
func main() {
// Address to listen for incoming UDP packets
addr, err := net.ResolveUDPAddr("udp", "localhost:8125")
if err != nil {
fmt.Println("Error resolving UDP address:", err)
return
}
// Create a UDP connection
conn, err := net.ListenUDP("udp", addr)
if err != nil {
fmt.Println("Error creating UDP connection:", err)
return
}
defer conn.Close()
fmt.Println("UDP server listening on localhost:8125...")
// Buffer to hold incoming data
buffer := make([]byte, 1024)
for {
// Read data from UDP connection
n, _, err := conn.ReadFromUDP(buffer)
if err != nil {
fmt.Println("Error reading data:", err)
continue
}
hms := time.Now()
// Process the received data (you can add your own custom logic here)
data := string(buffer[:n])
fmt.Printf("[%v] Received: %s\n", hms, data)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment