Skip to content

Commit 0e797c5

Browse files
committed
add x-forward-for message in log
1 parent 3f3f8d0 commit 0e797c5

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

rpc/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,9 @@ func (c *Client) dispatch(codec ServerCodec) {
552552
// Read path:
553553
case op := <-c.readOp:
554554
if op.batch {
555-
conn.handler.handleBatch(op.msgs)
555+
conn.handler.handleBatch(context.Background(), op.msgs)
556556
} else {
557-
conn.handler.handleMsg(op.msgs[0])
557+
conn.handler.handleMsg(context.Background(), op.msgs[0])
558558
}
559559

560560
case err := <-c.readErr:

rpc/handler.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func newHandler(connCtx context.Context, conn jsonWriter, idgen func() ID, reg *
9393
}
9494

9595
// handleBatch executes all messages in a batch and returns the responses.
96-
func (h *handler) handleBatch(msgs []*jsonrpcMessage) {
96+
func (h *handler) handleBatch(ctx context.Context, msgs []*jsonrpcMessage) {
9797
// Emit error response for empty batches:
9898
if len(msgs) == 0 {
9999
h.startCallProc(func(cp *callProc) {
@@ -116,7 +116,7 @@ func (h *handler) handleBatch(msgs []*jsonrpcMessage) {
116116
h.startCallProc(func(cp *callProc) {
117117
answers := make([]*jsonrpcMessage, 0, len(msgs))
118118
for _, msg := range calls {
119-
if answer := h.handleCallMsg(cp, msg); answer != nil {
119+
if answer := h.handleCallMsg(cp, ctx, msg); answer != nil {
120120
answers = append(answers, answer)
121121
}
122122
}
@@ -131,12 +131,12 @@ func (h *handler) handleBatch(msgs []*jsonrpcMessage) {
131131
}
132132

133133
// handleMsg handles a single message.
134-
func (h *handler) handleMsg(msg *jsonrpcMessage) {
134+
func (h *handler) handleMsg(ctx context.Context, msg *jsonrpcMessage) {
135135
if ok := h.handleImmediate(msg); ok {
136136
return
137137
}
138138
h.startCallProc(func(cp *callProc) {
139-
answer := h.handleCallMsg(cp, msg)
139+
answer := h.handleCallMsg(cp, ctx, msg)
140140
h.addSubscriptions(cp.notifiers)
141141
if answer != nil {
142142
h.conn.writeJSON(cp.ctx, answer)
@@ -287,7 +287,7 @@ func (h *handler) handleResponse(msg *jsonrpcMessage) {
287287
}
288288

289289
// handleCallMsg executes a call message and returns the answer.
290-
func (h *handler) handleCallMsg(ctx *callProc, msg *jsonrpcMessage) *jsonrpcMessage {
290+
func (h *handler) handleCallMsg(ctx *callProc, reqCtx context.Context, msg *jsonrpcMessage) *jsonrpcMessage {
291291
start := time.Now()
292292
switch {
293293
case msg.isNotification():
@@ -297,7 +297,8 @@ func (h *handler) handleCallMsg(ctx *callProc, msg *jsonrpcMessage) *jsonrpcMess
297297
case msg.isCall():
298298
resp := h.handleCall(ctx, msg)
299299
if resp.Error != nil {
300-
h.log.Warn("Served "+msg.Method, "reqid", idForLog{msg.ID}, "t", time.Since(start), "err", resp.Error.Message)
300+
xForward := reqCtx.Value("X-Forwarded-For")
301+
h.log.Warn("Served "+msg.Method, "reqid", idForLog{msg.ID}, "t", time.Since(start), "err", resp.Error.Message, "X-Forwarded-For", xForward)
301302
} else {
302303
h.log.Debug("Served "+msg.Method, "reqid", idForLog{msg.ID}, "t", time.Since(start))
303304
}

rpc/http.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
227227
if origin := r.Header.Get("Origin"); origin != "" {
228228
ctx = context.WithValue(ctx, "Origin", origin)
229229
}
230+
if xForward := r.Header.Get("X-Forwarded-For"); xForward != "" {
231+
ctx = context.WithValue(ctx, "X-Forwarded-For", xForward)
232+
}
230233

231234
w.Header().Set("content-type", contentType)
232235
codec := newHTTPServerConn(r, w)

rpc/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ func (s *Server) serveSingleRequest(ctx context.Context, codec ServerCodec) {
109109
return
110110
}
111111
if batch {
112-
h.handleBatch(reqs)
112+
h.handleBatch(ctx, reqs)
113113
} else {
114-
h.handleMsg(reqs[0])
114+
h.handleMsg(ctx, reqs[0])
115115
}
116116
}
117117

0 commit comments

Comments
 (0)