aboutsummaryrefslogtreecommitdiff
path: root/cmd/site/internal/middleware
diff options
context:
space:
mode:
authorChristine Dodrill <me@christine.website>2019-12-09 10:48:40 -0500
committerGitHub <noreply@github.com>2019-12-09 10:48:40 -0500
commiteb26857c1d5973bedc91c3fc1acaf4434809bbd5 (patch)
tree926be802355da24afb3cf6ed8496a2b2d656e1a1 /cmd/site/internal/middleware
parent583cf248b3fe7ebb06d89ba32fddeee70fb14c2c (diff)
downloadxesite-eb26857c1d5973bedc91c3fc1acaf4434809bbd5.tar.xz
xesite-eb26857c1d5973bedc91c3fc1acaf4434809bbd5.zip
Within package layout (#102)
* blog: go package layout * eat my own dogfood * internal: test date * blog/go-package-layout: streamline * oops
Diffstat (limited to 'cmd/site/internal/middleware')
-rw-r--r--cmd/site/internal/middleware/metrics.go43
-rw-r--r--cmd/site/internal/middleware/requestid.go31
2 files changed, 74 insertions, 0 deletions
diff --git a/cmd/site/internal/middleware/metrics.go b/cmd/site/internal/middleware/metrics.go
new file mode 100644
index 0000000..f9d7e0f
--- /dev/null
+++ b/cmd/site/internal/middleware/metrics.go
@@ -0,0 +1,43 @@
+package middleware
+
+import (
+ "net/http"
+
+ "github.com/prometheus/client_golang/prometheus"
+ "github.com/prometheus/client_golang/prometheus/promhttp"
+)
+
+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)
+}
+
+// Metrics captures request duration, request count and in-flight request count
+// metrics for HTTP handlers. The family field is used to discriminate handlers.
+func Metrics(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),
+ ),
+ )
+}
diff --git a/cmd/site/internal/middleware/requestid.go b/cmd/site/internal/middleware/requestid.go
new file mode 100644
index 0000000..6914137
--- /dev/null
+++ b/cmd/site/internal/middleware/requestid.go
@@ -0,0 +1,31 @@
+package middleware
+
+import (
+ "net/http"
+
+ "github.com/celrenheit/sandflake"
+ "within.website/ln"
+)
+
+// RequestID appends a unique (sandflake) request ID to each request's
+// X-Request-Id header field, much like Heroku's router does.
+func RequestID(next http.Handler) http.Handler {
+ var g sandflake.Generator
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ id := g.Next().String()
+
+ if rid := r.Header.Get("X-Request-Id"); rid != "" {
+ id = rid + "," + id
+ }
+
+ ctx := ln.WithF(r.Context(), ln.F{
+ "request_id": id,
+ })
+ r = r.WithContext(ctx)
+
+ w.Header().Set("X-Request-Id", id)
+ r.Header.Set("X-Request-Id", id)
+
+ next.ServeHTTP(w, r)
+ })
+}