Skip to content

[BUG] Request ID is empty with go-chi imported with major version suffix #71

@MarshalX

Description

@MarshalX

Describe the bug
chizap can't properly get the request id value from the context due to working with another major version of go-chi

To Reproduce
Steps to reproduce the behavior:

  1. Install go-chi with major version suffix like /v5 (github.com/go-chi/chi/v5)
  2. Build and run small example below
package main

import (
	"fmt"
	"net/http"

	"github.com/go-chi/chi/v5"
	"github.com/go-chi/chi/v5/middleware"
	"go.uber.org/zap"
	"moul.io/chizap"
)

func main() {
	logger, _ := zap.NewProduction()

	r := chi.NewRouter()
	r.Use(middleware.RequestID)
	r.Use(chizap.New(logger, &chizap.Opts{
		WithReferer:   true,
		WithUserAgent: true,
	}))

	r.Get("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Request ID: %s", middleware.GetReqID(r.Context()))
	})

	http.ListenAndServe(":3000", r)
}

Expected behavior
The request id should be presented in logs

Versions (please complete the following information, if relevant):

  • Software version: the latest
  • OS: macOS
  • Golang version 1.19.5

Additional context
I'm new in Go and idk how to contribute to your project, but I maybe know why it doesn't work. Each version of go-chi creates its own type (link to src). My project works on chi/v5 and writes the value to the context by own RequestIDKey. When chizap tries to read the value from the same context it attempts to read the value by RequestIDKey from chi version v1.5.4 (according to your go.mod). It doesn't exist and logs contain the empty request id.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions