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

feat: add request-ids to each request #149

Open
wants to merge 1 commit into
base: main
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
7 changes: 4 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"github.com/Layr-Labs/eigenda-proxy/commitments"
"github.com/Layr-Labs/eigenda-proxy/metrics"
"github.com/Layr-Labs/eigenda-proxy/store"
"github.com/Layr-Labs/eigenda-proxy/utils"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/log"
)
Expand Down Expand Up @@ -87,6 +88,9 @@
log log.Logger,
) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
ctx := utils.ContextWithNewRequestID(r.Context())
r = r.WithContext(ctx)
log = utils.RequestLogger(ctx, log)
log.Info("request", "method", r.Method, "url", r.URL)
err := handleFn(w, r)
if err != nil { // #nosec G104
Expand Down Expand Up @@ -272,18 +276,15 @@
}
}

func (svr *Server) WriteInternalError(w http.ResponseWriter, err error) {

Check failure on line 279 in server/server.go

View workflow job for this annotation

GitHub Actions / Linter

unused-parameter: parameter 'err' seems to be unused, consider removing or renaming it as _ (revive)
svr.log.Error("internal server error", "err", err)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing these because of duplicate logs (also printed in middleware). However this means we lose the error/info + status message.......

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking switching to echo webserver as in #142 would be a really good idea at this point.... @epociask let's discuss tomorrow

w.WriteHeader(http.StatusInternalServerError)
}

func (svr *Server) WriteNotFound(w http.ResponseWriter, err error) {

Check failure on line 283 in server/server.go

View workflow job for this annotation

GitHub Actions / Linter

unused-parameter: parameter 'err' seems to be unused, consider removing or renaming it as _ (revive)
svr.log.Info("not found", "err", err)
w.WriteHeader(http.StatusNotFound)
}

func (svr *Server) WriteBadRequest(w http.ResponseWriter, err error) {

Check failure on line 287 in server/server.go

View workflow job for this annotation

GitHub Actions / Linter

unused-parameter: parameter 'err' seems to be unused, consider removing or renaming it as _ (revive)
svr.log.Info("bad request", "err", err)
w.WriteHeader(http.StatusBadRequest)
}

Expand Down
36 changes: 36 additions & 0 deletions utils/request_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package utils

import (
"context"

"github.com/ethereum/go-ethereum/log"
"github.com/google/uuid"
)

type ctxKey string

const (
// requestIDKey is the context key for the request ID
// used both in the context and in the logger
requestIDKey ctxKey = "requestID"
)

// getRequestID retrieves the request ID from the context
func getRequestID(ctx context.Context) (string, bool) {
requestID, ok := ctx.Value(requestIDKey).(string)
return requestID, ok
}

// RequestLogger returns a new logger with the requestID added as a key-value pair
func RequestLogger(ctx context.Context, log log.Logger) log.Logger {
requestID, ok := getRequestID(ctx)
if !ok {
return log
}
return log.With("requestID", requestID)
}

func ContextWithNewRequestID(ctx context.Context) context.Context {
requestID := uuid.New().String()
return context.WithValue(ctx, requestIDKey, requestID)
}
Loading