collectd_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package collectdreceiver
  4. import (
  5. "encoding/json"
  6. "os"
  7. "path/filepath"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. "go.opentelemetry.io/collector/pdata/pcommon"
  13. "go.opentelemetry.io/collector/pdata/pmetric"
  14. "go.uber.org/zap"
  15. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
  16. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
  17. )
  18. func TestDecodeEvent(t *testing.T) {
  19. metrics := pmetric.NewMetrics()
  20. scopeMetrics := metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty()
  21. jsonData, err := os.ReadFile(filepath.Join("testdata", "event.json"))
  22. require.NoError(t, err)
  23. var records []collectDRecord
  24. err = json.Unmarshal(jsonData, &records)
  25. require.NoError(t, err)
  26. for _, cdr := range records {
  27. err := cdr.appendToMetrics(zap.NewNop(), scopeMetrics, map[string]string{})
  28. assert.NoError(t, err)
  29. assert.Equal(t, 0, metrics.MetricCount())
  30. }
  31. }
  32. func TestDecodeMetrics(t *testing.T) {
  33. metrics := pmetric.NewMetrics()
  34. scopeMemtrics := metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty()
  35. jsonData, err := os.ReadFile(filepath.Join("testdata", "collectd.json"))
  36. require.NoError(t, err)
  37. var records []collectDRecord
  38. err = json.Unmarshal(jsonData, &records)
  39. require.NoError(t, err)
  40. for _, cdr := range records {
  41. err = cdr.appendToMetrics(zap.NewNop(), scopeMemtrics, map[string]string{})
  42. assert.NoError(t, err)
  43. }
  44. assert.Equal(t, 10, metrics.MetricCount())
  45. assertMetricsEqual(t, metrics)
  46. }
  47. func createPtrFloat64(v float64) *float64 {
  48. return &v
  49. }
  50. func TestStartTimestamp(t *testing.T) {
  51. tests := []struct {
  52. name string
  53. record collectDRecord
  54. metricDescriptorType string
  55. wantStartTimestamp pcommon.Timestamp
  56. }{
  57. {
  58. name: "metric type cumulative distribution",
  59. record: collectDRecord{
  60. Time: createPtrFloat64(10),
  61. Interval: createPtrFloat64(5),
  62. },
  63. metricDescriptorType: "cumulative",
  64. wantStartTimestamp: pcommon.NewTimestampFromTime(time.Unix(5, 0)),
  65. },
  66. {
  67. name: "metric type cumulative double",
  68. record: collectDRecord{
  69. Time: createPtrFloat64(10),
  70. Interval: createPtrFloat64(5),
  71. },
  72. metricDescriptorType: "cumulative",
  73. wantStartTimestamp: pcommon.NewTimestampFromTime(time.Unix(5, 0)),
  74. },
  75. {
  76. name: "metric type cumulative int64",
  77. record: collectDRecord{
  78. Time: createPtrFloat64(10),
  79. Interval: createPtrFloat64(5),
  80. },
  81. metricDescriptorType: "cumulative",
  82. wantStartTimestamp: pcommon.NewTimestampFromTime(time.Unix(5, 0)),
  83. },
  84. {
  85. name: "metric type non-cumulative gauge distribution",
  86. record: collectDRecord{
  87. Time: createPtrFloat64(0),
  88. Interval: createPtrFloat64(0),
  89. },
  90. metricDescriptorType: "gauge",
  91. wantStartTimestamp: pcommon.NewTimestampFromTime(time.Unix(0, 0)),
  92. },
  93. {
  94. name: "metric type non-cumulative gauge int64",
  95. record: collectDRecord{
  96. Time: createPtrFloat64(0),
  97. Interval: createPtrFloat64(0),
  98. },
  99. metricDescriptorType: "gauge",
  100. wantStartTimestamp: pcommon.NewTimestampFromTime(time.Unix(0, 0)),
  101. },
  102. {
  103. name: "metric type non-cumulativegauge double",
  104. record: collectDRecord{
  105. Time: createPtrFloat64(0),
  106. Interval: createPtrFloat64(0),
  107. },
  108. metricDescriptorType: "gauge",
  109. wantStartTimestamp: pcommon.NewTimestampFromTime(time.Unix(0, 0)),
  110. },
  111. }
  112. for _, tc := range tests {
  113. t.Run(tc.name, func(t *testing.T) {
  114. gotStartTimestamp := tc.record.startTimestamp(tc.metricDescriptorType)
  115. assert.Equal(t, tc.wantStartTimestamp.AsTime(), gotStartTimestamp.AsTime())
  116. })
  117. }
  118. }
  119. func assertMetricsEqual(t *testing.T, actual pmetric.Metrics) {
  120. goldenPath := filepath.Join("testdata", "expected.yaml")
  121. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  122. require.NoError(t, err)
  123. err = pmetrictest.CompareMetrics(expectedMetrics, actual, pmetrictest.IgnoreStartTimestamp(),
  124. pmetrictest.IgnoreTimestamp(),
  125. pmetrictest.IgnoreMetricsOrder())
  126. require.NoError(t, err)
  127. }