// NewStatsdReporter returns a StatsReporter that reports to statsd on the given addr. func NewStatsdReporter(addr, prefix string) (tchannel.StatsReporter, error) { client, err := statsd.NewBufferedClient(addr, prefix, time.Second, 0) if err != nil { return nil, err } return NewStatsdReporterClient(client), nil }
// InitializeStatsdCollector creates the connection to the Statsd server // and should be called before any metrics are recorded. // // Users should ensure to call Close() on the client. func InitializeStatsdCollector(config *StatsdCollectorConfig) (*StatsdCollectorClient, error) { c, err := statsd.NewBufferedClient(config.StatsdAddr, config.Prefix, 1*time.Second, 512) if err != nil { log.Printf("Could not initiale buffered client: %s. Falling back to a Noop Statsd client", err) c, _ = statsd.NewNoopClient() } return &StatsdCollectorClient{ client: c, }, err }
func BenchmarkCactusTimingAsDuration(b *testing.B) { s := newServer() c, err := cactus.NewBufferedClient(addr, prefix, flushPeriod, 1432) if err != nil { b.Fatal(err) } b.StartTimer() for i := 0; i < b.N; i++ { c.Inc(counterKey, 1, 1) c.Gauge(gaugeKey, gaugeValue, 1) c.TimingDuration(timingKey, tValDur, 1) } c.Close() b.StopTimer() s.Close() }
func NewStatsdBuffer(c Emitter, hostname, serviceName string) (statsdbuffer statsd.Statter, err error) { if hostname == "" { hostname = "EventsServiceUnknownHost" } hostname = CleanStatsdComponent(hostname) if serviceName == "" { serviceName = "EventsServiceUnknownService" } serviceName = CleanStatsdComponent(serviceName) prefix := strings.Join([]string{c.Prefix, serviceName, hostname}, ".") prefix = strings.TrimLeft(prefix, ".") statsdbuffer, err = statsd.NewBufferedClient(net.JoinHostPort(c.Host, c.Port), prefix, time.Duration(c.Interval)*time.Second, 0) if err != nil { return } return }
func BenchmarkCactus(b *testing.B) { s := newServer() c, err := cactus.NewBufferedClient(s.Addr(), prefix, flushPeriod, 1432) if err != nil { b.Fatal(err) } b.ResetTimer() for i := 0; i < b.N; i++ { c.Inc(counterKey, 1, 1) c.Gauge(gaugeKey, gaugeValue, 1) c.Timing(timingKey, int64(timingValue), 1) } c.Close() s.Close() }
func main() { flag.Parse() cf_lager.AddFlags(flag.CommandLine) logger, reconfigurableSink := cf_lager.New("routing-api") err := checkFlags() if err != nil { logger.Error("failed to start", err) os.Exit(1) } cfg, err := config.NewConfigFromFile(*configPath) if err != nil { logger.Error("failed to start", err) os.Exit(1) } err = dropsonde.Initialize(cfg.MetronConfig.Address+":"+cfg.MetronConfig.Port, cfg.LogGuid) if err != nil { logger.Error("failed to initialize Dropsonde", err) os.Exit(1) } if cfg.DebugAddress != "" { cf_debug_server.Run(cfg.DebugAddress, reconfigurableSink) } database, err := initializeDatabase(cfg, logger) if err != nil { logger.Error("failed to initialize database", err) os.Exit(1) } err = database.Connect() if err != nil { logger.Error("failed to connect to database", err) os.Exit(1) } defer database.Disconnect() prefix := "routing_api" statsdClient, err := statsd.NewBufferedClient(cfg.StatsdEndpoint, prefix, cfg.StatsdClientFlushInterval, 512) if err != nil { logger.Error("failed to create a statsd client", err) os.Exit(1) } defer statsdClient.Close() stopChan := make(chan struct{}) apiServer := constructApiServer(cfg, database, statsdClient, stopChan, logger) stopper := constructStopper(stopChan) routerRegister := constructRouteRegister(cfg.LogGuid, database, logger) metricsTicker := time.NewTicker(cfg.MetricsReportingInterval) metricsReporter := metrics.NewMetricsReporter(database, statsdClient, metricsTicker) members := grouper.Members{ {"metrics", metricsReporter}, {"api-server", apiServer}, {"conn-stopper", stopper}, {"route-register", routerRegister}, } group := grouper.NewOrdered(os.Interrupt, members) process := ifrit.Invoke(sigmon.New(group)) // This is used by testrunner to signal ready for tests. logger.Info("started", lager.Data{"port": *port}) errChan := process.Wait() err = <-errChan if err != nil { logger.Error("shutdown-error", err) os.Exit(1) } logger.Info("exited") }
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."` Nil bool `long:"nil" default:"false" description:"Use nil client"` Buffered bool `long:"buffered" default:"false" description:"Use a buffered 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) } if opts.Nil && opts.Buffered { fmt.Printf("Specifying both nil and buffered together is invalid.") os.Exit(1) } var client statsd.Statter if !opts.Nil { if !opts.Buffered { client, err = statsd.NewClient(opts.HostPort, opts.Prefix) } else { client, err = statsd.NewBufferedClient(opts.HostPort, opts.Prefix, opts.Duration/time.Duration(4), 0) } if err != nil { log.Fatal(err) } defer client.Close() } 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 } } }