Skip to content

Commit

Permalink
Refactor for v2
Browse files Browse the repository at this point in the history
Signed-off-by: Xabier Larrakoetxea <me@slok.dev>
  • Loading branch information
slok committed May 28, 2020
1 parent ee4c4b8 commit 0b9edc8
Show file tree
Hide file tree
Showing 33 changed files with 651 additions and 388 deletions.
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@

## [Unreleased]

Breaking change. The library has been refactored to me more flexible when adding new framework/libraries.

### Added

- New middleware helper for the Echo framework
- New middleware helper for the Echo framework.

### Changed

- Refactored internally how the Middleware works and gets the data to make it easier to extend and more reliable.
- Added `Reporter` interface as the service responsible of getting the data to be measured.
- All different framwork helpers now implement with the new Reporter way.
- Fixed Gin returning duplicated data (#31).
- Standard handler now is on `middleware/std` instead of `middleware`.

### Removed

- Middleware interface in favor of a struct.

## [0.6.1] - 2020-02-07

Expand Down
3 changes: 2 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ the main Go net/http handler:
"github.com/prometheus/client_golang/prometheus/promhttp"
httpmetrics "github.com/slok/go-http-metrics/metrics/prometheus"
httpmiddleware "github.com/slok/go-http-metrics/middleware"
httpstdmiddleware "github.com/slok/go-http-metrics/middleware/std"
)
func main() {
Expand All @@ -24,7 +25,7 @@ the main Go net/http handler:
w.WriteHeader(http.StatusOK)
w.Write([]byte("hello world!"))
})
h := mdlw.Handler("", myHandler)
h := httpstdmiddleware.Measure("", mdlw, myHandler)
// Serve metrics.
log.Printf("serving metrics at: %s", ":9090")
Expand Down
9 changes: 5 additions & 4 deletions examples/custom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
metrics "github.com/slok/go-http-metrics/metrics/prometheus"
"github.com/slok/go-http-metrics/middleware"
"github.com/slok/go-http-metrics/middleware/std"
)

