diff --git a/lib/middleware.js b/lib/middleware.js index f04d6055..4744c443 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -228,7 +228,6 @@ module.exports = function(srf, logger) { const {rootSpan, application:app} = req.locals; let span; try { - if (app.tasks) { app.tasks = normalizeJambones(logger, app.tasks).map((tdata) => makeTask(logger, tdata)); if (0 === app.tasks.length) throw new Error('no application provided'); @@ -239,7 +238,10 @@ module.exports = function(srf, logger) { req.locals.callInfo); const obj = rootSpan.startChildSpan('performAppWebhook'); span = obj.span; - const json = await app.requestor.request('session:new', app.call_hook, params); + const b3 = rootSpan.getTracingPropagation(); + const httpHeaders = b3 && {b3}; + console.log(httpHeaders); + const json = await app.requestor.request('session:new', app.call_hook, params, httpHeaders); app.tasks = normalizeJambones(logger, json).map((tdata) => makeTask(logger, tdata)); span.setAttributes({ 'http.statusCode': 200, diff --git a/lib/tasks/task.js b/lib/tasks/task.js index d5e9c7ff..26372821 100644 --- a/lib/tasks/task.js +++ b/lib/tasks/task.js @@ -90,6 +90,13 @@ class Task extends Emitter { return {span, ctx}; } + getTracingPropagation(encoding) { + // TODO: support encodings beyond b3 https://github.com/openzipkin/b3-propagation + if (this.span) { + return `${this.span.spanContext().traceId}-${this.span.spanContext().spanId}-1`; + } + } + /** * when a subclass Task has completed its work, it should call this method */ diff --git a/lib/utils/call-tracer.js b/lib/utils/call-tracer.js index 75586cd0..b6b229e8 100644 --- a/lib/utils/call-tracer.js +++ b/lib/utils/call-tracer.js @@ -44,6 +44,21 @@ class RootSpan { return this._span.spanContext().traceId; } + get spanId() { + return this._span.spanContext().spanId; + } + + get traceFlags() { + return this._span.spanContext().traceFlags; + } + + getTracingPropagation(encoding) { + // TODO: support encodings beyond b3 https://github.com/openzipkin/b3-propagation + if (this._span && this.traceId !== '00000000000000000000000000000000') { + return `${this.traceId}-${this.spanId}-1`; + } + } + setAttributes(attrs) { this._span.setAttributes(attrs); } diff --git a/lib/utils/http-requestor.js b/lib/utils/http-requestor.js index 922162be..50ab80d4 100644 --- a/lib/utils/http-requestor.js +++ b/lib/utils/http-requestor.js @@ -49,7 +49,7 @@ class HttpRequestor extends BaseRequestor { * @param {string} [hook.password] - if basic auth is protecting the endpoint * @param {object} [params] - request parameters */ - async request(type, hook, params) { + async request(type, hook, params, httpHeaders = {}) { assert(HookMsgTypes.includes(type)); const payload = params ? snakeCaseKeys(params, ['customerData', 'sip']) : null; const url = hook.url || hook; @@ -64,8 +64,8 @@ class HttpRequestor extends BaseRequestor { let buf; try { const sigHeader = this._generateSigHeader(payload, this.secret); - const headers = {...sigHeader, ...this.authHeader}; - //this.logger.info({url, headers}, 'send webhook'); + const headers = {...sigHeader, ...this.authHeader, ...httpHeaders}; + this.logger.debug({url, headers}, 'send webhook'); buf = this._isRelativeUrl(url) ? await this.post(url, payload, headers) : await bent(method, 'buffer', 200, 201, 202)(url, payload, headers);