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

[docs] Document links maybe added after Activity creation #5972

Merged
merged 4 commits into from
Nov 13, 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
15 changes: 12 additions & 3 deletions src/OpenTelemetry.Api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,10 @@ chose not to sample this activity.
Apart from the parent-child relation, activities can be linked using
`ActivityLinks` which represent the OpenTelemetry
[Links](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/overview.md#links-between-spans).
The linked activities must be provided during the creation time, as shown
below.
The linked activities are recommended to be provided during the creation
time,though links may be added after activity creation time.
Only links provided during the creation time are accessible to samplers to
make sampling decision.

```csharp
var activityLinks = new List<ActivityLink>();
Expand All @@ -359,7 +361,14 @@ chose not to sample this activity.
ActivityKind.Server,
default(ActivityContext),
initialTags,
activityLinks);
activityLinks); // links provided at creation time.

// One may add links after activity is created too.
var linkedContext3 = new ActivityContext(
ActivityTraceId.CreateFromString("01260a70a81e1fa3ad5a8acfeaa0f711"),
ActivitySpanId.CreateFromString("34739aa9e2239da1"),
ActivityTraceFlags.None);
activity?.AddLink(linkedContext3);
```

Note that `Activity` above is created with `default(ActivityContext)`
Expand Down
25 changes: 25 additions & 0 deletions test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,31 @@ public void BuilderTypeDoesNotChangeTest()
Assert.NotNull(provider);
}

[Fact]
public void CheckActivityLinksAddedAfterActivityCreation()
{
var exportedItems = new List<Activity>();
using var source = new ActivitySource($"{Utils.GetCurrentMethodName()}.1");
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.SetSampler(new AlwaysOnSampler())
.AddInMemoryExporter(exportedItems)
.AddSource(source.Name)
.Build();

var link1 = new ActivityLink(new ActivityContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.Recorded));
var link2 = new ActivityLink(new ActivityContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.Recorded));

using (var activity = source.StartActivity("root"))
{
activity?.AddLink(link1);
activity?.AddLink(link1);
}

Assert.Single(exportedItems);
var exportedActivity = exportedItems[0];
Assert.Equal(2, exportedActivity.Links.Count());
}

public void Dispose()
{
GC.SuppressFinalize(this);
Expand Down
Loading