common_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package splunk
  4. import (
  5. "encoding/json"
  6. "strings"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestGetValues(t *testing.T) {
  11. metric := Event{
  12. Fields: map[string]any{},
  13. }
  14. assert.Equal(t, map[string]any{}, metric.GetMetricValues())
  15. metric.Fields["metric_name:foo"] = "bar"
  16. assert.Equal(t, map[string]any{"foo": "bar"}, metric.GetMetricValues())
  17. metric.Fields["metric_name:foo2"] = "foobar"
  18. assert.Equal(t, map[string]any{"foo": "bar", "foo2": "foobar"}, metric.GetMetricValues())
  19. }
  20. func TestIsMetric(t *testing.T) {
  21. ev := Event{
  22. Event: map[string]any{},
  23. }
  24. assert.False(t, ev.IsMetric())
  25. metric := Event{
  26. Event: "metric",
  27. }
  28. assert.True(t, metric.IsMetric())
  29. arr := Event{
  30. Event: []any{"foo", "bar"},
  31. }
  32. assert.False(t, arr.IsMetric())
  33. yo := Event{
  34. Event: "yo",
  35. }
  36. assert.False(t, yo.IsMetric())
  37. }
  38. func TestIsMetric_WithoutEventField(t *testing.T) {
  39. fieldsOnly := Event{
  40. Fields: map[string]any{
  41. "foo": "bar",
  42. },
  43. }
  44. assert.False(t, fieldsOnly.IsMetric())
  45. fieldsWithMetrics := Event{
  46. Fields: map[string]any{
  47. "foo": "bar",
  48. "metric_name:foo": 123,
  49. "foobar": "foobar",
  50. },
  51. }
  52. assert.True(t, fieldsWithMetrics.IsMetric())
  53. }
  54. func TestDecodeJsonWithNoTime(t *testing.T) {
  55. dec := json.NewDecoder(strings.NewReader("{\"event\":\"hello\"}"))
  56. dec.More()
  57. var msg Event
  58. err := dec.Decode(&msg)
  59. assert.NoError(t, err)
  60. assert.Zero(t, msg.Time)
  61. }
  62. func TestDecodeJsonWithNumberTime(t *testing.T) {
  63. dec := json.NewDecoder(strings.NewReader("{\"time\":1610760752.606,\"event\":\"hello\"}"))
  64. dec.More()
  65. var msg Event
  66. err := dec.Decode(&msg)
  67. assert.NoError(t, err)
  68. assert.Equal(t, 1610760752.606, msg.Time)
  69. }
  70. func TestDecodeJsonWithStringTime(t *testing.T) {
  71. dec := json.NewDecoder(strings.NewReader("{\"time\":\"1610760752.606\",\"event\":\"hello\"}"))
  72. dec.More()
  73. var msg Event
  74. err := dec.Decode(&msg)
  75. assert.NoError(t, err)
  76. assert.Equal(t, 1610760752.606, msg.Time)
  77. }
  78. func TestDecodeJsonWithInvalidStringTime(t *testing.T) {
  79. dec := json.NewDecoder(strings.NewReader("{\"time\":\"1610760752.606\\\"\",\"event\":\"hello\"}"))
  80. dec.More()
  81. var msg Event
  82. err := dec.Decode(&msg)
  83. assert.Error(t, err)
  84. }
  85. func TestDecodeJsonWithInvalidNumberStringTime(t *testing.T) {
  86. dec := json.NewDecoder(strings.NewReader("{\"time\":\"0xdeadbeef\",\"event\":\"hello\"}"))
  87. dec.More()
  88. var msg Event
  89. err := dec.Decode(&msg)
  90. assert.Error(t, err)
  91. }
  92. func TestDecodeJsonWithInvalidNumberTime(t *testing.T) {
  93. dec := json.NewDecoder(strings.NewReader("{\"time\":1e1024,\"event\":\"hello\"}"))
  94. dec.More()
  95. var msg Event
  96. err := dec.Decode(&msg)
  97. assert.Error(t, err)
  98. }