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

[Tracing] Refactor internal Span Links API #6341

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions tracer/src/Datadog.Trace/Activity/OtlpHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ private static void ExtractActivityLinks<TInner>(Span span, IActivity5? activity
spanContext.LastParentId = traceState.LastParent;
spanContext.PropagatedTags = traceTags;

var extractedSpan = new Span(spanContext, DateTimeOffset.Now, new CommonTags());
var spanLink = span.AddSpanLink(extractedSpan);
var spanLink = new SpanLink(spanContext);
span.AddLink(spanLink);

if (duckLink.Tags is not null)
{
Expand Down
14 changes: 6 additions & 8 deletions tracer/src/Datadog.Trace/Span.cs
Original file line number Diff line number Diff line change
Expand Up @@ -538,21 +538,19 @@ private void WriteCloseDebugMessage()
/// <summary>
/// Adds a SpanLink to the current Span if the Span is active.
/// </summary>
/// <param name="spanLinkToAdd">The Span to add as a SpanLink</param>
/// <param name="attributes">List of KeyValue pairings of attributes to add to the SpanLink. Defaults to null</param>
/// <returns>returns the SpanLink on success or null on failure (span is closed already)</returns>
internal SpanLink AddSpanLink(Span spanLinkToAdd, List<KeyValuePair<string, string>> attributes = null)
/// <param name="spanLink">The SpanLink to add</param>
/// <returns>This span to allow method chaining.</returns>
internal Span AddLink(SpanLink spanLink)
{
if (IsFinished)
{
Log.Warning("AddSpanLink should not be called after the span was closed");
return null;
Log.Warning("AddLink should not be called after the span was closed");
return this;
}

SpanLinks ??= new List<SpanLink>();
var spanLink = new SpanLink(spanLinkToAdd, this, attributes);
SpanLinks.Add(spanLink);
return spanLink;
return this;
}
}
}
16 changes: 3 additions & 13 deletions tracer/src/Datadog.Trace/SpanLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,29 @@ internal class SpanLink
/// in OpenTelemetry that's called a Span Context, which may also include tracestate and trace flags.
/// </summary>
/// <param name="spanLinkContext">The context of the spanlink to extract attributes from</param>
/// <param name="decoratedSpan">Reference to the span you're adding SpanLinks to</param>
/// <param name="attributes">Optional dictionary of attributes to take for the spanlink.</param>
public SpanLink(SpanContext spanLinkContext, Span decoratedSpan, List<KeyValuePair<string, string>>? attributes = null)
public SpanLink(SpanContext spanLinkContext, List<KeyValuePair<string, string>>? attributes = null)
{
Context = spanLinkContext;
DecoratedSpan = decoratedSpan;
Attributes = attributes;
}

public SpanLink(Span spanToLink, Span decoratedSpan, List<KeyValuePair<string, string>>? attributes = null)
: this(spanToLink.Context, decoratedSpan, attributes)
public SpanLink(Span spanToLink, List<KeyValuePair<string, string>>? attributes = null)
: this(spanToLink.Context, attributes)
{
}

public List<KeyValuePair<string, string>>? Attributes { get; private set; }

public SpanContext Context { get; }

public Span DecoratedSpan { get; }

/// <summary>
/// Adds an Attribute to the SpanLink.
/// </summary>
/// <param name="name">name of attribute</param>
/// <param name="value">value of attribute</param>
public void AddAttribute(string name, string value)
{
if (DecoratedSpan.IsFinished)
{
Log.Warning("AddAttribute should not be called after the decorated span was closed");
return;
}

var newAttribute = new KeyValuePair<string, string>(name, value);
Attributes ??= [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,14 @@ public void SpanLink_Tag_Serialization()
new("pair", "false"),
new("arbitrary", "56709")
};
spans[0].AddSpanLink(spans[1], attributesToAdd);
var tmpSpanLink = spans[1].AddSpanLink(spans[2]);
spans[0].AddLink(new SpanLink(spans[1].Context, attributesToAdd));

var tmpSpanLink = new SpanLink(spans[2].Context);
spans[1].AddLink(tmpSpanLink);
tmpSpanLink.AddAttribute("attribute1", "value1");
tmpSpanLink.AddAttribute("attribute2", "value2");
spans[1].AddSpanLink(spans[0]);

spans[1].AddLink(new SpanLink(spans[0].Context));

foreach (var span in spans)
{
Expand Down
20 changes: 14 additions & 6 deletions tracer/test/Datadog.Trace.Tests/SpanLinksTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>

using System.Collections.Generic;
using Castle.Core.Internal;
using Datadog.Trace.Agent;
using Datadog.Trace.Configuration;
using Datadog.Trace.Sampling;
using FluentAssertions;
using Moq;
using Xunit;

Expand All @@ -26,30 +28,36 @@ public SpanLinksTests()
}

[Fact]
public void AddLink_InCloseSpan()
public void AddLink_AfterSpanFinished_IsNoOp()
{
var parentScope = (Scope)_tracer.StartActive("Parent");
var childScope = (Scope)_tracer.StartActive("Child");

var parentSpan = parentScope.Span;
var childSpan = childScope.Span;
var spanLink = new SpanLink(parentSpan.Context);

childSpan.Finish();
Assert.Null(childSpan.AddSpanLink(parentSpan));

childSpan.AddLink(spanLink);
childSpan.SpanLinks.Should().BeNullOrEmpty();
}

[Fact]
public void AddAttribute_ToLink_InCloseSpan()
public void AddAttribute_BeforeSpanFinished_IsSuccessful()
{
var parentScope = (Scope)_tracer.StartActive("Parent");
var childScope = (Scope)_tracer.StartActive("Child");

var parentSpan = parentScope.Span;
var childSpan = childScope.Span;
var spanLink = childSpan.AddSpanLink(parentSpan);
var spanLink = new SpanLink(parentSpan.Context);

childSpan.AddLink(spanLink);
spanLink.AddAttribute("key", "value");

spanLink.Attributes.Should().BeEquivalentTo(new[] { new KeyValuePair<string, string>("key", "value") });
childSpan.Finish();
spanLink.AddAttribute("should", "return null");
Assert.Null(spanLink.Attributes);
}
}
}
Loading