github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/worker/uniter/metrics.go (about)

     1  // Copyright 2012-2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package uniter
     5  
     6  import (
     7  	"time"
     8  
     9  	corecharm "gopkg.in/juju/charm.v4"
    10  )
    11  
    12  const (
    13  	// interval at which the unit's metrics should be collected
    14  	metricsPollInterval = 5 * time.Minute
    15  )
    16  
    17  // CollectMetricsSignal is the signature of the function used to generate a
    18  // collect-metrics signal.
    19  type CollectMetricsSignal func(now, lastSignal time.Time, interval time.Duration) <-chan time.Time
    20  
    21  // activeMetricsTimer returns a channel that will signal the collect metrics hook
    22  // as close to interval after the last run as possible.
    23  var activeMetricsTimer = func(now, lastRun time.Time, interval time.Duration) <-chan time.Time {
    24  	waitDuration := interval - now.Sub(lastRun)
    25  	logger.Debugf("waiting for %v", waitDuration)
    26  	return time.After(waitDuration)
    27  }
    28  
    29  // inactiveMetricsTimer is the default metrics signal generation function, that
    30  // returns no signal. It will be used in charms that do not declare metrics.
    31  func inactiveMetricsTimer(_, _ time.Time, _ time.Duration) <-chan time.Time {
    32  	return nil
    33  }
    34  
    35  // getMetricsTimer returns the metrics timer we should be using, given the supplied
    36  // charm.
    37  func getMetricsTimer(ch corecharm.Charm) CollectMetricsSignal {
    38  	metrics := ch.Metrics()
    39  	if metrics != nil && len(metrics.Metrics) > 0 {
    40  		return activeMetricsTimer
    41  	}
    42  	return inactiveMetricsTimer
    43  }