From f567049bfb30c9f4102cda4ce5ecf17567320ea9 Mon Sep 17 00:00:00 2001 From: rhonda hollis Date: Fri, 15 May 2026 11:07:09 -0700 Subject: [PATCH] refactor to remove Redis - prevent drachtio from auto passing the non-success response --- lib/middleware.js | 9 --------- lib/session/call-session.js | 19 +++++++++++-------- lib/session/inbound-call-session.js | 11 +++-------- lib/tasks/sip_decline.js | 6 +++++- 4 files changed, 19 insertions(+), 26 deletions(-) diff --git a/lib/middleware.js b/lib/middleware.js index c581c60f..a8ef630c 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -28,7 +28,6 @@ module.exports = function(srf, logger) { registrar, lookupClientByAccountAndUsername } = srf.locals.dbHelpers; - const {addKey} = srf.locals.dbHelpers; const { writeAlerts, AlertType @@ -143,14 +142,6 @@ module.exports = function(srf, logger) { rootSpan }; - /* write a short-lived Redis key mapping the original Call-ID (X-CID) to the traceId, - so sbc-inbound can look it up for CDRs even when a CANCEL is sent to sbc-inbound */ - const externalCallId = req.get('X-CID'); - if (externalCallId) { - addKey(`callid:${externalCallId}`, traceId, 300) - .catch((err) => logger.error(err, 'createRootSpan: error writing traceId to Redis')); - } - /** * end the span on final failure or cancel from caller; * otherwise it will be closed when sip dialog is destroyed diff --git a/lib/session/call-session.js b/lib/session/call-session.js index d1bc8cd9..4a2c29ad 100644 --- a/lib/session/call-session.js +++ b/lib/session/call-session.js @@ -192,6 +192,15 @@ class CallSession extends Emitter { return this.callInfo.applicationSid; } + get _traceHeaders() { + const traceId = this.req?.locals?.traceId; + const applicationSid = this.applicationSid; + return { + ...(traceId && {'X-Trace-ID': traceId}), + ...(applicationSid && {'X-Application-Sid': applicationSid}) + }; + } + get callStatus() { return this.callInfo.callStatus; } @@ -1495,12 +1504,7 @@ class CallSession extends Emitter { else if (opts.call_status === CallStatus.NoAnswer) { if (this.direction === CallDirection.Inbound) { if (this.res && !this.res.finalResponseSent) { - const traceId = this.req?.locals?.traceId; - this.res.send(503, { - headers: { - ...(traceId && {'X-Trace-ID': traceId}) - } - }); + this.res.send(503, {headers: this._traceHeaders}); this._callReleased(); } } @@ -2535,12 +2539,11 @@ Duration=${duration} ` // Send SIP error response immediately for inbound calls if (this.res && !this.res.finalResponseSent) { this.logger.info(`Sending ${err.status} response to SBC due to SipError`); - const traceId = this.req?.locals?.traceId; this.res.send(err.status, { headers: { 'X-Reason': `endpoint allocation failure: ${err.reason || 'Endpoint Allocation Failed'}`, ...(sipReasonHeader && {'Reason': sipReasonHeader}), - ...(traceId && {'X-Trace-ID': traceId}) + ...this._traceHeaders } }); this._notifyCallStatusChange({ diff --git a/lib/session/inbound-call-session.js b/lib/session/inbound-call-session.js index ef69c556..10ca91e5 100644 --- a/lib/session/inbound-call-session.js +++ b/lib/session/inbound-call-session.js @@ -51,14 +51,13 @@ class InboundCallSession extends CallSession { _onTasksDone() { if (!this.res.finalResponseSent) { - const traceId = this.req.locals.traceId; if (this._mediaServerFailure) { this.rootSpan.setAttributes({'call.termination': 'media server failure'}); this.logger.info('InboundCallSession:_onTasksDone generating 480 due to media server failure'); this.res.send(480, { headers: { 'X-Reason': 'crankback: media server failure', - ...(traceId && {'X-Trace-ID': traceId}) + ...this._traceHeaders } }); } @@ -72,18 +71,14 @@ class InboundCallSession extends CallSession { headers: { 'X-Reason': `endpoint allocation failure: ${reason}`, ...(sipReasonHeader && {'Reason': sipReasonHeader}), - ...(traceId && {'X-Trace-ID': traceId}) + ...this._traceHeaders } }); } else { this.rootSpan.setAttributes({'call.termination': 'tasks completed without answering call'}); this.logger.info('InboundCallSession:_onTasksDone auto-generating non-success response to invite'); - this.res.send(603, { - headers: { - ...(traceId && {'X-Trace-ID': traceId}) - } - }); + this.res.send(603, {headers: this._traceHeaders}); } } this.req.removeAllListeners('cancel'); diff --git a/lib/tasks/sip_decline.js b/lib/tasks/sip_decline.js index 3f179983..2b57e07c 100644 --- a/lib/tasks/sip_decline.js +++ b/lib/tasks/sip_decline.js @@ -17,7 +17,11 @@ class TaskSipDecline extends Task { async exec(cs, {res}) { super.exec(cs); res.send(this.data.status, this.data.reason, { - headers: {'X-Reason': 'SIP Decline Verb', ...this.headers} + headers: { + 'X-Reason': 'SIP Decline Verb', + ...this.headers, + ...cs._traceHeaders + } }, (err) => { if (!err) { // Call was successfully declined