sumo_marshaler_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package awss3exporter
  4. import (
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. "go.opentelemetry.io/collector/pdata/pcommon"
  10. "go.opentelemetry.io/collector/pdata/plog"
  11. )
  12. func TestMarshalerMissingAttributes(t *testing.T) {
  13. logs := plog.NewLogs()
  14. rl := logs.ResourceLogs().AppendEmpty()
  15. rl.ScopeLogs().AppendEmpty()
  16. marshaler := &sumoMarshaler{}
  17. require.NotNil(t, marshaler)
  18. _, err := marshaler.MarshalLogs(logs)
  19. assert.Error(t, err)
  20. }
  21. func TestMarshalerMissingSourceHost(t *testing.T) {
  22. logs := plog.NewLogs()
  23. rls := logs.ResourceLogs().AppendEmpty()
  24. rls.Resource().Attributes().PutStr("_sourceCategory", "testcategory")
  25. marshaler := &sumoMarshaler{}
  26. require.NotNil(t, marshaler)
  27. _, err := marshaler.MarshalLogs(logs)
  28. assert.Error(t, err)
  29. }
  30. func TestMarshalerMissingScopedLogs(t *testing.T) {
  31. logs := plog.NewLogs()
  32. rls := logs.ResourceLogs().AppendEmpty()
  33. rls.Resource().Attributes().PutStr("_sourceCategory", "testcategory")
  34. rls.Resource().Attributes().PutStr("_sourceHost", "testHost")
  35. rls.Resource().Attributes().PutStr("_sourceName", "testName")
  36. marshaler := &sumoMarshaler{}
  37. require.NotNil(t, marshaler)
  38. _, err := marshaler.MarshalLogs(logs)
  39. assert.NoError(t, err)
  40. }
  41. func TestMarshalerMissingSourceName(t *testing.T) {
  42. logs := plog.NewLogs()
  43. rls := logs.ResourceLogs().AppendEmpty()
  44. rls.Resource().Attributes().PutStr("_sourceCategory", "testcategory")
  45. rls.Resource().Attributes().PutStr("_sourceHost", "testHost")
  46. sl := rls.ScopeLogs().AppendEmpty()
  47. const recordNum = 0
  48. ts := pcommon.Timestamp(int64(recordNum) * time.Millisecond.Nanoseconds())
  49. logRecord := sl.LogRecords().AppendEmpty()
  50. logRecord.Body().SetStr("entry1")
  51. logRecord.SetTimestamp(ts)
  52. marshaler := &sumoMarshaler{}
  53. require.NotNil(t, marshaler)
  54. _, err := marshaler.MarshalLogs(logs)
  55. assert.Error(t, err)
  56. }
  57. func TestMarshalerOkStructure(t *testing.T) {
  58. logs := plog.NewLogs()
  59. rls := logs.ResourceLogs().AppendEmpty()
  60. rls.Resource().Attributes().PutStr("_sourceCategory", "testcategory")
  61. rls.Resource().Attributes().PutStr("_sourceHost", "testHost")
  62. rls.Resource().Attributes().PutStr("_sourceName", "testSourceName")
  63. rls.Resource().Attributes().PutStr("42", "the question")
  64. slice := rls.Resource().Attributes().PutEmptySlice("slice")
  65. pcommon.NewValueInt(13).CopyTo(slice.AppendEmpty())
  66. m := pcommon.NewValueMap()
  67. m.Map().PutBool("b", true)
  68. m.CopyTo(slice.AppendEmpty())
  69. sl := rls.ScopeLogs().AppendEmpty()
  70. const recordNum = 0
  71. ts := pcommon.Timestamp(int64(recordNum) * time.Millisecond.Nanoseconds())
  72. logRecord := sl.LogRecords().AppendEmpty()
  73. logRecord.Body().SetStr("entry1")
  74. logRecord.SetTimestamp(ts)
  75. logRecord.Attributes().PutStr("key", "value")
  76. marshaler := &sumoMarshaler{}
  77. require.NotNil(t, marshaler)
  78. buf, err := marshaler.MarshalLogs(logs)
  79. assert.NoError(t, err)
  80. expectedEntry := "{\"date\": \"1970-01-01 00:00:00 +0000 UTC\",\"sourceName\":\"testSourceName\",\"sourceHost\":\"testHost\""
  81. expectedEntry += ",\"sourceCategory\":\"testcategory\",\"fields\":{\"42\":\"the question\",\"slice\":[13,{\"b\":true}]},\"message\":{\"key\":\"value\",\"log\":\"entry1\"}}\n"
  82. assert.Equal(t, expectedEntry, string(buf))
  83. }
  84. func TestMarshalerQuotes(t *testing.T) {
  85. logs := plog.NewLogs()
  86. rls := logs.ResourceLogs().AppendEmpty()
  87. rls.Resource().Attributes().PutStr("_sourceCategory", `"foo"bar"`)
  88. rls.Resource().Attributes().PutStr("_sourceHost", "testHost")
  89. rls.Resource().Attributes().PutStr("_sourceName", "testSourceName")
  90. sl := rls.ScopeLogs().AppendEmpty()
  91. const recordNum = 0
  92. ts := pcommon.Timestamp(int64(recordNum) * time.Millisecond.Nanoseconds())
  93. logRecord := sl.LogRecords().AppendEmpty()
  94. logRecord.Body().SetStr("entry1")
  95. logRecord.SetTimestamp(ts)
  96. marshaler := &sumoMarshaler{}
  97. require.NotNil(t, marshaler)
  98. buf, err := marshaler.MarshalLogs(logs)
  99. assert.NoError(t, err)
  100. expectedEntry := "{\"date\": \"1970-01-01 00:00:00 +0000 UTC\",\"sourceName\":\"testSourceName\",\"sourceHost\":\"testHost\""
  101. expectedEntry += ",\"sourceCategory\":\"\\\"foo\\\"bar\\\"\",\"fields\":{},\"message\":{\"log\":\"entry1\"}}\n"
  102. assert.Equal(t, expectedEntry, string(buf))
  103. }
  104. func TestAttributeValueToString(t *testing.T) {
  105. testCases := []struct {
  106. value pcommon.Value
  107. result string
  108. init func(pcommon.Value)
  109. }{
  110. {
  111. value: pcommon.NewValueBool(true),
  112. result: "true",
  113. },
  114. {
  115. value: pcommon.NewValueBytes(),
  116. result: "\"KiFN/wA=\"",
  117. init: func(v pcommon.Value) {
  118. v.Bytes().Append(42, 33, 77, 255, 0)
  119. },
  120. },
  121. {
  122. value: pcommon.NewValueDouble(1.69),
  123. result: "1.69",
  124. },
  125. {
  126. value: pcommon.NewValueInt(42),
  127. result: "42",
  128. },
  129. {
  130. // Format of a map entry:
  131. // " -> <key>: <type>(<value>)\n"
  132. // Type names: https://github.com/open-telemetry/opentelemetry-collector/blob/ed8547a8e5d6ed527e6d54136cb2e137b954f888/pdata/pcommon/value.go#L32
  133. value: pcommon.NewValueMap(),
  134. result: "{" +
  135. "\"bool\":false," +
  136. "\"map\":{}," +
  137. "\"string\":\"abc\"" +
  138. "}",
  139. init: func(v pcommon.Value) {
  140. m := v.Map()
  141. m.PutBool("bool", false)
  142. m.PutEmptyMap("map")
  143. m.PutStr("string", "abc")
  144. },
  145. },
  146. {
  147. value: pcommon.NewValueSlice(),
  148. result: "[110.37,[true],\"YWJj\",\"asdfg\"]",
  149. init: func(v pcommon.Value) {
  150. s := v.Slice()
  151. s.AppendEmpty().SetDouble(110.37)
  152. s.AppendEmpty().SetEmptySlice().AppendEmpty().SetBool(true)
  153. s.AppendEmpty().SetEmptyBytes().Append(97, 98, 99)
  154. s.AppendEmpty().SetStr("asdfg")
  155. },
  156. },
  157. {
  158. value: pcommon.NewValueStr("qwerty"),
  159. result: "qwerty",
  160. },
  161. }
  162. for _, testCase := range testCases {
  163. if testCase.init != nil {
  164. testCase.init(testCase.value)
  165. }
  166. val, err := attributeValueToString(testCase.value)
  167. assert.NoError(t, err)
  168. assert.Equal(t, testCase.result, val)
  169. }
  170. }