Request ID

Request ID middleware generates a unique id for a request.

Usage

  1. e.Use(middleware.RequestID())

Example

  1. e := echo.New()
  2. e.Use(middleware.RequestID())
  3. e.GET("/", func(c echo.Context) error {
  4. return c.String(http.StatusOK, c.Response().Header().Get(echo.HeaderXRequestID))
  5. })
  6. e.Logger.Fatal(e.Start(":1323"))

Custom Configuration

Usage

  1. e.Use(middleware.RequestIDWithConfig(middleware.RequestIDConfig{
  2. Generator: func() string {
  3. return customGenerator()
  4. },
  5. }))

Configuration

  1. RequestIDConfig struct {
  2. // Skipper defines a function to skip middleware.
  3. Skipper Skipper
  4. // Generator defines a function to generate an ID.
  5. // Optional. Default value random.String(32).
  6. Generator func() string
  7. // RequestIDHandler defines a function which is executed for a request id.
  8. RequestIDHandler func(echo.Context, string)
  9. // TargetHeader defines what header to look for to populate the id
  10. TargetHeader string
  11. }

Default Configuration

  1. DefaultRequestIDConfig = RequestIDConfig{
  2. Skipper: DefaultSkipper,
  3. Generator: generator,
  4. TargetHeader: echo.HeaderXRequestID,
  5. }

Set ID

You can set the id from the requester with the X-Request-ID-Header

Request

  1. curl -H "X-Request-ID: 3" --compressed -v "http://localhost:1323/?my=param"

Log

  1. {"time":"2017-11-13T20:26:28.6438003+01:00","id":"3","remote_ip":"::1","host":"localhost:1323","method":"GET","uri":"/?my=param","my":"param","status":200, "latency":0,"latency_human":"0s","bytes_in":0,"bytes_out":13}