Add StartOperation method to expose ContextCorrelationManager.runWithContext for non-HTTP
See original GitHub issueWe leverage Azure Service Bus heavily in our applications. We write a lot of code that semantically looks like this:
while(true){
const message = servicebus.getMessage();
if(message){
ProcessMessage(message);
}
}
function ProcessMessage(message){
request.get('http://some-service/get-stuff', (res) => {
if(res.statusCode == 200){
request.post('http://some-service/do-stuff');
}
});
}
I’d like to integrate Application Insights into these offline workloads. I’d like to:
- Correlate all trace/event/dependency messages produced by processing each message
- Tie each message back to it’s parent operation if it exists.
Similar to this article for .NET.
Looking around I don’t see anything exposed in this SDK to manually establish context.
The ContextCorrelationManager caught my eye but that does not appear to be exposed by the library.
Could you:
- Confirm that we don’t currently have a built-in way of doing this
- If so, provide suggestions for what we might be able to do in the interim?
Many Thanks
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top Results From Across the Web
No results found
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

You’re correct that there isn’t a built-in way of doing this. Your scenario is somewhat similar to what’s described in #386. As I mentioned there, ideally this SDK should add a
startOperationmethod like the .NET SDK has, that will allow you to manually create correlation contexts within your code.In the interim, you can use the code shown in #386 to manually supply
operation_Idandoperation_ParentIdfields, however this may become complicated if your scenario processes several events in parallel, as you’ll need to take advantage of some kind of context propagation to know what the correct id fields are at eachtracktime.A somewhat hacky solution is to take advantage of the SDK’s existing context propagation, even though it’s not currently exposed. While hacky, it shouldn’t break anything. Some sample (untested but the right idea) code:
One thing to note about this approach as-is, is that there isn’t currently a “root” telemetry item (like a Request would be in HTTP server secnarios), so in the transaction details in the Azure Portal UI, your timeline will show all correlated dependencies, but they wont be nested under a parent. If there is a “root” item that makes sense, just record it manually and use its id as the ParentId in
parentIdIfExists.For an async operation I managed to get it working with:
where asyncCallBackFunc is an async function