I have a nodeJS application that listens to Azure ServiceBus and would like to correlate all my logs under the same operationId for each message that it handles.
Because this is not a HttpRequest nor am I running an Azure Function app, I believe I do not have the automatic correlation from AppInsights and thus need to do it manually.
I am trying to use the SDK to create a context, log a Trace event, then end the context within the scope of each service bus message received.
The following is what I have so far, but results in a run time error.
/**
* Handles the incoming message from the service bus topic.
* @param messageReceived The incoming received message from the subscribed topic.
*/
public async handleMessage(messageReceived: ServiceBusReceivedMessage) {
const correlationContext = appInsights.startOperation({ traceId: '123', spanId: 'abc' } as SpanContext, 'myASBListener')
appInsights.wrapWithCorrelationContext(() => {
const traceTelem: TraceTelemetry = {
message: 'Processing service bus message',
properties: {
body: messageReceived.body,
},
}
telemetryClient.trackTrace(traceTelem)
}, correlationContext)
}