According to the docs around distributed tracing for Servicebus, the Diagnostic-Id
property should be used to store the WC3 traceparent
.
When an Azure Function is triggered via a service bus message, I would expect this value to be used to initialize the tracecontext
(so AppInsight logs would get tagged with the appropriate operation_Id
).
I've even tried to copy it over to the tracecontext and it doesn't seem to work.
Is it not possible to maintain distributed tracing with Service Bus in Azure functions?
Update
You can kinda of get this to work by implementing the wrapped app insights context, however, calls to context.log
will still have the original traceparent
reference :(
export default async function contextPropagatingHttpTrigger(context, req) {
// overwrite with the proper traceparent from the incoming SB message.
const sbTraceParent = context.bindingData.applicationProperties['diagnostic-Id'];
if (sbTraceParent) {
context.traceContext.traceparent = sbTraceParent;
}
const correlationContext = appInsights.startOperation(context, req) as CorrelationContext;
// Wrap the Function runtime with correlationContext
return appInsights.wrapWithCorrelationContext(async () => {
const startTime = Date.now(); // Start trackRequest timer
try {
// operation_Id matches SB 'diagnostic-Id'
appInsights.defaultClient.trackTrace({
message: 'Correct Trace Context',
});
// operation_Id is the original function traceparent
context.log('Incorrect Trace Context');
return await trigger(context, req);
} catch (e) {
context.log.error(e);
throw e;
} finally {
// Track Request on completion
appInsights.defaultClient.flush();
}
}, correlationContext)();
}