jbogard / MongoDB.Driver.Core.Extensions.DiagnosticSources

Apache License 2.0
46 stars 18 forks source link

Set the Activity.Current to the current one #1

Closed dgrozenok closed 4 years ago

dgrozenok commented 4 years ago

When Mongo driver asynchronous function is called the driver breaks the async flow by switching to another thread. As a result the Activity.Current is null in the new thread even while the activity is still in progress. The proposed change is in that case to set the Current.Activity to the current activity in that thread too to continue as expected. This scenario is described in more details here: https://github.com/dotnet/runtime/issues/25936.

dgrozenok commented 4 years ago

I was wrong, the fix only works when the Mongo Commands are executed as the root activities. In case of the nested scenario, when, for example, the web request has its own root activity and the Mongo commands are executed as children activities the Activity.Current is not null in the command succeeded event handler, but somehow the http request activity is there, but not the mongo command that already started. So, as a result the OpenTelemetry would give you the Activity.Current which is the http request activity while you are trying to stop the mongo command activity.