Beispiel #1
0
func main() {
	client, err := statsd.New(os.Getenv("STATSD_PORT_8125_TCP_ADDR")+":8125", "test-client")

	// handle any errors
	if err != nil {
		panic(err)
	}

	// make sure to clean up
	defer client.Close()

	for true {
		rand.Seed(time.Now().Unix())
		stat := int64(rand.Intn(10))

		// Send a stat
		err = client.Inc("stat1", stat, 1.0)
		// handle any errors
		if err != nil {
			panic(err)
		}

		fmt.Println("Stat", stat, "sent")

		time.Sleep(1 * time.Second)
	}
}
Beispiel #2
0
func MetricsStatsdTarget(target string) (err error) {
	statsdc, err = statsd.New(target, prefix)
	if err != nil {
		return
	}
	return
}
Beispiel #3
0
// connect makes a 'connection' to the statsd host (doing a DNS lookup),
// setting the internal client. If that fails we set the internal client to
// noop. If reconEvery is set non 0, periodically re-connects on that duration.
func (s *StatsdClient) connect() bool {
	s.Lock()
	defer s.Unlock()
	if s.reconTimer != nil {
		s.reconTimer.Stop()
		s.reconTimer = nil
	}
	var err error

	if s.client != nil {
		// TODO we could be closing a client that is in use by one of
		// the methods below. This code needs rewriting.
		s.client.Close()
	}
	s.client, err = statsd.New(s.hostPort, s.prefix)
	if err != nil {
		s.Error("STATSD Failed to create client (stats will noop): " + err.Error())
		s.client, err = statsd.NewNoopClient()
	} else {
		s.Finef("STATSD sending to [%s] with prefix [%s] at rate [%f]", s.hostPort, s.prefix, s.rate)
	}
	if s.reconEvery != 0 {
		s.reconTimer = time.AfterFunc(s.reconEvery, func() { s.connect() })
	}
	return true
}
Beispiel #4
0
func (m *Metrics) Init(app *Application, config interface{}) (err error) {
	conf := config.(*MetricsConfig)

	m.logger = app.Logger()

	if conf.StatsdServer != "" {
		name := strings.ToLower(conf.StatsdName)
		if m.statsd, err = statsd.New(conf.StatsdServer, name); err != nil {
			m.logger.Panic("metrics", "Could not init statsd connection",
				LogFields{"error": err.Error()})
			return err
		}
	}

	m.prefix = conf.Prefix
	m.born = time.Now()

	if m.storeSnapshots = conf.StoreSnapshots; m.storeSnapshots {
		m.counter = make(map[string]int64)
		m.timer = make(timer)
		m.gauge = make(map[string]int64)
	}

	return nil
}
func main() {
	log.Print("Starting sendgrid webhook endpoint...")

	statsdHost := flag.String("statsd_host", "127.0.0.1:8125", "")
	statsdClientName := flag.String("client_name", "sendgrid", "")

	conf, err := globalconf.New("sendgridstatsd")
	conf.ParseAll()

	log.Printf("Sending to statdsd host: %+v", *statsdHost)

	// first create a client
	client, err := statsd.New(*statsdHost, *statsdClientName)
	// handle any errors
	if err != nil {
		log.Fatal(err)
	}
	// make sure to clean up
	defer client.Close()

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		handler(w, r, client)
	})

	log.Print("done\n")
	http.ListenAndServe(":9090", nil)
}
Beispiel #6
0
func Init(addr string, ns string) error {
	var err error
	client, err = statsd.New(addr, ns)
	if err != nil {
		client = nil
	}
	return err
}
Beispiel #7
0
func initStatsd(statsPrefix, statsdHostport string) (stats statsd.Statter, err error) {
	if statsdHostport == "" {
		stats, _ = statsd.NewNoop()
	} else {
		if stats, err = statsd.New(statsdHostport, *stats_prefix); err != nil {
			log.Fatalf("Statsd configuration error: %v", err)
		}
	}
	return
}
Beispiel #8
0
// NewService creates a new service given a unique name.
// If no configuration is specified (nil), defaults values are used.
func NewService(name string, c *ServiceConfig) (*Service, error) {
	var err error

	if c == nil {
		c = new(ServiceConfig)
	}
	if c.PrespawnConns == 0 {
		c.PrespawnConns = DefaultPrespawnConns
	}
	if c.GetAttempts == 0 {
		c.GetAttempts = DefaultGetAttempts
	}
	if c.CloseDeadline == 0 {
		c.CloseDeadline = DefaultCloseDeadline
	}
	if c.DecayDuration == 0 {
		c.DecayDuration = DefaultDecayDuration
	}
	if c.MemoizeScoreDuration == 0 {
		c.MemoizeScoreDuration = DefaultMemoizeScoreDuration
	}
	if c.BanditStrategy == nil {
		c.BanditStrategy = NewRoundRobin()
	}

	s := &Service{
		ServiceConfig: c,
		name:          name,
		hosts:         make(map[string]*Host),
		add:           make(chan string),
		rm:            make(chan string),
		stop:          make(chan struct{}),
	}
	if _, ok := s.BanditStrategy.(*RoundRobin); !ok {
		s.decay = time.NewTicker(c.DecayDuration / seriesNum)
		s.memoize = time.NewTicker(c.MemoizeScoreDuration)
	}
	if c.StatsdAddr != "" {
		s.stats, err = statsd.New(c.StatsdAddr, "service."+name)
		if err != nil {
			return nil, err
		}
	}
	if s.stats == nil {
		s.stats, _ = statsd.NewNoop()
	} else {
		runtime.SetFinalizer(s.stats, func(s statsd.Statter) { s.Close() })
		s.stats.Gauge("conns.count", 0, sampleRate)
		s.stats.Gauge("hosts.count", 0, sampleRate)
		go s.monitor()
	}

	go s.serve()
	return s, nil
}
Beispiel #9
0
func InitStats(statsPrefix string) (stats Stats, err error) {
	// Set up statsd monitoring
	// - If the env is not set up we wil use a noop connection
	statsdHostport := os.Getenv("STATSD_HOSTPORT")

	if statsdHostport == "" {
		// Error is meaningless here.
		stats, _ = statsd.NewNoop(statsdHostport, statsPrefix)
	} else {
		if stats, err = statsd.New(statsdHostport, statsPrefix); err != nil {
			return
		}
		log.Printf("Connected to statsd at %s\n", statsdHostport)
	}
	return
}
Beispiel #10
0
func (a *App) initStatsd() {
	statsdHostport, _ := a.Cfg.Get("gop", "statsd_hostport", "localhost:8125")
	hostname, _ := os.Hostname()
	statsdPrefix := strings.Join([]string{a.ProjectName, a.AppName, strings.Replace(hostname, ".", "_", -1)}, ".")
	client, err := statsd.New(statsdHostport, statsdPrefix)
	if err != nil {
		// App will probably fall over due to nil client. That's OK.
		// We *could* panic below, but lets try and continue at least
		a.Error("Failed to create statsd client: " + err.Error())
		return
	}

	rate, _ := a.Cfg.GetFloat32("gop", "statsd_rate", 1.0)
	a.Stats = StatsdClient{
		client: client,
		rate:   rate,
		app:    a,
	}
}
Beispiel #11
0
func main() {
	if len(os.Args) < 4 {
		fmt.Fprintln(os.Stderr, "You need arguments")
		flag.Usage()
		os.Exit(10)
	}
	flag.Parse()

	// Seek to the end at the start
	seek := &tail.SeekInfo{
		Offset: 0,
		Whence: 2,
	}

	config := tail.Config{
		ReOpen:    true,
		MustExist: true,
		Follow:    true,
		Location:  seek,
	}

	t, err := tail.TailFile(path, config)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Could not tail: %v\n", err)
	}

	conn, err := statsd.New(statsd_host, "")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to connect to statsd: %v\n", err)
		os.Exit(10)
	}
	defer conn.Close()

	r := regexp.MustCompile(regex)

	for line := range t.Lines {
		if r.MatchString(line.Text) {
			_ = conn.Inc(metric, 1, 1.0)
		}
	}
}
Beispiel #12
0
func (m *Metrics) Init(app *Application, config interface{}) (err error) {
	conf := config.(*MetricsConfig)
	m.setApp(app)

	if conf.StatsdServer != "" {
		name := strings.ToLower(conf.StatsdName)
		if m.statsd, err = statsd.New(conf.StatsdServer, name); err != nil {
			m.logger.Panic("metrics", "Could not init statsd connection",
				LogFields{"error": err.Error()})
			return err
		}
	}

	err = m.setCounterAffixes(conf.Counters.Prefix, conf.Counters.Suffix)
	if err != nil {
		m.logger.Panic("metrics", "Error setting counter name affixes",
			LogFields{"error": err.Error()})
		return err
	}
	err = m.setTimerAffixes(conf.Timers.Prefix, conf.Timers.Suffix)
	if err != nil {
		m.logger.Panic("metrics", "Error setting timer name affixes",
			LogFields{"error": err.Error()})
		return err
	}
	err = m.setGaugeAffixes(conf.Gauges.Prefix, conf.Gauges.Suffix)
	if err != nil {
		m.logger.Panic("metrics", "Error parsing gauge name affixes",
			LogFields{"error": err.Error()})
		return err
	}
	m.born = time.Now()

	if m.storeSnapshots = conf.StoreSnapshots; m.storeSnapshots {
		m.counter = make(map[string]int64)
		m.timer = make(timer)
		m.gauge = make(map[string]int64)
	}

	return nil
}
Beispiel #13
0
func NewMetrics(prefix string, logger Logger, config *MzConfig) *Metric {

	var statsdc *statsd.Client
	if server := config.Get("statsd.server", ""); server != "" {
		name := strings.ToLower(config.Get("statsd.name", "undef"))
		client, err := statsd.New(server, name)
		if err != nil {
			logger.Error("metrics", "Could not init statsd connection",
				Fields{"error": err.Error()})
		} else {
			statsdc = client
		}
	}

	return &Metric{
		dict:   make(map[string]int64),
		timer:  make(timer),
		prefix: prefix,
		logger: logger,
		statsd: statsdc,
		born:   time.Now(),
	}
}
Beispiel #14
0
/* Run time metrics with statsd reporting
 */
