add b3 header for trace propagation on initial webhook

This commit is contained in:
Dave Horton
2022-03-30 15:12:44 -04:00
parent ff8d7f3648
commit dbe925af05
4 changed files with 29 additions and 5 deletions

View File

@@ -228,7 +228,6 @@ module.exports = function(srf, logger) {
const {rootSpan, application:app} = req.locals; const {rootSpan, application:app} = req.locals;
let span; let span;
try { try {
if (app.tasks) { if (app.tasks) {
app.tasks = normalizeJambones(logger, app.tasks).map((tdata) => makeTask(logger, tdata)); app.tasks = normalizeJambones(logger, app.tasks).map((tdata) => makeTask(logger, tdata));
if (0 === app.tasks.length) throw new Error('no application provided'); if (0 === app.tasks.length) throw new Error('no application provided');
@@ -239,7 +238,10 @@ module.exports = function(srf, logger) {
req.locals.callInfo); req.locals.callInfo);
const obj = rootSpan.startChildSpan('performAppWebhook'); const obj = rootSpan.startChildSpan('performAppWebhook');
span = obj.span; 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)); app.tasks = normalizeJambones(logger, json).map((tdata) => makeTask(logger, tdata));
span.setAttributes({ span.setAttributes({
'http.statusCode': 200, 'http.statusCode': 200,

View File

@@ -90,6 +90,13 @@ class Task extends Emitter {
return {span, ctx}; 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 * when a subclass Task has completed its work, it should call this method
*/ */

View File

@@ -44,6 +44,21 @@ class RootSpan {
return this._span.spanContext().traceId; 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) { setAttributes(attrs) {
this._span.setAttributes(attrs); this._span.setAttributes(attrs);
} }

View File

@@ -49,7 +49,7 @@ class HttpRequestor extends BaseRequestor {
* @param {string} [hook.password] - if basic auth is protecting the endpoint * @param {string} [hook.password] - if basic auth is protecting the endpoint
* @param {object} [params] - request parameters * @param {object} [params] - request parameters
*/ */
async request(type, hook, params) { async request(type, hook, params, httpHeaders = {}) {
assert(HookMsgTypes.includes(type)); assert(HookMsgTypes.includes(type));
const payload = params ? snakeCaseKeys(params, ['customerData', 'sip']) : null; const payload = params ? snakeCaseKeys(params, ['customerData', 'sip']) : null;
const url = hook.url || hook; const url = hook.url || hook;
@@ -64,8 +64,8 @@ class HttpRequestor extends BaseRequestor {
let buf; let buf;
try { try {
const sigHeader = this._generateSigHeader(payload, this.secret); const sigHeader = this._generateSigHeader(payload, this.secret);
const headers = {...sigHeader, ...this.authHeader}; const headers = {...sigHeader, ...this.authHeader, ...httpHeaders};
//this.logger.info({url, headers}, 'send webhook'); this.logger.debug({url, headers}, 'send webhook');
buf = this._isRelativeUrl(url) ? buf = this._isRelativeUrl(url) ?
await this.post(url, payload, headers) : await this.post(url, payload, headers) :
await bent(method, 'buffer', 200, 201, 202)(url, payload, headers); await bent(method, 'buffer', 200, 201, 202)(url, payload, headers);