Skip to content

Commit a316b78

Browse files
committed
Replace InitEmptyWithCapacity with EnsureCapacity
Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 2dbc66c commit a316b78

File tree

15 files changed

+85
-86
lines changed

15 files changed

+85
-86
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- Remove `configtest.NewViperFromYamlFile()`, use `config.Parser.NewParserFromFile()` (#2806)
2020
- Move `config.ViperSubExact()` to use `config.Parser.Sub()` (#2806)
2121
- Update LoadReceiver signature to remove unused params (#2823)
22+
- Replace InitEmptyWithCapacity with EnsureCapacity (#2845)
2223

2324
## 💡 Enhancements 💡
2425

consumer/pdata/common.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,15 @@ func (am AttributeMap) InitFromMap(attrMap map[string]AttributeValue) AttributeM
391391
return am
392392
}
393393

394-
// InitEmptyWithCapacity constructs an empty AttributeMap with predefined slice capacity.
395-
func (am AttributeMap) InitEmptyWithCapacity(cap int) {
396-
if cap == 0 {
397-
*am.orig = []otlpcommon.KeyValue(nil)
394+
// EnsureCapacity increases the capacity of this AttributeMap instance, if necessary,
395+
// to ensure that it can hold at least the number of elements specified by the capacity argument.
396+
func (am AttributeMap) EnsureCapacity(capacity int) {
397+
if capacity <= cap(*am.orig) {
398398
return
399399
}
400-
*am.orig = make([]otlpcommon.KeyValue, 0, cap)
400+
oldOrig := *am.orig
401+
*am.orig = make([]otlpcommon.KeyValue, 0, capacity)
402+
copy(*am.orig, oldOrig)
401403
}
402404

403405
// Get returns the AttributeValue associated with the key and true. Returned
@@ -684,13 +686,15 @@ func (sm StringMap) InitFromMap(attrMap map[string]string) StringMap {
684686
return sm
685687
}
686688

687-
// InitEmptyWithCapacity constructs an empty StringMap with predefined slice capacity.
688-
func (sm StringMap) InitEmptyWithCapacity(cap int) {
689-
if cap == 0 {
690-
*sm.orig = []otlpcommon.StringKeyValue(nil)
689+
// EnsureCapacity increases the capacity of this StringMap instance, if necessary,
690+
// to ensure that it can hold at least the number of elements specified by the capacity argument.
691+
func (sm StringMap) EnsureCapacity(capacity int) {
692+
if capacity <= cap(*sm.orig) {
691693
return
692694
}
693-
*sm.orig = make([]otlpcommon.StringKeyValue, 0, cap)
695+
oldOrig := *sm.orig
696+
*sm.orig = make([]otlpcommon.StringKeyValue, 0, capacity)
697+
copy(*sm.orig, oldOrig)
694698
}
695699

696700
// Get returns the StringValue associated with the key and true,

consumer/pdata/common_test.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -669,11 +669,24 @@ func TestAttributeMap_Update(t *testing.T) {
669669
assert.EqualValues(t, 123, av2.IntVal())
670670
}
671671

672-
func TestAttributeMap_InitEmptyWithCapacity(t *testing.T) {
672+
func TestAttributeMap_EnsureCapacity_Zero(t *testing.T) {
673673
am := NewAttributeMap()
674-
am.InitEmptyWithCapacity(0)
675-
assert.Equal(t, NewAttributeMap(), am)
674+
am.EnsureCapacity(0)
676675
assert.Equal(t, 0, am.Len())
676+
assert.Equal(t, 0, cap(*am.orig))
677+
}
678+
679+
func TestAttributeMap_EnsureCapacity(t *testing.T) {
680+
am := NewAttributeMap()
681+
am.EnsureCapacity(5)
682+
assert.Equal(t, 0, am.Len())
683+
assert.Equal(t, 5, cap(*am.orig))
684+
am.EnsureCapacity(3)
685+
assert.Equal(t, 0, am.Len())
686+
assert.Equal(t, 5, cap(*am.orig))
687+
am.EnsureCapacity(8)
688+
assert.Equal(t, 0, am.Len())
689+
assert.Equal(t, 8, cap(*am.orig))
677690
}
678691

679692
func TestNilStringMap(t *testing.T) {
@@ -846,11 +859,24 @@ func TestStringMap_CopyTo(t *testing.T) {
846859
assert.EqualValues(t, generateTestStringMap(), dest)
847860
}
848861

849-
func TestStringMap_InitEmptyWithCapacity(t *testing.T) {
862+
func TestStringMap_EnsureCapacity_Zero(t *testing.T) {
850863
sm := NewStringMap()
851-
sm.InitEmptyWithCapacity(0)
852-
assert.Equal(t, NewStringMap(), sm)
864+
sm.EnsureCapacity(0)
865+
assert.Equal(t, 0, sm.Len())
866+
assert.Equal(t, 0, cap(*sm.orig))
867+
}
868+
869+
func TestStringMap_EnsureCapacity(t *testing.T) {
870+
sm := NewStringMap()
871+
sm.EnsureCapacity(5)
872+
assert.Equal(t, 0, sm.Len())
873+
assert.Equal(t, 5, cap(*sm.orig))
874+
sm.EnsureCapacity(3)
875+
assert.Equal(t, 0, sm.Len())
876+
assert.Equal(t, 5, cap(*sm.orig))
877+
sm.EnsureCapacity(8)
853878
assert.Equal(t, 0, sm.Len())
879+
assert.Equal(t, 8, cap(*sm.orig))
854880
}
855881

856882
func TestStringMap_InitFromMap(t *testing.T) {

consumer/simple/metrics_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,6 @@ func BenchmarkPdataMetrics(b *testing.B) {
504504
{
505505
dp := dps.At(0)
506506
labels := dp.LabelsMap()
507-
labels.InitEmptyWithCapacity(3)
508507
labels.Insert("env", "prod")
509508
labels.Insert("app", "myapp")
510509
labels.Insert("version", "1.0")
@@ -514,7 +513,6 @@ func BenchmarkPdataMetrics(b *testing.B) {
514513
{
515514
dp := dps.At(1)
516515
labels := dp.LabelsMap()
517-
labels.InitEmptyWithCapacity(3)
518516
labels.Insert("env", "prod")
519517
labels.Insert("app", "myapp")
520518
labels.Insert("version", "1.0")

exporter/opencensusexporter/opencensus_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"go.opentelemetry.io/collector/config/configgrpc"
2828
"go.opentelemetry.io/collector/config/configtls"
2929
"go.opentelemetry.io/collector/consumer/consumertest"
30+
"go.opentelemetry.io/collector/consumer/pdata"
3031
"go.opentelemetry.io/collector/internal/testdata"
3132
"go.opentelemetry.io/collector/receiver/opencensusreceiver"
3233
"go.opentelemetry.io/collector/testutil"
@@ -75,14 +76,18 @@ func TestSendTraces(t *testing.T) {
7576

7677
sink.Reset()
7778
// Sending data no Node.
78-
td.ResourceSpans().At(0).Resource().Attributes().InitEmptyWithCapacity(0)
79+
attrs := td.ResourceSpans().At(0).Resource().Attributes()
80+
attrs.ForEach(func(k string, _ pdata.AttributeValue) {
81+
attrs.Delete(k)
82+
})
83+
td = td.Clone()
7984
assert.NoError(t, exp.ConsumeTraces(context.Background(), td))
8085
testutil.WaitFor(t, func() bool {
8186
return len(sink.AllTraces()) == 1
8287
})
8388
traces = sink.AllTraces()
8489
require.Len(t, traces, 1)
85-
assert.Equal(t, td, traces[0])
90+
assert.EqualValues(t, td, traces[0])
8691
}
8792

8893
func TestSendTraces_NoBackend(t *testing.T) {
@@ -172,7 +177,7 @@ func TestSendMetrics(t *testing.T) {
172177

173178
// Sending data no node.
174179
sink.Reset()
175-
md.ResourceMetrics().At(0).Resource().Attributes().InitEmptyWithCapacity(0)
180+
md.ResourceMetrics().At(0).Resource().Attributes().EnsureCapacity(0)
176181
assert.NoError(t, exp.ConsumeMetrics(context.Background(), md))
177182
testutil.WaitFor(t, func() bool {
178183
return len(sink.AllMetrics()) == 1

processor/resourceprocessor/resource_processor.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ type resourceProcessor struct {
2929
func (rp *resourceProcessor) ProcessTraces(_ context.Context, td pdata.Traces) (pdata.Traces, error) {
3030
rss := td.ResourceSpans()
3131
for i := 0; i < rss.Len(); i++ {
32-
resource := rss.At(i).Resource()
33-
attrs := resource.Attributes()
34-
rp.attrProc.Process(attrs)
32+
rp.attrProc.Process(rss.At(i).Resource().Attributes())
3533
}
3634
return td, nil
3735
}
@@ -40,11 +38,7 @@ func (rp *resourceProcessor) ProcessTraces(_ context.Context, td pdata.Traces) (
4038
func (rp *resourceProcessor) ProcessMetrics(_ context.Context, md pdata.Metrics) (pdata.Metrics, error) {
4139
rms := md.ResourceMetrics()
4240
for i := 0; i < rms.Len(); i++ {
43-
resource := rms.At(i).Resource()
44-
if resource.Attributes().Len() == 0 {
45-
resource.Attributes().InitEmptyWithCapacity(1)
46-
}
47-
rp.attrProc.Process(resource.Attributes())
41+
rp.attrProc.Process(rms.At(i).Resource().Attributes())
4842
}
4943
return md, nil
5044
}
@@ -53,9 +47,7 @@ func (rp *resourceProcessor) ProcessMetrics(_ context.Context, md pdata.Metrics)
5347
func (rp *resourceProcessor) ProcessLogs(_ context.Context, ld pdata.Logs) (pdata.Logs, error) {
5448
rls := ld.ResourceLogs()
5549
for i := 0; i < rls.Len(); i++ {
56-
resource := rls.At(i).Resource()
57-
attrs := resource.Attributes()
58-
rp.attrProc.Process(attrs)
50+
rp.attrProc.Process(rls.At(i).Resource().Attributes())
5951
}
6052
return ld, nil
6153
}

receiver/hostmetricsreceiver/internal/scraper/processscraper/process.go

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -49,45 +49,25 @@ type commandMetadata struct {
4949

5050
func (m *processMetadata) initializeResource(resource pdata.Resource) {
5151
attr := resource.Attributes()
52-
attr.InitEmptyWithCapacity(6)
53-
m.insertPid(attr)
54-
m.insertExecutable(attr)
55-
m.insertCommand(attr)
56-
m.insertUsername(attr)
57-
}
58-
59-
func (m *processMetadata) insertPid(attr pdata.AttributeMap) {
52+
attr.EnsureCapacity(6)
6053
attr.InsertInt(conventions.AttributeProcessID, int64(m.pid))
61-
}
62-
63-
func (m *processMetadata) insertExecutable(attr pdata.AttributeMap) {
6454
attr.InsertString(conventions.AttributeProcessExecutableName, m.executable.name)
6555
attr.InsertString(conventions.AttributeProcessExecutablePath, m.executable.path)
66-
}
67-
68-
func (m *processMetadata) insertCommand(attr pdata.AttributeMap) {
69-
if m.command == nil {
70-
return
56+
if m.command != nil {
57+
attr.InsertString(conventions.AttributeProcessCommand, m.command.command)
58+
if m.command.commandLineSlice != nil {
59+
// TODO insert slice here once this is supported by the data model
60+
// (see https://github.com/open-telemetry/opentelemetry-collector/pull/1142)
61+
attr.InsertString(conventions.AttributeProcessCommandLine, strings.Join(m.command.commandLineSlice, " "))
62+
} else {
63+
attr.InsertString(conventions.AttributeProcessCommandLine, m.command.commandLine)
64+
}
7165
}
72-
73-
attr.InsertString(conventions.AttributeProcessCommand, m.command.command)
74-
if m.command.commandLineSlice != nil {
75-
// TODO insert slice here once this is supported by the data model
76-
// (see https://github.com/open-telemetry/opentelemetry-collector/pull/1142)
77-
attr.InsertString(conventions.AttributeProcessCommandLine, strings.Join(m.command.commandLineSlice, " "))
78-
} else {
79-
attr.InsertString(conventions.AttributeProcessCommandLine, m.command.commandLine)
66+
if m.username != "" {
67+
attr.InsertString(conventions.AttributeProcessOwner, m.username)
8068
}
8169
}
8270

83-
func (m *processMetadata) insertUsername(attr pdata.AttributeMap) {
84-
if m.username == "" {
85-
return
86-
}
87-
88-
attr.InsertString(conventions.AttributeProcessOwner, m.username)
89-
}
90-
9171
// processHandles provides a wrapper around []*process.Process
9272
// to support testing
9373

testbed/testbed/data_providers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (dp *PerfTestDataProvider) GenerateMetrics() (pdata.Metrics, bool) {
132132
md.ResourceMetrics().At(0).InstrumentationLibraryMetrics().Resize(1)
133133
if dp.options.Attributes != nil {
134134
attrs := md.ResourceMetrics().At(0).Resource().Attributes()
135-
attrs.InitEmptyWithCapacity(len(dp.options.Attributes))
135+
attrs.EnsureCapacity(len(dp.options.Attributes))
136136
for k, v := range dp.options.Attributes {
137137
attrs.UpsertString(k, v)
138138
}
@@ -177,7 +177,7 @@ func (dp *PerfTestDataProvider) GenerateLogs() (pdata.Logs, bool) {
177177
logs.ResourceLogs().At(0).InstrumentationLibraryLogs().Resize(1)
178178
if dp.options.Attributes != nil {
179179
attrs := logs.ResourceLogs().At(0).Resource().Attributes()
180-
attrs.InitEmptyWithCapacity(len(dp.options.Attributes))
180+
attrs.EnsureCapacity(len(dp.options.Attributes))
181181
for k, v := range dp.options.Attributes {
182182
attrs.UpsertString(k, v)
183183
}

translator/internaldata/oc_to_metrics.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func setLabelsMap(ocLabelsKeys []*ocmetrics.LabelKey, ocLabelValues []*ocmetrics
250250
lablesCount = len(ocLabelValues)
251251
}
252252

253-
labelsMap.InitEmptyWithCapacity(lablesCount)
253+
labelsMap.EnsureCapacity(lablesCount)
254254
for i := 0; i < lablesCount; i++ {
255255
if !ocLabelValues[i].GetHasValue() {
256256
continue
@@ -417,7 +417,7 @@ func exemplarToMetrics(ocExemplar *ocmetrics.DistributionValue_Exemplar, exempla
417417
exemplar.SetValue(ocExemplar.GetValue())
418418
attachments := exemplar.FilteredLabels()
419419
ocAttachments := ocExemplar.GetAttachments()
420-
attachments.InitEmptyWithCapacity(len(ocAttachments))
420+
attachments.EnsureCapacity(len(ocAttachments))
421421
for k, v := range ocAttachments {
422422
attachments.Upsert(k, v)
423423
}

translator/internaldata/oc_to_resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func ocNodeResourceToInternal(ocNode *occommon.Node, ocResource *ocresource.Reso
8080
}
8181

8282
attrs := dest.Attributes()
83-
attrs.InitEmptyWithCapacity(maxTotalAttrCount)
83+
attrs.EnsureCapacity(maxTotalAttrCount)
8484

8585
if ocNode != nil {
8686
// Copy all Attributes.

0 commit comments

Comments
 (0)