zipkin_unmarshaler_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package kafkareceiver
  4. import (
  5. "context"
  6. "testing"
  7. "github.com/apache/thrift/lib/go/thrift"
  8. "github.com/jaegertracing/jaeger/thrift-gen/zipkincore"
  9. "github.com/openzipkin/zipkin-go/proto/zipkin_proto3"
  10. zipkinreporter "github.com/openzipkin/zipkin-go/reporter"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. "go.opentelemetry.io/collector/pdata/pcommon"
  14. "go.opentelemetry.io/collector/pdata/ptrace"
  15. conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
  16. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/zipkin/zipkinv2"
  17. )
  18. var v2FromTranslator zipkinv2.FromTranslator
  19. func TestUnmarshalZipkin(t *testing.T) {
  20. td := ptrace.NewTraces()
  21. rs := td.ResourceSpans().AppendEmpty()
  22. rs.Resource().Attributes().PutStr(conventions.AttributeServiceName, "my_service")
  23. span := rs.ScopeSpans().AppendEmpty().Spans().AppendEmpty()
  24. span.SetName("foo")
  25. span.SetStartTimestamp(pcommon.Timestamp(1597759000))
  26. span.SetEndTimestamp(pcommon.Timestamp(1597769000))
  27. span.SetTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
  28. span.SetSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})
  29. span.SetParentSpanID([8]byte{0, 0, 0, 0, 0, 0, 0, 0})
  30. spans, err := v2FromTranslator.FromTraces(td)
  31. require.NoError(t, err)
  32. serializer := zipkinreporter.JSONSerializer{}
  33. jsonBytes, err := serializer.Serialize(spans)
  34. require.NoError(t, err)
  35. tSpan := &zipkincore.Span{Name: "foo"}
  36. thriftTransport := thrift.NewTMemoryBuffer()
  37. protocolTransport := thrift.NewTBinaryProtocolConf(thriftTransport, nil)
  38. require.NoError(t, protocolTransport.WriteListBegin(context.Background(), thrift.STRUCT, 1))
  39. err = tSpan.Write(context.Background(), protocolTransport)
  40. require.NoError(t, err)
  41. require.NoError(t, protocolTransport.WriteListEnd(context.Background()))
  42. tdThrift, err := newZipkinThriftUnmarshaler().Unmarshal(thriftTransport.Buffer.Bytes())
  43. require.NoError(t, err)
  44. protoBytes, err := new(zipkin_proto3.SpanSerializer).Serialize(spans)
  45. require.NoError(t, err)
  46. tests := []struct {
  47. unmarshaler TracesUnmarshaler
  48. encoding string
  49. bytes []byte
  50. expected ptrace.Traces
  51. }{
  52. {
  53. unmarshaler: newZipkinProtobufUnmarshaler(),
  54. encoding: "zipkin_proto",
  55. bytes: protoBytes,
  56. expected: td,
  57. },
  58. {
  59. unmarshaler: newZipkinJSONUnmarshaler(),
  60. encoding: "zipkin_json",
  61. bytes: jsonBytes,
  62. expected: td,
  63. },
  64. {
  65. unmarshaler: newZipkinThriftUnmarshaler(),
  66. encoding: "zipkin_thrift",
  67. bytes: thriftTransport.Buffer.Bytes(),
  68. expected: tdThrift,
  69. },
  70. }
  71. for _, test := range tests {
  72. t.Run(test.encoding, func(t *testing.T) {
  73. traces, err := test.unmarshaler.Unmarshal(test.bytes)
  74. require.NoError(t, err)
  75. assert.Equal(t, test.expected, traces)
  76. assert.Equal(t, test.encoding, test.unmarshaler.Encoding())
  77. })
  78. }
  79. }
  80. func TestUnmarshalZipkinThrift_error(t *testing.T) {
  81. p := newZipkinThriftUnmarshaler()
  82. _, err := p.Unmarshal([]byte("+$%"))
  83. assert.Error(t, err)
  84. }
  85. func TestUnmarshalZipkinJSON_error(t *testing.T) {
  86. p := newZipkinJSONUnmarshaler()
  87. _, err := p.Unmarshal([]byte("+$%"))
  88. assert.Error(t, err)
  89. }
  90. func TestUnmarshalZipkinProto_error(t *testing.T) {
  91. p := newZipkinProtobufUnmarshaler()
  92. _, err := p.Unmarshal([]byte("+$%"))
  93. assert.Error(t, err)
  94. }