Created
August 23, 2023 00:10
-
-
Save faustind/2b6caddb9789bda5a844d421bdf5f39c to your computer and use it in GitHub Desktop.
Listens for dogstatsd metrics on localhost:8125 and prints them to stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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