Skip to content

Commit f6074af

Browse files
authored
Rename ForEach with Range to be consistent with sync.Map (#2931)
* Rename ForEach with Range to be consistent with sync.Map Signed-off-by: Bogdan Drutu <[email protected]> * Update changelog Signed-off-by: Bogdan Drutu <[email protected]> * Change Range func to return bool as sync.Map Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 9ff58cb commit f6074af

22 files changed

+111
-75
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
## 🛑 Breaking changes 🛑
66

7+
- Rename ForEach (in pdata) with Range to be consistent with sync.Map (#2931)
78
- Rename `componenthelper.Start` to `componenthelper.StartFunc` (#2880)
89
- Rename `componenthelper.Stop` to `componenthelper.StopFunc` (#2880)
910
- Remove `exporterheleper.WithCustomUnmarshaler`, `processorheleper.WithCustomUnmarshaler`, `receiverheleper.WithCustomUnmarshaler`, `extensionheleper.WithCustomUnmarshaler`, implement `config.CustomUnmarshaler` interface instead (#2867)

consumer/pdata/common.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -611,22 +611,24 @@ func (am AttributeMap) Sort() AttributeMap {
611611
// Len returns the length of this map.
612612
//
613613
// Because the AttributeMap is represented internally by a slice of pointers, and the data are comping from the wire,
614-
// it is possible that when iterating using "ForEach" to get access to fewer elements because nil elements are skipped.
614+
// it is possible that when iterating using "Range" to get access to fewer elements because nil elements are skipped.
615615
func (am AttributeMap) Len() int {
616616
return len(*am.orig)
617617
}
618618

619-
// ForEach iterates over the every elements in the map by calling the provided func.
619+
// Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.
620620
//
621621
// Example:
622622
//
623-
// it := sm.ForEach(func(k string, v AttributeValue) {
623+
// it := sm.Range(func(k string, v AttributeValue) {
624624
// ...
625625
// })
626-
func (am AttributeMap) ForEach(f func(k string, v AttributeValue)) {
626+
func (am AttributeMap) Range(f func(k string, v AttributeValue) bool) {
627627
for i := range *am.orig {
628628
kv := &(*am.orig)[i]
629-
f(kv.Key, AttributeValue{&kv.Value})
629+
if !f(kv.Key, AttributeValue{&kv.Value}) {
630+
break
631+
}
630632
}
631633
}
632634

@@ -761,22 +763,24 @@ func (sm StringMap) Upsert(k, v string) {
761763
// Len returns the length of this map.
762764
//
763765
// Because the AttributeMap is represented internally by a slice of pointers, and the data are comping from the wire,
764-
// it is possible that when iterating using "ForEach" to get access to fewer elements because nil elements are skipped.
766+
// it is possible that when iterating using "Range" to get access to fewer elements because nil elements are skipped.
765767
func (sm StringMap) Len() int {
766768
return len(*sm.orig)
767769
}
768770

769-
// ForEach iterates over the every elements in the map by calling the provided func.
771+
// Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.
770772
//
771773
// Example:
772774
//
773-
// it := sm.ForEach(func(k string, v StringValue) {
775+
// it := sm.Range(func(k string, v StringValue) {
774776
// ...
775777
// })
776-
func (sm StringMap) ForEach(f func(k string, v string)) {
778+
func (sm StringMap) Range(f func(k string, v string) bool) {
777779
for i := range *sm.orig {
778780
skv := &(*sm.orig)[i]
779-
f(skv.Key, skv.Value)
781+
if !f(skv.Key, skv.Value) {
782+
break
783+
}
780784
}
781785
}
782786

consumer/pdata/common_test.go

+31-11
Original file line numberDiff line numberDiff line change
@@ -540,13 +540,14 @@ func TestAttributeMapWithEmpty(t *testing.T) {
540540
}
541541

542542
func TestAttributeMapIterationNil(t *testing.T) {
543-
NewAttributeMap().ForEach(func(k string, v AttributeValue) {
543+
NewAttributeMap().Range(func(k string, v AttributeValue) bool {
544544
// Fail if any element is returned
545545
t.Fail()
546+
return true
546547
})
547548
}
548549

549-
func TestAttributeMap_ForEach(t *testing.T) {
550+
func TestAttributeMap_Range(t *testing.T) {
550551
rawMap := map[string]AttributeValue{
551552
"k_string": NewAttributeValueString("123"),
552553
"k_int": NewAttributeValueInt(123),
@@ -555,11 +556,19 @@ func TestAttributeMap_ForEach(t *testing.T) {
555556
"k_null": NewAttributeValueNull(),
556557
}
557558
am := NewAttributeMap().InitFromMap(rawMap)
558-
assert.EqualValues(t, 5, am.Len())
559+
assert.Equal(t, 5, am.Len())
559560

560-
am.ForEach(func(k string, v AttributeValue) {
561+
calls := 0
562+
am.Range(func(k string, v AttributeValue) bool {
563+
calls++
564+
return false
565+
})
566+
assert.Equal(t, 1, calls)
567+
568+
am.Range(func(k string, v AttributeValue) bool {
561569
assert.True(t, v.Equal(rawMap[k]))
562570
delete(rawMap, k)
571+
return true
563572
})
564573
assert.EqualValues(t, 0, len(rawMap))
565574
}
@@ -837,20 +846,29 @@ func TestStringMap(t *testing.T) {
837846
}
838847

839848
func TestStringMapIterationNil(t *testing.T) {
840-
NewStringMap().ForEach(func(k string, v string) {
849+
NewStringMap().Range(func(k string, v string) bool {
841850
// Fail if any element is returned
842851
t.Fail()
852+
return true
843853
})
844854
}
845855

846-
func TestStringMap_ForEach(t *testing.T) {
856+
func TestStringMap_Range(t *testing.T) {
847857
rawMap := map[string]string{"k0": "v0", "k1": "v1", "k2": "v2"}
848858
sm := NewStringMap().InitFromMap(rawMap)
849859
assert.EqualValues(t, 3, sm.Len())
850860

851-
sm.ForEach(func(k string, v string) {
861+
calls := 0
862+
sm.Range(func(k string, v string) bool {
863+
calls++
864+
return false
865+
})
866+
assert.Equal(t, 1, calls)
867+
868+
sm.Range(func(k string, v string) bool {
852869
assert.EqualValues(t, rawMap[k], v)
853870
delete(rawMap, k)
871+
return true
854872
})
855873
assert.EqualValues(t, 0, len(rawMap))
856874
}
@@ -949,7 +967,7 @@ func BenchmarkAttributeValue_SetIntVal(b *testing.B) {
949967
}
950968
}
951969

952-
func BenchmarkAttributeMap_ForEach(b *testing.B) {
970+
func BenchmarkAttributeMap_Range(b *testing.B) {
953971
const numElements = 20
954972
rawOrig := make([]otlpcommon.KeyValue, numElements)
955973
for i := 0; i < numElements; i++ {
@@ -964,8 +982,9 @@ func BenchmarkAttributeMap_ForEach(b *testing.B) {
964982
b.ResetTimer()
965983
for n := 0; n < b.N; n++ {
966984
numEls := 0
967-
am.ForEach(func(k string, v AttributeValue) {
985+
am.Range(func(k string, v AttributeValue) bool {
968986
numEls++
987+
return true
969988
})
970989
if numEls != numElements {
971990
b.Fail()
@@ -995,7 +1014,7 @@ func BenchmarkAttributeMap_RangeOverMap(b *testing.B) {
9951014
}
9961015
}
9971016

998-
func BenchmarkStringMap_ForEach(b *testing.B) {
1017+
func BenchmarkStringMap_Range(b *testing.B) {
9991018
const numElements = 20
10001019
rawOrig := make([]otlpcommon.StringKeyValue, numElements)
10011020
for i := 0; i < numElements; i++ {
@@ -1010,8 +1029,9 @@ func BenchmarkStringMap_ForEach(b *testing.B) {
10101029
b.ResetTimer()
10111030
for n := 0; n < b.N; n++ {
10121031
numEls := 0
1013-
sm.ForEach(func(s string, value string) {
1032+
sm.Range(func(s string, value string) bool {
10141033
numEls++
1034+
return true
10151035
})
10161036
if numEls != numElements {
10171037
b.Fail()

exporter/exporterhelper/resource_to_label.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ func extractLabelsFromResource(resource *pdata.Resource) pdata.StringMap {
6060
labelMap := pdata.NewStringMap()
6161

6262
attrMap := resource.Attributes()
63-
attrMap.ForEach(func(k string, av pdata.AttributeValue) {
63+
attrMap.Range(func(k string, av pdata.AttributeValue) bool {
6464
stringLabel := tracetranslator.AttributeValueToString(av, false)
6565
labelMap.Upsert(k, stringLabel)
66+
return true
6667
})
6768
return labelMap
6869
}
@@ -110,7 +111,8 @@ func addLabelsToDoubleHistogramDataPoints(ps pdata.HistogramDataPointSlice, newL
110111
}
111112

112113
func joinStringMaps(from, to pdata.StringMap) {
113-
from.ForEach(func(k, v string) {
114+
from.Range(func(k, v string) bool {
114115
to.Upsert(k, v)
116+
return true
115117
})
116118
}

exporter/loggingexporter/logging_exporter.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ func (b *logDataBuffer) logAttributeMap(label string, am pdata.AttributeMap) {
5050
}
5151

5252
b.logEntry("%s:", label)
53-
am.ForEach(func(k string, v pdata.AttributeValue) {
53+
am.Range(func(k string, v pdata.AttributeValue) bool {
5454
b.logEntry(" -> %s: %s(%s)", k, v.Type().String(), attributeValueToString(v))
55+
return true
5556
})
5657
}
5758

@@ -61,8 +62,9 @@ func (b *logDataBuffer) logStringMap(description string, sm pdata.StringMap) {
6162
}
6263

6364
b.logEntry("%s:", description)
64-
sm.ForEach(func(k string, v string) {
65+
sm.Range(func(k string, v string) bool {
6566
b.logEntry(" -> %s: %s", k, v)
67+
return true
6668
})
6769
}
6870

@@ -239,8 +241,9 @@ func (b *logDataBuffer) logEvents(description string, se pdata.SpanEventSlice) {
239241
continue
240242
}
241243
b.logEntry(" -> Attributes:")
242-
e.Attributes().ForEach(func(k string, v pdata.AttributeValue) {
244+
e.Attributes().Range(func(k string, v pdata.AttributeValue) bool {
243245
b.logEntry(" -> %s: %s(%s)", k, v.Type().String(), attributeValueToString(v))
246+
return true
244247
})
245248
}
246249
}
@@ -263,8 +266,9 @@ func (b *logDataBuffer) logLinks(description string, sl pdata.SpanLinkSlice) {
263266
continue
264267
}
265268
b.logEntry(" -> Attributes:")
266-
l.Attributes().ForEach(func(k string, v pdata.AttributeValue) {
269+
l.Attributes().Range(func(k string, v pdata.AttributeValue) bool {
267270
b.logEntry(" -> %s: %s(%s)", k, v.Type().String(), attributeValueToString(v))
271+
return true
268272
})
269273
}
270274
}
@@ -307,8 +311,9 @@ func attributeMapToString(av pdata.AttributeMap) string {
307311
var b strings.Builder
308312
b.WriteString("{\n")
309313

310-
av.Sort().ForEach(func(k string, v pdata.AttributeValue) {
314+
av.Sort().Range(func(k string, v pdata.AttributeValue) bool {
311315
fmt.Fprintf(&b, " -> %s: %s(%s)\n", k, v.Type(), tracetranslator.AttributeValueToString(v, false))
316+
return true
312317
})
313318
b.WriteByte('}')
314319
return b.String()

exporter/prometheusexporter/accumulator.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,9 @@ func timeseriesSignature(ilmName string, metric pdata.Metric, labels pdata.Strin
376376
b.WriteString(metric.DataType().String())
377377
b.WriteString("*" + ilmName)
378378
b.WriteString("*" + metric.Name())
379-
labels.ForEach(func(k string, v string) {
379+
labels.Range(func(k string, v string) bool {
380380
b.WriteString("*" + k + "*" + v)
381+
return true
381382
})
382383
return b.String()
383384
}

exporter/prometheusexporter/accumulator_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,10 @@ func TestAccumulateMetrics(t *testing.T) {
344344

345345
require.Equal(t, v.instrumentationLibrary.Name(), ilm.InstrumentationLibrary().Name())
346346
require.Equal(t, v.value.DataType(), m2.DataType())
347-
vLabels.ForEach(func(k, v string) {
347+
vLabels.Range(func(k, v string) bool {
348348
r, _ := m2Labels.Get(k)
349349
require.Equal(t, r, v)
350+
return true
350351
})
351352
require.Equal(t, m2Labels.Len(), vLabels.Len())
352353
require.Equal(t, m2Value, vValue)

exporter/prometheusexporter/collector.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ func (c *collector) getMetricMetadata(metric pdata.Metric, labels pdata.StringMa
8888
keys := make([]string, 0, labels.Len())
8989
values := make([]string, 0, labels.Len())
9090

91-
labels.ForEach(func(k string, v string) {
91+
labels.Range(func(k string, v string) bool {
9292
keys = append(keys, sanitize(k))
9393
values = append(values, v)
94+
return true
9495
})
9596

9697
return prometheus.NewDesc(

processor/filterprocessor/expr_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,12 @@ func testFilter(t *testing.T, mdType pdata.MetricDataType) {
130130
}
131131

132132
func assertFiltered(t *testing.T, lm pdata.StringMap) {
133-
lm.ForEach(func(k string, v string) {
133+
lm.Range(func(k string, v string) bool {
134134
if k == filteredLblKey && v == filteredLblVal {
135135
assert.Fail(t, "found metric that should have been filtered out")
136+
return false
136137
}
138+
return true
137139
})
138140
}
139141

testbed/correctness/metrics/metric_diff.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,9 @@ func diffValues(
314314

315315
func attrMapToString(m pdata.AttributeMap) string {
316316
out := ""
317-
m.ForEach(func(k string, v pdata.AttributeValue) {
317+
m.Range(func(k string, v pdata.AttributeValue) bool {
318318
out += "[" + k + "=" + v.StringVal() + "]"
319+
return true
319320
})
320321
return out
321322
}

testbed/testbed/validator.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ func (v *CorrectnessTestValidator) diffSpanStatus(sentSpan pdata.Span, recdSpan
414414

415415
func (v *CorrectnessTestValidator) diffAttributeMap(spanName string,
416416
sentAttrs pdata.AttributeMap, recdAttrs pdata.AttributeMap, fmtStr string) {
417-
sentAttrs.ForEach(func(sentKey string, sentVal pdata.AttributeValue) {
417+
sentAttrs.Range(func(sentKey string, sentVal pdata.AttributeValue) bool {
418418
recdVal, ok := recdAttrs.Get(sentKey)
419419
if !ok {
420420
af := &TraceAssertionFailure{
@@ -425,14 +425,15 @@ func (v *CorrectnessTestValidator) diffAttributeMap(spanName string,
425425
actualValue: nil,
426426
}
427427
v.assertionFailures = append(v.assertionFailures, af)
428-
return
428+
return true
429429
}
430430
switch sentVal.Type() {
431431
case pdata.AttributeValueMAP:
432432
v.compareKeyValueList(spanName, sentVal, recdVal, fmtStr, sentKey)
433433
default:
434434
v.compareSimpleValues(spanName, sentVal, recdVal, fmtStr, sentKey)
435435
}
436+
return true
436437
})
437438
}
438439

testbed/tests/resource_processor_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,12 @@ func TestMetricResourceProcessor(t *testing.T) {
289289
})
290290
}
291291
}
292+
293+
func attributesToMap(attributes pdata.AttributeMap) map[string]pdata.AttributeValue {
294+
out := map[string]pdata.AttributeValue{}
295+
attributes.Range(func(k string, v pdata.AttributeValue) bool {
296+
out[k] = v
297+
return true
298+
})
299+
return out
300+
}

testbed/tests/utils.go

-25
This file was deleted.

0 commit comments

Comments
 (0)