func mergeSpanGroups()

in aggregators/merger.go [201:242]


func mergeSpanGroups(
	to map[spanAggregationKey]*aggregationpb.KeyedSpanMetrics,
	from []*aggregationpb.KeyedSpanMetrics,
	perSvcConstraint, globalConstraint *constraint.Constraint,
	hash xxhash.Digest,
	overflowTo *overflowSpan,
) {
	for i := range from {
		fromSpan := from[i]
		var spk spanAggregationKey
		spk.FromProto(fromSpan.Key)
		toSpan, ok := to[spk]
		if !ok {
			// Protect against agents that send high cardinality span names by dropping
			// span.name if more than half of the per svc span group limit is reached.
			originalSpanName := fromSpan.Key.SpanName
			half := perSvcConstraint.Limit() / 2
			if perSvcConstraint.Value() >= half {
				spk.SpanName = ""
				fromSpan.Key.SpanName = ""
				toSpan, ok = to[spk]
			}
			if !ok {
				overflowed := perSvcConstraint.Maxed() || globalConstraint.Maxed()
				if overflowed {
					// Restore span name in case it was dropped above,
					// for cardinality estimation.
					fromSpan.Key.SpanName = originalSpanName
					fromSpanKeyHash := protohash.HashSpanAggregationKey(hash, fromSpan.Key)
					overflowTo.Merge(fromSpan.Metrics, fromSpanKeyHash.Sum64())
					continue
				}
				perSvcConstraint.Add(1)
				globalConstraint.Add(1)

				to[spk] = fromSpan.CloneVT()
				continue
			}
		}
		mergeKeyedSpanMetrics(toSpan, fromSpan)
	}
}