const (
Expand Down Expand Up @@ -50,10 +51,10 @@ func main() {
// Wrape our middleware on each of the different handlers with the ID of the handler
// this way we reduce the cardinality, for example: `/test/2` and `/test/4` will
// have the same `handler` label on the metric: `/test/:testID`
mux.Handle("/", mdlw.Handler("/", rooth))
mux.Handle("/test/1", mdlw.Handler("/test/:testID", testh))
mux.Handle("/test/2", mdlw.Handler("/test/:testID", testh2))
mux.Handle("/other-test", mdlw.Handler("/other-test", othetesth))
mux.Handle("/", std.Measure("/", mdlw, rooth))
mux.Handle("/test/1", std.Measure("/test/:testID", mdlw, testh))
mux.Handle("/test/2", std.Measure("/test/:testID", mdlw, testh2))
mux.Handle("/other-test", std.Measure("/other-test", mdlw, othetesth))

// Serve our handler.
go func() {
Expand Down
3 changes: 2 additions & 1 deletion examples/default/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
metrics "github.com/slok/go-http-metrics/metrics/prometheus"
"github.com/slok/go-http-metrics/middleware"
"github.com/slok/go-http-metrics/middleware/std"
)

const (
Expand Down Expand Up @@ -45,7 +46,7 @@ func main() {

// Wrap our main handler, we pass empty handler ID so the middleware inferes
// the handler label from the URL.
h := mdlw.Handler("", mux)
h := std.Measure("", mdlw, mux)

// Serve our handler.
go func() {
Expand Down
5 changes: 4 additions & 1 deletion examples/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ func main() {

// Create Echo instance and global middleware.
e := echo.New()
e.Use(echoMiddleware.Handler("", mdlw))
e.Use(echoMiddleware.Measure("", mdlw))

// Add our handler.
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello world")
})
e.GET("/json", func(c echo.Context) error {
return c.JSON(http.StatusAccepted, map[string]string{"hello": "world"})
})
e.GET("/wrong", func(c echo.Context) error {
return c.String(http.StatusTooManyRequests, "oops")
})
Expand Down
10 changes: 8 additions & 2 deletions examples/gin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ func main() {

// Create Gin engine and global middleware.
engine := gin.New()
engine.Use(ginmiddleware.Handler("", mdlw))
engine.Use(ginmiddleware.Measure("", mdlw))

// Add our handler.
engine.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Hello world")
c.String(http.StatusOK, "Hello %s", "world")
})
engine.GET("/json", func(c *gin.Context) {
c.JSON(http.StatusAccepted, map[string]string{"hello": "world"})
})
engine.GET("/yaml", func(c *gin.Context) {
c.YAML(http.StatusCreated, map[string]string{"hello": "world"})
})
engine.GET("/wrong", func(c *gin.Context) {
c.String(http.StatusTooManyRequests, "oops")
Expand Down
2 changes: 1 addition & 1 deletion examples/gorestful/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
c := gorestful.NewContainer()

// Add the middleware for all routes.
c.Filter(gorestfulmiddleware.Handler("", mdlw))
c.Filter(gorestfulmiddleware.Measure("", mdlw))

// Add our handler.
ws := &gorestful.WebService{}
Expand Down
6 changes: 3 additions & 3 deletions examples/httprouter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ func main() {
r := httprouter.New()

// Add the middleware to each route.
r.GET("/", httproutermiddleware.Handler("/", h, mdlw))
r.GET("/test/:id", httproutermiddleware.Handler("/test/:id", h1, mdlw))
r.GET("/test2/:id", httproutermiddleware.Handler("/test2/:id", h2, mdlw))
r.GET("/", httproutermiddleware.Measure("/", h, mdlw))
r.GET("/test/:id", httproutermiddleware.Measure("/test/:id", h1, mdlw))
r.GET("/test2/:id", httproutermiddleware.Measure("/test2/:id", h2, mdlw))

// Serve our handler.
go func() {
Expand Down
2 changes: 1 addition & 1 deletion examples/negroni/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func main() {
n := negroni.Classic()

// Add the middleware to negroni.
n.Use(negronimiddleware.Handler("", mdlw))
n.Use(negronimiddleware.Measure("", mdlw))

// Finally set our router on negroni.
n.UseHandler(mux)
Expand Down
6 changes: 4 additions & 2 deletions examples/opencensus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (

ocprometheus "contrib.go.opencensus.io/exporter/prometheus"
ocmmetrics "github.com/slok/go-http-metrics/metrics/opencensus"
"github.com/slok/go-http-metrics/middleware"
"go.opencensus.io/stats/view"

"github.com/slok/go-http-metrics/middleware"
"github.com/slok/go-http-metrics/middleware/std"
)

const (
Expand Down Expand Up @@ -52,7 +54,7 @@ func main() {

// Wrap our main handler, we pass empty handler ID so the middleware inferes
// the handler label from the URL.
h := mdlw.Handler("", mux)
h := std.Measure("", mdlw, mux)

// Serve our handler.
go func() {
Expand Down
1 change: 1 addition & 0 deletions internal/mocks/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ Package mocks will have all the mocks of the library.
package mocks // import "github.com/slok/go-http-metrics/internal/mocks"

//go:generate mockery -output ./metrics -outpkg metrics -dir ../../metrics -name Recorder
//go:generate mockery -output ./middleware -outpkg middleware -dir ../../middleware -name Reporter
12 changes: 8 additions & 4 deletions internal/mocks/metrics/Recorder.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 86 additions & 0 deletions internal/mocks/middleware/Reporter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 27 additions & 8 deletions middleware/echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,36 @@
package echo

import (
"net/http"
"context"

"github.com/labstack/echo/v4"
"github.com/slok/go-http-metrics/middleware"
)

// Handler returns a Echo compatible middleware from a Middleware factory instance.
// The first handlerID argument is the same argument passed on Middleware.Handler method.
func Handler(handlerID string, m middleware.Middleware) echo.MiddlewareFunc {
// Wrap wrapping handler with echo's WrapMiddleware helper
return echo.WrapMiddleware(func(next http.Handler) http.Handler {
return m.Handler(handlerID, next)
})
// Measure returns a Echo measure middleware.
func Measure(handlerID string, m middleware.Middleware) echo.MiddlewareFunc {
return func(h echo.HandlerFunc) echo.HandlerFunc {
return echo.HandlerFunc(func(c echo.Context) error {
r := &reporter{c: c}
var err error
m.Measure(handlerID, r, func() {
err = h(c)
})
return err
})
}
}

type reporter struct {
c echo.Context
}

func (r *reporter) Method() string { return r.c.Request().Method }

func (r *reporter) Context() context.Context { return r.c.Request().Context() }

func (r *reporter) URLPath() string { return r.c.Request().URL.Path }

func (r *reporter) StatusCode() int { return r.c.Response().Status }

func (r *reporter) BytesWritten() int64 { return r.c.Response().Size }
12 changes: 5 additions & 7 deletions middleware/echo/echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ func getTestHandler(statusCode int) echo.HandlerFunc {
}

func TestMiddlewareIntegration(t *testing.T) {
tests := []struct {
name string
tests := map[string]struct {
handlerID string
statusCode int
req *http.Request
Expand All @@ -33,8 +32,7 @@ func TestMiddlewareIntegration(t *testing.T) {
expMethod string
expStatusCode string
}{
{
name: "A default HTTP middleware should call the recorder to measure.",
"A default HTTP middleware should call the recorder to measure.": {
statusCode: http.StatusAccepted,
req: httptest.NewRequest(http.MethodPost, "/test", nil),
expHandlerID: "/test",
Expand All @@ -43,8 +41,8 @@ func TestMiddlewareIntegration(t *testing.T) {
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
for name, test := range tests {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)

// Mocks.
Expand All @@ -67,7 +65,7 @@ func TestMiddlewareIntegration(t *testing.T) {
// Create our echo instance with the middleware.
mdlw := middleware.New(middleware.Config{Recorder: mr})
e := echo.New()
e.POST("/test", getTestHandler(test.statusCode), echoMiddleware.Handler("", mdlw))
e.POST("/test", getTestHandler(test.statusCode), echoMiddleware.Measure("", mdlw))

// Make the request.
resp := httptest.NewRecorder()
Expand Down
2 changes: 1 addition & 1 deletion middleware/echo/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Example_echoMiddleware() {
h := func(c echo.Context) error {
return c.String(http.StatusOK, "Hello world")
}
e.GET("/", h, echoMiddleware.Handler("", mdlw))
e.GET("/", h, echoMiddleware.Measure("", mdlw))

// Serve metrics from the default prometheus registry.
log.Printf("serving metrics at: %s", ":8081")
Expand Down
2 changes: 1 addition & 1 deletion middleware/gin/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Example_ginMiddleware() {
h := func(c *gin.Context) {
c.String(http.StatusOK, "Hello world")
}
engine.GET("/", ginmiddleware.Handler("", mdlw), h)
engine.GET("/", ginmiddleware.Measure("", mdlw), h)

// Serve metrics from the default prometheus registry.
log.Printf("serving metrics at: %s", ":8081")
Expand Down
Loading

0 comments on commit 0b9edc8

Please sign in to comment.