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

Merge Span.AddLink tests #5115

Merged
merged 10 commits into from
May 9, 2024
236 changes: 112 additions & 124 deletions sdk/trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1717,61 +1717,6 @@ func TestAddEventsWithMoreAttributesThanLimit(t *testing.T) {
}
}

func TestAddLinksWithMoreAttributesThanLimit(t *testing.T) {
te := NewTestExporter()
sl := NewSpanLimits()
sl.AttributePerLinkCountLimit = 1
tp := NewTracerProvider(
WithSpanLimits(sl),
WithSyncer(te),
WithResource(resource.Empty()),
)

k1v1 := attribute.String("key1", "value1")
k2v2 := attribute.String("key2", "value2")
k3v3 := attribute.String("key3", "value3")
k4v4 := attribute.String("key4", "value4")

sc1 := trace.NewSpanContext(trace.SpanContextConfig{TraceID: trace.TraceID([16]byte{1, 1}), SpanID: trace.SpanID{3}})
sc2 := trace.NewSpanContext(trace.SpanContextConfig{TraceID: trace.TraceID([16]byte{1, 1}), SpanID: trace.SpanID{3}})

span := startSpan(tp, "Links", trace.WithLinks([]trace.Link{
{SpanContext: sc1, Attributes: []attribute.KeyValue{k1v1, k2v2}},
{SpanContext: sc2, Attributes: []attribute.KeyValue{k2v2, k3v3, k4v4}},
}...))

got, err := endSpan(te, span)
if err != nil {
t.Fatal(err)
}

want := &snapshot{
spanContext: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: tid,
TraceFlags: 0x1,
}),
parent: sc.WithRemote(true),
name: "span0",
links: []Link{
{
SpanContext: sc1,
Attributes: []attribute.KeyValue{k1v1},
DroppedAttributeCount: 1,
},
{
SpanContext: sc2,
Attributes: []attribute.KeyValue{k2v2},
DroppedAttributeCount: 2,
},
},
spanKind: trace.SpanKindInternal,
instrumentationScope: instrumentation.Scope{Name: "Links"},
}
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("Link: -got +want %s", diff)
}
}

type stateSampler struct {
prefix string
f func(trace.TraceState) trace.TraceState
Expand Down Expand Up @@ -1977,80 +1922,123 @@ func TestEmptyRecordingSpanDroppedAttributes(t *testing.T) {
assert.Equal(t, 0, (&recordingSpan{}).DroppedAttributes())
}

func TestAddLinkWithInvalidSpanContext(t *testing.T) {
te := NewTestExporter()
sl := NewSpanLimits()
tp := NewTracerProvider(
WithSpanLimits(sl),
WithSyncer(te),
WithResource(resource.Empty()),
)
span := startSpan(tp, "AddSpanWithInvalidSpanContext")
inValidContext := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: trace.TraceID([16]byte{}),
SpanID: [8]byte{},
})
attrs := []attribute.KeyValue{{Key: "k", Value: attribute.StringValue("v")}}
span.AddLink(trace.Link{
SpanContext: inValidContext,
Attributes: attrs,
})

