From 7ecd8c63d2e87c03aa51efc802c399fc520f6c5b Mon Sep 17 00:00:00 2001 From: Brian Shannon Date: Thu, 10 Nov 2022 13:18:13 +0000 Subject: [PATCH] Better ip lookup within trace --- index.js | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index fe0fa0c..35e140d 100644 --- a/index.js +++ b/index.js @@ -338,7 +338,7 @@ scribbles.middleware = { // if the request is part of a larger sequence // pull the traceparent from the header - express:function correlateMiddleware({headers}, res, next){ + express:function correlateMiddleware({headers, socket, connection, ip}, res, next){ let headersOut = {} if(config.headers){ @@ -388,14 +388,34 @@ scribbles.middleware = { } }) // END forEach }//END config.headersMapping - + let spanLabel = headers['x-forwarded-for'] + if(! spanLabel + && socket + && socket.remoteAddress){ + spanLabel = socket.remoteAddress + } + + if(! spanLabel + && connection){ + if(connection.remoteAddress){ + spanLabel = connection.remoteAddress + }else if(connection.socket + && connection.socket.remoteAddress){ + spanLabel = connection.socket.remoteAddress + } + } + + if(! spanLabel && ip){ + spanLabel = ip + } + scribbles.trace({ // this traceId is embedded within the traceparent traceId:headers.traceparent && headers.traceparent.split('-')[1], tracestate:headers.tracestate, headers:headersOut, // lets tag the current trace/span with the caller's IP - spanLabel:headers['x-forwarded-for'] + spanLabel },(spanId) => next()) } // END express } // END scribbles.middleware