예제 #1
0
func main() {
	ddClient, err := statsd.New("127.0.0.1:8125")
	if err != nil {
		log.Fatal(err)
	}
	ddClient.Namespace = namespace

	http.HandleFunc("/heartbeat", func(w http.ResponseWriter, r *http.Request) {
		io.WriteString(w, "OK")
	})

	http.HandleFunc("/gauge/", makeHandler(func(m string, bodyData []byte) error {
		val, err := strconv.ParseFloat(string(bodyData), 64)
		if err != nil {
			return err
		}
		return ddClient.Gauge(m, val, tags, 1)
	}))

	http.HandleFunc("/count/", makeHandler(func(m string, bodyData []byte) error {
		val, err := strconv.ParseInt(string(bodyData), 10, 64)
		if err != nil {
			return err
		}
		return ddClient.Count(m, val, tags, 1)
	}))

	http.HandleFunc("/histogram/", makeHandler(func(m string, bodyData []byte) error {
		val, err := strconv.ParseFloat(string(bodyData), 64)
		if err != nil {
			return err
		}
		return ddClient.Histogram(m, val, tags, 1)
	}))

	http.HandleFunc("/set/", makeHandler(func(m string, bodyData []byte) error {
		val := string(bodyData)
		if err != nil {
			return err
		}
		return ddClient.Set(m, val, tags, 1)
	}))

	log.Println("Starting service...")
	log.Println("--------------------------------------")
	log.Printf("Namespace : %s", namespace)
	log.Printf("Tags      : %s", tags)
	log.Printf("Listening : %v", listen)

	if whitelistPath != "" {
		log.Printf("Whitelisted metrics: ")
		for k, _ := range whitelist {
			log.Printf(" - %s ", k)
		}
	} else {
		log.Printf("Whitelist : <all allowed>")
	}

	log.Fatal(http.ListenAndServe(listen, nil))
}
예제 #2
0
func main() {
	// Connect to the statsd/datadogd server
	dd, err = statsd.New(ddAgentServer)
	if err != nil {
		log.Fatalf("Error connecting to Datadog Agent: %s", err.Error())
	}

	// The default prefix for my test metrics
	dd.Namespace = "test.rob-egan."

	// Send the following tags with all metrics
	dd.Tags = append(dd.Tags, "us-east-1a", "production", "my_app")

	// Handler functions
	http.HandleFunc("/", handler)
	http.HandleFunc("/lissajous", func(w http.ResponseWriter, r *http.Request) {
		lissajous(w)
	})
	http.HandleFunc("/debug", debug)
	http.HandleFunc("/count", counter)
	http.HandleFunc("/help", help)

	// The actual web server
	log.Print("Starting web server...\n")
	log.Print("Point your browser to http://", webServerHost, "/help for more info...\n")
	log.Fatal(http.ListenAndServe(webServerHost, nil))
}
예제 #3
0
파일: data_dog.go 프로젝트: wptad/dvara
func NewDataDogStatsDClient(address string, replica string) DatadogStatsClient {
	c, err := statsd.New(address)
	if err != nil {
		log.Fatal(err)
	}
	return DatadogStatsClient{c, replica}
}
예제 #4
0
파일: factories.go 프로젝트: atmos/conveyor
func newBuilder(c *cli.Context) builder.Builder {
	db, err := docker.NewBuilderFromEnv()
	if err != nil {
		must(err)
	}
	db.DryRun = c.Bool("dry")
	db.Image = c.String("builder.image")

	g := builder.NewGitHubClient(c.String("github.token"))

	var backend builder.Builder = builder.UpdateGitHubCommitStatus(db, g, fmt.Sprintf(logsURLTemplate, c.String("url")))

	if uri := c.String("stats"); uri != "" {
		u := urlParse(uri)

		switch u.Scheme {
		case "dogstatsd":
			c, err := statsd.New(u.Host)
			must(err)

			backend = datadog.WithStats(
				backend,
				c,
			)
		default:
			must(fmt.Errorf("Unknown stats backend: %v", u.Scheme))
		}
	}

	b := worker.NewBuilder(backend)
	b.Reporter = newReporter(c)

	return b
}
예제 #5
0
파일: init.go 프로젝트: ronpastore/grafana
func New(addr, prefix string) (Backend, error) {
	client, err := statsd.New(addr)
	if err == nil {
		client.Namespace = prefix
		client.Tags = append(client.Tags, "instance:"+setting.InstanceId)
	}
	return Backend{client}, err
}
예제 #6
0
파일: init.go 프로젝트: raintank/met
// note: library does not auto-add ending dot to prefix, specify it if you want it
func New(addr, prefix string, tags []string) (Backend, error) {
	client, err := statsd.New(addr)
	if err == nil {
		client.Namespace = prefix
		client.Tags = tags
	}
	return Backend{client}, err
}
예제 #7
0
파일: dogstatsd.go 프로젝트: mhahn/empire
// NewDogstatsd returns a new Dogstatsd instance that sends statsd metrics to addr.
func NewDogstatsd(addr string) (*Dogstatsd, error) {
	c, err := statsd.New(addr)
	if err != nil {
		return nil, err
	}

	return &Dogstatsd{
		Client: c,
	}, nil
}
예제 #8
0
파일: dogstatsd.go 프로젝트: darron/goshe
// DogStatsdSetup sets up a connection to DogStatsd.
func DogStatsdSetup() *statsd.Client {
	dogstatsd := ""
	if dogstatsd = viper.GetString("dogtstatsd_address"); dogstatsd == "" {
		dogstatsd = DogStatsdAddr
	}
	c, err := statsd.New(dogstatsd)
	if err != nil {
		Log(fmt.Sprintf("DogStatsdSetup Error: %#v", err), "info")
	}
	c.Namespace = fmt.Sprintf("%s.", MetricPrefix)
	return c
}
예제 #9
0
// NewDogStatsdSink is used to create a new DogStatsdSink with sane defaults
func NewDogStatsdSink(addr string, hostName string) (*DogStatsdSink, error) {
	client, err := statsd.New(addr)
	if err != nil {
		return nil, err
	}
	sink := &DogStatsdSink{
		client:            client,
		hostName:          hostName,
		propagateHostname: false,
	}
	return sink, nil
}
예제 #10
0
func init() {
	var err error
	client, err = statsd.New("127.0.0.1:8125")
	if err != nil {
		log.Fatal(err)
	}

	enabled, err := strconv.ParseBool(os.Getenv("ENABLE_DRAIN_METRICS"))
	if err != nil {
		enableDrainLogging = enabled
	}
}
예제 #11
0
func NewMiddleWare(statsdHost string, namespace string, tags []string) *NegroniDog {
	c, err := statsd.New(statsdHost)
	if err != nil {
		panic(err)
	}
	nd := NegroniDog{
		Cli: c,
	}
	// prefix every metric with the namespace
	c.Namespace = namespace + "."
	c.Tags = tags
	return &nd
}
예제 #12
0
// NewDogStatsdSink is used to create a new DogStatsdSink
func NewDogStatsdSink(addr string, tags []string, tagWithHostname bool) (*DogStatsdSink, error) {
	client, err := statsd.New(addr)
	if err != nil {
		return nil, err
	}

	client.Tags = tags
	sink := &DogStatsdSink{
		client:                client,
		hostnameGetter:        getHostname,
		enableHostnameTagging: tagWithHostname,
	}
	return sink, nil
}
예제 #13
0
// NewServer returns a new initialized *Server that will bind to listen and will look up answers from zone.
func NewServer(listen string, registry *Registry, statsdHost string) (*Server, error) {
	var err error
	var s *Server
	s = &Server{registry: registry}
	s.server = &dns.Server{
		Addr:    listen,
		Net:     "udp",
		Handler: s,
	}
	if statsdHost != "" {
		s.statsd, err = statsd.New(statsdHost)
		if err != nil {
			return nil, err
		}
		s.statsd.Namespace = "realm."
	}
	return s, err
}
예제 #14
0
func TestBuilder(t *testing.T) {
	c, err := statsd.New("localhost:8125")
	assert.NoError(t, err)

	b := datadog.WithStats(
		builder.BuilderFunc(func(ctx context.Context, w io.Writer, options builder.BuildOptions) (string, error) {
			return "remind101/acme-inc:1234", nil
		}),
		c,
	)

	_, err = b.Build(context.Background(), ioutil.Discard, builder.BuildOptions{
		Repository: "remind101/acme-inc",
		Branch:     "master",
		Sha:        "1234",
	})
	assert.NoError(t, err)
}
func statsdClient(addr string) (*Client, error) {

	c, err := statsd.New(addr)
	return &Client{c}, err
}
예제 #16
0
파일: main.go 프로젝트: MEDIGO/laika
func main() {
	app := cli.NewApp()
	app.Author = "MEDIGO GmbH"
	app.Flags = []cli.Flag{
		cli.StringFlag{
			Name:   "port",
			Value:  "8000",
			Usage:  "Service port",
			EnvVar: "LAIKA_PORT",
		},
		cli.IntFlag{
			Name:   "timeout",
			Value:  10,
			Usage:  "Shutdown timeout",
			EnvVar: "LAIKA_TIMEOUT",
		},
		cli.StringFlag{
			Name:   "mysql-host",
			Value:  "mysql",
			Usage:  "MySQL host",
			EnvVar: "LAIKA_MYSQL_HOST",
		},
		cli.StringFlag{
			Name:   "mysql-port",
			Value:  "3306",
			Usage:  "MySQL port",
			EnvVar: "LAIKA_MYSQL_PORT",
		},
		cli.StringFlag{
			Name:   "mysql-username",
			Value:  "root",
			Usage:  "MySQL username",
			EnvVar: "LAIKA_MYSQL_USERNAME",
		},
		cli.StringFlag{
			Name:   "mysql-password",
			Value:  "root",
			Usage:  "MySQL password",
			EnvVar: "LAIKA_MYSQL_PASSWORD",
		},
		cli.StringFlag{
			Name:   "mysql-dbname",
			Value:  "laika",
			Usage:  "MySQL dbname",
			EnvVar: "LAIKA_MYSQL_DBNAME",
		},
		cli.StringFlag{
			Name:   "statsd-host",
			Value:  "localhost",
			Usage:  "Statsd host",
			EnvVar: "LAIKA_STATSD_HOST",
		},
		cli.StringFlag{
			Name:   "statsd-port",
			Value:  "8125",
			Usage:  "Statsd port",
			EnvVar: "LAIKA_STATSD_PORT",
		},
		cli.StringFlag{
			Name:   "root-username",
			Usage:  "Root username",
			EnvVar: "LAIKA_ROOT_USERNAME",
		},
		cli.StringFlag{
			Name:   "root-password",
			Usage:  "Root password",
			EnvVar: "LAIKA_ROOT_PASSWORD",
		},
		cli.StringFlag{
			Name:   "slack-webhook-url",
			Usage:  "Slack webhook URL",
			EnvVar: "LAIKA_SLACK_WEBHOOK_URL",
		},
	}
	app.Commands = []cli.Command{
		{
			Name:  "run",
			Usage: "Runs laika's feature flag service",
			Action: func(c *cli.Context) {
				store, err := store.NewMySQLStore(
					c.GlobalString("mysql-username"),
					c.GlobalString("mysql-password"),
					c.GlobalString("mysql-host"),
					c.GlobalString("mysql-port"),
					c.GlobalString("mysql-dbname"),
				)

				if err != nil {
					log.Fatal("Failed to create Store: ", err)
				}

				stats, err := statsd.New(c.GlobalString("statsd-host") + ":" + c.GlobalString("statsd-port"))
				if err != nil {
					log.Fatal("Failed to create Statsd client: ", err)
				}

				notifier := notifier.NewSlackNotifier(c.GlobalString("slack-webhook-url"))

				server, err := api.NewServer(api.ServerConfig{
					RootUsername: c.GlobalString("root-username"),
					RootPassword: c.GlobalString("root-password"),
					Store:        store,
					Stats:        stats,
					Notifier:     notifier,
				})
				if err != nil {
					log.Fatal("Failed to create server: ", err)

				}

				log.Info("Starting server on port ", c.GlobalString("port"))
				graceful.Run(":"+c.GlobalString("port"), time.Duration(c.Int("timeout"))*time.Second, server)
			},
		},
		{
			Name:  "migrate",
			Usage: "Migrates the store schema to the latest available version",
			Action: func(c *cli.Context) {
				store, err := store.NewMySQLStore(
					c.GlobalString("mysql-username"),
					c.GlobalString("mysql-password"),
					c.GlobalString("mysql-host"),
					c.GlobalString("mysql-port"),
					c.GlobalString("mysql-dbname"),
				)

				if err != nil {
					log.Fatal("Failed to create Store: ", err)
				}

				if err := store.Ping(); err != nil {
					log.Fatal("Failed to connect with store: ", err)
				}

				if err := store.Migrate(); err != nil {
					log.Fatal("Failed to migrate store schema: ", err)
				}

			},
		},
	}

	app.Run(os.Args)
}
package datadog

import (
	"os"

	log "github.com/Sirupsen/logrus"

	"github.com/DataDog/datadog-go/statsd"
)

var (
	dataDogHostPort = os.Getenv("DD_AGENT_SERVICE_HOST_PORT")
	client, err     = statsd.New(dataDogHostPort) //in host:port format
)

func init() {
	if err != nil {
		log.Error("Couldn't connect to statsd", dataDogHostPort, err)
	} else {
		log.Info("Connected to statsd", dataDogHostPort)
		// prefix every metric with the app name
		client.Namespace = "k8s_dns."
		// send the pod hostname as a tag with every metric
		client.Tags = append(client.Tags, os.Getenv("ENVIRONMENT"))
	}
}

func Count(name string, value int64, tags []string, rate float64) {
	//See init(). If connecting to DD-Agent failed, err is not nil
	if err != nil {
		return