Skip to content

Commit 4aa4ab4

Browse files
authored
[processor/spanpruning] Fix spans with the same name not being aggregated (#47770)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description If spans at two different depths each have the same name, kind, and status (such as two HTTP GET spans from different services) then highest in the tree will overwrite the aggregation. This causes the group to not be aggregated creating orphan spans. Adding the depth to the key when building the parent group key fixes this problem as each level will be considered on its own. <!--Describe what testing was performed and which tests were added.--> #### Testing Unit test added and I have also verified the behavior on some internal traces that exposed this bug.
1 parent 77bff71 commit 4aa4ab4

4 files changed

Lines changed: 180 additions & 3 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. receiver/filelog)
7+
component: processor/spanpruning
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Fix bug in span pruning where spans with the same name at different depths could become orphaned instead of deleted.
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [47770]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

processor/spanpruningprocessor/grouping.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,15 @@ func writeAttributeSliceKey(builder *strings.Builder, value pcommon.Slice) {
133133
}
134134

135135
// buildParentGroupKey constructs a parent grouping key from name and status
136-
// only; attributes are intentionally excluded for parent aggregation.
137-
func (*spanPruningProcessor) buildParentGroupKey(span ptrace.Span) string {
136+
// only; attributes are intentionally excluded for parent aggregation. Depth is
137+
// required to avoid duplicate names and status entries overwriting each other.
138+
func (*spanPruningProcessor) buildParentGroupKey(span ptrace.Span, depth int) string {
138139
builder := builderPool.Get().(*strings.Builder)
139140
builder.Reset()
140141
defer builderPool.Put(builder)
141142

143+
builder.WriteString(strconv.Itoa(depth))
144+
builder.WriteByte('|')
142145
builder.WriteString(span.Name())
143146
builder.WriteString("|kind=")
144147
builder.WriteString(span.Kind().String())

processor/spanpruningprocessor/processor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func (p *spanPruningProcessor) analyzeAggregationsWithTree(tree *traceTree) map[
224224
// Group parent candidates by name + status
225225
parentGroups := make(map[string][]*spanNode)
226226
for _, node := range eligibleParents {
227-
parentKey := p.buildParentGroupKey(node.span)
227+
parentKey := p.buildParentGroupKey(node.span, depth)
228228
parentGroups[parentKey] = append(parentGroups[parentKey], node)
229229
}
230230

processor/spanpruningprocessor/processor_test.go

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,6 +1738,153 @@ func TestLeafSpanPruning_TraceStateGrouping_EmptyTraceState(t *testing.T) {
17381738
assert.Equal(t, int64(3), spanCount.Int())
17391739
}
17401740

1741+
// TestParentKeyCollisionAcrossDepths verifies that when the same span name
1742+
// appears at multiple tree depths (causing buildParentGroupKey collisions),
1743+
// nodes from overwritten groups are not incorrectly removed. This is a
1744+
// regression test for a bug where the depth-2 parent group overwrites the
1745+
// depth-1 entry in aggregationGroups, leaving depth-1 nodes marked for
1746+
// removal but absent from the final plan.
1747+
//
1748+
// Trace structure (3 copies to meet MinSpansToAggregate=2 for parents):
1749+
//
1750+
// root
1751+
// ├── svc [depth-2 parent candidate]
1752+
// │ ├── svc [depth-1 parent candidate, SAME name as depth-2]
1753+
// │ │ ├── SELECT (leaf)
1754+
// │ │ └── SELECT (leaf)
1755+
// │ └── svc
1756+
// │ ├── SELECT (leaf)
1757+
// │ └── SELECT (leaf)
1758+
// ├── svc
1759+
// │ ├── svc
1760+
// │ │ ├── SELECT (leaf)
1761+
// │ │ └── SELECT (leaf)
1762+
// │ └── svc
1763+
// │ ├── SELECT (leaf)
1764+
// │ └── SELECT (leaf)
1765+
// └── svc
1766+
// ├── svc
1767+
// │ ├── SELECT (leaf)
1768+
// │ └── SELECT (leaf)
1769+
// └── svc
1770+
// ├── SELECT (leaf)
1771+
// └── SELECT (leaf)
1772+
//
1773+
// Expected result (22 spans → 4 spans):
1774+
//
1775+
// root
1776+
// ├── summary(svc, aggregates 3 outer svc spans)
1777+
// │ └── summary(svc, aggregates 6 inner svc spans)
1778+
// │ └── summary(SELECT, aggregates 12 leaf spans)
1779+
func TestParentKeyCollisionAcrossDepths(t *testing.T) {
1780+
factory := NewFactory()
1781+
cfg := factory.CreateDefaultConfig().(*Config)
1782+
cfg.MinSpansToAggregate = 2
1783+
cfg.MaxParentDepth = -1
1784+
1785+
tp, err := factory.CreateTraces(t.Context(), processortest.NewNopSettings(metadata.Type), cfg, consumertest.NewNop())
1786+
require.NoError(t, err)
1787+
1788+
td := createTestTraceWithParentKeyCollision(t)
1789+
1790+
// Before: 1 root + 3 outer-svc + 6 inner-svc + 12 SELECT = 22 spans
1791+
originalSpanCount := countSpans(td)
1792+
assert.Equal(t, 22, originalSpanCount)
1793+
1794+
err = tp.ConsumeTraces(t.Context(), td)
1795+
require.NoError(t, err)
1796+
1797+
finalSpanCount := countSpans(td)
1798+
1799+
// Verify every non-summary span that remains is NOT an orphan: its parent
1800+
// must either be another span in the output or it must be the root.
1801+
remainingByID := make(map[pcommon.SpanID]ptrace.Span)
1802+
rss := td.ResourceSpans()
1803+
for i := 0; i < rss.Len(); i++ {
1804+
ilss := rss.At(i).ScopeSpans()
1805+
for j := 0; j < ilss.Len(); j++ {
1806+
spans := ilss.At(j).Spans()
1807+
for k := 0; k < spans.Len(); k++ {
1808+
span := spans.At(k)
1809+
remainingByID[span.SpanID()] = span
1810+
}
1811+
}
1812+
}
1813+
for id, span := range remainingByID {
1814+
parentID := span.ParentSpanID()
1815+
if parentID.IsEmpty() {
1816+
continue // root span
1817+
}
1818+
_, parentExists := remainingByID[parentID]
1819+
assert.True(t, parentExists, "span %s (name=%s) has dangling parent %s — parent was removed but span was kept",
1820+
id, span.Name(), parentID)
1821+
}
1822+
1823+
// With the bug, depth-1 "svc" nodes (6 spans) get incorrectly removed,
1824+
// dropping the count too low. The expected result: root + 3 summaries.
1825+
t.Logf("original=%d final=%d", originalSpanCount, finalSpanCount)
1826+
assert.Equal(t, 4, finalSpanCount,
1827+
"expected root + 3 summary spans (SELECT, inner svc, outer svc)")
1828+
}
1829+
1830+
func createTestTraceWithParentKeyCollision(t *testing.T) ptrace.Traces {
1831+
t.Helper()
1832+
td := ptrace.NewTraces()
1833+
rs := td.ResourceSpans().AppendEmpty()
1834+
ss := rs.ScopeSpans().AppendEmpty()
1835+
1836+
traceID := pcommon.TraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
1837+
rootSpanID := pcommon.SpanID([8]byte{1, 0, 0, 0, 0, 0, 0, 0})
1838+
1839+
root := ss.Spans().AppendEmpty()
1840+
root.SetTraceID(traceID)
1841+
root.SetSpanID(rootSpanID)
1842+
root.SetName("root")
1843+
1844+
spanIDCounter := byte(2)
1845+
nextID := func() pcommon.SpanID {
1846+
id := pcommon.SpanID([8]byte{spanIDCounter, 0, 0, 0, 0, 0, 0, 0})
1847+
spanIDCounter++
1848+
return id
1849+
}
1850+
1851+
// 3 outer "svc" spans (same name/kind/status → same parent group key)
1852+
for range 3 {
1853+
outerID := nextID()
1854+
outer := ss.Spans().AppendEmpty()
1855+
outer.SetTraceID(traceID)
1856+
outer.SetSpanID(outerID)
1857+
outer.SetParentSpanID(rootSpanID)
1858+
outer.SetName("svc")
1859+
outer.Status().SetCode(ptrace.StatusCodeOk)
1860+
1861+
// Each outer has 2 inner "svc" spans (same name → key collision with outer)
1862+
for range 2 {
1863+
innerID := nextID()
1864+
inner := ss.Spans().AppendEmpty()
1865+
inner.SetTraceID(traceID)
1866+
inner.SetSpanID(innerID)
1867+
inner.SetParentSpanID(outerID)
1868+
inner.SetName("svc")
1869+
inner.Status().SetCode(ptrace.StatusCodeOk)
1870+
1871+
// Each inner has 2 leaf SELECT spans
1872+
for range 2 {
1873+
leaf := ss.Spans().AppendEmpty()
1874+
leaf.SetTraceID(traceID)
1875+
leaf.SetSpanID(nextID())
1876+
leaf.SetParentSpanID(innerID)
1877+
leaf.SetName("SELECT")
1878+
leaf.Status().SetCode(ptrace.StatusCodeOk)
1879+
leaf.SetStartTimestamp(pcommon.Timestamp(1000000000))
1880+
leaf.SetEndTimestamp(pcommon.Timestamp(1000000100))
1881+
}
1882+
}
1883+
}
1884+
1885+
return td
1886+
}
1887+
17411888
// Helper functions for TraceState tests
17421889

17431890
func createTestTraceWithSameTraceState(t *testing.T, traceState string) ptrace.Traces {

0 commit comments

Comments
 (0)