want := &snapshot{
name: "span0",
spanContext: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: tid,
TraceFlags: 0x1,
}),
parent: sc.WithRemote(true),
links: nil,
spanKind: trace.SpanKindInternal,
instrumentationScope: instrumentation.Scope{Name: "AddSpanWithInvalidSpanContext"},
}
got, err := endSpan(te, span)
if err != nil {
t.Fatal(err)
}
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("AddLinkWithInvalidSpanContext: -got +want %s", diff)
}
}
func TestSpanAddLink(t *testing.T) {
tests := []struct {
name string
spanContext trace.SpanContext
validate func(*testing.T, *snapshot)
perhapsmaple marked this conversation as resolved.
Show resolved Hide resolved
}{
{
name: "AddLinkWithInvalidSpanContext",
spanContext: trace.NewSpanContext(trace.SpanContextConfig{TraceID: trace.TraceID([16]byte{}), SpanID: [8]byte{}}),
validate: func(t *testing.T, got *snapshot) {
want := &snapshot{
name: "span0",
spanContext: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: tid,
TraceFlags: 0x1,
}),
parent: sc.WithRemote(true),
links: nil,
spanKind: trace.SpanKindInternal,
instrumentationScope: instrumentation.Scope{Name: "AddLinkWithInvalidSpanContext"},
}

func TestAddLink(t *testing.T) {
te := NewTestExporter()
sl := NewSpanLimits()
tp := NewTracerProvider(
WithSpanLimits(sl),
WithSyncer(te),
WithResource(resource.Empty()),
)
attrs := []attribute.KeyValue{{Key: "k", Value: attribute.StringValue("v")}}
span := startSpan(tp, "AddSpan")
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("AddLinkWithInvalidSpanContext: -got +want %s", diff)
}
},
},
{
name: "AddLink",
spanContext: sc,
validate: func(t *testing.T, got *snapshot) {
attrs := []attribute.KeyValue{
{Key: "k1", Value: attribute.StringValue("v1")},
{Key: "k2", Value: attribute.StringValue("v2")},
{Key: "k3", Value: attribute.StringValue("v3")},
{Key: "k4", Value: attribute.StringValue("v4")},
}
want := &snapshot{
name: "span0",
spanContext: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: tid,
TraceFlags: 0x1,
}),
parent: sc.WithRemote(true),
links: []Link{
{
SpanContext: sc,
Attributes: attrs,
},
},
spanKind: trace.SpanKindInternal,
instrumentationScope: instrumentation.Scope{Name: "AddLink"},
}

link := trace.Link{SpanContext: sc, Attributes: attrs}
span.AddLink(link)
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("AddLink: -got +want %s", diff)
}
},
},
{
name: "AddLinkWithMoreAttributesThanLimit",
spanContext: sc,
validate: func(t *testing.T, got *snapshot) {
attrs := []attribute.KeyValue{{Key: "k1", Value: attribute.StringValue("v1")}}
want := &snapshot{
name: "span0",
spanContext: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: tid,
TraceFlags: 0x1,
}),
parent: sc.WithRemote(true),
links: []Link{
{
SpanContext: sc,
Attributes: attrs,
DroppedAttributeCount: 3,
},
},
spanKind: trace.SpanKindInternal,
instrumentationScope: instrumentation.Scope{Name: "AddLinkWithMoreAttributesThanLimit"},
}

want := &snapshot{
name: "span0",
spanContext: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: tid,
TraceFlags: 0x1,
}),
parent: sc.WithRemote(true),
links: []Link{
{
SpanContext: sc,
Attributes: attrs,
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("AddLinkWithMoreAttributesThanLimit: -got +want %s", diff)
}
},
},
spanKind: trace.SpanKindInternal,
instrumentationScope: instrumentation.Scope{Name: "AddSpan"},
}
got, err := endSpan(te, span)
if err != nil {
t.Fatal(err)
}
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("AddLink: -got +want %s", diff)

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
te := NewTestExporter()
sl := NewSpanLimits()

if tc.name == "AddLinkWithMoreAttributesThanLimit" {
sl.AttributePerLinkCountLimit = 1
perhapsmaple marked this conversation as resolved.
Show resolved Hide resolved
}

tp := NewTracerProvider(WithSpanLimits(sl), WithSyncer(te), WithResource(resource.Empty()))

attrs := []attribute.KeyValue{
{Key: "k1", Value: attribute.StringValue("v1")},
{Key: "k2", Value: attribute.StringValue("v2")},
{Key: "k3", Value: attribute.StringValue("v3")},
{Key: "k4", Value: attribute.StringValue("v4")},
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
}

span := startSpan(tp, tc.name, trace.WithLinks([]trace.Link{
perhapsmaple marked this conversation as resolved.
Show resolved Hide resolved
{SpanContext: tc.spanContext, Attributes: attrs},
}...))

got, err := endSpan(te, span)
if err != nil {
t.Fatal(err)
}

tc.validate(t, got)
perhapsmaple marked this conversation as resolved.
Show resolved Hide resolved
})
}
}