github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/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  	// interval at which the uniter sends metrics to the state server.
    17  	metricsSendInterval = 5 * time.Minute
    18  )
    19  
    20  // activeCollectMetricsTimer returns a channel that will signal the collect metrics hook
    21  // as close to interval after the last run as possible.
    22  var activeCollectMetricsTimer = func(now, lastRun time.Time, interval time.Duration) <-chan time.Time {
    23  	waitDuration := interval - now.Sub(lastRun)
    24  	logger.Debugf("metrics waiting for %v", waitDuration)
    25  	return time.After(waitDuration)
    26  }
    27  
    28  var activeSendMetricsTimer = activeCollectMetricsTimer
    29  
    30  // inactiveMetricsTimer is the default metrics signal generation function, that
    31  // returns no signal. It will be used in charms that do not declare metrics.
    32  func inactiveMetricsTimer(_, _ time.Time, _ time.Duration) <-chan time.Time {
    33  	return nil
    34  }
    35  
    36  // timerChooser allows modeAbide to choose a proper timer for metrics
    37  // depending on the charm.
    38  type timerChooser struct {
    39  	collector TimedSignal
    40  	sender    TimedSignal
    41  	inactive  TimedSignal
    42  }
    43  
    44  // getCollectMetricsTimer returns a timer used to trigger the collect-metrics hook,
    45  // given the supplied charm.
    46  func (t *timerChooser) getCollectMetricsTimer(ch corecharm.Charm) TimedSignal {
    47  	metrics := ch.Metrics()
    48  	if metrics != nil && len(metrics.Metrics) > 0 {
    49  		return t.collector
    50  	}
    51  	return t.inactive
    52  }
    53  
    54  // getSendMetricsTimer returns a timer used to trigger sending metrics
    55  // to the state server, given the supplied charm.
    56  func (t *timerChooser) getSendMetricsTimer(ch corecharm.Charm) TimedSignal {
    57  	metrics := ch.Metrics()
    58  	if metrics != nil && len(metrics.Metrics) > 0 {
    59  		return t.sender
    60  	}
    61  	return t.inactive
    62  }
    63  
    64  // NewMetricsTimerChooser returns a timerChooser for
    65  // collect-metrics hook and the send-metrics operation.
    66  func NewMetricsTimerChooser() *timerChooser {
    67  	return &timerChooser{
    68  		collector: activeCollectMetricsTimer,
    69  		sender:    activeSendMetricsTimer,
    70  		inactive:  inactiveMetricsTimer,
    71  	}
    72  }