github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/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.v5"
    10  )
    11  
    12  const (
    13  	// interval at which the unit's metrics should be collected
    14  	metricsPollInterval = 5 * time.Minute
    15  )
    16  
    17  // activeMetricsTimer returns a channel that will signal the collect metrics hook
    18  // as close to interval after the last run as possible.
    19  var activeMetricsTimer = func(now, lastRun time.Time, interval time.Duration) <-chan time.Time {
    20  	waitDuration := interval - now.Sub(lastRun)
    21  	logger.Debugf("metrics waiting for %v", waitDuration)
    22  	return time.After(waitDuration)
    23  }
    24  
    25  // inactiveMetricsTimer is the default metrics signal generation function, that
    26  // returns no signal. It will be used in charms that do not declare metrics.
    27  func inactiveMetricsTimer(_, _ time.Time, _ time.Duration) <-chan time.Time {
    28  	return nil
    29  }
    30  
    31  // timerChooser allows modeAbide to choose a proper timer for metrics
    32  // depending on the charm.
    33  type timerChooser struct {
    34  	active   TimedSignal
    35  	inactive TimedSignal
    36  }
    37  
    38  // getMetricsTimer returns the metrics timer we should be using, given the supplied
    39  // charm.
    40  func (t *timerChooser) getMetricsTimer(ch corecharm.Charm) TimedSignal {
    41  	metrics := ch.Metrics()
    42  	if metrics != nil && len(metrics.Metrics) > 0 {
    43  		return t.active
    44  	}
    45  	return t.inactive
    46  }
    47  
    48  // NewMetricsTimerChooser returns a timerChooser for
    49  // collect-metrics.
    50  func NewMetricsTimerChooser() *timerChooser {
    51  	return &timerChooser{
    52  		active:   activeMetricsTimer,
    53  		inactive: inactiveMetricsTimer,
    54  	}
    55  }