func NewDatadogStats(datadogHost string) (*DatadogStats, error) { var ip net.IP = nil var err error = nil // Assume datadogHost is an IP and try to parse it ip = net.ParseIP(datadogHost) // Parsing failed if ip == nil { ips, _ := net.LookupIP(datadogHost) if len(ips) > 0 { ip = ips[0] } } if ip != nil { gdsp, err := godspeed.New(ip.String(), godspeed.DefaultPort, false) if err == nil { return &DatadogStats{gdsp}, nil } } return nil, err }
func (t *TestSuite) TestNew(c *C) { // build Godspeed var g *godspeed.Godspeed g, err := godspeed.New("127.0.0.1", 8125, false) c.Assert(err, IsNil) defer g.Conn.Close() // test defined basic functionality testBasicFunc(t, c, g) }
func New(ddUrl string, tags []string) (*DDHandler, error) { sw := &DDHandler{} sw.tags = tags var err error sw.client, err = godspeed.New(ddUrl, godspeed.DefaultPort, false) if err != nil { return nil, fmt.Errorf("Error connecting to datadog agent. %s", err) } return sw, nil }
func ExampleNew() { g, err := godspeed.New(godspeed.DefaultHost, godspeed.DefaultPort, false) if err != nil { // handle error } defer g.Conn.Close() err = g.Gauge("example.stat", 1, nil) if err != nil { // handle error } }
func (e *Datadog) getGodspeed() { var gs *godspeed.Godspeed var err error // if config options not set, use defaults if e.Config.Host == "" || e.Config.Port == "" { gs, err = godspeed.NewDefault() } else { port, err := strconv.Atoi(e.Config.Port) if err != nil { log.Error(err.Error()) } gs, err = godspeed.New(e.Config.Host, port, false) } if err != nil { log.Error(err.Error()) } e._godspeed = gs }
// SendStats sends statistics to the Datadog API using either Dogstatsd or the // REST API. func SendStats(nodes []Node, start time.Time) { elapsed := time.Since(start) hostCount := int(len(nodes)) milliseconds := int64(elapsed / time.Millisecond) var tags []string envTag := fmt.Sprintf("environment:%s", ChefEnvironment) tags = append(tags, envTag) if DogStatsd { statsd, _ := godspeed.New(DogStatsdAddress, godspeed.DefaultPort, false) defer statsd.Conn.Close() statsd.Gauge("envoy.time", float64(milliseconds), tags) statsd.Gauge("envoy.hosts", float64(hostCount), tags) Log(fmt.Sprintf("create: dogstatsd='true' time='%s' hosts='%d'", elapsed, hostCount), "info") } if DatadogAPIKey != "" && DatadogAPPKey != "" { client := datadog.NewClient(DatadogAPIKey, DatadogAPPKey) timestamp := float64(time.Now().Unix()) err := client.PostMetrics([]datadog.Metric{ { Metric: "envoy.time", Points: []datadog.DataPoint{ {timestamp, float64(milliseconds)}, }, Tags: tags, }, { Metric: "envoy.hosts", Points: []datadog.DataPoint{ {timestamp, float64(hostCount)}, }, Tags: tags, }, }) if err != nil { Log("Something went wrong.", "info") } } }
func main() { cfg := config.LoadConfig() store := resolver.LoadStore(cfg) rp := repo.New(cfg) var gs *godspeed.Godspeed var err error if cfg.StatsEnabled() { gs, err = godspeed.New(cfg.Stats.Host, cfg.Stats.Port, false) if err != nil { printer.SayErr("%v", err) os.Exit(1) } } kv := api.New(store, rp, cfg, gs) ctrl := controller.New(cfg, kv) dcdr := cli.New(ctrl) dcdr.Run() }
func InitStats() error { log.Println("Initializing statsd: " + os.Getenv("DATADOG_PORT_8125_UDP_ADDR") + ":" + os.Getenv("DATADOG_PORT_8125_UDP_PORT")) port := os.Getenv("DATADOG_PORT_8125_UDP_PORT") intPort, err := strconv.ParseInt(port, 10, 64) if err != nil { return err } client, err := godspeed.New(os.Getenv("DATADOG_PORT_8125_UDP_ADDR"), int(intPort), true) if err != nil { return err } StatsClient = client StatsEnabled = true StatsClient.SetNamespace(os.Getenv("DATADOG_STATS_NAMESPACE")) tags := os.Getenv("DATADOG_STATS_TAGS") if len(tags) > 0 { tag := strings.Split(tags, ",") StatsClient.AddTags(tag) } return nil }
func (t *TestSuite) TestSend(c *C) { gs, err := godspeed.New("127.0.0.1", 8125, true) c.Assert(err, IsNil) // // Test whether auto-truncation works // // add a bunch of tags the pad the body with a lot of content for i := 0; i < 2100; i++ { gs.AddTag(randString(3)) } err = gs.Send("test.metric", "c", 42, 1, nil) c.Assert(err, IsNil) a, ok := <-t.o c.Assert(ok, Equals, true) c.Check(len(a), Equals, godspeed.MaxBytes) gs.Conn.Close() gs = nil // // test whether sending a plain metric works // err = t.g.Send("testing.metric", "ms", 256.512, 1, nil) c.Assert(err, IsNil) a, ok = <-t.o c.Assert(ok, Equals, true) c.Check(string(a), Equals, "testing.metric:256.512|ms") // // test whether sending a large metric works // err = t.g.Send("testing.metric", "g", 5536650702696, 1, nil) c.Assert(err, IsNil) a, ok = <-t.o c.Assert(ok, Equals, true) c.Check(string(a), Equals, "testing.metric:5536650702696|g") // // test whether sending a metric with a sample rate works // err = t.g.Send("testing.metric", "ms", 256.512, 0.99, nil) c.Assert(err, IsNil) a, ok = <-t.o c.Assert(ok, Equals, true) c.Check(string(a), Equals, "testing.metric:256.512|ms|@0.99") // // test whether metrics are properly sent with the namespace // t.g.SetNamespace("godspeed") err = t.g.Send("testing.metric", "ms", 512.1024, 1, nil) c.Assert(err, IsNil) a, ok = <-t.o c.Assert(ok, Equals, true) c.Check(string(a), Equals, "godspeed.testing.metric:512.1024|ms") // // test that adding a tag to the instance sends it with the metric // t.g.AddTag("test") err = t.g.Send("testing.metric", "ms", 512.1024, 1, nil) c.Assert(err, IsNil) a, ok = <-t.o c.Assert(ok, Equals, true) c.Check(string(a), Equals, "godspeed.testing.metric:512.1024|ms|#test") // // test whether adding a second tag causes both to get sent with the stat // t.g.AddTag("test1") err = t.g.Send("testing.metric", "ms", 512.1024, 1, nil) c.Assert(err, IsNil) a, ok = <-t.o c.Assert(ok, Equals, true) c.Check(string(a), Equals, "godspeed.testing.metric:512.1024|ms|#test,test1") // // test whether adding multiple tags sends all tags with the metric // t.g.AddTags([]string{"test2", "test3"}) err = t.g.Send("testing.metric", "ms", 512.1024, 1, nil) c.Assert(err, IsNil) a, ok = <-t.o c.Assert(ok, Equals, true) c.Check(string(a), Equals, "godspeed.testing.metric:512.1024|ms|#test,test1,test2,test3") // // test that adding metrics to the stat sends all instance tags with provided tags appended // err = t.g.Send("testing.metric", "ms", 512.1024, 1, []string{"test4", "test5", "test3"}) c.Assert(err, IsNil) a, ok = <-t.o c.Assert(ok, Equals, true) c.Check(string(a), Equals, "godspeed.testing.metric:512.1024|ms|#test,test1,test2,test3,test4,test5") // // test that adding tags to a single metric doesn't persist on future stats // err = t.g.Send("testing.metric", "ms", 512.1024, 1, nil) c.Assert(err, IsNil) a, ok = <-t.o c.Assert(ok, Equals, true) c.Check(string(a), Equals, "godspeed.testing.metric:512.1024|ms|#test,test1,test2,test3") // // test that a failure is returned when autoTruncate is false, and the body is larger than MAX_BYTES // for i := 0; i < 2100; i++ { t.g.AddTag(randString(3)) } err = t.g.Send("test.metric", "c", 42, 1, nil) c.Assert(err, Not(IsNil)) }