From 526d9de73c9b43d19d9cef7df8e75b6ccae5e16c Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Thu, 21 Mar 2019 07:31:49 -0700 Subject: [PATCH] cmd/site: prometheus support --- cmd/site/main.go | 53 +++++++++++++++++++++++++++++++++++++++++------- go.mod | 2 ++ go.sum | 18 ++++++++++++++++ 3 files changed, 66 insertions(+), 7 deletions(-) diff --git a/cmd/site/main.go b/cmd/site/main.go index 14aec02..1690842 100644 --- a/cmd/site/main.go +++ b/cmd/site/main.go @@ -14,6 +14,9 @@ import ( "github.com/Xe/jsonfeed" "github.com/gorilla/feeds" + "github.com/povilasv/prommod" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" blackfriday "github.com/russross/blackfriday" "github.com/tj/front" "within.website/ln" @@ -21,11 +24,46 @@ import ( var port = os.Getenv("PORT") +var ( + requestCounter = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Name: "handler_requests_total", + Help: "Total number of request/responses by HTTP status code.", + }, []string{"handler", "code"}) + + requestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ + Name: "handler_request_duration", + Help: "Handler request duration.", + }, []string{"handler", "method"}) + + requestInFlight = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Name: "handler_requests_in_flight", + Help: "Current number of requests being served.", + }, []string{"handler"}) +) + +func init() { + prometheus.Register(requestCounter) + prometheus.Register(requestDuration) + prometheus.Register(requestInFlight) +} + +func middlewareMetrics(family string, next http.Handler) http.Handler { + return promhttp.InstrumentHandlerDuration( + requestDuration.MustCurryWith(prometheus.Labels{"handler": family}), + promhttp.InstrumentHandlerCounter(requestCounter.MustCurryWith(prometheus.Labels{"handler": family}), + promhttp.InstrumentHandlerInFlight(requestInFlight.With(prometheus.Labels{"handler": family}), next), + ), + ) +} + func main() { if port == "" { port = "29384" } + prometheus.Register(prommod.NewCollector("christine")) + s, err := Build() if err != nil { ln.FatalErr(context.Background(), err, ln.Action("Build")) @@ -173,13 +211,14 @@ func Build() (*Site, error) { s.mux.HandleFunc("/.within/health", func(w http.ResponseWriter, r *http.Request) { http.Error(w, "OK", http.StatusOK) }) - s.mux.Handle("/resume", s.renderTemplatePage("resume.html", s.Resume)) - s.mux.Handle("/blog", s.renderTemplatePage("blogindex.html", s.Posts)) - s.mux.Handle("/contact", s.renderTemplatePage("contact.html", nil)) - s.mux.HandleFunc("/blog.rss", s.createFeed) - s.mux.HandleFunc("/blog.atom", s.createAtom) - s.mux.HandleFunc("/blog.json", s.createJsonFeed) - s.mux.HandleFunc("/blog/", s.showPost) + s.mux.Handle("/metrics", promhttp.Handler()) + s.mux.Handle("/resume", middlewareMetrics("resume", s.renderTemplatePage("resume.html", s.Resume))) + s.mux.Handle("/blog", middlewareMetrics("blog", s.renderTemplatePage("blogindex.html", s.Posts))) + s.mux.Handle("/contact", middlewareMetrics("contact", s.renderTemplatePage("contact.html", nil))) + s.mux.Handle("/blog.rss", middlewareMetrics("blog.rss", http.HandlerFunc(s.createFeed))) + s.mux.Handle("/blog.atom", middlewareMetrics("blog.atom", http.HandlerFunc(s.createAtom))) + s.mux.Handle("/blog.json", middlewareMetrics("blog.json", http.HandlerFunc(s.createJsonFeed))) + s.mux.Handle("/blog/", middlewareMetrics("blogpost", http.HandlerFunc(s.showPost))) s.mux.Handle("/css/", http.FileServer(http.Dir("."))) s.mux.Handle("/static/", http.FileServer(http.Dir("."))) s.mux.HandleFunc("/sw.js", func(w http.ResponseWriter, r *http.Request) { diff --git a/go.mod b/go.mod index 867b2a7..87da3b4 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,8 @@ require ( github.com/kr/pretty v0.1.0 // indirect github.com/pkg/errors v0.8.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/povilasv/prommod v0.0.11 + github.com/prometheus/client_golang v0.9.2 github.com/russross/blackfriday v0.0.0-20170806171014-cadec560ec52 github.com/shurcooL/sanitized_anchor_name v0.0.0-20170918181015-86672fcb3f95 // indirect github.com/st3fan/jsonfeed v0.0.0-20170519104842-498b2850d26b // indirect diff --git a/go.sum b/go.sum index a748d18..ed2a4c2 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,12 @@ github.com/Xe/gopreload v0.0.0-20170326043426-a00a8beb369c h1:lqTJqaoonxgJMvvfl1 github.com/Xe/gopreload v0.0.0-20170326043426-a00a8beb369c/go.mod h1:0aSWHJguPNHo6zlU7A4Ktua1A/VUr5Jdr1QZ2amOkAQ= github.com/Xe/jsonfeed v0.0.0-20170520170432-e21591505612 h1:5cPld6YTMozzm3lK9VCnOErgoFbADM2hZc4KDu0YNKs= github.com/Xe/jsonfeed v0.0.0-20170520170432-e21591505612/go.mod h1:UKXAbYA/G9yE+cPbpfiP7FlT6Kr3N2mbEjQEJ9hT004= +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0= +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/google/gops v0.3.2 h1:n9jMkrye8dh3WQ0IxG5dzLRIhQeZDZoGaj0D7T7x7hQ= github.com/google/gops v0.3.2/go.mod h1:pMQgrscwEK/aUSW1IFSaBPbJX82FPHWaSoJw1axQfD0= github.com/gorilla/feeds v1.0.0 h1:EbkEvaYf+PXhYNHS20heBG7Rl2X6Zy8l11ZBWAHkWqE= @@ -15,12 +19,24 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= +github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/povilasv/prommod v0.0.11 h1:uiCU9z2UpNoMFyd5h3evJB8mTBuT9lZU3CYpMZkkyE0= +github.com/povilasv/prommod v0.0.11/go.mod h1:kMc6cpm22gp7m0cPEFRoRgIzXq75ZIJvNY6GbNu9EJk= +github.com/prometheus/client_golang v0.9.2 h1:awm861/B8OKDd2I/6o1dy3ra4BamzKhYOiGItCeZ740= +github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 h1:idejC8f05m9MGOsuEi1ATq9shN03HrxNkD/luQvxCv8= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/common v0.0.0-20181126121408-4724e9255275 h1:PnBWHBf+6L0jOqq0gIVUe6Yk0/QMZ640k6NvkxcBf+8= +github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= +github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a h1:9a8MnZMP0X2nLJdBg+pBmGgkJlSaKC2KaQmTCk1XDtE= +github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/russross/blackfriday v0.0.0-20170806171014-cadec560ec52 h1:xZ0R4UuR0EDx7m0OmvsqZaomfqCeYJ/PyLm94WLhnIM= github.com/russross/blackfriday v0.0.0-20170806171014-cadec560ec52/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/shurcooL/sanitized_anchor_name v0.0.0-20170918181015-86672fcb3f95 h1:/vdW8Cb7EXrkqWGufVMES1OH2sU9gKVb2n9/1y5NMBY= @@ -32,7 +48,9 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/tj/front v0.0.0-20170212063142-739be213b0a1 h1:lA+aPRvltlx2fwv/BnxyYSDQo3pIeqzHgMO5GvK0T9E= github.com/tj/front v0.0.0-20170212063142-739be213b0a1/go.mod h1:deJrtusCTptAW4EUn5vBLpl3dhNqPqUwEjWJz5UNxpQ= golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0 h1:POO/ycCATvegFmVuPpQzZFJ+pGZeX22Ufu6fibxDVjU=