From 9664dc284782ab03e9388d72399467d118009294 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Thu, 21 Mar 2019 08:05:23 -0700 Subject: [PATCH] cmd/site: per post view counters --- cmd/site/html.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cmd/site/html.go b/cmd/site/html.go index afb0790..468a149 100644 --- a/cmd/site/html.go +++ b/cmd/site/html.go @@ -5,8 +5,11 @@ import ( "fmt" "html/template" "net/http" + "path/filepath" "time" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "within.website/ln" ) @@ -49,6 +52,11 @@ func (s *Site) renderTemplatePage(templateFname string, data interface{}) http.H }) } +var postView = promauto.NewCounterVec(prometheus.CounterOpts{ + Name: "posts_viewed", + Help: "The number of views per post", +}, []string{"base"}) + func (s *Site) showPost(w http.ResponseWriter, r *http.Request) { if r.RequestURI == "/blog/" { http.Redirect(w, r, "/blog", http.StatusSeeOther) @@ -70,4 +78,5 @@ func (s *Site) showPost(w http.ResponseWriter, r *http.Request) { } s.renderTemplatePage("blogpost.html", p).ServeHTTP(w, r) + postView.With(prometheus.Labels{"base": filepath.Base(p.Link)}).Inc() }