Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
implement Activity.AddLink
  • Loading branch information
antonfirsov committed Apr 22, 2024
commit 080ed80864619b330d7ab2af9d5a6c4008a34bbb
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)));

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