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;
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,

View File

@@ -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
*/

View File

@@ -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);
}

View File

@@ -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);