diff --git a/lib/instrumentation/http/http-request-trace-builder.js b/lib/instrumentation/http/http-request-trace-builder.js index b7795f93..ca26a148 100644 --- a/lib/instrumentation/http/http-request-trace-builder.js +++ b/lib/instrumentation/http/http-request-trace-builder.js @@ -41,7 +41,7 @@ class IncomingMessageHeaderReader { } getRemoteAddress() { - return this.getHeader('x-forwarded-for')?.split(',').trim().shift() ?? this.socket?.remoteAddress + return this.getHeader('x-forwarded-for')?.split(',').trim().shift() ?? this.request.socket?.remoteAddress?.replace?.('::ffff:', '') } getQuery() { diff --git a/test/instrumentation/context/nested-async-trace.test.js b/test/instrumentation/context/nested-async-trace.test.js index 026a5c0f..97b73d72 100644 --- a/test/instrumentation/context/nested-async-trace.test.js +++ b/test/instrumentation/context/nested-async-trace.test.js @@ -360,6 +360,7 @@ test(`nested mysql2 async query with express`, async (t) => { agent.callbackTraceClose(async (trace) => { t.equal(trace.spanBuilder.serviceType, ServiceType.node.getCode(), 'Span serviceType is node') + t.true(trace.spanBuilder.remoteAddress === '127.0.0.1' || trace.spanBuilder.remoteAddress === '::1', `remoteAddress is ${trace.spanBuilder.remoteAddress}`) let actualBuilder = new MethodDescriptorBuilder(expected('get', 'app.get')) .setClassName(expected('app', 'Function')) .setLineNumber(319) @@ -453,6 +454,9 @@ test(`nested mysql2 async query with express`, async (t) => { t.equal(actualSpanChunk.spanEventList[0].serviceType, ServiceType.async.getCode(), 'serviceType is mysql') spanMessageEndEventCallback = async () => { + const trace = agent.getTraces()[0] + t.true(trace.spanBuilder.remoteAddress === '127.0.0.1' || trace.spanBuilder.remoteAddress === '::1', `remoteAddress is ${trace.spanBuilder.remoteAddress}`) + connection.end() t.equal(agent.getSendedApiMetaInfos().length, 0, 'agent.getSendedApiMetaInfos() is empty') t.end()