Skip to content

Commit

Permalink
[chore][plogutiltest] Use native structs in options (#36354)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski authored Nov 14, 2024
1 parent c06be6d commit 816d80c
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 66 deletions.
60 changes: 44 additions & 16 deletions connector/routingconnector/internal/plogutil/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
),
),
},
}
Expand Down Expand Up @@ -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"),
},
Expand All @@ -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"),
},
Expand All @@ -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"),
},
Expand All @@ -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")),
),
),
},
}
Expand Down
60 changes: 26 additions & 34 deletions connector/routingconnector/internal/plogutiltest/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ 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:
// - Resources have an attribute called "resourceName" with a value of "resourceN".
// - 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
Expand All @@ -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
}
35 changes: 22 additions & 13 deletions connector/routingconnector/internal/plogutiltest/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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"))),
)))
})

Expand All @@ -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"))),
)))
})

Expand All @@ -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")),
),
)))
})

Expand All @@ -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"))),
)))
})

Expand All @@ -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")),
),
)))
})
}
11 changes: 8 additions & 3 deletions connector/routingconnector/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
),
),
},
{
Expand Down

0 comments on commit 816d80c

Please sign in to comment.