circonus-gometrics.go
func New(cfg *Config) (*CirconusMetrics, error) {

	if cfg == nil {
		return nil, errors.New("invalid configuration (nil)")
	}

	cm := &CirconusMetrics{
		counters:     make(map[string]uint64),
		counterFuncs: make(map[string]func() uint64),
		gauges:       make(map[string]interface{}),
		gaugeFuncs:   make(map[string]func() int64),
		histograms:   make(map[string]*Histogram),
		text:         make(map[string]string),
		textFuncs:    make(map[string]func() string),
		lastMetrics:  &prevMetrics{},
	}

	// Logging
	{
		cm.Debug = cfg.Debug
		cm.Log = cfg.Log

		if cm.Debug && cm.Log == nil {
			cm.Log = log.New(os.Stderr, "", log.LstdFlags)
		}
		if cm.Log == nil {
			cm.Log = log.New(ioutil.Discard, "", log.LstdFlags)
		}
	}

	// Flush Interval
	{
		fi := defaultFlushInterval
		if cfg.Interval != "" {
			fi = cfg.Interval
		}

		dur, err := time.ParseDuration(fi)
		if err != nil {
			return nil, errors.Wrap(err, "parsing flush interval")
		}
		cm.flushInterval = dur
	}

	// metric resets

	cm.resetCounters = true
	if cfg.ResetCounters != "" {
		setting, err := strconv.ParseBool(cfg.ResetCounters)
		if err != nil {
			return nil, errors.Wrap(err, "parsing reset counters")
		}
		cm.resetCounters = setting
	}

	cm.resetGauges = true
	if cfg.ResetGauges != "" {
		setting, err := strconv.ParseBool(cfg.ResetGauges)
		if err != nil {
			return nil, errors.Wrap(err, "parsing reset gauges")
		}
		cm.resetGauges = setting
	}

	cm.resetHistograms = true
	if cfg.ResetHistograms != "" {
		setting, err := strconv.ParseBool(cfg.ResetHistograms)
		if err != nil {
			return nil, errors.Wrap(err, "parsing reset histograms")
		}
		cm.resetHistograms = setting
	}

	cm.resetText = true
	if cfg.ResetText != "" {
		setting, err := strconv.ParseBool(cfg.ResetText)
		if err != nil {
			return nil, errors.Wrap(err, "parsing reset text")
		}
		cm.resetText = setting
	}

	// check manager
	{
		cfg.CheckManager.Debug = cm.Debug
		cfg.CheckManager.Log = cm.Log

		check, err := checkmgr.New(&cfg.CheckManager)
		if err != nil {
			return nil, errors.Wrap(err, "creating new check manager")
		}
		cm.check = check
	}

	// start background initialization
	cm.check.Initialize()

	// if automatic flush is enabled, start it.
	// NOTE: submit will jettison metrics until initialization has completed.
	if cm.flushInterval > time.Duration(0) {
		go func() {
			for range time.NewTicker(cm.flushInterval).C {
				cm.Flush()
			}
		}()
	}

	return cm, nil
}
func New(cfg *Config) (*CirconusMetrics, error) {

	if cfg == nil {
		return nil, errors.New("invalid configuration (nil)")
	}

	cm := &CirconusMetrics{
		counters:     make(map[string]uint64),
		counterFuncs: make(map[string]func() uint64),
		gauges:       make(map[string]interface{}),
		gaugeFuncs:   make(map[string]func() int64),
		histograms:   make(map[string]*Histogram),
		text:         make(map[string]string),
		textFuncs:    make(map[string]func() string),
		lastMetrics:  &prevMetrics{},
	}

	// Logging
	{
		cm.Debug = cfg.Debug
		cm.Log = cfg.Log

		if cm.Debug && cm.Log == nil {
			cm.Log = log.New(os.Stderr, "", log.LstdFlags)
		}
		if cm.Log == nil {
			cm.Log = log.New(ioutil.Discard, "", log.LstdFlags)
		}
	}

	// Flush Interval
	{
		fi := defaultFlushInterval
		if cfg.Interval != "" {
			fi = cfg.Interval
		}

		dur, err := time.ParseDuration(fi)
		if err != nil {
			return nil, errors.Wrap(err, "parsing flush interval")
		}
		cm.flushInterval = dur
	}

	// metric resets

	cm.resetCounters = true
	if cfg.ResetCounters != "" {
		setting, err := strconv.ParseBool(cfg.ResetCounters)
		if err != nil {
			return nil, errors.Wrap(err, "parsing reset counters")
		}
		cm.resetCounters = setting
	}

	cm.resetGauges = true
	if cfg.ResetGauges != "" {
		setting, err := strconv.ParseBool(cfg.ResetGauges)
		if err != nil {
			return nil, errors.Wrap(err, "parsing reset gauges")
		}
		cm.resetGauges = setting
	}

	cm.resetHistograms = true
	if cfg.ResetHistograms != "" {
		setting, err := strconv.ParseBool(cfg.ResetHistograms)
		if err != nil {
			return nil, errors.Wrap(err, "parsing reset histograms")
		}
		cm.resetHistograms = setting
	}

	cm.resetText = true
	if cfg.ResetText != "" {
		setting, err := strconv.ParseBool(cfg.ResetText)
		if err != nil {
			return nil, errors.Wrap(err, "parsing reset text")
		}
		cm.resetText = setting
	}

	// check manager
	{
		cfg.CheckManager.Debug = cm.Debug
		cfg.CheckManager.Log = cm.Log

		check, err := checkmgr.New(&cfg.CheckManager)
		if err != nil {
			return nil, errors.Wrap(err, "creating new check manager")
		}
		cm.check = check
	}

	// start background initialization
	cm.check.Initialize()

	// if automatic flush is enabled, start it.
	// NOTE: submit will jettison metrics until initialization has completed.
	if cm.flushInterval > time.Duration(0) {
		go func() {
			for range time.NewTicker(cm.flushInterval).C {
				cm.Flush()
			}
		}()
	}

	return cm, nil
}
func New ( cfg * Config ) ( * CirconusMetrics , error ) { if cfg == nil { return nil , errors . New ( " " ) } cm := & CirconusMetrics { counters : make ( map [ string ] uint64 ) , counterFuncs : make ( map [ string ] func ( ) uint64 ) , gauges : make ( map [ string ] interface { } ) , gaugeFuncs : make ( map [ string ] func ( ) int64 ) , histograms : make ( map [ string ] * Histogram ) , text : make ( map [ string ] string ) , textFuncs : make ( map [ string ] func ( ) string ) , lastMetrics : & prevMetrics { } , } // Logging { cm . Debug = cfg . Debug cm . Log = cfg . Log if cm . Debug && cm . Log == nil { cm . Log = log . New ( os . Stderr , " " , log . LstdFlags ) } if cm . Log == nil { cm . Log = log . New ( ioutil . Discard , " " , log . LstdFlags ) } } // Flush Interval { fi := defaultFlushInterval if cfg . Interval != " " { fi = cfg . Interval } dur , err := time . ParseDuration ( fi ) if err != nil { return nil , errors . Wrap ( err , " " ) } cm . flushInterval = dur } // metric resets cm . resetCounters = true if cfg . ResetCounters != " " { setting , err := strconv . ParseBool ( cfg . ResetCounters ) if err != nil { return nil , errors . Wrap ( err , " " ) } cm . resetCounters = setting } cm . resetGauges = true if cfg . ResetGauges != " " { setting , err := strconv . ParseBool ( cfg . ResetGauges ) if err != nil { return nil , errors . Wrap ( err , " " ) } cm . resetGauges = setting } cm . resetHistograms = true if cfg . ResetHistograms != " " { setting , err := strconv . ParseBool ( cfg . ResetHistograms ) if err != nil { return nil , errors . Wrap ( err , " " ) } cm . resetHistograms = setting } cm . resetText = true if cfg . ResetText != " " { setting , err := strconv . ParseBool ( cfg . ResetText ) if err != nil { return nil , errors . Wrap ( err , " " ) } cm . resetText = setting } // check manager { cfg . CheckManager . Debug = cm . Debug cfg . CheckManager . Log = cm . Log check , err := checkmgr . New ( & cfg . CheckManager ) if err != nil { return nil , errors . Wrap ( err , " " ) } cm . check = check } // start background initialization cm . check . Initialize ( ) // if automatic flush is enabled, start it. // NOTE: submit will jettison metrics until initialization has completed. if cm . flushInterval > time . Duration ( 0 ) { go func ( ) { for range time . NewTicker ( cm . flushInterval ) . C { cm . Flush ( ) } } ( ) } return cm , nil }
// New returns a CirconusMetrics instance
New returns a CirconusMetrics instance