Skip to content

Commit 74fc70e

Browse files
authored
[docs] Document links maybe added after Activity creation (open-telemetry#5972)
1 parent a5fa611 commit 74fc70e

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

src/OpenTelemetry.Api/README.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,13 @@ chose not to sample this activity.
332332
333333
4. Activity Links
334334
335-
Apart from the parent-child relation, activities can be linked using
336-
`ActivityLinks` which represent the OpenTelemetry
337-
[Links](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/overview.md#links-between-spans).
338-
The linked activities must be provided during the creation time, as shown
339-
below.
335+
In addition to parent-child relationships, activities can also be linked
336+
using `ActivityLinks`, which represent
337+
[Links](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/overview.md#links-between-spans)
338+
in OpenTelemetry. Providing activity links during creation is recommended, as
339+
this allows samplers to consider them when deciding whether to sample an
340+
activity. However, starting with `System.Diagnostics.DiagnosticSource` 9.0.0,
341+
links can also be added after an activity is created.
340342
341343
```csharp
342344
var activityLinks = new List<ActivityLink>();
@@ -359,12 +361,19 @@ chose not to sample this activity.
359361
ActivityKind.Server,
360362
default(ActivityContext),
361363
initialTags,
362-
activityLinks);
364+
activityLinks); // links provided at creation time.
365+
366+
// One may add links after activity is created too.
367+
var linkedContext3 = new ActivityContext(
368+
ActivityTraceId.CreateFromString("01260a70a81e1fa3ad5a8acfeaa0f711"),
369+
ActivitySpanId.CreateFromString("34739aa9e2239da1"),
370+
ActivityTraceFlags.None);
371+
activity?.AddLink(linkedContext3);
363372
```
364373
365-
Note that `Activity` above is created with `default(ActivityContext)`
366-
parent, which makes it child of implicit `Activity.Current` or orphan if
367-
there is no `Current`.
374+
Note that `Activity` above is created with `default(ActivityContext)`
375+
parent, which makes it child of implicit `Activity.Current` or orphan if
376+
there is no `Current`.
368377
369378
### Adding Events
370379

test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,35 @@ public void BuilderTypeDoesNotChangeTest()
13031303
Assert.NotNull(provider);
13041304
}
13051305

1306+
[Fact]
1307+
public void CheckActivityLinksAddedAfterActivityCreation()
1308+
{
1309+
var exportedItems = new List<Activity>();
1310+
using var source = new ActivitySource($"{Utils.GetCurrentMethodName()}.1");
1311+
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
1312+
.SetSampler(new AlwaysOnSampler())
1313+
.AddInMemoryExporter(exportedItems)
1314+
.AddSource(source.Name)
1315+
.Build();
1316+
1317+
var link1 = new ActivityLink(new ActivityContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.Recorded));
1318+
var link2 = new ActivityLink(new ActivityContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.Recorded));
1319+
1320+
using (var activity = source.StartActivity("root"))
1321+
{
1322+
activity?.AddLink(link1);
1323+
activity?.AddLink(link2);
1324+
}
1325+
1326+
Assert.Single(exportedItems);
1327+
var exportedActivity = exportedItems[0];
1328+
Assert.Equal(2, exportedActivity.Links.Count());
1329+
1330+
// verify that the links retain the order as they were added.
1331+
Assert.Equal(link1.Context, exportedActivity.Links.ElementAt(0).Context);
1332+
Assert.Equal(link2.Context, exportedActivity.Links.ElementAt(1).Context);
1333+
}
1334+
13061335
public void Dispose()
13071336
{
13081337
GC.SuppressFinalize(this);

0 commit comments

Comments
 (0)