Skip to content

Commit

Permalink
[chore][ptraceutiltest] Use native structs in options (open-telemetry…
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski authored Nov 15, 2024
1 parent f6cebd2 commit 4711a64
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 130 deletions.
10 changes: 5 additions & 5 deletions connector/routingconnector/internal/plogutil/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ 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.New(
expectTo: plogutiltest.NewLogsFromOpts(
plogutiltest.Resource("1",
plogutiltest.Scope("2", plogutiltest.LogRecord("3")),
),
Expand Down Expand Up @@ -132,7 +132,7 @@ func TestMoveRecordsWithContextIf(t *testing.T) {
},
from: plogutiltest.NewLogs("AB", "CD", "EF"),
to: plog.NewLogs(),
expectFrom: plogutiltest.New(
expectFrom: plogutiltest.NewLogsFromOpts(
plogutiltest.Resource("A",
plogutiltest.Scope("C", plogutiltest.LogRecord("E"), plogutiltest.LogRecord("F")),
plogutiltest.Scope("D", plogutiltest.LogRecord("E"), plogutiltest.LogRecord("F")),
Expand Down Expand Up @@ -161,7 +161,7 @@ func TestMoveRecordsWithContextIf(t *testing.T) {
},
from: plogutiltest.NewLogs("AB", "CD", "EF"),
to: plog.NewLogs(),
expectFrom: plogutiltest.New(
expectFrom: plogutiltest.NewLogsFromOpts(
plogutiltest.Resource("A",
plogutiltest.Scope("C", plogutiltest.LogRecord("E"), plogutiltest.LogRecord("F")),
plogutiltest.Scope("D", plogutiltest.LogRecord("E")),
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestMoveRecordsWithContextIf(t *testing.T) {
},
from: plogutiltest.NewLogs("AB", "CD", "EF"),
to: plog.NewLogs(),
expectFrom: plogutiltest.New(
expectFrom: plogutiltest.NewLogsFromOpts(
plogutiltest.Resource("A",
plogutiltest.Scope("C", plogutiltest.LogRecord("E"), plogutiltest.LogRecord("F")),
plogutiltest.Scope("D", plogutiltest.LogRecord("E"), plogutiltest.LogRecord("F")),
Expand All @@ -211,7 +211,7 @@ 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.New(
expectTo: plogutiltest.NewLogsFromOpts(
plogutiltest.Resource("1",
plogutiltest.Scope("2", plogutiltest.LogRecord("3")),
),
Expand Down
2 changes: 1 addition & 1 deletion connector/routingconnector/internal/plogutiltest/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewLogs(resourceIDs, scopeIDs, logRecordIDs string) plog.Logs {
return ld
}

func New(resources ...plog.ResourceLogs) plog.Logs {
func NewLogsFromOpts(resources ...plog.ResourceLogs) plog.Logs {
ld := plog.NewLogs()
for _, resource := range resources {
resource.CopyTo(ld.ResourceLogs().AppendEmpty())
Expand Down
12 changes: 6 additions & 6 deletions connector/routingconnector/internal/plogutiltest/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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.New()))
assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.NewLogsFromOpts()))
})

t.Run("simple", func(t *testing.T) {
Expand All @@ -32,7 +32,7 @@ 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.New(
assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.NewLogsFromOpts(
plogutiltest.Resource("A", plogutiltest.Scope("B", plogutiltest.LogRecord("C"))),
)))
})
Expand All @@ -55,7 +55,7 @@ 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.New(
assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.NewLogsFromOpts(
plogutiltest.Resource("A", plogutiltest.Scope("C", plogutiltest.LogRecord("D"))),
plogutiltest.Resource("B", plogutiltest.Scope("C", plogutiltest.LogRecord("D"))),
)))
Expand All @@ -77,7 +77,7 @@ 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.New(
assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.NewLogsFromOpts(
plogutiltest.Resource("A",
plogutiltest.Scope("B", plogutiltest.LogRecord("D")),
plogutiltest.Scope("C", plogutiltest.LogRecord("D")),
Expand All @@ -99,7 +99,7 @@ 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.New(
assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.NewLogsFromOpts(
plogutiltest.Resource("A", plogutiltest.Scope("B", plogutiltest.LogRecord("C"), plogutiltest.LogRecord("D"))),
)))
})
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestNewLogs(t *testing.T) {
l.Body().SetStr("logG") // resourceB.scopeD.logG
return ld
}()
assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.New(
assert.NoError(t, plogtest.CompareLogs(expected, plogutiltest.NewLogsFromOpts(
plogutiltest.Resource("A",
plogutiltest.Scope("C", plogutiltest.LogRecord("E")),
plogutiltest.Scope("D", plogutiltest.LogRecord("E")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package pmetricutiltest // import "github.com/open-telemetry/opentelemetry-colle

import "go.opentelemetry.io/collector/pdata/pmetric"

// TestMetrics returns a pmetric.Metrics with a uniform structure where resources, scopes, metrics,
// NewMetrics returns a pmetric.Metrics with a uniform structure where resources, scopes, metrics,
// and datapoints are identical across all instances, except for one identifying field.
//
// Identifying fields:
Expand All @@ -14,7 +14,7 @@ import "go.opentelemetry.io/collector/pdata/pmetric"
// - Metrics have a name with a value of "metricN" and a single time series of data points.
// - DataPoints have an attribute "dpName" with a value of "dpN".
//
// Example: TestMetrics("AB", "XYZ", "MN", "1234") returns:
// Example: NewMetrics("AB", "XYZ", "MN", "1234") returns:
//
// resourceA, resourceB
// each with scopeX, scopeY, scopeZ
Expand Down
80 changes: 60 additions & 20 deletions connector/routingconnector/internal/ptraceutil/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,21 @@ func TestMoveSpansWithContextIf(t *testing.T) {
from: ptraceutiltest.NewTraces("AB", "CD", "EF", "GH"),
to: ptrace.NewTraces(),
expectFrom: ptraceutiltest.NewTracesFromOpts(
ptraceutiltest.WithResource('A',
ptraceutiltest.WithScope('C', ptraceutiltest.WithSpan('E', "GH"), ptraceutiltest.WithSpan('F', "GH")),
ptraceutiltest.WithScope('D', ptraceutiltest.WithSpan('E', "GH"), ptraceutiltest.WithSpan('F', "GH")),
ptraceutiltest.Resource("A",
ptraceutiltest.Scope("C",
ptraceutiltest.Span("E", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
ptraceutiltest.Span("F", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
),
ptraceutiltest.Scope("D",
ptraceutiltest.Span("E", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
ptraceutiltest.Span("F", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
),
),
ptraceutiltest.WithResource('B',
ptraceutiltest.WithScope('D', ptraceutiltest.WithSpan('E', "GH"), ptraceutiltest.WithSpan('F', "GH")),
ptraceutiltest.Resource("B",
ptraceutiltest.Scope("D",
ptraceutiltest.Span("E", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
ptraceutiltest.Span("F", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
),
),
),
expectTo: ptraceutiltest.NewTraces("B", "C", "EF", "GH"),
Expand All @@ -159,13 +168,24 @@ func TestMoveSpansWithContextIf(t *testing.T) {
from: ptraceutiltest.NewTraces("AB", "CD", "EF", "GH"),
to: ptrace.NewTraces(),
expectFrom: ptraceutiltest.NewTracesFromOpts(
ptraceutiltest.WithResource('A',
ptraceutiltest.WithScope('C', ptraceutiltest.WithSpan('E', "GH"), ptraceutiltest.WithSpan('F', "GH")),
ptraceutiltest.WithScope('D', ptraceutiltest.WithSpan('E', "GH")),
ptraceutiltest.Resource("A",
ptraceutiltest.Scope("C",
ptraceutiltest.Span("E", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
ptraceutiltest.Span("F", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
),
ptraceutiltest.Scope("D",
ptraceutiltest.Span("E", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
),
),
ptraceutiltest.WithResource('B',
ptraceutiltest.WithScope('C', ptraceutiltest.WithSpan('E', "GH"), ptraceutiltest.WithSpan('F', "GH")),
ptraceutiltest.WithScope('D', ptraceutiltest.WithSpan('E', "GH"), ptraceutiltest.WithSpan('F', "GH")),
ptraceutiltest.Resource("B",
ptraceutiltest.Scope("C",
ptraceutiltest.Span("E", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
ptraceutiltest.Span("F", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
),
ptraceutiltest.Scope("D",
ptraceutiltest.Span("E", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
ptraceutiltest.Span("F", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
),
),
),
expectTo: ptraceutiltest.NewTraces("A", "D", "F", "GH"),
Expand All @@ -189,13 +209,23 @@ func TestMoveSpansWithContextIf(t *testing.T) {
from: ptraceutiltest.NewTraces("AB", "CD", "EF", "GH"),
to: ptrace.NewTraces(),
expectFrom: ptraceutiltest.NewTracesFromOpts(
ptraceutiltest.WithResource('A',
ptraceutiltest.WithScope('C', ptraceutiltest.WithSpan('E', "GH"), ptraceutiltest.WithSpan('F', "GH")),
ptraceutiltest.WithScope('D', ptraceutiltest.WithSpan('E', "GH"), ptraceutiltest.WithSpan('F', "GH")),
ptraceutiltest.Resource("A",
ptraceutiltest.Scope("C",
ptraceutiltest.Span("E", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
ptraceutiltest.Span("F", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
),
ptraceutiltest.Scope("D",
ptraceutiltest.Span("E", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
ptraceutiltest.Span("F", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
),
),
ptraceutiltest.WithResource('B',
ptraceutiltest.WithScope('C', ptraceutiltest.WithSpan('F', "GH")),
ptraceutiltest.WithScope('D', ptraceutiltest.WithSpan('F', "GH")),
ptraceutiltest.Resource("B",
ptraceutiltest.Scope("C",
ptraceutiltest.Span("F", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
),
ptraceutiltest.Scope("D",
ptraceutiltest.Span("F", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
),
),
),
expectTo: ptraceutiltest.NewTraces("B", "CD", "E", "GH"),
Expand All @@ -209,9 +239,19 @@ func TestMoveSpansWithContextIf(t *testing.T) {
to: ptraceutiltest.NewTraces("1", "2", "3", "4"),
expectFrom: ptraceutiltest.NewTraces("AB", "C", "EF", "GH"),
expectTo: ptraceutiltest.NewTracesFromOpts(
ptraceutiltest.WithResource('1', ptraceutiltest.WithScope('2', ptraceutiltest.WithSpan('3', "4"))),
ptraceutiltest.WithResource('A', ptraceutiltest.WithScope('D', ptraceutiltest.WithSpan('E', "GH"), ptraceutiltest.WithSpan('F', "GH"))),
ptraceutiltest.WithResource('B', ptraceutiltest.WithScope('D', ptraceutiltest.WithSpan('E', "GH"), ptraceutiltest.WithSpan('F', "GH"))),
ptraceutiltest.Resource("1", ptraceutiltest.Scope("2", ptraceutiltest.Span("3", ptraceutiltest.SpanEvent("4")))),
ptraceutiltest.Resource("A",
ptraceutiltest.Scope("D",
ptraceutiltest.Span("E", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
ptraceutiltest.Span("F", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
),
),
ptraceutiltest.Resource("B",
ptraceutiltest.Scope("D",
ptraceutiltest.Span("E", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
ptraceutiltest.Span("F", ptraceutiltest.SpanEvent("G"), ptraceutiltest.SpanEvent("H")),
),
),
),
},
}
Expand Down
73 changes: 30 additions & 43 deletions connector/routingconnector/internal/ptraceutiltest/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,56 +43,43 @@ func NewTraces(resourceIDs, scopeIDs, spanIDs, spanEventIDs string) ptrace.Trace
return td
}

type Resource struct {
id byte
scopes []Scope
}

type Scope struct {
id byte
spans []Span
}

type Span struct {
id byte
spanEvents string
func NewTracesFromOpts(resources ...ptrace.ResourceSpans) ptrace.Traces {
md := ptrace.NewTraces()
for _, resource := range resources {
resource.CopyTo(md.ResourceSpans().AppendEmpty())
}
return md
}

func WithResource(id byte, scopes ...Scope) Resource {
r := Resource{id: id}
r.scopes = append(r.scopes, scopes...)
return r
func Resource(id string, scopes ...ptrace.ScopeSpans) ptrace.ResourceSpans {
rm := ptrace.NewResourceSpans()
rm.Resource().Attributes().PutStr("resourceName", "resource"+id)
for _, scope := range scopes {
scope.CopyTo(rm.ScopeSpans().AppendEmpty())
}
return rm
}

func WithScope(id byte, spans ...Span) Scope {
s := Scope{id: id}
s.spans = append(s.spans, spans...)
func Scope(id string, spans ...ptrace.Span) ptrace.ScopeSpans {
s := ptrace.NewScopeSpans()
s.Scope().SetName("scope" + id)
for _, span := range spans {
span.CopyTo(s.Spans().AppendEmpty())
}
return s
}

func WithSpan(id byte, spanEvents string) Span {
return Span{id: id, spanEvents: spanEvents}
func Span(id string, ses ...ptrace.SpanEvent) ptrace.Span {
m := ptrace.NewSpan()
m.SetName("span" + id)
for _, se := range ses {
se.CopyTo(m.Events().AppendEmpty())
}
return m
}

// NewTracesFromOpts creates a ptrace.Traces with the specified resources, scopes, metrics,
// and data points. The general idea is the same as NewMetrics, but this function allows for
// more flexibility in creating non-uniform structures.
func NewTracesFromOpts(resources ...Resource) ptrace.Traces {
td := ptrace.NewTraces()
for _, resource := range resources {
r := td.ResourceSpans().AppendEmpty()
r.Resource().Attributes().PutStr("resourceName", "resource"+string(resource.id))
for _, scope := range resource.scopes {
ss := r.ScopeSpans().AppendEmpty()
ss.Scope().SetName("scope" + string(scope.id))
for _, span := range scope.spans {
s := ss.Spans().AppendEmpty()
s.SetName("span" + string(span.id))
for i := 0; i < len(span.spanEvents); i++ {
s.Events().AppendEmpty().Attributes().PutStr("spanEventName", "spanEvent"+string(span.spanEvents[i]))
}
}
}
}
return td
func SpanEvent(id string) ptrace.SpanEvent {
dp := ptrace.NewSpanEvent()
dp.Attributes().PutStr("spanEventName", "spanEvent"+id)
return dp
}
Loading

0 comments on commit 4711a64

Please sign in to comment.