Skip to content

Commit

Permalink
Remove some of the Append usages, replace with AppendEmpty (#3198)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Apr 21, 2021
1 parent c98d8ae commit ab68513
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 348 deletions.
50 changes: 11 additions & 39 deletions exporter/lokiexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,17 @@ var (

func createLogData(numberOfLogs int, attributes pdata.AttributeMap) pdata.Logs {
logs := pdata.NewLogs()
logs.ResourceLogs().Resize(1)
rl := logs.ResourceLogs().At(0)
rl.InstrumentationLibraryLogs().Resize(1)
ill := rl.InstrumentationLibraryLogs().At(0)
ill := logs.ResourceLogs().AppendEmpty().InstrumentationLibraryLogs().AppendEmpty()

for i := 0; i < numberOfLogs; i++ {
ts := pdata.Timestamp(int64(i) * time.Millisecond.Nanoseconds())
logRecord := pdata.NewLogRecord()
logRecord := ill.Logs().AppendEmpty()
logRecord.Body().SetStringVal("mylog")
attributes.Range(func(k string, v pdata.AttributeValue) bool {
logRecord.Attributes().Insert(k, v)
return true
})
logRecord.SetTimestamp(ts)

ill.Logs().Append(logRecord)
}

return logs
Expand Down Expand Up @@ -287,17 +282,11 @@ func TestExporter_logDataToLoki(t *testing.T) {

t.Run("with attributes that match config", func(t *testing.T) {
logs := pdata.NewLogs()
logs.ResourceLogs().Resize(1)
rl := logs.ResourceLogs().At(0)
rl.InstrumentationLibraryLogs().Resize(1)
ill := rl.InstrumentationLibraryLogs().At(0)

ts := pdata.Timestamp(int64(1) * time.Millisecond.Nanoseconds())
lr := pdata.NewLogRecord()
lr := logs.ResourceLogs().AppendEmpty().InstrumentationLibraryLogs().AppendEmpty().Logs().AppendEmpty()
lr.Body().SetStringVal("log message")
lr.Attributes().InsertString("not.in.config", "not allowed")
lr.SetTimestamp(ts)
ill.Logs().Append(lr)

pr, numDroppedLogs := exp.logDataToLoki(logs)
expectedPr := &logproto.PushRequest{Streams: make([]logproto.Stream, 0)}
Expand All @@ -307,19 +296,13 @@ func TestExporter_logDataToLoki(t *testing.T) {

t.Run("with partial attributes that match config", func(t *testing.T) {
logs := pdata.NewLogs()
logs.ResourceLogs().Resize(1)
rl := logs.ResourceLogs().At(0)
rl.InstrumentationLibraryLogs().Resize(1)
ill := rl.InstrumentationLibraryLogs().At(0)

ts := pdata.Timestamp(int64(1) * time.Millisecond.Nanoseconds())
lr := pdata.NewLogRecord()
lr := logs.ResourceLogs().AppendEmpty().InstrumentationLibraryLogs().AppendEmpty().Logs().AppendEmpty()
lr.Body().SetStringVal("log message")
lr.Attributes().InsertString(conventions.AttributeContainerName, "mycontainer")
lr.Attributes().InsertString("severity", "info")
lr.Attributes().InsertString("random.attribute", "random")
lr.SetTimestamp(ts)
ill.Logs().Append(lr)

pr, numDroppedLogs := exp.logDataToLoki(logs)
require.Equal(t, 0, numDroppedLogs)
Expand All @@ -329,27 +312,21 @@ func TestExporter_logDataToLoki(t *testing.T) {

t.Run("with multiple logs and same attributes", func(t *testing.T) {
logs := pdata.NewLogs()
logs.ResourceLogs().Resize(1)
rl := logs.ResourceLogs().At(0)
rl.InstrumentationLibraryLogs().Resize(1)
ill := rl.InstrumentationLibraryLogs().At(0)

ts := pdata.Timestamp(int64(1) * time.Millisecond.Nanoseconds())
lr1 := pdata.NewLogRecord()
ill := logs.ResourceLogs().AppendEmpty().InstrumentationLibraryLogs().AppendEmpty()
lr1 := ill.Logs().AppendEmpty()
lr1.Body().SetStringVal("log message 1")
lr1.Attributes().InsertString(conventions.AttributeContainerName, "mycontainer")
lr1.Attributes().InsertString(conventions.AttributeK8sCluster, "mycluster")
lr1.Attributes().InsertString("severity", "info")
lr1.SetTimestamp(ts)
ill.Logs().Append(lr1)

lr2 := pdata.NewLogRecord()
lr2 := ill.Logs().AppendEmpty()
lr2.Body().SetStringVal("log message 2")
lr2.Attributes().InsertString(conventions.AttributeContainerName, "mycontainer")
lr2.Attributes().InsertString(conventions.AttributeK8sCluster, "mycluster")
lr2.Attributes().InsertString("severity", "info")
lr2.SetTimestamp(ts)
ill.Logs().Append(lr2)

pr, numDroppedLogs := exp.logDataToLoki(logs)
require.Equal(t, 0, numDroppedLogs)
Expand All @@ -360,27 +337,22 @@ func TestExporter_logDataToLoki(t *testing.T) {

t.Run("with multiple logs and different attributes", func(t *testing.T) {
logs := pdata.NewLogs()
logs.ResourceLogs().Resize(1)
rl := logs.ResourceLogs().At(0)
rl.InstrumentationLibraryLogs().Resize(1)
ill := rl.InstrumentationLibraryLogs().At(0)

ts := pdata.Timestamp(int64(1) * time.Millisecond.Nanoseconds())
lr1 := pdata.NewLogRecord()
ill := logs.ResourceLogs().AppendEmpty().InstrumentationLibraryLogs().AppendEmpty()

lr1 := ill.Logs().AppendEmpty()
lr1.Body().SetStringVal("log message 1")
lr1.Attributes().InsertString(conventions.AttributeContainerName, "mycontainer1")
lr1.Attributes().InsertString(conventions.AttributeK8sCluster, "mycluster1")
lr1.Attributes().InsertString("severity", "debug")
lr1.SetTimestamp(ts)
ill.Logs().Append(lr1)

lr2 := pdata.NewLogRecord()
lr2 := ill.Logs().AppendEmpty()
lr2.Body().SetStringVal("log message 2")
lr2.Attributes().InsertString(conventions.AttributeContainerName, "mycontainer2")
lr2.Attributes().InsertString(conventions.AttributeK8sCluster, "mycluster2")
lr2.Attributes().InsertString("severity", "error")
lr2.SetTimestamp(ts)
ill.Logs().Append(lr2)

pr, numDroppedLogs := exp.logDataToLoki(logs)
require.Equal(t, 0, numDroppedLogs)
Expand Down
80 changes: 23 additions & 57 deletions exporter/newrelicexporter/newrelic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,20 +440,13 @@ func TestExportTraceDataFullTrace(t *testing.T) {
}

func TestExportMetricUnsupported(t *testing.T) {
m := pdata.NewMetric()
ms := pdata.NewMetrics()
m := ms.ResourceMetrics().AppendEmpty().InstrumentationLibraryMetrics().AppendEmpty().Metrics().AppendEmpty()
m.SetDataType(pdata.MetricDataTypeHistogram)
dp := pdata.NewHistogramDataPoint()
dp := m.Histogram().DataPoints().AppendEmpty()
dp.SetCount(1)
dp.SetSum(1)
dp.SetTimestamp(pdata.TimestampFromTime(time.Now()))
m.Histogram().DataPoints().Append(dp)

ms := pdata.NewMetrics()
rm := pdata.NewResourceMetrics()
ilm := pdata.NewInstrumentationLibraryMetrics()
ilm.Metrics().Append(m)
rm.InstrumentationLibraryMetrics().Append(ilm)
ms.ResourceMetrics().Append(rm)

_, err := runMetricMock(context.Background(), ms, mockConfig{useAPIKeyHeader: false})
var unsupportedErr *errUnsupportedMetricType
Expand Down Expand Up @@ -696,21 +689,16 @@ func TestExportMetricDataFull(t *testing.T) {

func TestExportLogs(t *testing.T) {
timestamp := time.Now()
l := pdata.NewLogRecord()
logs := pdata.NewLogs()
rlog := logs.ResourceLogs().AppendEmpty()
rlog.Resource().Attributes().InsertString("resource", "R1")
rlog.Resource().Attributes().InsertString("service.name", "test-service")
l := rlog.InstrumentationLibraryLogs().AppendEmpty().Logs().AppendEmpty()
l.SetName("logname")
l.SetTimestamp(pdata.TimestampFromTime(timestamp))
l.Body().SetStringVal("log body")
l.Attributes().InsertString("foo", "bar")

ilog := pdata.NewInstrumentationLibraryLogs()
ilog.Logs().Append(l)
rlog := pdata.NewResourceLogs()
rlog.InstrumentationLibraryLogs().Append(ilog)
rlog.Resource().Attributes().InsertString("resource", "R1")
rlog.Resource().Attributes().InsertString("service.name", "test-service")
logs := pdata.NewLogs()
logs.ResourceLogs().Append(rlog)

expected := []Batch{
{
Common: Common{
Expand Down Expand Up @@ -780,16 +768,11 @@ func testUserAgentContainsCollectorInfo(t *testing.T, version string, gitHash st
exp, err := f.CreateTracesExporter(context.Background(), params, c)
require.NoError(t, err)

s := pdata.NewSpan()
ptrace := pdata.NewTraces()
s := ptrace.ResourceSpans().AppendEmpty().InstrumentationLibrarySpans().AppendEmpty().Spans().AppendEmpty()
s.SetName("root")
s.SetTraceID(pdata.NewTraceID([16]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}))
s.SetSpanID(pdata.NewSpanID([8]byte{0, 0, 0, 0, 0, 0, 0, 1}))
ils := pdata.NewInstrumentationLibrarySpans()
ils.Spans().Append(s)
rs := pdata.NewResourceSpans()
rs.InstrumentationLibrarySpans().Append(ils)
ptrace := pdata.NewTraces()
ptrace.ResourceSpans().Append(rs)

err = exp.ConsumeTraces(ctx, ptrace)
require.NoError(t, err)
Expand Down Expand Up @@ -834,17 +817,13 @@ func TestBadSpanResourceGeneratesError(t *testing.T) {
exp, err := f.CreateTracesExporter(context.Background(), params, c)
require.NoError(t, err)

s := pdata.NewSpan()
ptrace := pdata.NewTraces()
rs := ptrace.ResourceSpans().AppendEmpty()
rs.Resource().Attributes().InsertDouble("badattribute", math.Inf(1))
s := rs.InstrumentationLibrarySpans().AppendEmpty().Spans().AppendEmpty()
s.SetName("root")
s.SetTraceID(pdata.NewTraceID([16]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}))
s.SetSpanID(pdata.NewSpanID([8]byte{0, 0, 0, 0, 0, 0, 0, 1}))
ils := pdata.NewInstrumentationLibrarySpans()
ils.Spans().Append(s)
rs := pdata.NewResourceSpans()
rs.InstrumentationLibrarySpans().Append(ils)
rs.Resource().Attributes().InsertDouble("badattribute", math.Inf(1))
ptrace := pdata.NewTraces()
ptrace.ResourceSpans().Append(rs)

errorFromConsumeTraces := exp.ConsumeTraces(ctx, ptrace)

Expand Down Expand Up @@ -889,15 +868,11 @@ func TestBadMetricResourceGeneratesError(t *testing.T) {
exp, err := f.CreateMetricsExporter(context.Background(), params, c)
require.NoError(t, err)

metric := pdata.NewMetric()
metric.SetName("testmetric")
ilm := pdata.NewInstrumentationLibraryMetrics()
ilm.Metrics().Append(metric)
rm := pdata.NewResourceMetrics()
rm.InstrumentationLibraryMetrics().Append(ilm)
rm.Resource().Attributes().InsertDouble("badattribute", math.Inf(1))
md := pdata.NewMetrics()
md.ResourceMetrics().Append(rm)
rm := md.ResourceMetrics().AppendEmpty()
rm.Resource().Attributes().InsertDouble("badattribute", math.Inf(1))
metric := rm.InstrumentationLibraryMetrics().AppendEmpty().Metrics().AppendEmpty()
metric.SetName("testmetric")

errorFromConsumeMetrics := exp.ConsumeMetrics(ctx, md)

Expand Down Expand Up @@ -942,14 +917,10 @@ func TestBadLogResourceGeneratesError(t *testing.T) {
exp, err := f.CreateLogsExporter(context.Background(), params, c)
require.NoError(t, err)

log := pdata.NewLogRecord()
ill := pdata.NewInstrumentationLibraryLogs()
ill.Logs().Append(log)
rl := pdata.NewResourceLogs()
rl.InstrumentationLibraryLogs().Append(ill)
rl.Resource().Attributes().InsertDouble("badattribute", math.Inf(1))
ld := pdata.NewLogs()
ld.ResourceLogs().Append(rl)
rl := ld.ResourceLogs().AppendEmpty()
rl.Resource().Attributes().InsertDouble("badattribute", math.Inf(1))
rl.InstrumentationLibraryLogs().AppendEmpty().Logs().AppendEmpty()

errorFromConsumeLogs := exp.ConsumeLogs(ctx, ld)

Expand Down Expand Up @@ -999,16 +970,11 @@ func TestFailureToRecordMetricsDoesNotAffectExportingData(t *testing.T) {
exp, err := f.CreateTracesExporter(context.Background(), params, c)
require.NoError(t, err)

s := pdata.NewSpan()
ptrace := pdata.NewTraces()
s := ptrace.ResourceSpans().AppendEmpty().InstrumentationLibrarySpans().AppendEmpty().Spans().AppendEmpty()
s.SetName("root")
s.SetTraceID(pdata.NewTraceID([16]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}))
s.SetSpanID(pdata.NewSpanID([8]byte{0, 0, 0, 0, 0, 0, 0, 1}))
ils := pdata.NewInstrumentationLibrarySpans()
ils.Spans().Append(s)
rs := pdata.NewResourceSpans()
rs.InstrumentationLibrarySpans().Append(ils)
ptrace := pdata.NewTraces()
ptrace.ResourceSpans().Append(rs)

// Create a long string so that the user-agent will be too long and cause RecordMetric to fail
b := make([]byte, 300)
Expand Down
Loading

0 comments on commit ab68513

Please sign in to comment.