Example #1
0
func main() {
	// Initialize the StatsD client
	prefix := "test.rob-egan."
	statsdclient := statsd.NewStatsdClient(agentServer, prefix)
	err := statsdclient.CreateSocket()
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
	interval := time.Second * 2 // aggregate stats and flush every 2 seconds
	stats := statsd.NewStatsdBuffer(interval, statsdclient)
	defer stats.Close()

	// 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))
}
Example #2
0
func runLoop(redisHost string, redisPort int64, redisDb int64, statsdHost string, statsdPort int64, statsPrefix string, interval int64, queues []string) {
	client := redis.NewClient(&redis.Options{
		Addr: fmt.Sprintf("%s:%d", redisHost, redisPort),
		DB:   redisDb,
	})
	_, connErr := client.Ping().Result()
	fmt.Println("connection errors? ", connErr)

	statsdClient := statsd.NewStatsdClient(
		fmt.Sprintf("%s:%d", statsdHost, statsdPort),
		fmt.Sprintf("%s.", statsPrefix),
	)
	statsdClient.CreateSocket()
	stats := statsd.NewStatsdBuffer(time.Second*2, statsdClient)
	defer stats.Close()
	timeoutInterval := time.Duration(interval) * time.Millisecond
	for true {
		for _, name := range queues {
			go func(oneName string) {
				listLength, err := client.LLen(oneName).Result()
				stats.Gauge(oneName, listLength)
				fmt.Println(oneName, listLength, err)
			}(name)
		}
		time.Sleep(timeoutInterval)
	}
}
func (varnishncsa *Varnishncsa) Connect() {
	statsdclient := statsd.NewStatsdClient(varnishncsa.StatsdServer, varnishncsa.StatsdPrefix)
	statsdclient.CreateSocket()
	interval := time.Duration(int(time.Millisecond) * varnishncsa.StatsdSendInterval)
	stats := statsd.NewStatsdBuffer(interval, statsdclient)
	defer stats.Close()

	cmd := exec.Command("varnishncsa", "-F", `%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-agent}i" %{Varnish:time_firstbyte}x`)
	stdout, outerr := cmd.StdoutPipe()
	if outerr != nil {
		log.Fatal(outerr)
	}

	err := cmd.Start()
	if err != nil {
		log.Fatal(err)
	}

	scanner := bufio.NewScanner(stdout)
	for scanner.Scan() {
		r := Record{scanner.Text(), stats, varnishncsa.RecordRegex}
		r.Process()
	}
	if err := scanner.Err(); err != nil {
		fmt.Fprintln(os.Stderr, "There was an error with the scanner attached to varnishncsa", err)
	}
	cmd.Wait()
}
Example #4
0
// CreateStatsdClient creates a local instances of a statsd client. Any errors will be logged to
// console and ignored.
func CreateStatsdClient(statsdURL, statsdPrefix string) error {
	lock.Lock()
	defer lock.Unlock()

	if stats != noopClient {
		// Already initialized. Don't overwrite
		return nil
	}

	if statsdURL != "" {
		hostname, err := os.Hostname()
		if err != nil {
			log.Printf("Could not read hostname. Using default noop statsd client: %s", err)
			return err
		}
		prefix := fmt.Sprintf("%s.%s.artifacts.", statsdPrefix, hostname)

		statsdClient := statsd.NewStatsdClient(statsdURL, prefix)

		if statsdClient != nil {
			stats = statsd.NewStatsdBuffer(updateInterval, statsdClient)
		}
	} else {
		log.Println("No statsd URL provided. Using default noop statsd client")
	}

	return nil
}
Example #5
0
func InitStatsd(ch chan *map[string]map[string]int64, conf *StatsdConfig) error {
	go func() {
		statsdclient := statsd.NewStatsdClient(conf.Host, conf.Prefix)
		statsdclient.CreateSocket()
		interval, err := time.ParseDuration(conf.Interval)
		if err != nil {
			Conf.Logger.Fatalf("can't parse interval %+v", err)
		}
		stats := statsd.NewStatsdBuffer(interval, statsdclient)
		defer stats.Close()
		for data := range statsdCh {
			for op, values := range *data {
				for key, val := range values {
					switch op {
					case "increment":
						debug("send incr", key, val)
						go stats.Incr(key, val)
					case "timing":
						debug("send timing", key, val)
						go stats.Timing(key, val)
					case "gauge":
						debug("send gauge", key, val)
						go stats.Gauge(key, val)
					}
				}

			}
		}
	}()
	return nil
}
Example #6
0
func initStatsd(address string, prefix string) *statsd.StatsdBuffer {
	statsdclient := statsd.NewStatsdClient(address, prefix)
	statsdclient.CreateSocket()
	interval := time.Second * 10 // aggregate stats and flush every 2 seconds
	stats := statsd.NewStatsdBuffer(interval, statsdclient)
	return stats
}
func mustStartStatsd(host string, prefix string, frequency time.Duration) *statsd.StatsdBuffer {
	sClient := statsd.NewStatsdClient(opts.StatsDHost, opts.StatsDPrefix)
	if err := sClient.CreateSocket(); err != nil {
		log.WithField("error", err).Fatal("could not open socket to statsd")
	}

	return statsd.NewStatsdBuffer(opts.statsdFrequency, sClient)
}
Example #8
0
func main() {

	setupHTTP()

	uaaURL, err := url.Parse(uaa)

	if nil != err {
		panic("Failed to parse uaa url!")
	}

	creds, err := uaaclientcredentials.New(uaaURL, true, clientID, clientSecret)

	if nil != err {
		panic("Failed to obtain creds!")
	}

	consumer := noaa.NewConsumer(dopplerAddress, &tls.Config{InsecureSkipVerify: true}, nil)

	httpStartStopProcessor := processors.NewHttpStartStopProcessor()
	sender := statsd.NewStatsdClient(statsdAddress, statsdPrefix)
	sender.CreateSocket()

	var processedMetrics []metrics.Metric

	msgChan := make(chan *events.Envelope)
	go func() {
		defer close(msgChan)
		errorChan := make(chan error)
		token, err := creds.GetBearerToken()
		if nil != err {
			panic(err)
		}
		go consumer.Firehose(firehoseSubscriptionID, token, msgChan, errorChan, nil)

		for err := range errorChan {
			fmt.Fprintf(os.Stderr, "%v\n", err.Error())
		}
	}()

	for msg := range msgChan {
		eventType := msg.GetEventType()

		switch eventType {
		case events.Envelope_HttpStartStop:
			processedMetrics = httpStartStopProcessor.Process(msg)
		default:
			atomic.AddUint64(&count, 1)
			// do nothing
		}

		if len(processedMetrics) > 0 {
			for _, metric := range processedMetrics {
				metric.Send(sender)
			}
		}
		processedMetrics = nil
	}
}
Example #9
0
func getStatsdBuffer(host string) (*statsd.StatsdBuffer, error) {
	statsdclient := statsd.NewStatsdClient(host, "aws.sqs.")
	if err := statsdclient.CreateSocket(); err != nil {
		log.WithField("error", err).Warn("unable to open socket for statsd")
		return nil, err
	}

	return statsd.NewStatsdBuffer(time.Minute, statsdclient), nil
}
Example #10
0
func NewStatsdClient(simulationPrefix string) *StatsdClient {
	statsdClient := statsd.NewStatsdClient("localhost:8125", simulationPrefix)
	statsdClient.CreateSocket()
	interval := time.Second * 2
	stats := statsd.NewStatsdBuffer(interval, statsdClient)
	return &StatsdClient{
		Stats: stats,
	}
}
Example #11
0
func ConnectToURL(s string, prefix string) (c *statsd.StatsdClient, err error) {
	statsdUrl, err := url.Parse(s)

	if err != nil {
		return nil, err
	}

	c = statsd.NewStatsdClient(statsdUrl.Host, prefix)
	err = c.CreateSocket()
	return c, err
}
Example #12
0
func main() {
	consumer := noaa.NewConsumer(DopplerAddress, &tls.Config{InsecureSkipVerify: true}, nil)

	httpStartStopProcessor := processors.NewHttpStartStopProcessor()
	valueMetricProcessor := processors.NewValueMetricProcessor()
	containerMetricProcessor := processors.NewContainerMetricProcessor()
	heartbeatProcessor := processors.NewHeartbeatProcessor()
	counterProcessor := processors.NewCounterProcessor()

	sender := statsd.NewStatsdClient(statsdAddress, statsdPrefix)
	sender.CreateSocket()

	var processedMetrics []metrics.Metric

	msgChan := make(chan *events.Envelope)
	go func() {
		defer close(msgChan)
		errorChan := make(chan error)
		go consumer.Firehose(firehoseSubscriptionId, authToken, msgChan, errorChan, nil)

		for err := range errorChan {
			fmt.Fprintf(os.Stderr, "%v\n", err.Error())
		}
	}()

	for msg := range msgChan {
		eventType := msg.GetEventType()

		// graphite-nozzle can handle CounterEvent, ContainerMetric, Heartbeat,
		// HttpStartStop and ValueMetric events
		switch eventType {
		case events.Envelope_ContainerMetric:
			processedMetrics = containerMetricProcessor.Process(msg)
		case events.Envelope_CounterEvent:
			processedMetrics = counterProcessor.Process(msg)
		case events.Envelope_Heartbeat:
			processedMetrics = heartbeatProcessor.Process(msg)
		case events.Envelope_HttpStartStop:
			processedMetrics = httpStartStopProcessor.Process(msg)
		case events.Envelope_ValueMetric:
			processedMetrics = valueMetricProcessor.Process(msg)
		default:
			// do nothing
		}

		if len(processedMetrics) > 0 {
			for _, metric := range processedMetrics {
				metric.Send(sender)
			}
		}
		processedMetrics = nil
	}
}
Example #13
0
func (driver *QuipoStatsdDriver) Initialize(connectionString string) error {
	client := quipo.NewStatsdClient(connectionString, "")
	err := client.CreateSocket()

	if err != nil {
		return err
	}

	driver.conn = client
	driver.connectionString = connectionString

	return nil
}
Example #14
0
func main() {
	fmt.Println("Starting eISCP (ethernet Integra Serial Communication Protocol) Gateway")
	// Command line options
	flag.BoolVar(&debug, "debug", false, "enable verbose debugging")
	flag.BoolVar(&statsEnabled, "stats", false, "enable stats collecting")
	flag.StringVar(&defaultDevice, "device", "192.168.2.143", "IP address of device to connect to")
	flag.IntVar(&devicePort, "port", 60128, "port on device to commmunicate with")
	flag.IntVar(&defaultPort, "serve", 3000, "port to host REST API on")
	flag.StringVar(&statsdAddress, "statsd", "localhost:8125", "IP and Port of Statsd server")
	flag.StringVar(&statsdPrefix, "prefix", "eiscp", "A prefix prepended to all stats")

	// Now that we've defined our flags, parse them
	flag.Parse()

	if debug {
		fmt.Println("Displaying debug output.")
	}

	// init
	statsdclient := statsd.NewStatsdClient(statsdAddress, statsdPrefix)
	if statsEnabled {
		if debug {
			fmt.Println("Attempting connection to statsd")
		}
		statsdclient.CreateSocket()
		interval := time.Second * 2 // aggregate stats and flush every 2 seconds
		stats = statsd.NewStatsdBuffer(interval, statsdclient)
		defer stats.Close()
	}

	fmt.Println("Searching for device on port", devicePort, "at", defaultDevice)

	// Do our device stuff here
	go func() {
		for true {
			deviceLoop()
		}
	}()

	r := mux.NewRouter()
	r.HandleFunc("/kill", HandleKill) //Debug Function
	r.HandleFunc("/status/", GetStatus).Methods("GET")
	r.HandleFunc("/device/", DeleteDevice).Methods("DELETE")
	r.HandleFunc("/device/{ip}/{port}", PutDevice).Methods("PUT")
	r.HandleFunc("/device/{property}", GetProperty).Methods("GET")
	r.HandleFunc("/device/{property}/{value}", PostProperty).Methods("POST")
	http.Handle("/", r)

	fmt.Println("REST API listening on port", strconv.Itoa(defaultPort))
	http.ListenAndServe(":"+strconv.Itoa(defaultPort), nil)
}
Example #15
0
func TestMain(m *testing.M) {
	var statsdHost = flag.String("test_statsd_host", "", "Statsd server hostname or IP")
	var statsdInterval = flag.Int("test_statsd_interval", 3, "Seconds between metric flush")
	var statsdPrefix = flag.String("test_statsd_prefix", "gopassivedns", "statsd metric prefix")

	flag.Parse()

	if *statsdHost != "" {
		statsdclient := statsd.NewStatsdClient(*statsdHost, *statsdPrefix)
		stats = statsd.NewStatsdBuffer(time.Duration(*statsdInterval)*time.Second, statsdclient)
	}

	os.Exit(m.Run())
}
Example #16
0
func (s *StatsdPump) connect() *statsd.StatsdClient {
	client := statsd.NewStatsdClient(s.dbConf.Address, "")

	err := client.CreateSocket()
	if err != nil {
		log.WithFields(logrus.Fields{
			"prefix": statsdPrefix,
		}).Error("StatsD connection failed:", err)
		time.Sleep(5)
		s.connect()
	}

	return client
}
Example #17
0
func BenchmarkQuipoTimingAsDuration(b *testing.B) {
	s := newServer()
	c := quipo.NewStatsdBuffer(flushPeriod, quipo.NewStatsdClient(addr, prefix))
	c.Logger = logger{}
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		c.Incr(counterKey, 1)
		c.Gauge(gaugeKey, gaugeValue)
		c.PrecisionTiming(timingKey, tValDur)
	}
	c.Close()
	b.StopTimer()
	s.Close()
}
func BenchmarkQuipo(b *testing.B) {
	s := newServer()
	c := quipo.NewStatsdBuffer(flushPeriod, quipo.NewStatsdClient(s.Addr(), prefix))
	c.Logger = logger{}

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		c.Incr(counterKey, 1)
		c.Gauge(gaugeKey, gaugeValue)
		c.Timing(timingKey, int64(timingValue))
	}
	c.Close()
	s.Close()
}
Example #19
0
func main() {

	//insert the ENV as defaults here, then after the parse we add the true defaults if nothing has been set
	//also convert true/false strings to true/false types

	config := initConfig()

	if config.cpuprofile != "" {
		f, err := os.Create(config.cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	var stats *statsd.StatsdBuffer = nil

	if config.statsdHost != "" {
		statsdclient := statsd.NewStatsdClient(config.statsdHost, config.statsdPrefix+"."+config.sensorName+".")
		stats = statsd.NewStatsdBuffer(time.Duration(config.statsdInterval)*time.Second, statsdclient)
	}

	handle := initHandle(config)

	if handle == nil {
		log.Fatal("Could not initilize the capture.")
	}

	logOpts := NewLogOptions(config)

	logChan := initLogging(logOpts)

	reChan := make(chan tcpDataStruct)

	//spin up logging thread(s)
	go logConn(logChan, logOpts, stats)

	//spin up the actual capture threads
	doCapture(handle, logChan, config, reChan, stats)

	log.Debug("Done!  Goodbye.")
	os.Exit(0)

}
Example #20
0
func InitStatsd(conf interface{}) {
	host := conf.(map[string]interface{})["host"].(string)
	prefix := ""
	_interval := "1s"
	if val, ok := conf.(map[string]interface{})["prefix"].(string); ok {
		prefix = val
	}
	if val, ok := conf.(map[string]interface{})["interval"].(string); ok {
		_interval = val
	}
	statsdclient := statsd.NewStatsdClient(host, prefix)

	statsdclient.CreateSocket()
	interval, err := time.ParseDuration(_interval)
	if err != nil {
		Conf.Logger.Fatalf("can't parse interval %+v", err)
	}
	stats := statsd.NewStatsdBuffer(interval, statsdclient)
	go func() {
		defer stats.Close()
		Conf.Logger.Println("Statsd queue is starts")
		for data := range statsdCh {
			for op, values := range *data {
				for key, val := range values {
					switch op {
					case "increment":
						debug("send incr", key, val)
						go stats.Incr(key, val)
					case "timing":
						debug("send timing", key, val)
						go stats.Timing(key, val)
					case "gauge":
						debug("send gauge", key, val)
						go stats.Gauge(key, val)
					}
				}

			}
		}

	}()
	return
}
Example #21
0
func NewMasterWithStatsd(host string, workerConfig *WorkerConfig, command, balanceConfigTemplate, balanceConfigFile, digitalOceanToken, digitalOceanImageID string,
	overloadedCpuThreshold, underusedCpuThreshold float64, minWorkers, maxWorkers int64, pollInterval, cooldownInterval, surveyDeadline, queryInterval time.Duration,
	scaleNodes, changeWeights bool,
	statsdAddr, statsdPrefix string, statsdInterval time.Duration) *Master {

	master := NewMaster(
		host, workerConfig, command,
		balanceConfigTemplate, balanceConfigFile,
		digitalOceanToken, digitalOceanImageID,
		overloadedCpuThreshold, underusedCpuThreshold,
		minWorkers, maxWorkers,
		pollInterval, cooldownInterval, surveyDeadline, queryInterval,
		scaleNodes, changeWeights,
	)
	statsdClient := statsd.NewStatsdClient(statsdAddr, statsdPrefix)
	statsdClient.CreateSocket()
	master.statsdClientBuffer = statsd.NewStatsdBuffer(statsdInterval, statsdClient)

	return master
}
Example #22
0
func NewMetricsManager(givenQueueManager *queueManager) *MetricsManager {
	m := MetricsManager{}

	m.queueManager = givenQueueManager

	m.metricsChannel = make(chan *Metric, 100)

	if Configuration.StatsDEndpoint != "" {
		m.statsdEnabled = true
		statsClient := statsd.NewStatsdClient(Configuration.StatsDEndpoint, "gamq.")
		statsClient.CreateSocket()
		interval := time.Second
		m.statsBuffer = statsd.NewStatsdBuffer(interval, statsClient)
		log.Debugf("Initialized StatsD - sending metrics to: %s", Configuration.StatsDEndpoint)
	} else {
		m.statsdEnabled = false
	}

	go m.listenForMetrics()

	return &m
}
Example #23
0
func main() {
	// init
	prefix := "myproject."
	statsdclient := statsd.NewStatsdClient("localhost:8125", prefix)
	err := statsdclient.CreateSocket()
	if nil != err {
		log.Println(err)
		os.Exit(1)
	}
	interval := time.Second * 2 // aggregate stats and flush every 2 seconds
	stats := statsd.NewStatsdBuffer(interval, statsdclient)
	defer stats.Close()

	// not buffered: send immediately
	statsdclient.Incr("mymetric", 4)

	// buffered: aggregate in memory before flushing
	stats.Incr("mymetric", 1)
	stats.Incr("mymetric", 3)
	stats.Incr("mymetric", 1)
	stats.Incr("mymetric", 1)
}
Example #24
0
func main() {
	c, err := parseArgs()
	if err != nil {
		log.Fatalf("Error parsing arguments: %s", err.Error())
	}

	if len(c.logfile) > 0 {
		logfile, err := os.OpenFile(c.logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
		if err != nil {
			log.Fatalf("Error opening log file %s: %v", c.logfile, err)
		}
		defer logfile.Close()
		log.SetOutput(logfile)
	}
	if len(c.statsdServer) > 0 {
		c.stats = statsd.NewStatsdClient(c.statsdServer, c.statsdPrefix)
		c.stats.CreateSocket()
		c.debug("Statsd enabled.")
		go c.sendStats()
	} else {
		c.stats = statsd.NoopClient{}
	}

	getter := s3getter{region: c.region, bucket: c.bucket, prefix: c.prefix}
	c.debug("Fetching zones...")
	z, err := c.getZones(getter)
	if err != nil {
		log.Fatal(err)
	}
	c.stats.Gauge("zones", int64(len(z)))
	c.debug(fmt.Sprintf("Fetched %d zones...", len(z)))

	c.debug("Loading zones...")
	err = c.loadZones(z)
	if err != nil {
		log.Fatal(err)
	}
	c.registerVersionHandler()
	c.debug("Starting server...")
	c.startServer()
	log.Printf("DNS server running on TCP/UDP port %s (v%s)", c.port, version)
	c.stats.Incr("started", 1)

	doUpdate := make(chan bool)
	go func() {
		for {
			select {
			case <-doUpdate:
				c.debug("Update signal... fetching updating zones")
			case <-time.After(c.update):
				c.debug("Update timeout... fetching updating zones")
			}
			z, err := c.getZones(getter)
			if err != nil {
				log.Fatal(err)
			}
			c.debug(fmt.Sprintf("Fetched %d updated zones", len(z)))
			if len(z) > 0 {
				c.stats.Incr("zoneupdates", int64(len(z)))
				c.debug(fmt.Sprintf("Reloading %d zones now", len(z)))
				err = c.loadZones(z)
				if err != nil {
					log.Fatal(err)
				}
			}
			c.debug("Updated zones successfully")
		}
	}()

	sig := make(chan os.Signal)
	signal.Notify(sig, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
	for {
		select {
		case s := <-sig:
			if s == syscall.SIGHUP {
				doUpdate <- true
			} else {
				log.Fatalf("Signal (%d) received, stopping", s)
			}
		}
	}
}
Example #25
0
func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)

	cfg := config.Config{}
	err := config.LoadConfigurationFromFileAndEnvironment("config.json", &cfg)
	if err != nil {
		log.Fatal("Could not load configuration file=config.json due to error=", err)
	}

	//Check for arguments.
	if len(os.Args) > 1 {
		auth, err := aws.GetAuth(cfg.AWSAccessKey, cfg.AWSSecretKey)
		if err != nil {
			log.Fatal("Could not load aws authentication")
		}

		pool := createRedisPool(cfg.RedisHost, cfg.RedisPassword)
		conn := pool.Get()
		_, err = conn.Do("PING")
		if err != nil {
			pool = nil
		}

		ctx := routes.CreateContext(nil, pool, auth, &cfg)
		cli.Dispatch(os.Args[1:], ctx)
		return
	}

	// Open up the DB and Redis connections.
	db, err := sqlx.Open("postgres", cfg.PostgresConnectionURL)
	if err != nil {
		log.Fatal("Could not open postgres connection with url=", cfg.PostgresConnectionURL, " error=", err)
	}

	err = db.Ping()
	if err != nil {
		log.Fatal("Could not ping postgres connection with url=", cfg.PostgresConnectionURL, " error=", err)
	}

	pool := createRedisPool(cfg.RedisHost, cfg.RedisPassword)
	conn := pool.Get()
	_, err = conn.Do("PING")
	if err != nil {
		log.Fatal("Could not ping redis DB with host=", cfg.RedisHost, " error=", err)
	}

	client := statsd.NewStatsdClient(cfg.StatsDHost, cfg.StatsDPrefix)
	err = client.CreateSocket()
	if err != nil {
		log.Fatal("Could not create statsd socket with host=", cfg.RedisHost, " error=", err)
	}

	auth, err := aws.GetAuth(cfg.AWSAccessKey, cfg.AWSSecretKey)
	if err != nil {
		log.Fatal("Could not load aws authentication")
	}

	ctx := routes.CreateContext(db, pool, auth, &cfg)

	router := mux.NewRouter()
	router.NotFoundHandler = http.HandlerFunc(routes.NotFound)
	router.HandleFunc("/favicon{suffix}", serveFavicon)
	router.HandleFunc("/mstile{suffix}", serveFavicon)

	blog.RegisterRoutes(router, ctx)
	admin.RegisterRoutes(router, ctx)
	gallery.RegisterRoutes(router, ctx)
	if cfg.Debug {
		debug.RegisterRoutes(router, ctx)
	}

	go collectStats(db, pool, client)

	listeningAddress := fmt.Sprintf("localhost:%d", cfg.Port)

	log.Print("Listening on addr=", listeningAddress)
	err = http.ListenAndServe(listeningAddress, router)
	if err != nil {
		log.Print(err)
	}
}
Example #26
0
func main() {
	in := ""

	prefix := "MemFlash."
	stats := statsd.NewStatsdClient(STATSD_IP+STATSD_PORT, prefix)
	err := stats.CreateSocket()
	check(err)
	//stats:=statsd.NewStatsdBuffer(1*time.Second, statsdclient)

	//mc := memcache.New("192.168.99.100:11211")
	//fmt.Printf("mc: %T\n", mc)
	mydb := memflash.DB{MEM_IP + MEM_PORT, "", nil}

	ref := mydb.New()

	session, err := mgo.Dial(MONGO_IP + MONGO_PORT)
	if err != nil {
		panic(err)
	}

	defer session.Close()

	session.SetMode(mgo.Monotonic, true)
	c := session.DB("test").C("people")

	result := Person{}
	// s1 := rand.NewSource(time.Now().UnixNano())
	// r1 := rand.New(s1)

	start := time.Now()

	for i := 0; i < 10000; i++ {
		memCh := make(chan bool)
		mongoCh := make(chan bool)
		timeout := make(chan bool, 1)
		fmt.Println("Iteration: ", i)

		//time.Sleep(5*time.Millisecond)

		start1 := time.Now()
		go func() {
			time.Sleep(TIMEOUT * time.Millisecond)
			timeout <- true
		}()

		go func(i int) {

			item, err := ref.Get(strconv.Itoa(i))
			if err == memcache.ErrCacheMiss {
				//Cache MISS
			}
			if item != nil {
				//HIT

				memCh <- true

			}

		}(i)

		go func(i int) {
			err = c.Find(bson.M{"index": strconv.Itoa(i)}).One(&result)
			check(err)
			//time.Sleep(time.Duration(r1.Intn(5)) * time.Millisecond)

			mongoCh <- true

		}(i)

		select {
		case <-memCh:
			fmt.Println("Memcache WINS")
			fmt.Printf("*** time %s \n ", time.Since(start1))
			stats.Gauge("p1", int64(time.Since(start1)))
			<-timeout
		case <-mongoCh:
			fmt.Println("MONGO wins")
			fmt.Printf("*** time %s \n ", time.Since(start1))
			stats.Gauge("p1", int64(time.Since(start1)))
			fmt.Println(int64(time.Since(start1)))
			<-timeout
		case <-timeout:
			fmt.Println("Slow connection")
			continue
		}

	}

	fmt.Printf("****** Executed in time %s \n ", time.Since(start))

	fmt.Scanf("%s", &in)

}
Example #27
0
func main() {
	in := ""
	memCh := make(chan bool)
	mongoCh := make(chan bool)
	timeout := make(chan bool, 1)
	prefix := "MemFlash."
	statsdclient := statsd.NewStatsdClient(STATSD_IP+STATSD_PORT, prefix)
	err := statsdclient.CreateSocket()
	check(err)

	//mc := memcache.New("192.168.99.100:11211")
	//fmt.Printf("mc: %T\n", mc)
	mydb := memflash.DB{MEM_IP + MEM_PORT, "", nil}

	ref := mydb.New()

	session, err := mgo.Dial(MONGO_IP + MONGO_PORT)
	if err != nil {
		panic(err)
	}

	defer session.Close()

	session.SetMode(mgo.Monotonic, true)
	c := session.DB("test").C("people")

	result := Person{}

	start := time.Now()

	for i := 0; i < 10000; i++ {

		time.Sleep(10 * time.Millisecond)

		start1 := time.Now()
		go func() {
			time.Sleep(3 * time.Second)
			timeout <- true
		}()

		go func(i int) {

			item, err := ref.Get(strconv.Itoa(i))
			if err == memcache.ErrCacheMiss {
				//fmt.Println("CACHE MISS!!!!!")
				// err = c.Find(bson.M{"index": strconv.Itoa(i)}).One(&result)
				// //fmt.Println(err)
				// check(err)

				// //log.Println("Mongo Result:", result.Data)
				// //fmt.Println("MONGO RES ~~~~~~~~~~~~~~")
				// mongoCh <- true
			}
			if item != nil {
				//log.Println("*******************  Mem Result:", string(item.Value))
				//fmt.Println("HIT")
				memCh <- true

			}

		}(i)

		go func(i int) {
			err = c.Find(bson.M{"index": strconv.Itoa(i)}).One(&result)
			//fmt.Println(err)
			check(err)

			//log.Println("Mongo Result:", result.Data)
			//fmt.Println("MONGO RES ~~~~~~~~~~~~~~")
			mongoCh <- true

		}(i)

		select {
		case <-memCh:
			fmt.Println("MEMCACHE WINS!!")
			fmt.Printf("* time %s \n ", time.Since(start1))
			statsdclient.Absolute("p11", int64(time.Since(start1)))
		case <-mongoCh:
			fmt.Println("MONGO wins xxxxxxxxxxxxxxxxxx")
			fmt.Printf("* time %s \n ", time.Since(start1))
			statsdclient.Absolute("p11", int64(time.Since(start1)))
		case <-timeout:
			fmt.Println("TOO SLOW------------------------------------------")
			continue

		}

	}

	fmt.Printf("****** Executed in time %s \n ", time.Since(start))

	fmt.Scanf("%s", &in)

}
Example #28
0
func main() {
	kingpin.Parse()

	fmt.Println(*uaaEndpoint)
	fmt.Println(*username)
	fmt.Println(*password)
	tokenFetcher := &token.UAATokenFetcher{
		UaaUrl:                *uaaEndpoint,
		Username:              *username,
		Password:              *password,
		InsecureSSLSkipVerify: *skipSSLValidation,
	}

	authToken, err := tokenFetcher.FetchAuthToken()
	if err != nil {
		fmt.Println(err)
		os.Exit(-1)
	}

	consumer := noaa.NewConsumer(*dopplerEndpoint, &tls.Config{InsecureSkipVerify: *skipSSLValidation}, nil)

	httpStartStopProcessor := processors.NewHttpStartStopProcessor()
	valueMetricProcessor := processors.NewValueMetricProcessor()
	containerMetricProcessor := processors.NewContainerMetricProcessor()
	heartbeatProcessor := processors.NewHeartbeatProcessor()
	counterProcessor := processors.NewCounterProcessor()

	sender := statsd.NewStatsdClient(*statsdEndpoint, *statsdPrefix)
	sender.CreateSocket()

	var processedMetrics []metrics.Metric

	msgChan := make(chan *events.Envelope)
	go func() {
		defer close(msgChan)
		errorChan := make(chan error)
		go consumer.Firehose(*subscriptionId, authToken, msgChan, errorChan, nil)

		for err := range errorChan {
			fmt.Fprintf(os.Stderr, "%v\n", err.Error())
		}
	}()

	for msg := range msgChan {
		eventType := msg.GetEventType()

		// graphite-nozzle can handle CounterEvent, ContainerMetric, Heartbeat,
		// HttpStartStop and ValueMetric events
		switch eventType {
		case events.Envelope_ContainerMetric:
			processedMetrics = containerMetricProcessor.Process(msg)
		case events.Envelope_CounterEvent:
			processedMetrics = counterProcessor.Process(msg)
		case events.Envelope_Heartbeat:
			processedMetrics = heartbeatProcessor.Process(msg)
		case events.Envelope_HttpStartStop:
			processedMetrics = httpStartStopProcessor.Process(msg)
		case events.Envelope_ValueMetric:
			processedMetrics = valueMetricProcessor.Process(msg)
		default:
			// do nothing
		}

		if !*debug {
			if len(processedMetrics) > 0 {
				for _, metric := range processedMetrics {
					var prefix string
					if *prefixJob {
						prefix = msg.GetJob() + "." + msg.GetIndex()
					}
					metric.Send(sender, prefix)
				}
			}
		} else {
			for _, msg := range processedMetrics {
				fmt.Println(msg)
			}
		}
		processedMetrics = nil
	}
}
Example #29
0
		metric = metric[:2] + "bit" + metric[4:]
		value = value * 1024 * 8
	}

	if record[3] != "-" && record[3] != "all" {
		// add device name to metric
		metric = record[3] + "." + metric
	}

	metric = strings.Replace(metric, "%", "pct_", -1)
	sd.FGauge(metric, value)
	return nil
}

var statsdServer = flag.String("d", ":8125", "Destination statsd server address")
var statsdPrefix = flag.String("p", "sar", "Statsd prefix for metrics")
var interval = flag.Duration("i", 10*time.Second, "Interval to send metrics")
var sd = statsd.NewStatsdClient(*statsdServer, *statsdPrefix+".")

func main() {
	flag.Parse()
	err := sd.CreateSocket()
	if err != nil {
		log.Fatal(err)
	}
	c := time.Tick(*interval)
	for _ = range c {
		go getStats()
	}
}