Skip to content

Commit

Permalink
[docs] Document links maybe added after Activity creation (#5972)
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas authored Nov 13, 2024
1 parent a5fa611 commit 74fc70e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/OpenTelemetry.Api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,13 @@ chose not to sample this activity.
4. Activity Links
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.
In addition to parent-child relationships, activities can also be linked
using `ActivityLinks`, which represent
[Links](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/overview.md#links-between-spans)
in OpenTelemetry. Providing activity links during creation is recommended, as
this allows samplers to consider them when deciding whether to sample an
activity. However, starting with `System.Diagnostics.DiagnosticSource` 9.0.0,
links can also be added after an activity is created.
```csharp
var activityLinks = new List<ActivityLink>();
Expand All @@ -359,12 +361,19 @@ 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)`
parent, which makes it child of implicit `Activity.Current` or orphan if
there is no `Current`.
Note that `Activity` above is created with `default(ActivityContext)`
parent, which makes it child of implicit `Activity.Current` or orphan if
there is no `Current`.
### Adding Events
Expand Down
29 changes: 29 additions & 0 deletions test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,35 @@ 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(link2);
}

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

// verify that the links retain the order as they were added.
Assert.Equal(link1.Context, exportedActivity.Links.ElementAt(0).Context);
Assert.Equal(link2.Context, exportedActivity.Links.ElementAt(1).Context);
}

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

0 comments on commit 74fc70e

Please sign in to comment.