| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otlptext |
| 5 |
|
| 6 |
import ( |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
|
| 15 |
"go.opentelemetry.io/collector/pdata/ptrace" |
| 16 |
"go.opentelemetry.io/collector/pdata/testdata" |
| 17 |
) |
| 18 |
|
| 19 |
func TestTracesText(t *testing.T) { |
| 20 |
tests := []struct { |
| 21 |
name string |
| 22 |
in ptrace.Traces |
| 23 |
out string |
| 24 |
}{ |
| 25 |
{ |
| 26 |
name: "empty_traces", |
| 27 |
in: ptrace.NewTraces(), |
| 28 |
out: "empty.out", |
| 29 |
}, |
| 30 |
{ |
| 31 |
name: "two_spans", |
| 32 |
in: testdata.GenerateTraces(2), |
| 33 |
out: "two_spans.out", |
| 34 |
}, |
| 35 |
{ |
| 36 |
name: "traces_with_entity_refs", |
| 37 |
in: generateTracesWithEntityRefs(), |
| 38 |
out: "traces_with_entity_refs.out", |
| 39 |
}, |
| 40 |
} |
| 41 |
for _, tt := range tests { |
| 42 |
t.Run(tt.name, func(t *testing.T) { |
| 43 |
got, err := NewTextTracesMarshaler().MarshalTraces(tt.in) |
| 44 |
require.NoError(t, err) |
| 45 |
out, err := os.ReadFile(filepath.Join("testdata", "traces", tt.out)) |
| 46 |
require.NoError(t, err) |
| 47 |
expected := strings.ReplaceAll(string(out), "\r", "") |
| 48 |
assert.Equal(t, expected, string(got)) |
| 49 |
}) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func generateTracesWithEntityRefs() ptrace.Traces { |
| 54 |
td := ptrace.NewTraces() |
| 55 |
rs := td.ResourceSpans().AppendEmpty() |
| 56 |
|
| 57 |
setupResourceWithEntityRefs(rs.Resource()) |
| 58 |
|
| 59 |
ss := rs.ScopeSpans().AppendEmpty() |
| 60 |
ss.Scope().SetName("test-scope") |
| 61 |
span := ss.Spans().AppendEmpty() |
| 62 |
span.SetName("test-span") |
| 63 |
span.SetSpanID([8]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}) |
| 64 |
span.SetTraceID([16]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10}) |
| 65 |
|
| 66 |
return td |
| 67 |
} |