diff --git a/connector/routingconnector/internal/plogutil/logs_test.go b/connector/routingconnector/internal/plogutil/logs_test.go index 5f0a4b00975a..56637d3f9a28 100644 --- a/connector/routingconnector/internal/plogutil/logs_test.go +++ b/connector/routingconnector/internal/plogutil/logs_test.go @@ -63,9 +63,14 @@ func TestMoveResourcesIf(t *testing.T) { from: plogutiltest.NewLogs("AB", "CD", "EF"), to: plogutiltest.NewLogs("1", "2", "3"), expectFrom: plogutiltest.NewLogs("A", "CD", "EF"), - expectTo: plogutiltest.NewLogsFromOpts( - plogutiltest.WithResource('1', plogutiltest.WithScope('2', "3")), - plogutiltest.WithResource('B', plogutiltest.WithScope('C', "EF"), plogutiltest.WithScope('D', "EF")), + expectTo: plogutiltest.New( + plogutiltest.Resource("1", + plogutiltest.Scope("2", plogutiltest.LogRecord("3")), + ), + plogutiltest.Resource("B", + plogutiltest.Scope("C", plogutiltest.LogRecord("E"), plogutiltest.LogRecord("F")), + plogutiltest.Scope("D", plogutiltest.LogRecord("E"), plogutiltest.LogRecord("F")), + ), ), }, } @@ -127,9 +132,14 @@ func TestMoveRecordsWithContextIf(t *testing.T) { }, from: plogutiltest.NewLogs("AB", "CD", "EF"), to: plog.NewLogs(), - expectFrom: plogutiltest.NewLogsFromOpts( - plogutiltest.WithResource('A', plogutiltest.WithScope('C', "EF"), plogutiltest.WithScope('D', "EF")), - plogutiltest.WithResource('B', plogutiltest.WithScope('D', "EF")), + expectFrom: plogutiltest.New( + plogutiltest.Resource("A", + plogutiltest.Scope("C", plogutiltest.LogRecord("E"), plogutiltest.LogRecord("F")), + plogutiltest.Scope("D", plogutiltest.LogRecord("E"), plogutiltest.LogRecord("F")), + ), + plogutiltest.Resource("B", + plogutiltest.Scope("D", plogutiltest.LogRecord("E"), plogutiltest.LogRecord("F")), + ), ), expectTo: plogutiltest.NewLogs("B", "C", "EF"), }, @@ -151,9 +161,15 @@ func TestMoveRecordsWithContextIf(t *testing.T) { }, from: plogutiltest.NewLogs("AB", "CD", "EF"), to: plog.NewLogs(), - expectFrom: plogutiltest.NewLogsFromOpts( - plogutiltest.WithResource('A', plogutiltest.WithScope('C', "EF"), plogutiltest.WithScope('D', "E")), - plogutiltest.WithResource('B', plogutiltest.WithScope('C', "EF"), plogutiltest.WithScope('D', "EF")), + expectFrom: plogutiltest.New( + plogutiltest.Resource("A", + plogutiltest.Scope("C", plogutiltest.LogRecord("E"), plogutiltest.LogRecord("F")), + plogutiltest.Scope("D", plogutiltest.LogRecord("E")), + ), + plogutiltest.Resource("B", + plogutiltest.Scope("C", plogutiltest.LogRecord("E"), plogutiltest.LogRecord("F")), + plogutiltest.Scope("D", plogutiltest.LogRecord("E"), plogutiltest.LogRecord("F")), + ), ), expectTo: plogutiltest.NewLogs("A", "D", "F"), }, @@ -175,9 +191,15 @@ func TestMoveRecordsWithContextIf(t *testing.T) { }, from: plogutiltest.NewLogs("AB", "CD", "EF"), to: plog.NewLogs(), - expectFrom: plogutiltest.NewLogsFromOpts( - plogutiltest.WithResource('A', plogutiltest.WithScope('C', "EF"), plogutiltest.WithScope('D', "EF")), - plogutiltest.WithResource('B', plogutiltest.WithScope('C', "F"), plogutiltest.WithScope('D', "F")), + expectFrom: plogutiltest.New( + plogutiltest.Resource("A", + plogutiltest.Scope("C", plogutiltest.LogRecord("E"), plogutiltest.LogRecord("F")), + plogutiltest.Scope("D", plogutiltest.LogRecord("E"), plogutiltest.LogRecord("F")), + ), + plogutiltest.Resource("B", + plogutiltest.Scope("C", plogutiltest.LogRecord("F")), + plogutiltest.Scope("D", plogutiltest.LogRecord("F")), + ), ), expectTo: plogutiltest.NewLogs("B", "CD", "E"), }, @@ -189,10 +211,16 @@ func TestMoveRecordsWithContextIf(t *testing.T) { from: plogutiltest.NewLogs("AB", "CD", "EF"), to: plogutiltest.NewLogs("1", "2", "3"), expectFrom: plogutiltest.NewLogs("AB", "C", "EF"), - expectTo: plogutiltest.NewLogsFromOpts( - plogutiltest.WithResource('1', plogutiltest.WithScope('2', "3")), - plogutiltest.WithResource('A', plogutiltest.WithScope('D', "EF")), - plogutiltest.WithResource('B', plogutiltest.WithScope('D', "EF")), + expectTo: plogutiltest.New( + plogutiltest.Resource("1", + plogutiltest.Scope("2", plogutiltest.LogRecord("3")), + ), + plogutiltest.Resource("A", + plogutiltest.Scope("D", plogutiltest.LogRecord("E"), plogutiltest.LogRecord("F")), + ), + plogutiltest.Resource("B", + plogutiltest.Scope("D", plogutiltest.LogRecord("E"), plogutiltest.LogRecord("F")), + ), ), }, } diff --git a/connector/routingconnector/internal/plogutiltest/logs.go b/connector/routingconnector/internal/plogutiltest/logs.go index 034162eb25b8..675d5ad3eb85 100644 --- a/connector/routingconnector/internal/plogutiltest/logs.go +++ b/connector/routingconnector/internal/plogutiltest/logs.go @@ -5,7 +5,7 @@ package plogutiltest // import "github.com/open-telemetry/opentelemetry-collecto import "go.opentelemetry.io/collector/pdata/plog" -// TestLogs returns a plog.Logs with a uniform structure where resources, scopes, and +// NewLogs returns a plog.Logs with a uniform structure where resources, scopes, and // log records are identical across all instances, except for one identifying field. // // Identifying fields: @@ -13,7 +13,7 @@ import "go.opentelemetry.io/collector/pdata/plog" // - Scopes have a name with a value of "scopeN". // - LogRecords have a body with a value of "logN". // -// Example: TestLogs("AB", "XYZ", "1234") returns: +// Example: NewLogs("AB", "XYZ", "1234") returns: // // resourceA, resourceB // each with scopeX, scopeY, scopeZ @@ -37,42 +37,34 @@ func NewLogs(resourceIDs, scopeIDs, logRecordIDs string) plog.Logs { return ld } -type Resource struct { - id byte - scopes []Scope -} - -type Scope struct { - id byte - logs string +func New(resources ...plog.ResourceLogs) plog.Logs { + ld := plog.NewLogs() + for _, resource := range resources { + resource.CopyTo(ld.ResourceLogs().AppendEmpty()) + } + return ld } -func WithResource(id byte, scopes ...Scope) Resource { - r := Resource{id: id} - r.scopes = append(r.scopes, scopes...) - return r +func Resource(id string, scopes ...plog.ScopeLogs) plog.ResourceLogs { + rl := plog.NewResourceLogs() + rl.Resource().Attributes().PutStr("resourceName", "resource"+id) + for _, scope := range scopes { + scope.CopyTo(rl.ScopeLogs().AppendEmpty()) + } + return rl } -func WithScope(id byte, logs string) Scope { - return Scope{id: id, logs: logs} +func Scope(id string, logs ...plog.LogRecord) plog.ScopeLogs { + s := plog.NewScopeLogs() + s.Scope().SetName("scope" + id) + for _, log := range logs { + log.CopyTo(s.LogRecords().AppendEmpty()) + } + return s } -// NewLogsFromOpts creates a plog.Logs with the specified resources, scopes, and logs. -// The general idea is the same as NewLogs, but this function allows for more flexibility -// in creating non-uniform structures. -func NewLogsFromOpts(resources ...Resource) plog.Logs { - ld := plog.NewLogs() - for _, resource := range resources { - r := ld.ResourceLogs().AppendEmpty() - r.Resource().Attributes().PutStr("resourceName", "resource"+string(resource.id)) - for _, scope := range resource.scopes { - s := r.ScopeLogs().AppendEmpty() - s.Scope().SetName("scope" + string(scope.id)) - for i := 0; i < len(scope.logs); i++ { - l := s.LogRecords().AppendEmpty() - l.Body().SetStr("log" + string(scope.logs[i])) - } - } - } - return ld +func LogRecord(id string) plog.LogRecord { + lr := plog.NewLogRecord() + lr.Body().SetStr("log" + id) + return lr } diff --git a/connector/routingconnector/internal/plogutiltest/logs_test.go b/connector/routingconnector/internal/plogutiltest/logs_test.go index b056e51eef5e..7b3378bbabf4 100644 --- a/connector/routingconnector/internal/plogutiltest/logs_test.go +++ b/connector/routingconnector/internal/plogutiltest/logs_test.go @@ -17,7 +17,8 @@ func TestNewLogs(t *testing.T) { t.Run("empty", func(t *testing.T) { expected := plog.NewLogs() assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.NewLogs("", "", ""))) - assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.NewLogsFromOpts())) + assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.New())) + assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.New())) }) t.Run("simple", func(t *testing.T) { @@ -32,8 +33,8 @@ func TestNewLogs(t *testing.T) { return ld }() assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.NewLogs("A", "B", "C"))) - assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.NewLogsFromOpts( - plogutiltest.WithResource('A', plogutiltest.WithScope('B', "C")), + assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.New( + plogutiltest.Resource("A", plogutiltest.Scope("B", plogutiltest.LogRecord("C"))), ))) }) @@ -55,9 +56,9 @@ func TestNewLogs(t *testing.T) { return ld }() assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.NewLogs("AB", "C", "D"))) - assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.NewLogsFromOpts( - plogutiltest.WithResource('A', plogutiltest.WithScope('C', "D")), - plogutiltest.WithResource('B', plogutiltest.WithScope('C', "D")), + assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.New( + plogutiltest.Resource("A", plogutiltest.Scope("C", plogutiltest.LogRecord("D"))), + plogutiltest.Resource("B", plogutiltest.Scope("C", plogutiltest.LogRecord("D"))), ))) }) @@ -77,8 +78,11 @@ func TestNewLogs(t *testing.T) { return ld }() assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.NewLogs("A", "BC", "D"))) - assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.NewLogsFromOpts( - plogutiltest.WithResource('A', plogutiltest.WithScope('B', "D"), plogutiltest.WithScope('C', "D")), + assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.New( + plogutiltest.Resource("A", + plogutiltest.Scope("B", plogutiltest.LogRecord("D")), + plogutiltest.Scope("C", plogutiltest.LogRecord("D")), + ), ))) }) @@ -96,8 +100,8 @@ func TestNewLogs(t *testing.T) { return ld }() assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.NewLogs("A", "B", "CD"))) - assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.NewLogsFromOpts( - plogutiltest.WithResource('A', plogutiltest.WithScope('B', "CD")), + assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.New( + plogutiltest.Resource("A", plogutiltest.Scope("B", plogutiltest.LogRecord("C"), plogutiltest.LogRecord("D"))), ))) }) @@ -124,9 +128,14 @@ func TestNewLogs(t *testing.T) { l.Body().SetStr("logG") // resourceB.scopeD.logG return ld }() - assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.NewLogsFromOpts( - plogutiltest.WithResource('A', plogutiltest.WithScope('C', "E"), plogutiltest.WithScope('D', "E")), - plogutiltest.WithResource('B', plogutiltest.WithScope('D', "FG")), + assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.New( + plogutiltest.Resource("A", + plogutiltest.Scope("C", plogutiltest.LogRecord("E")), + plogutiltest.Scope("D", plogutiltest.LogRecord("E")), + ), + plogutiltest.Resource("B", + plogutiltest.Scope("D", plogutiltest.LogRecord("F"), plogutiltest.LogRecord("G")), + ), ))) }) } diff --git a/connector/routingconnector/logs_test.go b/connector/routingconnector/logs_test.go index c0198fe16523..bcad290a28bc 100644 --- a/connector/routingconnector/logs_test.go +++ b/connector/routingconnector/logs_test.go @@ -811,9 +811,14 @@ func TestLogsConnectorDetailed(t *testing.T) { input: plogutiltest.NewLogs("AB", "CD", "EF"), expectSink0: plogutiltest.NewLogs("B", "D", "EF"), expectSink1: plog.Logs{}, - expectSinkD: plogutiltest.NewLogsFromOpts( - plogutiltest.WithResource('A', plogutiltest.WithScope('C', "EF"), plogutiltest.WithScope('D', "EF")), - plogutiltest.WithResource('B', plogutiltest.WithScope('C', "EF")), + expectSinkD: plogutiltest.New( + plogutiltest.Resource("A", + plogutiltest.Scope("C", plogutiltest.LogRecord("E"), plogutiltest.LogRecord("F")), + plogutiltest.Scope("D", plogutiltest.LogRecord("E"), plogutiltest.LogRecord("F")), + ), + plogutiltest.Resource("B", + plogutiltest.Scope("C", plogutiltest.LogRecord("E"), plogutiltest.LogRecord("F")), + ), ), }, {