receiver_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package collectdreceiver
  4. import (
  5. "bytes"
  6. "context"
  7. "net/http"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. "go.opentelemetry.io/collector/component"
  13. "go.opentelemetry.io/collector/component/componenttest"
  14. "go.opentelemetry.io/collector/config/confighttp"
  15. "go.opentelemetry.io/collector/consumer"
  16. "go.opentelemetry.io/collector/consumer/consumertest"
  17. "go.opentelemetry.io/collector/pdata/pcommon"
  18. "go.opentelemetry.io/collector/pdata/pmetric"
  19. "go.opentelemetry.io/collector/receiver/receivertest"
  20. "go.uber.org/zap"
  21. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
  22. )
  23. type wantedBody struct {
  24. Name string
  25. Time float64
  26. Attributes map[string]string
  27. Value float64
  28. }
  29. func TestNewReceiver(t *testing.T) {
  30. type args struct {
  31. config *Config
  32. attrsPrefix string
  33. nextConsumer consumer.Metrics
  34. }
  35. tests := []struct {
  36. name string
  37. args args
  38. wantErr error
  39. }{
  40. {
  41. name: "nil next Consumer",
  42. args: args{
  43. config: &Config{
  44. HTTPServerSettings: confighttp.HTTPServerSettings{
  45. Endpoint: ":0",
  46. },
  47. },
  48. attrsPrefix: "default_attr_",
  49. },
  50. wantErr: component.ErrNilNextConsumer,
  51. },
  52. {
  53. name: "happy path",
  54. args: args{
  55. config: &Config{
  56. HTTPServerSettings: confighttp.HTTPServerSettings{
  57. Endpoint: ":0",
  58. },
  59. },
  60. attrsPrefix: "default_attr_",
  61. nextConsumer: consumertest.NewNop(),
  62. },
  63. },
  64. }
  65. logger := zap.NewNop()
  66. for _, tt := range tests {
  67. t.Run(tt.name, func(t *testing.T) {
  68. _, err := newCollectdReceiver(logger, tt.args.config, "", tt.args.nextConsumer, receivertest.NewNopCreateSettings())
  69. require.ErrorIs(t, err, tt.wantErr)
  70. })
  71. }
  72. }
  73. func TestCollectDServer(t *testing.T) {
  74. t.Parallel()
  75. type testCase struct {
  76. Name string
  77. HTTPMethod string
  78. QueryParams string
  79. RequestBody string
  80. ResponseCode int
  81. WantData []pmetric.Metrics
  82. }
  83. config := &Config{
  84. HTTPServerSettings: confighttp.HTTPServerSettings{
  85. Endpoint: "localhost:8081",
  86. },
  87. }
  88. defaultAttrsPrefix := "dap_"
  89. wantedRequestBody := wantedBody{
  90. Name: "memory.free",
  91. Time: 1415062577.4949999,
  92. Attributes: map[string]string{
  93. "plugin": "memory",
  94. "host": "i-b13d1e5f",
  95. "dsname": "value",
  96. "attr1": "attr1val",
  97. },
  98. Value: 2.1474,
  99. }
  100. wantedRequestBodyMetrics := createWantedMetrics(wantedRequestBody)
  101. testInvalidHTTPMethodCase := testCase{
  102. Name: "invalid-http-method",
  103. HTTPMethod: "GET",
  104. RequestBody: `invalid-body`,
  105. ResponseCode: 400,
  106. WantData: []pmetric.Metrics{},
  107. }
  108. testValidRequestBodyCase := testCase{
  109. Name: "valid-request-body",
  110. HTTPMethod: "POST",
  111. QueryParams: "dap_attr1=attr1val",
  112. RequestBody: `[
  113. {
  114. "dsnames": [
  115. "value"
  116. ],
  117. "dstypes": [
  118. "derive"
  119. ],
  120. "host": "i-b13d1e5f",
  121. "interval": 10.0,
  122. "plugin": "memory",
  123. "plugin_instance": "",
  124. "time": 1415062577.4949999,
  125. "type": "memory",
  126. "type_instance": "free",
  127. "values": [
  128. 2.1474
  129. ]
  130. }
  131. ]`,
  132. ResponseCode: 200,
  133. WantData: []pmetric.Metrics{wantedRequestBodyMetrics},
  134. }
  135. testInValidRequestBodyCase := testCase{
  136. Name: "invalid-request-body",
  137. HTTPMethod: "POST",
  138. RequestBody: `invalid-body`,
  139. ResponseCode: 400,
  140. WantData: []pmetric.Metrics{},
  141. }
  142. testCases := []testCase{testInvalidHTTPMethodCase, testValidRequestBodyCase, testInValidRequestBodyCase}
  143. sink := new(consumertest.MetricsSink)
  144. logger := zap.NewNop()
  145. cdr, err := newCollectdReceiver(logger, config, defaultAttrsPrefix, sink, receivertest.NewNopCreateSettings())
  146. if err != nil {
  147. t.Fatalf("Failed to create receiver: %v", err)
  148. }
  149. require.NoError(t, cdr.Start(context.Background(), componenttest.NewNopHost()))
  150. t.Cleanup(func() {
  151. err := cdr.Shutdown(context.Background())
  152. if err != nil {
  153. t.Fatalf("Error stopping metrics reception: %v", err)
  154. }
  155. })
  156. time.Sleep(time.Second)
  157. for _, tt := range testCases {
  158. t.Run(tt.Name, func(t *testing.T) {
  159. sink.Reset()
  160. req, err := http.NewRequest(
  161. tt.HTTPMethod,
  162. "http://"+config.HTTPServerSettings.Endpoint+"?"+tt.QueryParams,
  163. bytes.NewBuffer([]byte(tt.RequestBody)),
  164. )
  165. require.NoError(t, err)
  166. req.Header.Set("Content-Type", "application/json")
  167. client := &http.Client{}
  168. resp, err := client.Do(req)
  169. require.NoError(t, err)
  170. assert.Equal(t, tt.ResponseCode, resp.StatusCode)
  171. defer resp.Body.Close()
  172. if tt.ResponseCode != 200 {
  173. return
  174. }
  175. assert.Eventually(t, func() bool {
  176. return len(sink.AllMetrics()) == 1
  177. }, 10*time.Second, 5*time.Millisecond)
  178. mds := sink.AllMetrics()
  179. require.Len(t, mds, 1)
  180. assertMetricsAreEqual(t, tt.WantData, mds)
  181. })
  182. }
  183. }
  184. func createWantedMetrics(wantedRequestBody wantedBody) pmetric.Metrics {
  185. var dataPoint pmetric.NumberDataPoint
  186. testMetrics := pmetric.NewMetrics()
  187. scopeMemtrics := testMetrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty()
  188. testMetric := pmetric.NewMetric()
  189. testMetric.SetName(wantedRequestBody.Name)
  190. sum := testMetric.SetEmptySum()
  191. sum.SetIsMonotonic(true)
  192. dataPoint = sum.DataPoints().AppendEmpty()
  193. dataPoint.SetTimestamp(pcommon.NewTimestampFromTime(time.Unix(0, int64(float64(time.Second)*wantedRequestBody.Time))))
  194. attributes := pcommon.NewMap()
  195. for key, value := range wantedRequestBody.Attributes {
  196. attributes.PutStr(key, value)
  197. }
  198. attributes.CopyTo(dataPoint.Attributes())
  199. dataPoint.SetDoubleValue(wantedRequestBody.Value)
  200. newMetric := scopeMemtrics.Metrics().AppendEmpty()
  201. testMetric.MoveTo(newMetric)
  202. return testMetrics
  203. }
  204. func assertMetricsAreEqual(t *testing.T, expectedData []pmetric.Metrics, actualData []pmetric.Metrics) {
  205. for i := 0; i < len(expectedData); i++ {
  206. err := pmetrictest.CompareMetrics(expectedData[i], actualData[i])
  207. require.NoError(t, err)
  208. }
  209. }