github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/metrics/collect/context.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package collect
     5  
     6  import (
     7  	"fmt"
     8  	"math/rand"
     9  	"time"
    10  
    11  	"github.com/juju/errors"
    12  
    13  	"github.com/juju/juju/worker/metrics/spool"
    14  	"github.com/juju/juju/worker/uniter/runner/context"
    15  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    16  )
    17  
    18  type hookContext struct {
    19  	jujuc.RestrictedContext
    20  
    21  	unitName string
    22  	id       string
    23  	recorder spool.MetricRecorder
    24  }
    25  
    26  func newHookContext(unitName string, recorder spool.MetricRecorder) *hookContext {
    27  	// TODO(fwereade): 2016-03-17 lp:1558657
    28  	id := fmt.Sprintf("%s-%s-%d", unitName, "collect-metrics", rand.New(rand.NewSource(time.Now().Unix())).Int63())
    29  	return &hookContext{unitName: unitName, id: id, recorder: recorder}
    30  }
    31  
    32  // HookVars implements runner.Context.
    33  func (ctx *hookContext) HookVars(paths context.Paths) ([]string, error) {
    34  	vars := []string{
    35  		"CHARM_DIR=" + paths.GetCharmDir(), // legacy
    36  		"JUJU_CHARM_DIR=" + paths.GetCharmDir(),
    37  		"JUJU_CONTEXT_ID=" + ctx.id,
    38  		"JUJU_AGENT_SOCKET=" + paths.GetJujucSocket(),
    39  		"JUJU_UNIT_NAME=" + ctx.unitName,
    40  	}
    41  	return append(vars, context.OSDependentEnvVars(paths)...), nil
    42  }
    43  
    44  // UnitName implements runner.Context.
    45  func (ctx *hookContext) UnitName() string {
    46  	return ctx.unitName
    47  }
    48  
    49  // Flush implements runner.Context.
    50  func (ctx *hookContext) Flush(process string, ctxErr error) (err error) {
    51  	return ctx.recorder.Close()
    52  }
    53  
    54  // AddMetric implements runner.Context.
    55  func (ctx *hookContext) AddMetric(key string, value string, created time.Time) error {
    56  	return ctx.recorder.AddMetric(key, value, created, nil)
    57  }
    58  
    59  // AddMetricLabels implements runner.Context.
    60  func (ctx *hookContext) AddMetricLabels(key string, value string, created time.Time, labels map[string]string) error {
    61  	return ctx.recorder.AddMetric(key, value, created, labels)
    62  }
    63  
    64  // addJujuUnitsMetric adds the juju-units built in metric if it
    65  // is defined for this context.
    66  func (ctx *hookContext) addJujuUnitsMetric() error {
    67  	if ctx.recorder.IsDeclaredMetric("juju-units") {
    68  		// TODO(fwereade): 2016-03-17 lp:1558657
    69  		err := ctx.recorder.AddMetric("juju-units", "1", time.Now().UTC(), nil)
    70  		if err != nil {
    71  			return errors.Trace(err)
    72  		}
    73  	}
    74  	return nil
    75  }
    76  
    77  // SetProcess implements runner.Context.
    78  func (ctx *hookContext) SetProcess(process context.HookProcess) {}
    79  
    80  // ActionData implements runner.Context.
    81  func (ctx *hookContext) ActionData() (*context.ActionData, error) {
    82  	return nil, jujuc.ErrRestrictedContext
    83  }
    84  
    85  // HasExecutionSetUnitStatus implements runner.Context.
    86  func (ctx *hookContext) HasExecutionSetUnitStatus() bool { return false }
    87  
    88  // ResetExecutionSetUnitStatus implements runner.Context.
    89  func (ctx *hookContext) ResetExecutionSetUnitStatus() {}
    90  
    91  // Id implements runner.Context.
    92  func (ctx *hookContext) Id() string { return ctx.id }
    93  
    94  // Prepare implements runner.Context.
    95  func (ctx *hookContext) Prepare() error {
    96  	return jujuc.ErrRestrictedContext
    97  }
    98  
    99  // Component implements runner.Context.
   100  func (ctx *hookContext) Component(name string) (jujuc.ContextComponent, error) {
   101  	return nil, errors.NotFoundf("context component %q", name)
   102  }