Using System.Diagnostics.Activity
, and given the following C# code:
Activity parentActivity = Activity.Current;
Is there a way to get all Activity
objects which have parentActivity
as their Parent
? Or some other way to get the "tree" of Activity
objects that are contained within the current operation?
CodePudding user response:
I'm afraid it doesn't work this way.
For example, think of distributed tracing in .NET, which is built on top of the Activity API. Distributed tracing is often required for microservice architecture where the services don't really know much about each other. There are requests here and there and the best that services can do is to pass some metadata from the incoming request to the outgoing request (and add some).
These metadata is where such Activity IDs can go but then a service only knows where it wants to send the request (with these metadata) and not where it actually arrives. So a service can't know which Activities will eventually have its Activity ID as a parent Activity ID.
It's just that you can "onboard" different services you own to some Distributed Tracing tooling (like Jaeger) which will build all those beautiful trees you want once the requests are completed and it's known what ended where. But you can't do this in the middle of a cross-service request - you don't know the future :)