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

Implement Activity.AddLink #101381

Merged
merged 7 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -50,6 +50,7 @@ public string? Id
public string? TraceStateString { get { throw null; } set { } }
public System.Diagnostics.Activity AddBaggage(string key, string? value) { throw null; }
public System.Diagnostics.Activity AddEvent(System.Diagnostics.ActivityEvent e) { throw null; }
public System.Diagnostics.Activity AddLink(System.Diagnostics.ActivityLink link) { throw null; }
public System.Diagnostics.Activity AddTag(string key, string? value) { throw null; }
public System.Diagnostics.Activity AddTag(string key, object? value) { throw null; }
public System.Diagnostics.Activity SetTag(string key, object? value) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,21 @@ public Activity AddEvent(ActivityEvent e)
return this;
}

/// <summary>
/// Add an <see cref="ActivityLink"/> to the <see cref="Links"/> list.
/// </summary>
/// <param name="link">The <see cref="ActivityLink"/> to add.</param>
/// <returns><see langword="this" /> for convenient chaining.</returns>
public Activity AddLink(ActivityLink link)
{
if (_links != null || Interlocked.CompareExchange(ref _links, new DiagLinkedList<ActivityLink>(link), null) != null)
{
_links.Add(link);
}

return this;
}

/// <summary>
/// Update the Activity to have baggage with an additional 'key' and value 'value'.
/// This shows up in the <see cref="Baggage"/> enumeration as well as the <see cref="GetBaggageItem(string)"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,31 @@ public void TestEvent()
Assert.Equal(0, activity.Events.ElementAt(1).Tags.Count());
}

[Fact]
public void AddLinkTest()
{
ActivityContext c1 = new ActivityContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None);
ActivityContext c2 = new ActivityContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None);

ActivityLink l1 = new ActivityLink(c1);
ActivityLink l2 = new ActivityLink(c2, new ActivityTagsCollection()
{
new KeyValuePair<string, object?>("foo", 99)
});

Activity activity = new Activity("LinkTest");
Assert.True(ReferenceEquals(activity, activity.AddLink(l1)));
Assert.True(ReferenceEquals(activity, activity.AddLink(l2)));
tarekgh marked this conversation as resolved.
Show resolved Hide resolved

ActivityLink[] links = activity.Links.ToArray();
Assert.Equal(2, links.Length);
Assert.Equal(c1, links[0].Context);
Assert.Equal(c2, links[1].Context);
KeyValuePair<string, object> tag = links[1].Tags.Single();
Assert.Equal("foo", tag.Key);
Assert.Equal(99, tag.Value);
}

[Fact]
public void TestIsAllDataRequested()
{
Expand Down
Loading