From b4a14c25fe85c41b79497be27ead128502a4dd7b Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Tue, 26 Sep 2017 13:03:04 -0500 Subject: metrics: Add simple template metrics feature --- tpl/template.go | 7 ++++++- tpl/tplimpl/template.go | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) (limited to 'tpl') diff --git a/tpl/template.go b/tpl/template.go index 7be496df9..bdb917ba9 100644 --- a/tpl/template.go +++ b/tpl/template.go @@ -15,6 +15,7 @@ package tpl import ( "io" + "time" "text/template/parse" @@ -22,6 +23,7 @@ import ( texttemplate "text/template" bp "github.com/gohugoio/hugo/bufferpool" + "github.com/gohugoio/hugo/metrics" ) var ( @@ -66,13 +68,16 @@ type TemplateDebugger interface { // TemplateAdapter implements the TemplateExecutor interface. type TemplateAdapter struct { Template + Metrics metrics.Provider } // Execute executes the current template. The actual execution is performed // by the embedded text or html template, but we add an implementation here so // we can add a timer for some metrics. func (t *TemplateAdapter) Execute(w io.Writer, data interface{}) error { - // TODO(moorereason) metrics fmt.Println("Execute:", t.Name()) + if t.Metrics != nil { + defer t.Metrics.MeasureSince(t.Name(), time.Now()) + } return t.Template.Execute(w, data) } diff --git a/tpl/tplimpl/template.go b/tpl/tplimpl/template.go index a8417819e..f25ff8909 100644 --- a/tpl/tplimpl/template.go +++ b/tpl/tplimpl/template.go @@ -112,15 +112,25 @@ func (t *templateHandler) Lookup(name string) *tpl.TemplateAdapter { // in the text template collection. // The templates are stored without the prefix identificator. name = strings.TrimPrefix(name, textTmplNamePrefix) - return t.text.Lookup(name) + + te := t.text.Lookup(name) + if te != nil { + te.Metrics = t.Deps.Metrics + } + return te } // Look in both if te := t.html.Lookup(name); te != nil { + te.Metrics = t.Deps.Metrics return te } - return t.text.Lookup(name) + te := t.text.Lookup(name) + if te != nil { + te.Metrics = t.Deps.Metrics + } + return te } func (t *templateHandler) clone(d *deps.Deps) *templateHandler { -- cgit v1.2.3