Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the handlerID to various request contexts for downstream use #127

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor/
.test_coverage.txt
.test_coverage.txt
.idea
1 change: 1 addition & 0 deletions middleware/echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
func Handler(handlerID string, m middleware.Middleware) echo.MiddlewareFunc {
return func(h echo.HandlerFunc) echo.HandlerFunc {
return echo.HandlerFunc(func(c echo.Context) error {
c.Set(middleware.HandlerIDCtx, handlerID)
r := &reporter{c: c}
var err error
m.Measure(handlerID, r, func() {
Expand Down
1 change: 1 addition & 0 deletions middleware/fasthttp/fasthttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
// Handler returns a fasthttp measuring middleware.
func Handler(handlerID string, m middleware.Middleware, next fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(c *fasthttp.RequestCtx) {
c.SetUserValue(middleware.HandlerIDCtx, handlerID)
m.Measure(handlerID, reporter{c}, func() {
next(c)
})
Expand Down
1 change: 1 addition & 0 deletions middleware/gin/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
// Handler returns a Gin measuring middleware.
func Handler(handlerID string, m middleware.Middleware) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set(middleware.HandlerIDCtx, handlerID)
r := &reporter{c: c}
m.Measure(handlerID, r, func() {
c.Next()
Expand Down
1 change: 1 addition & 0 deletions middleware/gorestful/gorestful.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
// Handler returns a gorestful measuring middleware.
func Handler(handlerID string, m middleware.Middleware) gorestful.FilterFunction {
return func(req *gorestful.Request, resp *gorestful.Response, chain *gorestful.FilterChain) {
req.SetAttribute(middleware.HandlerIDCtx, handlerID)
r := &reporter{req: req, resp: resp}
m.Measure(handlerID, r, func() {
chain.ProcessFilter(req, resp)
Expand Down
5 changes: 4 additions & 1 deletion middleware/httprouter/httprouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package httprouter

import (
"context"
"net/http"

"github.com/julienschmidt/httprouter"
Expand All @@ -15,7 +16,9 @@ func Handler(handlerID string, next httprouter.Handle, m middleware.Middleware)
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
// Dummy handler to wrap httprouter Handle type.
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next(w, r, p)
ctx := context.WithValue(r.Context(), middleware.HandlerIDCtx, handlerID)
req := r.WithContext(ctx)
next(w, req, p)
})

std.Handler(handlerID, m, h).ServeHTTP(w, r)
Expand Down
3 changes: 3 additions & 0 deletions middleware/iris/iris.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
// Handler returns a Iris measuring middleware.
func Handler(handlerID string, m middleware.Middleware) iris.Handler {
return func(ctx iris.Context) {
c := context.WithValue(ctx.Request().Context(), middleware.HandlerIDCtx, handlerID)
req := ctx.Request().WithContext(c)
ctx.ResetRequest(req)
r := &reporter{ctx: ctx}
m.Measure(handlerID, r, func() {
ctx.Next()
Expand Down
2 changes: 2 additions & 0 deletions middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/slok/go-http-metrics/metrics"
)

const HandlerIDCtx = "HandlerIDCtx"

// Config is the configuration for the middleware factory.
type Config struct {
// Recorder is the way the metrics will be recorder in the different backends.
Expand Down
5 changes: 4 additions & 1 deletion middleware/negroni/negroni.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package negroni

import (
"context"
"net/http"

"github.com/urfave/negroni"
Expand All @@ -13,6 +14,8 @@ import (
// Handler returns a Negroni measuring middleware.
func Handler(handlerID string, m middleware.Middleware) negroni.Handler {
return negroni.HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
std.Handler(handlerID, m, next).ServeHTTP(rw, r)
ctx := context.WithValue(r.Context(), middleware.HandlerIDCtx, handlerID)
req := r.WithContext(ctx)
std.Handler(handlerID, m, next).ServeHTTP(rw, req)
})
}
6 changes: 4 additions & 2 deletions middleware/std/std.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ import (
// Handler returns an measuring standard http.Handler.
func Handler(handlerID string, m middleware.Middleware, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), middleware.HandlerIDCtx, handlerID)
req := r.WithContext(ctx)
wi := &responseWriterInterceptor{
statusCode: http.StatusOK,
ResponseWriter: w,
}
reporter := &stdReporter{
w: wi,
r: r,
r: req,
}

m.Measure(handlerID, reporter, func() {
h.ServeHTTP(wi, r)
h.ServeHTTP(wi, req)
})
})
}
Expand Down