diff options
| -rw-r--r-- | gomplate.go | 15 | ||||
| -rw-r--r-- | gomplate_test.go | 3 | ||||
| -rw-r--r-- | metrics.go | 24 |
3 files changed, 41 insertions, 1 deletions
diff --git a/gomplate.go b/gomplate.go index debfed74..e698f03e 100644 --- a/gomplate.go +++ b/gomplate.go @@ -3,6 +3,7 @@ package gomplate import ( "io" "text/template" + "time" "github.com/hairyhenderson/gomplate/data" ) @@ -59,6 +60,7 @@ func newGomplate(d *data.Data, leftDelim, rightDelim string) *gomplate { // RunTemplates - run all gomplate templates specified by the given configuration func RunTemplates(o *Config) error { + Metrics = newMetrics() defer runCleanupHooks() d := data.NewData(o.DataSources, o.DataSourceHeaders) addCleanupHook(d.Cleanup) @@ -69,14 +71,25 @@ func RunTemplates(o *Config) error { } func (g *gomplate) runTemplates(o *Config) error { + start := time.Now() tmpl, err := gatherTemplates(o) + Metrics.GatherDuration = time.Now().Sub(start) if err != nil { + Metrics.Errors++ return err } + Metrics.TemplatesGathered = len(tmpl) + start = time.Now() + defer func() { Metrics.TotalRenderDuration = time.Now().Sub(start) }() for _, t := range tmpl { - if err := g.runTemplate(t); err != nil { + tstart := time.Now() + err := g.runTemplate(t) + Metrics.RenderDuration[t.name] = time.Now().Sub(tstart) + if err != nil { + Metrics.Errors++ return err } + Metrics.TemplatesProcessed++ } return nil } diff --git a/gomplate_test.go b/gomplate_test.go index f97487bc..5dc81ad9 100644 --- a/gomplate_test.go +++ b/gomplate_test.go @@ -167,4 +167,7 @@ func TestRunTemplates(t *testing.T) { err := RunTemplates(config) assert.NoError(t, err) assert.Equal(t, "foo", buf.String()) + assert.Equal(t, 1, Metrics.TemplatesGathered) + assert.Equal(t, 1, Metrics.TemplatesProcessed) + assert.Equal(t, 0, Metrics.Errors) } diff --git a/metrics.go b/metrics.go new file mode 100644 index 00000000..d7e83d3e --- /dev/null +++ b/metrics.go @@ -0,0 +1,24 @@ +package gomplate + +import "time" + +// Metrics tracks interesting basic metrics around gomplate executions. Warning: experimental! +// This may change in breaking ways without warning. This is not subject to any semantic versioning guarantees! +var Metrics *MetricsType + +// MetricsType - Warning: experimental! This may change in breaking ways without warning. +// This is not subject to any semantic versioning guarantees! +type MetricsType struct { + TemplatesGathered int + TemplatesProcessed int + Errors int + GatherDuration time.Duration // time it took to gather templates + TotalRenderDuration time.Duration // time it took to render all templates + RenderDuration map[string]time.Duration // times for rendering each template +} + +func newMetrics() *MetricsType { + return &MetricsType{ + RenderDuration: make(map[string]time.Duration), + } +} |
