Skip to content

Commit 33ed459

Browse files
committed
Updates after comments from Boris.
1 parent c10588c commit 33ed459

3 files changed

Lines changed: 16 additions & 21 deletions

File tree

src/MongoDB.Driver/Core/Connections/CommandEventHelper.cs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ internal class CommandEventHelper
3636
private readonly EventLogger<LogCategories.Command> _eventLogger;
3737
private ConcurrentDictionary<int, CommandState> _state;
3838

39-
private readonly bool _shouldProcessRequestMessages;
39+
private readonly bool _eventsNeedBeforeSending;
4040
private readonly bool _shouldTrackStarted;
41-
private readonly bool _shouldTrackState;
41+
private readonly bool _eventsNeedState;
4242
private readonly bool _shouldTrackFailed;
4343
private readonly bool _shouldTrackSucceeded;
4444
private readonly bool _tracingDisabled;
@@ -57,26 +57,26 @@ public CommandEventHelper(EventLogger<LogCategories.Command> eventLogger, Tracin
5757
_tracingDisabled = tracingOptions?.Disabled == true;
5858
_queryTextMaxLength = tracingOptions?.QueryTextMaxLength ?? 0;
5959

60-
_shouldTrackState = _shouldTrackSucceeded || _shouldTrackFailed;
61-
_shouldProcessRequestMessages = _shouldTrackStarted || _shouldTrackState;
60+
_eventsNeedState = _shouldTrackSucceeded || _shouldTrackFailed;
61+
_eventsNeedBeforeSending = _shouldTrackStarted || _eventsNeedState;
6262

63-
if (_shouldTrackState)
63+
if (_eventsNeedState)
6464
{
6565
// we only need to track state if we have to raise
6666
// a succeeded or failed event or for tracing
6767
_state = new ConcurrentDictionary<int, CommandState>();
6868
}
6969
}
7070

71-
public bool ShouldCallBeforeSending => _shouldProcessRequestMessages || ShouldTraceWithActivityListener();
71+
public bool ShouldCallBeforeSending => _eventsNeedBeforeSending || ShouldTraceWithActivityListener();
7272

73-
public bool ShouldCallAfterSending => _shouldTrackState || ShouldTraceWithActivityListener();
73+
public bool ShouldCallAfterSending => _eventsNeedState || ShouldTraceWithActivityListener();
7474

75-
public bool ShouldCallErrorSending => _shouldTrackState || ShouldTraceWithActivityListener();
75+
public bool ShouldCallErrorSending => _eventsNeedState || ShouldTraceWithActivityListener();
7676

77-
public bool ShouldCallAfterReceiving => _shouldTrackState || ShouldTraceWithActivityListener();
77+
public bool ShouldCallAfterReceiving => _eventsNeedState || ShouldTraceWithActivityListener();
7878

79-
public bool ShouldCallErrorReceiving => _shouldTrackState || ShouldTraceWithActivityListener();
79+
public bool ShouldCallErrorReceiving => _eventsNeedState || ShouldTraceWithActivityListener();
8080

8181
private bool ShouldTraceWithActivityListener()
8282
=> !_tracingDisabled && MongoTelemetry.ActivitySource.HasListeners();
@@ -742,16 +742,12 @@ private void TrackCommandState(
742742
{
743743
var shouldTraceCommand = ShouldTraceWithActivityListener() && !shouldRedactCommand && !skipLogging;
744744

745-
if (!_shouldTrackState && !shouldTraceCommand)
745+
if (!_eventsNeedState && !shouldTraceCommand)
746746
{
747747
return;
748748
}
749749

750-
if (!_shouldTrackState && shouldTraceCommand)
751-
{
752-
// Lazy initialize state dictionary when tracing is needed but event tracking is not
753-
_state ??= new ConcurrentDictionary<int, CommandState>();
754-
}
750+
_state ??= new ConcurrentDictionary<int, CommandState>();
755751

756752
var commandState = new CommandState
757753
{

tests/MongoDB.Driver.Tests/Core/Connections/CommandEventHelperTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public void ShouldTrackState_should_be_correct(
7878
var tracingOptions = new TracingOptions { Disabled = true };
7979
var commandHelper = new CommandEventHelper(eventLogger, tracingOptions);
8080

81-
// No ActivityListener, so tracing doesn't contribute to _shouldTrackState
82-
commandHelper._shouldTrackState().Should().Be(logCommands || captureCommandSucceeded || captureCommandFailed);
81+
// No ActivityListener, so tracing doesn't contribute to _eventsNeedState
82+
commandHelper._eventsNeedState().Should().Be(logCommands || captureCommandSucceeded || captureCommandFailed);
8383
}
8484

8585
[Theory]
@@ -142,8 +142,8 @@ public void Callbacks_turn_on_when_listener_is_added_even_if_no_events(
142142

143143
internal static class CommandEventHelperReflector
144144
{
145-
public static bool _shouldTrackState(this CommandEventHelper commandEventHelper) =>
146-
(bool)Reflector.GetFieldValue(commandEventHelper, nameof(_shouldTrackState));
145+
public static bool _eventsNeedState(this CommandEventHelper commandEventHelper) =>
146+
(bool)Reflector.GetFieldValue(commandEventHelper, nameof(_eventsNeedState));
147147

148148

149149
public static bool ShouldRedactCommand(BsonDocument command) =>

tests/SmokeTests/MongoDB.Driver.SmokeTests.Sdk/OpenTelemetryTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ namespace MongoDB.Driver.SmokeTests.Sdk
2626
[Trait("Category", "Integration")]
2727
public sealed class OpenTelemetryTests
2828
{
29-
3029
[Fact]
3130
public void MongoClient_should_create_activities_when_tracing_enabled()
3231
{

0 commit comments

Comments
 (0)