Skip to content

Commit

Permalink
Merge pull request #20 from SimonRichardson/metrics
Browse files Browse the repository at this point in the history
#20

The following starts to expose the events of a worker, so that we can
better observe the changes of a juju instance. The only one that seems
relevant at the moment, is how many times a start is done, but this can
easily be expanded at a later date.

I chose RecordStart to be explicit, rather than picking Record with a
state. Although changing that in the future should be really easy.
  • Loading branch information
jujubot authored Dec 2, 2021
2 parents 7586be9 + e41ec58 commit a08892a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
17 changes: 17 additions & 0 deletions dependency/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ type EngineConfig struct {
// Clock will be a wall clock for production, and test clocks for tests.
Clock Clock

// Metrics defines a type for reporting the workers lifecycle in the engine.
// Currently this only supports recording of the number of starts for a
// given worker, but could be expanded to record other measurements.
Metrics Metrics

// Logger is used to provide an implementation for where the logging
// messages go for the runner.
Logger Logger
Expand Down Expand Up @@ -110,6 +115,9 @@ func (config *EngineConfig) Validate() error {
if config.Clock == nil {
return errors.NotValidf("missing Clock")
}
if config.Metrics == nil {
return errors.NotValidf("missing Metrics")
}
if config.Logger == nil {
return errors.NotValidf("missing Logger")
}
Expand All @@ -136,6 +144,8 @@ func NewEngine(config EngineConfig) (*Engine, error) {
started: make(chan startedTicket),
stopped: make(chan stoppedTicket),
report: make(chan reportTicket),

metrics: config.Metrics,
}
engine.tomb.Go(engine.loop)
return engine, nil
Expand Down Expand Up @@ -175,6 +185,10 @@ type Engine struct {
started chan startedTicket
stopped chan stoppedTicket
report chan reportTicket

// Metrics is used to record the number of changes a given worker has
// performed.
metrics Metrics
}

// loop serializes manifold install operations and worker start/stop notifications.
Expand Down Expand Up @@ -564,6 +578,9 @@ func (engine *Engine) gotStarted(name string, worker worker.Worker, resourceLog
engine.config.Logger.Debugf("%q manifold worker started at %v", name, info.startedTime)
engine.current[name] = info

// Record the start of a worker.
engine.metrics.RecordStart(name)

// Any manifold that declares this one as an input needs to be restarted.
engine.bounceDependents(name)
}
Expand Down
5 changes: 5 additions & 0 deletions dependency/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,10 @@ func (s *EngineSuite) TestConfigValidate(c *gc.C) {
func(config *dependency.EngineConfig) {
config.Clock = nil
}, "missing Clock not valid",
}, {
func(config *dependency.EngineConfig) {
config.Metrics = nil
}, "missing Metrics not valid",
}, {
func(config *dependency.EngineConfig) {
config.Logger = nil
Expand All @@ -720,6 +724,7 @@ func (s *EngineSuite) TestConfigValidate(c *gc.C) {
ErrorDelay: time.Second,
BounceDelay: time.Second,
Clock: clock.WallClock,
Metrics: dependency.DefaultMetrics(),
Logger: loggo.GetLogger("test"),
}
test.breakConfig(&config)
Expand Down
21 changes: 21 additions & 0 deletions dependency/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2021 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package dependency

// Metrics defines a type for recording the worker life cycle in the dependency
// engine.
type Metrics interface {
RecordStart(name string)
}

// noopMetrics gives a metric that doesn't do anything.
type noopMetric struct{}

func (noopMetric) RecordStart(name string) {}

// DefaultMetrics returns a metrics implementation that performs no operations,
// but can be used for scenarios where metrics output isn't required.
func DefaultMetrics() Metrics {
return noopMetric{}
}
1 change: 1 addition & 0 deletions dependency/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (fix *engineFixture) defaultEngineConfig(clock clock.Clock) dependency.Engi
MaxDelay: time.Second,
BackoffResetTime: time.Minute,
Clock: clock,
Metrics: dependency.DefaultMetrics(),
Logger: loggo.GetLogger("test"),
}
}
Expand Down

0 comments on commit a08892a

Please sign in to comment.