func Clear() {
	global.Clear()
}
Clear will empty out the global stats, all counters are set to 0, all values set to 0
package stats
import (
	"encoding/csv"
	"flag"
	"fmt"
	"io"
	"os"
	"sync"
	"time"
	"github.com/facebookincubator/flog"
)
type Stats struct {
	OutputFile   string
	LogToStderr  bool
	counters     map[string]int64
	countersLock sync.RWMutex
	values       map[string]float64
	valuesLock   sync.RWMutex
}
func New() *Stats {
	s := Stats{}
	s.Clear()  
	return &s
}
func (s *Stats) AreLogged() bool {
	return s.LogToStderr || s.OutputFile != ""
}
func (s *Stats) AddFlags() {
	flag.StringVar(&s.OutputFile, "output_stats", "", "output stats to this file")
	flag.BoolVar(&s.LogToStderr, "log_stats", false, "log stats to stderr")
}
func (s *Stats) IncrementCounter(key string) {
	s.IncrementCounterBy(key, 1)
}
func (s *Stats) IncrementCounterBy(key string, value int64) {
	s.countersLock.Lock()
	s.counters[key] += value
	s.countersLock.Unlock()
}
func (s *Stats) AddToValue(key string, value float64) {
	s.valuesLock.Lock()
	s.values[key] += value
	s.valuesLock.Unlock()
}
func (s *Stats) TrackTime(key string, start time.Time, unit time.Duration) {
	elapsed := time.Since(start)
	value := float64(elapsed / unit)
	s.AddToValue(key, value)
}
func (s *Stats) GetCounter(key string) int64 {
	s.countersLock.RLock()
	defer s.countersLock.RUnlock()
	return s.counters[key]
}
func (s *Stats) GetValue(key string) float64 {
	s.valuesLock.RLock()
	defer s.valuesLock.RUnlock()
	return s.values[key]
}
func (s *Stats) Clear() {
	s.counters = make(map[string]int64)
	s.values = make(map[string]float64)
}
func (s *Stats) Write() error {
	if s.LogToStderr {
		if err := s.write(os.Stderr); err != nil {
			return fmt.Errorf("failed to write stats to stderr: %v", err)
		}
	}
	if s.OutputFile != "" {
		f, err := os.OpenFile(s.OutputFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
		if err != nil {
			return fmt.Errorf("failed to open stats file: %v", err)
		}
		defer f.Close()
		if err = s.write(f); err != nil {
			return fmt.Errorf("failed to write stats to file: %v", err)
		}
	}
	return nil
}
func (s *Stats) WriteAndLogError() {
	if err := s.Write(); err != nil {
		flog.Errorf("failed to write stats: %v", err)
	}
}
var (
	global = New()
)
func AreLogged() bool {
	return global.AreLogged()
}
func AddFlags() {
	global.AddFlags()
}
func IncrementCounter(key string) {
	global.IncrementCounter(key)
}
func IncrementCounterBy(key string, value int64) {
	global.IncrementCounterBy(key, value)
}
func AddToValue(key string, value float64) {
	global.AddToValue(key, value)
}
func TrackTime(key string, start time.Time, unit time.Duration) {
	global.TrackTime(key, start, unit)
}
func GetCounter(key string) int64 {
	return global.GetCounter(key)
}
func GetValue(key string) float64 {
	return global.GetValue(key)
}