| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package normal |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
|
| 12 |
"go.opentelemetry.io/collector/pdata/ptrace" |
| 13 |
) |
| 14 |
|
| 15 |
func TestMarshalTraces(t *testing.T) { |
| 16 |
tests := []struct { |
| 17 |
name string |
| 18 |
input ptrace.Traces |
| 19 |
expected string |
| 20 |
}{ |
| 21 |
{ |
| 22 |
name: "empty traces", |
| 23 |
input: ptrace.NewTraces(), |
| 24 |
expected: "", |
| 25 |
}, |
| 26 |
{ |
| 27 |
name: "one span with resource and scope attributes", |
| 28 |
input: func() ptrace.Traces { |
| 29 |
traces := ptrace.NewTraces() |
| 30 |
resourceSpans := traces.ResourceSpans().AppendEmpty() |
| 31 |
resourceSpans.SetSchemaUrl("https://opentelemetry.io/resource-schema-url") |
| 32 |
resourceSpans.Resource().Attributes().PutStr("resourceKey1", "resourceValue1") |
| 33 |
resourceSpans.Resource().Attributes().PutBool("resourceKey2", false) |
| 34 |
scopeSpans := resourceSpans.ScopeSpans().AppendEmpty() |
| 35 |
scopeSpans.SetSchemaUrl("http://opentelemetry.io/scope-schema-url") |
| 36 |
scopeSpans.Scope().SetName("scope-name") |
| 37 |
scopeSpans.Scope().SetVersion("1.2.3") |
| 38 |
scopeSpans.Scope().Attributes().PutStr("scopeKey1", "scopeValue1") |
| 39 |
scopeSpans.Scope().Attributes().PutBool("scopeKey2", true) |
| 40 |
span := scopeSpans.Spans().AppendEmpty() |
| 41 |
span.SetName("span-name") |
| 42 |
span.SetTraceID([16]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10}) |
| 43 |
span.SetSpanID([8]byte{0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18}) |
| 44 |
span.Attributes().PutStr("key1", "value1") |
| 45 |
span.Attributes().PutStr("key2", "value2") |
| 46 |
return traces |
| 47 |
}(), |
| 48 |
expected: `ResourceTraces #0 [https://opentelemetry.io/resource-schema-url] resourceKey1=resourceValue1 resourceKey2=false |
| 49 |
ScopeTraces #0 [email protected] [http://opentelemetry.io/scope-schema-url] scopeKey1=scopeValue1 scopeKey2=true |
| 50 |
span-name 0102030405060708090a0b0c0d0e0f10 1112131415161718 key1=value1 key2=value2 |
| 51 |
`, |
| 52 |
}, |
| 53 |
{ |
| 54 |
name: "one span", |
| 55 |
input: func() ptrace.Traces { |
| 56 |
traces := ptrace.NewTraces() |
| 57 |
span := traces.ResourceSpans().AppendEmpty().ScopeSpans().AppendEmpty().Spans().AppendEmpty() |
| 58 |
span.SetName("span-name") |
| 59 |
span.SetTraceID([16]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10}) |
| 60 |
span.SetSpanID([8]byte{0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18}) |
| 61 |
span.Attributes().PutStr("key1", "value1") |
| 62 |
span.Attributes().PutStr("key2", "value2") |
| 63 |
return traces |
| 64 |
}(), |
| 65 |
expected: `ResourceTraces #0 |
| 66 |
ScopeTraces #0 |
| 67 |
span-name 0102030405060708090a0b0c0d0e0f10 1112131415161718 key1=value1 key2=value2 |
| 68 |
`, |
| 69 |
}, |
| 70 |
} |
| 71 |
for _, tt := range tests { |
| 72 |
t.Run(tt.name, func(t *testing.T) { |
| 73 |
output, err := NewNormalTracesMarshaler().MarshalTraces(tt.input) |
| 74 |
require.NoError(t, err) |
| 75 |
assert.Equal(t, tt.expected, string(output)) |
| 76 |
}) |
| 77 |
} |
| 78 |
} |