func New(prefix, server string) (self *Metrics) {

	var statsdc *statsd.Client
	if server != "" {
		name := strings.ToLower(prefix)
		client, err := statsd.New(server, name)
		if err != nil {
			log.Printf("Metrics ERROR: Could not init statsd connection: %s",
				err.Error())
		} else {
			statsdc = client
		}
	}

	self = &Metrics{
		dict:   make(map[string]int64),
		timer:  make(map[string]float64),
		prefix: prefix,
		statsd: statsdc,
		born:   time.Now(),
	}
	return self
}
Beispiel #15
0
func main() {
	flag.Parse()

	// read our configuration
	config := ReadConfig(configPath)

	// connect to the database
	db := DBInit(config.PG.ConnectionString)
	defer db.con.Close()

	// connect to statsd
	statsd, err := statsd.New(config.ST.ConnectionString, config.ST.Prefix)
	// handle any errors
	if err != nil {
		log.Fatal(err)
	}
	// make sure to clean up
	defer statsd.Close()

	err = statsd.Gauge("biggest_relation_bytes", db.GetBiggestRelation(), 1.0)
	if err != nil {
		log.Printf("Error sending metric: %+v", err)
	}
	/*
		statementStats := db.GetStatementStats()
		for _, s := range statementStats {
			log.Printf("%#v", s)
		}

		sizeStats := db.GetSizeStats(50)
		for _, s := range sizeStats {
			log.Printf("%#v", s)
		}
	*/

}
Beispiel #16
0
func main() {
	flag.Parse()

	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Fatalf("Creating cpu profile: %s", err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	if *statsdserver != "" {
		s, err := statsd.New(*statsdserver, *statsdname)
		if err != nil {
			log.Fatalf("Initializing statsd: %s", err)
		}

		cobe.SetStatter(s)
	}

	args := flag.Args()
	if len(args) < 1 {
		fmt.Println("Usage: cobe console")
		os.Exit(1)
	}

	b, err := cobe.OpenCobe2Brain("cobe.brain")
	if err != nil {
		log.Fatalf("Opening brain file: %s", err)
	}

	var cmd = args[0]
	switch {
	case cmd == "console":
		console.RunForever(b)
	case cmd == "ircbot" || cmd == "irc-client":
		opts := &ircbot.Options{
			Server:   *ircserver,
			Nick:     *ircnick,
			Channels: []string{*ircchannel},
		}
		ircbot.RunForever(b, opts)
	case cmd == "learn":
		for _, f := range args[1:] {
			learnFileLines(b, f)
		}
	case cmd == "del-stemmer":
		err := b.DelStemmer()
		if err != nil {
			log.Fatalf("Deleting stemmer: %s", err)
		}
	case cmd == "set-stemmer":
		if len(args) < 2 {
			log.Fatal("Usage: set-stemmer <language>")
		}
		err := b.SetStemmer(args[1])
		if err != nil {
			log.Fatalf("Setting stemmer: %s", err)
		}
	default:
		log.Fatalf("Unknown command: %s", cmd)
	}
}
Beispiel #17
0
func main() {

	// command line flags
	var opts struct {
		HostPort  string        `long:"host" default:"127.0.0.1:8125" description:"host:port of statsd server"`
		Prefix    string        `long:"prefix" default:"test-client" description:"Statsd prefix"`
		StatType  string        `long:"type" default:"count" description:"stat type to send. Can be timing, count, guage"`
		StatValue int64         `long:"value" default:"1" description:"Value to send"`
		Name      string        `short:"n" long:"name" default:"counter" description:"stat name"`
		Rate      float32       `short:"r" long:"rate" default:"1.0" description:"sample rate"`
		Volume    int           `short:"c" long:"count" default:"1000" description:"Number of stats to send. Volume."`
		Noop      bool          `long:"noop" default:"false" description:"Use noop client"`
		Duration  time.Duration `short:"d" long:"duration" default:"10s" description:"How long to spread the volume across. Each second of duration volume/seconds events will be sent."`
	}

	// parse said flags
	_, err := flags.Parse(&opts)
	if err != nil {
		if e, ok := err.(*flags.Error); ok {
			if e.Type == flags.ErrHelp {
				os.Exit(0)
			}
		}
		fmt.Printf("Error: %+v\n", err)
		os.Exit(1)
	}

	var client statsd.Statter
	if !opts.Noop {
		client, err = statsd.New(opts.HostPort, opts.Prefix)
		if err != nil {
			log.Fatal(err)
		}
		defer client.Close()
	} else {
		client, err = statsd.NewNoop(opts.HostPort, opts.Prefix)
	}

	var stat func(stat string, value int64, rate float32) error
	switch opts.StatType {
	case "count":
		stat = func(stat string, value int64, rate float32) error {
			return client.Inc(stat, value, rate)
		}
	case "gauge":
		stat = func(stat string, value int64, rate float32) error {
			return client.Gauge(stat, value, rate)
		}
	case "timing":
		stat = func(stat string, value int64, rate float32) error {
			return client.Timing(stat, value, rate)
		}
	default:
		log.Fatal("Unsupported state type")
	}

	pertick := opts.Volume / int(opts.Duration.Seconds()) / 10
	// add some extra tiem, because the first tick takes a while
	ender := time.After(opts.Duration + 100*time.Millisecond)
	c := time.Tick(time.Second / 10)
	count := 0
	for {
		select {
		case <-c:
			for x := 0; x < pertick; x++ {
				err := stat(opts.Name, opts.StatValue, opts.Rate)
				if err != nil {
					log.Printf("Got Error: %+v", err)
					break
				}
				count += 1
			}
		case <-ender:
			log.Printf("%d events called", count)
			os.Exit(0)
			return
		}
	}
}