Пример #1
0
	`

	BeforeEach(func() {
		c := redigomock.NewFakeRedis()
		db = filter.NewDbConnector(&redis.Pool{
			MaxIdle:     3,
			IdleTimeout: 240 * time.Second,
			Dial: func() (redis.Conn, error) {
				return c, nil
			},
		})
		for _, pattern := range testPatterns {
			c.Do("SADD", "moira-pattern-list", pattern)
		}

		filter.InitGraphiteMetrics()

		patterns = filter.NewPatternStorage()
		patterns.DoRefresh(db)
		cache = &filter.CacheStorage{}
		cache.BuildRetentions(bufio.NewScanner(strings.NewReader(testRetentions)))
	})

	Context("When invalid metric arrives", func() {
		BeforeEach(func() {
			for _, metric := range invalidRawMetrics {
				process(metric)
			}
		})

		It("should be properly counted", func() {
Пример #2
0
func main() {

	runtime.GOMAXPROCS(runtime.NumCPU())

	flag.Parse()
	log.SetFlags(log.Lmicroseconds | log.Lshortfile)
	log.SetPrefix(fmt.Sprintf("pid:%d ", syscall.Getpid()))

	if err := readConfig(configFileName); err != nil {
		log.Fatalf("error reading config %s: %s", *configFileName, err)
	}
	logFile, err := os.OpenFile(logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
	if err != nil {
		log.Fatalf("error opening log file %s: %s", logFileName, err)
	}
	defer logFile.Close()
	log.SetOutput(logFile)

	err = ioutil.WriteFile(pidFileName, []byte(fmt.Sprint(syscall.Getpid())), 0644)
	if err != nil {
		log.Fatalf("error writing pid file %s: %s", pidFileName, err)
	}

	retentionConfigFile, err := os.Open(retentionConfigFileName)
	if err != nil {
		log.Fatalf("error open retentions file %s: %s", pidFileName, err)
	}

	filter.InitGraphiteMetrics()

	db = filter.NewDbConnector(newRedisPool(redisURI))
	patterns = filter.NewPatternStorage()
	cache, err = filter.NewCacheStorage(retentionConfigFile)
	if err != nil {
		log.Fatalf("failed to initialize cache with config %s: %s", retentionConfigFileName, err.Error())
	}

	go patterns.Refresh(db)

	if graphiteURI != "" {
		graphiteAddr, _ := net.ResolveTCPAddr("tcp", graphiteURI)
		go graphite.Graphite(metrics.DefaultRegistry, time.Duration(graphiteInterval)*time.Second, fmt.Sprintf("%s.cache", graphitePrefix), graphiteAddr)
	}

	l, err := goagain.Listener()
	if err != nil {
		l, err = net.Listen("tcp", listen)
		if err != nil {
			log.Fatalf("failed to listen on %s: %s", listen, err.Error())
		}
		log.Printf("listening on %s", listen)

		go serve(l)

	} else {
		log.Printf("resuming listening on %s", listen)

		go serve(l)

		if err := goagain.Kill(); err != nil {
			log.Fatalf("failed to kill parent process: %s", err.Error())
		}
	}

	if _, err := goagain.Wait(l); err != nil {
		log.Fatalf("failed to block main goroutine: %s", err.Error())
	}

	log.Printf("shutting down")
	if err := l.Close(); err != nil {
		log.Fatalf("failed to stop listening: %s", err.Error())
	}
	time.Sleep(time.Second)
	log.Printf("shutdown complete")
}