Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Activity.IsStopped Property #63713

Merged
merged 4 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public string? Id
}

public bool IsAllDataRequested { get { throw null; } set { throw null; } }
public bool IsStopped { get { throw null; } }
public System.Diagnostics.ActivityIdFormat IdFormat { get { throw null; } }
public System.Diagnostics.ActivityKind Kind { get { throw null; } }
public string OperationName { get { throw null; } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -684,9 +684,9 @@ public void Stop()
return;
}

if (!IsFinished)
if (!IsStopped)
{
IsFinished = true;
IsStopped = true;

if (Duration == TimeSpan.Zero)
{
Expand Down Expand Up @@ -951,7 +951,7 @@ internal static bool TryConvertIdToContext(string traceParent, string? traceStat
/// </summary>
public void Dispose()
{
if (!IsFinished)
if (!IsStopped)
{
Stop();
}
Expand Down Expand Up @@ -1232,7 +1232,7 @@ private static unsafe long GetRandomNumber()

private static bool ValidateSetCurrent(Activity? activity)
{
bool canSet = activity == null || (activity.Id != null && !activity.IsFinished);
bool canSet = activity == null || (activity.Id != null && !activity.IsStopped);
if (!canSet)
{
NotifyError(new InvalidOperationException(SR.ActivityNotRunning));
Expand Down Expand Up @@ -1298,18 +1298,24 @@ private bool W3CIdFlagsSet
get => (_w3CIdFlags & ActivityTraceFlagsIsSet) != 0;
}

private bool IsFinished
/// <summary>
/// Indicates whether this <see cref="Activity"/> object is stopped
/// </summary>
/// <remarks>
/// When subscribing to <see cref="Activity"/> stop event using <see cref="ActivityListener.ActivityStopped"/>, the received <see cref="Activity"/> object in the event callback will have <see cref="IsStopped"/> as true.
/// </remarks>
public bool IsStopped
{
get => (_state & State.IsFinished) != 0;
set
get => (_state & State.IsStopped) != 0;
private set
{
if (value)
{
_state |= State.IsFinished;
_state |= State.IsStopped;
}
else
{
_state &= ~State.IsFinished;
_state &= ~State.IsStopped;
}
}
}
Expand Down Expand Up @@ -1623,7 +1629,7 @@ private enum State : byte
FormatW3C = 0b_0_00000_10,
FormatFlags = 0b_0_00000_11,

IsFinished = 0b_1_00000_00,
IsStopped = 0b_1_00000_00,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@ public void ActivityIdOverflow()
Assert.Equal('#', activity.Id[activity.Id.Length - 1]);
}


/// <summary>
/// Tests activity start and stop
/// Checks Activity.Current correctness, Id generation
Expand All @@ -320,6 +319,51 @@ public void StartStop()
Assert.Null(Activity.Current);
}

/// <summary>
/// Tests Activity.IsStopped
/// </summary>
[Fact]
public void IsStoppedTest()
{
using var activity = new Activity("activity");
Assert.False(activity.IsStopped);
activity.Start();
Assert.False(activity.IsStopped);
Assert.Equal(TimeSpan.Zero, activity.Duration);
activity.Stop();
Assert.NotEqual(TimeSpan.Zero, activity.Duration);
Assert.True(activity.IsStopped);

using var activity1 = new Activity("activity");
Assert.False(activity1.IsStopped);
activity1.Start();
Assert.False(activity1.IsStopped);
activity1.SetEndTime(DateTime.UtcNow.AddMinutes(1)); // Setting end time shouldn't mark the activity as stopped
Assert.False(activity1.IsStopped);
activity1.Stop();
Assert.True(activity1.IsStopped);

//
// Validate when receiving Start/Stop Activity events
//

using ActivitySource aSource = new ActivitySource("TestActivityIsStopped");
using ActivityListener listener = new ActivityListener();

listener.ShouldListenTo = (activitySource) => activitySource.Name == "TestActivityIsStopped";
listener.Sample = (ref ActivityCreationOptions<ActivityContext> activityOptions) => ActivitySamplingResult.AllData;
listener.ActivityStarted = a => Assert.False(a.IsStopped);
listener.ActivityStopped = a => Assert.True(a.IsStopped);
ActivitySource.AddActivityListener(listener);
Activity sourceActivity;
using (sourceActivity = aSource.StartActivity("a1"))
{
Assert.NotNull(sourceActivity);
Assert.False(sourceActivity.IsStopped);
}
Assert.True(sourceActivity.IsStopped);
}

/// <summary>
/// Tests Id generation
/// </summary>
Expand Down