github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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 "JUJU_CHARM_DIR=" + paths.GetCharmDir(), 36 "JUJU_CONTEXT_ID=" + ctx.id, 37 "JUJU_AGENT_SOCKET=" + paths.GetJujucSocket(), 38 "JUJU_UNIT_NAME=" + ctx.unitName, 39 } 40 return append(vars, context.OSDependentEnvVars(paths)...), nil 41 } 42 43 // UnitName implements runner.Context. 44 func (ctx *hookContext) UnitName() string { 45 return ctx.unitName 46 } 47 48 // Flush implements runner.Context. 49 func (ctx *hookContext) Flush(process string, ctxErr error) (err error) { 50 return ctx.recorder.Close() 51 } 52 53 // AddMetric implements runner.Context. 54 func (ctx *hookContext) AddMetric(key string, value string, created time.Time) error { 55 return ctx.recorder.AddMetric(key, value, created) 56 } 57 58 // addJujuUnitsMetric adds the juju-units built in metric if it 59 // is defined for this context. 60 func (ctx *hookContext) addJujuUnitsMetric() error { 61 if ctx.recorder.IsDeclaredMetric("juju-units") { 62 // TODO(fwereade): 2016-03-17 lp:1558657 63 err := ctx.recorder.AddMetric("juju-units", "1", time.Now().UTC()) 64 if err != nil { 65 return errors.Trace(err) 66 } 67 } 68 return nil 69 } 70 71 // SetProcess implements runner.Context. 72 func (ctx *hookContext) SetProcess(process context.HookProcess) {} 73 74 // ActionData implements runner.Context. 75 func (ctx *hookContext) ActionData() (*context.ActionData, error) { 76 return nil, jujuc.ErrRestrictedContext 77 } 78 79 // HasExecutionSetUnitStatus implements runner.Context. 80 func (ctx *hookContext) HasExecutionSetUnitStatus() bool { return false } 81 82 // ResetExecutionSetUnitStatus implements runner.Context. 83 func (ctx *hookContext) ResetExecutionSetUnitStatus() {} 84 85 // Id implements runner.Context. 86 func (ctx *hookContext) Id() string { return ctx.id } 87 88 // Prepare implements runner.Context. 89 func (ctx *hookContext) Prepare() error { 90 return jujuc.ErrRestrictedContext 91 } 92 93 // Component implements runner.Context. 94 func (ctx *hookContext) Component(name string) (jujuc.ContextComponent, error) { 95 return nil, errors.NotFoundf("context component %q", name) 96 }