Skip to content

Commit

Permalink
chore(web): Use request ID helper function, remove unneeded code (#21)
Browse files Browse the repository at this point in the history
* Make use of the helper function to grab request ID

* Remove code from request ID middleware that we don't need

* Lint fix
  • Loading branch information
javorszky committed Feb 8, 2023
1 parent f5fc2b0 commit 085b962
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 12 deletions.
4 changes: 3 additions & 1 deletion web/error/handler.go
Expand Up @@ -5,6 +5,8 @@ import (

"github.com/labstack/echo/v4"
"github.com/rs/zerolog"

kitHttp "github.com/suborbital/go-kit/web/http"
)

func Handler(logger zerolog.Logger) echo.HTTPErrorHandler {
Expand All @@ -15,7 +17,7 @@ func Handler(logger zerolog.Logger) echo.HTTPErrorHandler {
}

ll.Err(err).
Str("requestID", c.Request().Header.Get(echo.HeaderXRequestID)).
Str("requestID", kitHttp.RID(c)).
Msg("request returned an error")

he, ok := err.(*echo.HTTPError)
Expand Down
4 changes: 3 additions & 1 deletion web/mid/logger.go
Expand Up @@ -4,6 +4,8 @@ import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/rs/zerolog"

"github.com/suborbital/go-kit/web/http"
)

// Logger middleware configures echo's built in middleware.RequestLoggerWithConfig middleware. The passed in
Expand Down Expand Up @@ -43,7 +45,7 @@ func Logger(l zerolog.Logger, skipPaths []string) echo.MiddlewareFunc {
l.Info().
Str("path", c.Path()).
Str("URI", c.Request().RequestURI).
Str("requestID", c.Response().Header().Get(echo.HeaderXRequestID)).
Str("requestID", http.RID(c)).
Str("method", c.Request().Method).
Msg("request started")
},
Expand Down
10 changes: 0 additions & 10 deletions web/mid/requestid.go
@@ -1,15 +1,11 @@
package mid

import (
"context"

"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)

const RequestIDKey = "requestID"

// UUIDRequestID configures echo's built in request id middleware so that the ID generated is an UUIDv4, and the
// generated request ID is added to the following three parts of the request:
// - the echo.HeaderXRequestID header, this is by default
Expand All @@ -22,11 +18,5 @@ func UUIDRequestID() echo.MiddlewareFunc {
Generator: func() string {
return uuid.New().String()
},
RequestIDHandler: func(c echo.Context, s string) {
c.Set(RequestIDKey, s)
ctx := c.Request().Context()
ctx = context.WithValue(ctx, RequestIDKey, s)
c.SetRequest(c.Request().WithContext(ctx))
},
})
}

0 comments on commit 085b962

Please sign in to comment.