Skip to content

Instantly share code, notes, and snippets.

@truthbk
Created August 9, 2018 11:40
Show Gist options
  • Save truthbk/e91a567c3230da357f2836d54a2a1956 to your computer and use it in GitHub Desktop.
Save truthbk/e91a567c3230da357f2836d54a2a1956 to your computer and use it in GitHub Desktop.
Random dogstatsd sampler
package main
import (
"fmt"
"math/rand"
"os"
"os/signal"
"time"
"github.com/DataDog/datadog-go/statsd"
)
func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
// getting environment varialbles
addr := fmt.Sprintf("%s:%s",
os.Getenv("DOGSTATSD_HOST_IP"),
os.Getenv("DOGSTATSD_HOST_PORT"))
fmt.Printf("attempting to connect to: %s", addr)
client, err := statsd.New(addr)
if err != nil {
fmt.Printf("could not setup client: %v", err)
os.Exit(1)
}
// prefix every metric with the app name
client.Namespace = "datadog.flubber."
// send the EC2 availability zone as a tag with every metric
client.Tags = append(client.Tags, "region:minikube")
r := rand.New(rand.NewSource(time.Now().UnixNano()))
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for {
select {
case <-c:
os.Exit(0)
case <-ticker.C:
err = client.Gauge("random.sample", float64(r.Intn(5)), nil, 1)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment