Пример #1
0
var (
	connectionCountTcpPool  int
	connectionCountTcp      int
	connectionCountUdp      int
	connectionCountUnixPool int
	connectionCountUnix     int
	connectionErrorTcpPool  int
	connectionErrorTcp      int
	connectionErrorUdp      int
	connectionErrorUnixPool int
	connectionErrorUnix     int
	tcpPool                 *statsgod.ConnectionPool
	unixPool                *statsgod.ConnectionPool
)

var logger = *statsgod.CreateLogger(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr)

func main() {
	flag.Parse()
	fmt.Printf("Starting test with %d metrics on %d concurrent threads for %s.\n", *numMetrics, *concurrency, *runTime)

	tcpPool, _ = statsgod.CreateConnectionPool(*statsPoolCount, fmt.Sprintf("%s:%d", *statsHost, *statsPortTcp), statsgod.ConnPoolTypeTcp, 10*time.Second, logger)
	unixPool, _ = statsgod.CreateConnectionPool(*statsPoolCount, *statsSock, statsgod.ConnPoolTypeUnix, 10*time.Second, logger)

	startTime := time.Now()

	finishChannel := make(chan int)
	flushChannel := make(chan Metric)

	// If the user specified an auth token, construct it here.
	metricToken := ""
Пример #2
0
func main() {
	// Load command line options.
	flag.Parse()

	// Load the YAML config.
	var config, _ = statsgod.CreateConfig(*configFile)

	// Set up the logger based on the configuration.
	var logger statsgod.Logger
	if config.Debug.Verbose {
		logger = *statsgod.CreateLogger(os.Stdout, os.Stdout, os.Stdout, os.Stderr)
		logger.Info.Println("Debugging mode enabled")
		logger.Info.Printf("Loaded Config: %v", config)
	} else {
		logger = *statsgod.CreateLogger(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr)
	}

	// Set up profiling.
	if config.Debug.Profile {
		f, err := os.Create("statsgod.prof")
		if err != nil {
			panic(err)
		}
		pprof.StartCPUProfile(f)
	}

	// Set up the backend relay.
	relay := statsgod.CreateRelay(config, logger)

	// Set up the authentication.
	auth := statsgod.CreateAuth(config)

	// Parse the incoming messages and convert to metrics.
	go statsgod.ParseMetrics(parseChannel, relayChannel, auth, logger, &quit)

	// Flush the metrics to the remote stats collector.
	go statsgod.RelayMetrics(relay, relayChannel, parseChannel, logger, &config, &quit)

	var socketTcp statsgod.Socket
	var socketUdp statsgod.Socket
	var socketUnix statsgod.Socket

	// Listen on the TCP socket.
	if config.Connection.Tcp.Enabled {
		tcpAddr := fmt.Sprintf("%s:%d", config.Connection.Tcp.Host, config.Connection.Tcp.Port)
		socketTcp = statsgod.CreateSocket(statsgod.SocketTypeTcp, tcpAddr).(*statsgod.SocketTcp)
		go socketTcp.Listen(parseChannel, logger, &config)
	}

	// Listen on the UDP socket.
	if config.Connection.Udp.Enabled {
		udpAddr := fmt.Sprintf("%s:%d", config.Connection.Udp.Host, config.Connection.Udp.Port)
		socketUdp = statsgod.CreateSocket(statsgod.SocketTypeUdp, udpAddr).(*statsgod.SocketUdp)
		go socketUdp.Listen(parseChannel, logger, &config)
	}

	// Listen on the Unix socket.
	if config.Connection.Unix.Enabled {
		socketUnix = statsgod.CreateSocket(statsgod.SocketTypeUnix, config.Connection.Unix.File).(*statsgod.SocketUnix)
		go socketUnix.Listen(parseChannel, logger, &config)
	}

	// Listen for OS signals.
	statsgod.ListenForSignals(finishChannel, &config, configFile, logger)

	// Wait until the program is finished.
	select {
	case <-finishChannel:
		logger.Info.Println("Exiting program.")
		quit = true
		if config.Connection.Tcp.Enabled {
			socketTcp.Close(logger)
		}
		if config.Connection.Udp.Enabled {
			socketUdp.Close(logger)
		}
		if config.Connection.Unix.Enabled {
			socketUnix.Close(logger)
		}
		if config.Debug.Profile {
			pprof.StopCPUProfile()
		}
	}
}