Skip to content

Commit

Permalink
Replace InitEmptyWithCapacity with EnsureCapacity
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Mar 30, 2021
1 parent 2dbc66c commit a316b78
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 86 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Remove `configtest.NewViperFromYamlFile()`, use `config.Parser.NewParserFromFile()` (#2806)
- Move `config.ViperSubExact()` to use `config.Parser.Sub()` (#2806)
- Update LoadReceiver signature to remove unused params (#2823)
- Replace InitEmptyWithCapacity with EnsureCapacity (#2845)

## 💡 Enhancements 💡

Expand Down
24 changes: 14 additions & 10 deletions consumer/pdata/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,15 @@ func (am AttributeMap) InitFromMap(attrMap map[string]AttributeValue) AttributeM
return am
}

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

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

// InitEmptyWithCapacity constructs an empty StringMap with predefined slice capacity.
func (sm StringMap) InitEmptyWithCapacity(cap int) {
if cap == 0 {
*sm.orig = []otlpcommon.StringKeyValue(nil)
// EnsureCapacity increases the capacity of this StringMap instance, if necessary,
// to ensure that it can hold at least the number of elements specified by the capacity argument.
func (sm StringMap) EnsureCapacity(capacity int) {
if capacity <= cap(*sm.orig) {
return
}
*sm.orig = make([]otlpcommon.StringKeyValue, 0, cap)
oldOrig := *sm.orig
*sm.orig = make([]otlpcommon.StringKeyValue, 0, capacity)
copy(*sm.orig, oldOrig)
}

// Get returns the StringValue associated with the key and true,
Expand Down
38 changes: 32 additions & 6 deletions consumer/pdata/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,11 +669,24 @@ func TestAttributeMap_Update(t *testing.T) {
assert.EqualValues(t, 123, av2.IntVal())
}

func TestAttributeMap_InitEmptyWithCapacity(t *testing.T) {
func TestAttributeMap_EnsureCapacity_Zero(t *testing.T) {
am := NewAttributeMap()
am.InitEmptyWithCapacity(0)
assert.Equal(t, NewAttributeMap(), am)
am.EnsureCapacity(0)
assert.Equal(t, 0, am.Len())
assert.Equal(t, 0, cap(*am.orig))
}

func TestAttributeMap_EnsureCapacity(t *testing.T) {
am := NewAttributeMap()
am.EnsureCapacity(5)
assert.Equal(t, 0, am.Len())
assert.Equal(t, 5, cap(*am.orig))
am.EnsureCapacity(3)
assert.Equal(t, 0, am.Len())
assert.Equal(t, 5, cap(*am.orig))
am.EnsureCapacity(8)
assert.Equal(t, 0, am.Len())
assert.Equal(t, 8, cap(*am.orig))
}

func TestNilStringMap(t *testing.T) {
Expand Down Expand Up @@ -846,11 +859,24 @@ func TestStringMap_CopyTo(t *testing.T) {
assert.EqualValues(t, generateTestStringMap(), dest)
}

func TestStringMap_InitEmptyWithCapacity(t *testing.T) {
func TestStringMap_EnsureCapacity_Zero(t *testing.T) {
sm := NewStringMap()
sm.InitEmptyWithCapacity(0)
assert.Equal(t, NewStringMap(), sm)
sm.EnsureCapacity(0)
assert.Equal(t, 0, sm.Len())
assert.Equal(t, 0, cap(*sm.orig))
}

func TestStringMap_EnsureCapacity(t *testing.T) {
sm := NewStringMap()
sm.EnsureCapacity(5)
assert.Equal(t, 0, sm.Len())
assert.Equal(t, 5, cap(*sm.orig))
sm.EnsureCapacity(3)
assert.Equal(t, 0, sm.Len())
assert.Equal(t, 5, cap(*sm.orig))
sm.EnsureCapacity(8)
assert.Equal(t, 0, sm.Len())
assert.Equal(t, 8, cap(*sm.orig))
}

func TestStringMap_InitFromMap(t *testing.T) {
Expand Down
2 changes: 0 additions & 2 deletions consumer/simple/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,6 @@ func BenchmarkPdataMetrics(b *testing.B) {
{
dp := dps.At(0)
labels := dp.LabelsMap()
labels.InitEmptyWithCapacity(3)
labels.Insert("env", "prod")
labels.Insert("app", "myapp")
labels.Insert("version", "1.0")
Expand All @@ -514,7 +513,6 @@ func BenchmarkPdataMetrics(b *testing.B) {
{
dp := dps.At(1)
labels := dp.LabelsMap()
labels.InitEmptyWithCapacity(3)
labels.Insert("env", "prod")
labels.Insert("app", "myapp")
labels.Insert("version", "1.0")
Expand Down
11 changes: 8 additions & 3 deletions exporter/opencensusexporter/opencensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/internal/testdata"
"go.opentelemetry.io/collector/receiver/opencensusreceiver"
"go.opentelemetry.io/collector/testutil"
Expand Down Expand Up @@ -75,14 +76,18 @@ func TestSendTraces(t *testing.T) {

sink.Reset()
// Sending data no Node.
td.ResourceSpans().At(0).Resource().Attributes().InitEmptyWithCapacity(0)
attrs := td.ResourceSpans().At(0).Resource().Attributes()
attrs.ForEach(func(k string, _ pdata.AttributeValue) {
attrs.Delete(k)
})
td = td.Clone()
assert.NoError(t, exp.ConsumeTraces(context.Background(), td))
testutil.WaitFor(t, func() bool {
return len(sink.AllTraces()) == 1
})
traces = sink.AllTraces()
require.Len(t, traces, 1)
assert.Equal(t, td, traces[0])
assert.EqualValues(t, td, traces[0])
}

func TestSendTraces_NoBackend(t *testing.T) {
Expand Down Expand Up @@ -172,7 +177,7 @@ func TestSendMetrics(t *testing.T) {

// Sending data no node.
sink.Reset()
md.ResourceMetrics().At(0).Resource().Attributes().InitEmptyWithCapacity(0)
md.ResourceMetrics().At(0).Resource().Attributes().EnsureCapacity(0)
assert.NoError(t, exp.ConsumeMetrics(context.Background(), md))
testutil.WaitFor(t, func() bool {
return len(sink.AllMetrics()) == 1
Expand Down
14 changes: 3 additions & 11 deletions processor/resourceprocessor/resource_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ type resourceProcessor struct {
func (rp *resourceProcessor) ProcessTraces(_ context.Context, td pdata.Traces) (pdata.Traces, error) {
rss := td.ResourceSpans()
for i := 0; i < rss.Len(); i++ {
resource := rss.At(i).Resource()
attrs := resource.Attributes()
rp.attrProc.Process(attrs)
rp.attrProc.Process(rss.At(i).Resource().Attributes())
}
return td, nil
}
Expand All @@ -40,11 +38,7 @@ func (rp *resourceProcessor) ProcessTraces(_ context.Context, td pdata.Traces) (
func (rp *resourceProcessor) ProcessMetrics(_ context.Context, md pdata.Metrics) (pdata.Metrics, error) {
rms := md.ResourceMetrics()
for i := 0; i < rms.Len(); i++ {
resource := rms.At(i).Resource()
if resource.Attributes().Len() == 0 {
resource.Attributes().InitEmptyWithCapacity(1)
}
rp.attrProc.Process(resource.Attributes())
rp.attrProc.Process(rms.At(i).Resource().Attributes())
}
return md, nil
}
Expand All @@ -53,9 +47,7 @@ func (rp *resourceProcessor) ProcessMetrics(_ context.Context, md pdata.Metrics)
func (rp *resourceProcessor) ProcessLogs(_ context.Context, ld pdata.Logs) (pdata.Logs, error) {
rls := ld.ResourceLogs()
for i := 0; i < rls.Len(); i++ {
resource := rls.At(i).Resource()
attrs := resource.Attributes()
rp.attrProc.Process(attrs)
rp.attrProc.Process(rls.At(i).Resource().Attributes())
}
return ld, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,45 +49,25 @@ type commandMetadata struct {

func (m *processMetadata) initializeResource(resource pdata.Resource) {
attr := resource.Attributes()
attr.InitEmptyWithCapacity(6)
m.insertPid(attr)
m.insertExecutable(attr)
m.insertCommand(attr)
m.insertUsername(attr)
}

func (m *processMetadata) insertPid(attr pdata.AttributeMap) {
attr.EnsureCapacity(6)
attr.InsertInt(conventions.AttributeProcessID, int64(m.pid))
}

func (m *processMetadata) insertExecutable(attr pdata.AttributeMap) {
attr.InsertString(conventions.AttributeProcessExecutableName, m.executable.name)
attr.InsertString(conventions.AttributeProcessExecutablePath, m.executable.path)
}

func (m *processMetadata) insertCommand(attr pdata.AttributeMap) {
if m.command == nil {
return
if m.command != nil {
attr.InsertString(conventions.AttributeProcessCommand, m.command.command)
if m.command.commandLineSlice != nil {
// TODO insert slice here once this is supported by the data model
// (see https://github.com/open-telemetry/opentelemetry-collector/pull/1142)
attr.InsertString(conventions.AttributeProcessCommandLine, strings.Join(m.command.commandLineSlice, " "))
} else {
attr.InsertString(conventions.AttributeProcessCommandLine, m.command.commandLine)
}
}

attr.InsertString(conventions.AttributeProcessCommand, m.command.command)
if m.command.commandLineSlice != nil {
// TODO insert slice here once this is supported by the data model
// (see https://github.com/open-telemetry/opentelemetry-collector/pull/1142)
attr.InsertString(conventions.AttributeProcessCommandLine, strings.Join(m.command.commandLineSlice, " "))
} else {
attr.InsertString(conventions.AttributeProcessCommandLine, m.command.commandLine)
if m.username != "" {
attr.InsertString(conventions.AttributeProcessOwner, m.username)
}
}

func (m *processMetadata) insertUsername(attr pdata.AttributeMap) {
if m.username == "" {
return
}

attr.InsertString(conventions.AttributeProcessOwner, m.username)
}

// processHandles provides a wrapper around []*process.Process
// to support testing

Expand Down
4 changes: 2 additions & 2 deletions testbed/testbed/data_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (dp *PerfTestDataProvider) GenerateMetrics() (pdata.Metrics, bool) {
md.ResourceMetrics().At(0).InstrumentationLibraryMetrics().Resize(1)
if dp.options.Attributes != nil {
attrs := md.ResourceMetrics().At(0).Resource().Attributes()
attrs.InitEmptyWithCapacity(len(dp.options.Attributes))
attrs.EnsureCapacity(len(dp.options.Attributes))
for k, v := range dp.options.Attributes {
attrs.UpsertString(k, v)
}
Expand Down Expand Up @@ -177,7 +177,7 @@ func (dp *PerfTestDataProvider) GenerateLogs() (pdata.Logs, bool) {
logs.ResourceLogs().At(0).InstrumentationLibraryLogs().Resize(1)
if dp.options.Attributes != nil {
attrs := logs.ResourceLogs().At(0).Resource().Attributes()
attrs.InitEmptyWithCapacity(len(dp.options.Attributes))
attrs.EnsureCapacity(len(dp.options.Attributes))
for k, v := range dp.options.Attributes {
attrs.UpsertString(k, v)
}
Expand Down
4 changes: 2 additions & 2 deletions translator/internaldata/oc_to_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func setLabelsMap(ocLabelsKeys []*ocmetrics.LabelKey, ocLabelValues []*ocmetrics
lablesCount = len(ocLabelValues)
}

labelsMap.InitEmptyWithCapacity(lablesCount)
labelsMap.EnsureCapacity(lablesCount)
for i := 0; i < lablesCount; i++ {
if !ocLabelValues[i].GetHasValue() {
continue
Expand Down Expand Up @@ -417,7 +417,7 @@ func exemplarToMetrics(ocExemplar *ocmetrics.DistributionValue_Exemplar, exempla
exemplar.SetValue(ocExemplar.GetValue())
attachments := exemplar.FilteredLabels()
ocAttachments := ocExemplar.GetAttachments()
attachments.InitEmptyWithCapacity(len(ocAttachments))
attachments.EnsureCapacity(len(ocAttachments))
for k, v := range ocAttachments {
attachments.Upsert(k, v)
}
Expand Down
2 changes: 1 addition & 1 deletion translator/internaldata/oc_to_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func ocNodeResourceToInternal(ocNode *occommon.Node, ocResource *ocresource.Reso
}

attrs := dest.Attributes()
attrs.InitEmptyWithCapacity(maxTotalAttrCount)
attrs.EnsureCapacity(maxTotalAttrCount)

if ocNode != nil {
// Copy all Attributes.
Expand Down
6 changes: 1 addition & 5 deletions translator/internaldata/oc_to_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,17 @@ func initAttributeMapFromOC(ocAttrs *octrace.Span_Attributes, dest pdata.Attribu
}

if len(ocAttrs.AttributeMap) > 0 {
dest.InitEmptyWithCapacity(len(ocAttrs.AttributeMap))
dest.EnsureCapacity(len(ocAttrs.AttributeMap))
for key, ocAttr := range ocAttrs.AttributeMap {
switch attribValue := ocAttr.Value.(type) {
case *octrace.AttributeValue_StringValue:
dest.UpsertString(key, attribValue.StringValue.GetValue())

case *octrace.AttributeValue_IntValue:
dest.UpsertInt(key, attribValue.IntValue)

case *octrace.AttributeValue_BoolValue:
dest.UpsertBool(key, attribValue.BoolValue)

case *octrace.AttributeValue_DoubleValue:
dest.UpsertDouble(key, attribValue.DoubleValue)

default:
dest.UpsertString(key, "<Unknown OpenCensus attribute value type>")
}
Expand Down
8 changes: 4 additions & 4 deletions translator/trace/jaeger/jaegerproto_to_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ func jProcessToInternalResource(process *model.Process, dest pdata.Resource) {

attrs := dest.Attributes()
if serviceName != "" {
attrs.InitEmptyWithCapacity(len(tags) + 1)
attrs.EnsureCapacity(len(tags) + 1)
attrs.UpsertString(conventions.AttributeServiceName, serviceName)
} else {
attrs.InitEmptyWithCapacity(len(tags))
attrs.EnsureCapacity(len(tags))
}
jTagsToInternalAttributes(tags, attrs)

Expand Down Expand Up @@ -176,7 +176,7 @@ func jSpanToInternal(span *model.Span) (pdata.Span, instrumentationLibrary) {
}

attrs := dest.Attributes()
attrs.InitEmptyWithCapacity(len(span.Tags))
attrs.EnsureCapacity(len(span.Tags))
jTagsToInternalAttributes(span.Tags, attrs)
setInternalSpanStatus(attrs, dest.Status())
if spanKindAttr, ok := attrs.Get(tracetranslator.TagSpanKind); ok {
Expand Down Expand Up @@ -332,7 +332,7 @@ func jLogsToSpanEvents(logs []model.Log, dest pdata.SpanEventSlice) {
}

attrs := event.Attributes()
attrs.InitEmptyWithCapacity(len(log.Fields))
attrs.EnsureCapacity(len(log.Fields))
jTagsToInternalAttributes(log.Fields, attrs)
if name, ok := attrs.Get(tracetranslator.TagMessage); ok {
event.SetName(name.StringVal())
Expand Down
8 changes: 4 additions & 4 deletions translator/trace/jaeger/jaegerthrift_to_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ func jThriftProcessToInternalResource(process *jaeger.Process, dest pdata.Resour

attrs := dest.Attributes()
if serviceName != "" {
attrs.InitEmptyWithCapacity(len(tags) + 1)
attrs.EnsureCapacity(len(tags) + 1)
attrs.UpsertString(conventions.AttributeServiceName, serviceName)
} else {
attrs.InitEmptyWithCapacity(len(tags))
attrs.EnsureCapacity(len(tags))
}
jThriftTagsToInternalAttributes(tags, attrs)

Expand Down Expand Up @@ -109,7 +109,7 @@ func jThriftSpanToInternal(span *jaeger.Span, dest pdata.Span) {
}

attrs := dest.Attributes()
attrs.InitEmptyWithCapacity(len(span.Tags))
attrs.EnsureCapacity(len(span.Tags))
jThriftTagsToInternalAttributes(span.Tags, attrs)
setInternalSpanStatus(attrs, dest.Status())
if spanKindAttr, ok := attrs.Get(tracetranslator.TagSpanKind); ok {
Expand Down Expand Up @@ -162,7 +162,7 @@ func jThriftLogsToSpanEvents(logs []*jaeger.Log, dest pdata.SpanEventSlice) {
}

attrs := event.Attributes()
attrs.InitEmptyWithCapacity(len(log.Fields))
attrs.EnsureCapacity(len(log.Fields))
jThriftTagsToInternalAttributes(log.Fields, attrs)
if name, ok := attrs.Get(tracetranslator.TagMessage); ok {
event.SetName(name.StringVal())
Expand Down
2 changes: 1 addition & 1 deletion translator/trace/zipkin/zipkinv2_to_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func zSpanToInternal(zspan *zipkinmodel.SpanModel, tags map[string]string, dest
}

attrs := dest.Attributes()
attrs.InitEmptyWithCapacity(len(tags))
attrs.EnsureCapacity(len(tags))
if err := zTagsToInternalAttrs(zspan, tags, attrs, parseStringTags); err != nil {
return err
}
Expand Down
Loading

0 comments on commit a316b78

Please sign in to comment.