Skip to content

Commit 1430e3f

Browse files
authored
Merge Span.AddLink tests (#5115)
1 parent a8d4eef commit 1430e3f

File tree

1 file changed

+98
-126
lines changed

1 file changed

+98
-126
lines changed

sdk/trace/trace_test.go

Lines changed: 98 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,61 +1717,6 @@ func TestAddEventsWithMoreAttributesThanLimit(t *testing.T) {
17171717
}
17181718
}
17191719

1720-
func TestAddLinksWithMoreAttributesThanLimit(t *testing.T) {
1721-
te := NewTestExporter()
1722-
sl := NewSpanLimits()
1723-
sl.AttributePerLinkCountLimit = 1
1724-
tp := NewTracerProvider(
1725-
WithSpanLimits(sl),
1726-
WithSyncer(te),
1727-
WithResource(resource.Empty()),
1728-
)
1729-
1730-
k1v1 := attribute.String("key1", "value1")
1731-
k2v2 := attribute.String("key2", "value2")
1732-
k3v3 := attribute.String("key3", "value3")
1733-
k4v4 := attribute.String("key4", "value4")
1734-
1735-
sc1 := trace.NewSpanContext(trace.SpanContextConfig{TraceID: trace.TraceID([16]byte{1, 1}), SpanID: trace.SpanID{3}})
1736-
sc2 := trace.NewSpanContext(trace.SpanContextConfig{TraceID: trace.TraceID([16]byte{1, 1}), SpanID: trace.SpanID{3}})
1737-
1738-
span := startSpan(tp, "Links", trace.WithLinks([]trace.Link{
1739-
{SpanContext: sc1, Attributes: []attribute.KeyValue{k1v1, k2v2}},
1740-
{SpanContext: sc2, Attributes: []attribute.KeyValue{k2v2, k3v3, k4v4}},
1741-
}...))
1742-
1743-
got, err := endSpan(te, span)
1744-
if err != nil {
1745-
t.Fatal(err)
1746-
}
1747-
1748-
want := &snapshot{
1749-
spanContext: trace.NewSpanContext(trace.SpanContextConfig{
1750-
TraceID: tid,
1751-
TraceFlags: 0x1,
1752-
}),
1753-
parent: sc.WithRemote(true),
1754-
name: "span0",
1755-
links: []Link{
1756-
{
1757-
SpanContext: sc1,
1758-
Attributes: []attribute.KeyValue{k1v1},
1759-
DroppedAttributeCount: 1,
1760-
},
1761-
{
1762-
SpanContext: sc2,
1763-
Attributes: []attribute.KeyValue{k2v2},
1764-
DroppedAttributeCount: 2,
1765-
},
1766-
},
1767-
spanKind: trace.SpanKindInternal,
1768-
instrumentationScope: instrumentation.Scope{Name: "Links"},
1769-
}
1770-
if diff := cmpDiff(got, want); diff != "" {
1771-
t.Errorf("Link: -got +want %s", diff)
1772-
}
1773-
}
1774-
17751720
type stateSampler struct {
17761721
prefix string
17771722
f func(trace.TraceState) trace.TraceState
@@ -1977,80 +1922,107 @@ func TestEmptyRecordingSpanDroppedAttributes(t *testing.T) {
19771922
assert.Equal(t, 0, (&recordingSpan{}).DroppedAttributes())
19781923
}
19791924

1980-
func TestAddLinkWithInvalidSpanContext(t *testing.T) {
1981-
te := NewTestExporter()
1982-
sl := NewSpanLimits()
1983-
tp := NewTracerProvider(
1984-
WithSpanLimits(sl),
1985-
WithSyncer(te),
1986-
WithResource(resource.Empty()),
1987-
)
1988-
span := startSpan(tp, "AddSpanWithInvalidSpanContext")
1989-
inValidContext := trace.NewSpanContext(trace.SpanContextConfig{
1990-
TraceID: trace.TraceID([16]byte{}),
1991-
SpanID: [8]byte{},
1992-
})
1993-
attrs := []attribute.KeyValue{{Key: "k", Value: attribute.StringValue("v")}}
1994-
span.AddLink(trace.Link{
1995-
SpanContext: inValidContext,
1996-
Attributes: attrs,
1997-
})
1998-
1999-
want := &snapshot{
2000-
name: "span0",
2001-
spanContext: trace.NewSpanContext(trace.SpanContextConfig{
2002-
TraceID: tid,
2003-
TraceFlags: 0x1,
2004-
}),
2005-
parent: sc.WithRemote(true),
2006-
links: nil,
2007-
spanKind: trace.SpanKindInternal,
2008-
instrumentationScope: instrumentation.Scope{Name: "AddSpanWithInvalidSpanContext"},
2009-
}
2010-
got, err := endSpan(te, span)
2011-
if err != nil {
2012-
t.Fatal(err)
2013-
}
2014-
if diff := cmpDiff(got, want); diff != "" {
2015-
t.Errorf("AddLinkWithInvalidSpanContext: -got +want %s", diff)
2016-
}
2017-
}
2018-
2019-
func TestAddLink(t *testing.T) {
2020-
te := NewTestExporter()
2021-
sl := NewSpanLimits()
2022-
tp := NewTracerProvider(
2023-
WithSpanLimits(sl),
2024-
WithSyncer(te),
2025-
WithResource(resource.Empty()),
2026-
)
2027-
attrs := []attribute.KeyValue{{Key: "k", Value: attribute.StringValue("v")}}
2028-
span := startSpan(tp, "AddSpan")
2029-
2030-
link := trace.Link{SpanContext: sc, Attributes: attrs}
2031-
span.AddLink(link)
2032-
2033-
want := &snapshot{
2034-
name: "span0",
2035-
spanContext: trace.NewSpanContext(trace.SpanContextConfig{
2036-
TraceID: tid,
2037-
TraceFlags: 0x1,
2038-
}),
2039-
parent: sc.WithRemote(true),
2040-
links: []Link{
2041-
{
1925+
func TestSpanAddLink(t *testing.T) {
1926+
tests := []struct {
1927+
name string
1928+
attrLinkCountLimit int
1929+
link trace.Link
1930+
want *snapshot
1931+
}{
1932+
{
1933+
name: "AddLinkWithInvalidSpanContext",
1934+
attrLinkCountLimit: 128,
1935+
link: trace.Link{
1936+
SpanContext: trace.NewSpanContext(trace.SpanContextConfig{TraceID: trace.TraceID([16]byte{}), SpanID: [8]byte{}}),
1937+
Attributes: []attribute.KeyValue{{Key: "k1", Value: attribute.StringValue("v1")}},
1938+
},
1939+
want: &snapshot{
1940+
name: "span0",
1941+
spanContext: trace.NewSpanContext(trace.SpanContextConfig{
1942+
TraceID: tid,
1943+
TraceFlags: 0x1,
1944+
}),
1945+
parent: sc.WithRemote(true),
1946+
links: nil,
1947+
spanKind: trace.SpanKindInternal,
1948+
instrumentationScope: instrumentation.Scope{Name: "AddLinkWithInvalidSpanContext"},
1949+
},
1950+
},
1951+
{
1952+
name: "AddLink",
1953+
attrLinkCountLimit: 128,
1954+
link: trace.Link{
20421955
SpanContext: sc,
2043-
Attributes: attrs,
1956+
Attributes: []attribute.KeyValue{{Key: "k1", Value: attribute.StringValue("v1")}},
1957+
},
1958+
want: &snapshot{
1959+
name: "span0",
1960+
spanContext: trace.NewSpanContext(trace.SpanContextConfig{
1961+
TraceID: tid,
1962+
TraceFlags: 0x1,
1963+
}),
1964+
parent: sc.WithRemote(true),
1965+
links: []Link{
1966+
{
1967+
SpanContext: sc,
1968+
Attributes: []attribute.KeyValue{{Key: "k1", Value: attribute.StringValue("v1")}},
1969+
},
1970+
},
1971+
spanKind: trace.SpanKindInternal,
1972+
instrumentationScope: instrumentation.Scope{Name: "AddLink"},
1973+
},
1974+
},
1975+
{
1976+
name: "AddLinkWithMoreAttributesThanLimit",
1977+
attrLinkCountLimit: 1,
1978+
link: trace.Link{
1979+
SpanContext: sc,
1980+
Attributes: []attribute.KeyValue{
1981+
{Key: "k1", Value: attribute.StringValue("v1")},
1982+
{Key: "k2", Value: attribute.StringValue("v2")},
1983+
{Key: "k3", Value: attribute.StringValue("v3")},
1984+
{Key: "k4", Value: attribute.StringValue("v4")},
1985+
},
1986+
},
1987+
want: &snapshot{
1988+
name: "span0",
1989+
spanContext: trace.NewSpanContext(trace.SpanContextConfig{
1990+
TraceID: tid,
1991+
TraceFlags: 0x1,
1992+
}),
1993+
parent: sc.WithRemote(true),
1994+
links: []Link{
1995+
{
1996+
SpanContext: sc,
1997+
Attributes: []attribute.KeyValue{{Key: "k1", Value: attribute.StringValue("v1")}},
1998+
DroppedAttributeCount: 3,
1999+
},
2000+
},
2001+
spanKind: trace.SpanKindInternal,
2002+
instrumentationScope: instrumentation.Scope{Name: "AddLinkWithMoreAttributesThanLimit"},
20442003
},
20452004
},
2046-
spanKind: trace.SpanKindInternal,
2047-
instrumentationScope: instrumentation.Scope{Name: "AddSpan"},
2048-
}
2049-
got, err := endSpan(te, span)
2050-
if err != nil {
2051-
t.Fatal(err)
20522005
}
2053-
if diff := cmpDiff(got, want); diff != "" {
2054-
t.Errorf("AddLink: -got +want %s", diff)
2006+
2007+
for _, tc := range tests {
2008+
t.Run(tc.name, func(t *testing.T) {
2009+
te := NewTestExporter()
2010+
sl := NewSpanLimits()
2011+
sl.AttributePerLinkCountLimit = tc.attrLinkCountLimit
2012+
2013+
tp := NewTracerProvider(WithSpanLimits(sl), WithSyncer(te), WithResource(resource.Empty()))
2014+
2015+
span := startSpan(tp, tc.name)
2016+
span.AddLink(tc.link)
2017+
2018+
got, err := endSpan(te, span)
2019+
if err != nil {
2020+
t.Fatal(err)
2021+
}
2022+
2023+
if diff := cmpDiff(got, tc.want); diff != "" {
2024+
t.Errorf("-got +want %s", diff)
2025+
}
2026+
})
20552027
}
20562028
}

0 commit comments

Comments
 (0)