scraper_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package aerospikereceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/aerospikereceiver"
  4. import (
  5. "context"
  6. "errors"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/require"
  10. "go.opentelemetry.io/collector/component/componenttest"
  11. "go.opentelemetry.io/collector/consumer"
  12. "go.opentelemetry.io/collector/pdata/pcommon"
  13. "go.opentelemetry.io/collector/pdata/pmetric"
  14. "go.opentelemetry.io/collector/receiver"
  15. "go.opentelemetry.io/collector/receiver/receivertest"
  16. "go.uber.org/zap"
  17. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
  18. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/aerospikereceiver/internal/metadata"
  19. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/aerospikereceiver/mocks"
  20. )
  21. func TestNewAerospikeReceiver_BadEndpoint(t *testing.T) {
  22. testCases := []struct {
  23. name string
  24. endpoint string
  25. errMsg string
  26. }{
  27. {
  28. name: "no port",
  29. endpoint: "localhost",
  30. errMsg: "missing port in address",
  31. },
  32. {
  33. name: "no address",
  34. endpoint: "",
  35. errMsg: "missing port in address",
  36. },
  37. }
  38. cs, err := consumer.NewMetrics(func(ctx context.Context, ld pmetric.Metrics) error { return nil })
  39. require.NoError(t, err)
  40. for _, tc := range testCases {
  41. t.Run(tc.name, func(t *testing.T) {
  42. t.Parallel()
  43. cfg := &Config{Endpoint: tc.endpoint}
  44. receiver, err := newAerospikeReceiver(receiver.CreateSettings{}, cfg, cs)
  45. require.ErrorContains(t, err, tc.errMsg)
  46. require.Nil(t, receiver)
  47. })
  48. }
  49. }
  50. func TestScrape_CollectClusterMetrics(t *testing.T) {
  51. t.Parallel()
  52. logger, err := zap.NewDevelopment()
  53. require.NoError(t, err)
  54. now := pcommon.NewTimestampFromTime(time.Now().UTC())
  55. expectedMB := metadata.NewMetricsBuilder(metadata.DefaultMetricsBuilderConfig(), receivertest.NewNopCreateSettings())
  56. rb := metadata.NewResourceBuilder(metadata.DefaultResourceAttributesConfig())
  57. require.NoError(t, expectedMB.RecordAerospikeNodeConnectionOpenDataPoint(now, "22", metadata.AttributeConnectionTypeClient))
  58. rb.SetAerospikeNodeName("BB990C28F270008")
  59. expectedMB.EmitForResource(metadata.WithResource(rb.Emit()))
  60. require.NoError(t, expectedMB.RecordAerospikeNamespaceMemoryFreeDataPoint(now, "45"))
  61. rb.SetAerospikeNamespace("test")
  62. rb.SetAerospikeNodeName("BB990C28F270008")
  63. expectedMB.EmitForResource(metadata.WithResource(rb.Emit()))
  64. require.NoError(t, expectedMB.RecordAerospikeNamespaceMemoryFreeDataPoint(now, "30"))
  65. rb.SetAerospikeNamespace("bar")
  66. rb.SetAerospikeNodeName("BB990C28F270008")
  67. expectedMB.EmitForResource(metadata.WithResource(rb.Emit()))
  68. require.NoError(t, expectedMB.RecordAerospikeNodeConnectionOpenDataPoint(now, "1", metadata.AttributeConnectionTypeClient))
  69. rb.SetAerospikeNodeName("BB990C28F270009")
  70. expectedMB.EmitForResource(metadata.WithResource(rb.Emit()))
  71. require.NoError(t, expectedMB.RecordAerospikeNamespaceMemoryUsageDataPoint(now, "128", metadata.AttributeNamespaceComponentData))
  72. rb.SetAerospikeNamespace("test")
  73. rb.SetAerospikeNodeName("BB990C28F270009")
  74. expectedMB.EmitForResource(metadata.WithResource(rb.Emit()))
  75. // require.NoError(t, expectedMB.RecordAerospikeNamespaceMemoryUsageDataPoint(now, "badval", metadata.AttributeNamespaceComponentData))
  76. // expectedMB.EmitForResource(metadata.WithAerospikeNamespace("bar"), metadata.WithAerospikeNodeName("BB990C28F270009"))
  77. initialClient := mocks.NewAerospike(t)
  78. initialClient.On("Info").Return(clusterInfo{
  79. "BB990C28F270008": metricsMap{
  80. "node": "BB990C28F270008",
  81. "client_connections": "22",
  82. },
  83. "BB990C28F270009": metricsMap{
  84. "node": "BB990C28F270009",
  85. "client_connections": "1",
  86. },
  87. }, nil)
  88. initialClient.On("NamespaceInfo").Return(namespaceInfo{
  89. "BB990C28F270008": map[string]map[string]string{
  90. "test": metricsMap{
  91. "name": "test",
  92. "memory_free_pct": "45",
  93. },
  94. "bar": metricsMap{
  95. "name": "bar",
  96. "memory_free_pct": "30",
  97. },
  98. },
  99. "BB990C28F270009": map[string]map[string]string{
  100. "test": metricsMap{
  101. "name": "test",
  102. "memory_used_data_bytes": "128",
  103. },
  104. "bar": metricsMap{
  105. "name": "bar",
  106. "memory_used_data_bytes": "badval",
  107. },
  108. },
  109. }, nil)
  110. initialClient.On("Close").Return(nil)
  111. clientFactory := func() (Aerospike, error) {
  112. return initialClient, nil
  113. }
  114. clientFactoryNeg := func() (Aerospike, error) {
  115. return nil, errors.New("connection timeout")
  116. }
  117. receiver := &aerospikeReceiver{
  118. clientFactory: clientFactory,
  119. mb: metadata.NewMetricsBuilder(metadata.DefaultMetricsBuilderConfig(), receivertest.NewNopCreateSettings()),
  120. logger: logger.Sugar(),
  121. config: &Config{
  122. CollectClusterMetrics: true,
  123. },
  124. }
  125. require.NoError(t, receiver.start(context.Background(), componenttest.NewNopHost()))
  126. actualMetrics, err := receiver.scrape(context.Background())
  127. require.EqualError(t, err, "failed to parse int64 for AerospikeNamespaceMemoryUsage, value was badval: strconv.ParseInt: parsing \"badval\": invalid syntax")
  128. expectedMetrics := expectedMB.Emit()
  129. require.NoError(t, pmetrictest.CompareMetrics(expectedMetrics, actualMetrics, pmetrictest.IgnoreResourceMetricsOrder(),
  130. pmetrictest.IgnoreMetricDataPointsOrder(), pmetrictest.IgnoreStartTimestamp(), pmetrictest.IgnoreTimestamp()))
  131. require.NoError(t, receiver.shutdown(context.Background()))
  132. initialClient.AssertExpectations(t)
  133. receiverConnErr := &aerospikeReceiver{
  134. clientFactory: clientFactoryNeg,
  135. mb: metadata.NewMetricsBuilder(metadata.DefaultMetricsBuilderConfig(), receivertest.NewNopCreateSettings()),
  136. logger: logger.Sugar(),
  137. config: &Config{
  138. CollectClusterMetrics: true,
  139. },
  140. }
  141. initialClient.AssertNumberOfCalls(t, "Close", 1)
  142. err = receiverConnErr.start(context.Background(), componenttest.NewNopHost())
  143. require.NoError(t, err)
  144. require.Equal(t, receiverConnErr.client, nil, "client should be set to nil because of connection error")
  145. }