scraper_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package oracledbreceiver
  4. import (
  5. "context"
  6. "database/sql"
  7. "errors"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. "go.opentelemetry.io/collector/component"
  12. "go.opentelemetry.io/collector/component/componenttest"
  13. "go.opentelemetry.io/collector/pdata/pmetric"
  14. "go.opentelemetry.io/collector/receiver/receivertest"
  15. "go.opentelemetry.io/collector/receiver/scrapererror"
  16. "go.uber.org/zap"
  17. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/oracledbreceiver/internal/metadata"
  18. )
  19. func TestScraper_ErrorOnStart(t *testing.T) {
  20. scrpr := scraper{
  21. dbProviderFunc: func() (*sql.DB, error) {
  22. return nil, errors.New("oops")
  23. },
  24. }
  25. err := scrpr.start(context.Background(), componenttest.NewNopHost())
  26. require.Error(t, err)
  27. }
  28. var queryResponses = map[string][]metricRow{
  29. statsSQL: {{"NAME": enqueueDeadlocks, "VALUE": "18"}, {"NAME": exchangeDeadlocks, "VALUE": "88898"}, {"NAME": executeCount, "VALUE": "178878"}, {"NAME": parseCountTotal, "VALUE": "1999"}, {"NAME": parseCountHard, "VALUE": "1"}, {"NAME": userCommits, "VALUE": "187778888"}, {"NAME": userRollbacks, "VALUE": "1898979879789"}, {"NAME": physicalReads, "VALUE": "1887777"}, {"NAME": sessionLogicalReads, "VALUE": "189"}, {"NAME": cpuTime, "VALUE": "1887"}, {"NAME": pgaMemory, "VALUE": "1999887"}, {"NAME": dbBlockGets, "VALUE": "42"}, {"NAME": consistentGets, "VALUE": "78944"}},
  30. sessionCountSQL: {{"VALUE": "1"}},
  31. systemResourceLimitsSQL: {{"RESOURCE_NAME": "processes", "CURRENT_UTILIZATION": "3", "MAX_UTILIZATION": "10", "INITIAL_ALLOCATION": "100", "LIMIT_VALUE": "100"},
  32. {"RESOURCE_NAME": "locks", "CURRENT_UTILIZATION": "3", "MAX_UTILIZATION": "10", "INITIAL_ALLOCATION": "-1", "LIMIT_VALUE": "-1"}},
  33. tablespaceUsageSQL: {{"TABLESPACE_NAME": "SYS", "BYTES": "1024"}},
  34. tablespaceMaxSpaceSQL: {{"TABLESPACE_NAME": "SYS", "VALUE": "1024"}},
  35. }
  36. func TestScraper_Scrape(t *testing.T) {
  37. tests := []struct {
  38. name string
  39. dbclientFn func(db *sql.DB, s string, logger *zap.Logger) dbClient
  40. errWanted string
  41. }{
  42. {
  43. name: "valid",
  44. dbclientFn: func(db *sql.DB, s string, logger *zap.Logger) dbClient {
  45. return &fakeDbClient{
  46. Responses: [][]metricRow{
  47. queryResponses[s],
  48. },
  49. }
  50. },
  51. },
  52. {
  53. name: "bad tablespace usage",
  54. dbclientFn: func(db *sql.DB, s string, logger *zap.Logger) dbClient {
  55. if s == tablespaceUsageSQL {
  56. return &fakeDbClient{Responses: [][]metricRow{
  57. {
  58. {},
  59. },
  60. }}
  61. }
  62. return &fakeDbClient{Responses: [][]metricRow{
  63. queryResponses[s],
  64. }}
  65. },
  66. errWanted: `failed to parse int64 for OracledbTablespaceSizeUsage, value was : strconv.ParseInt: parsing "": invalid syntax`,
  67. },
  68. {
  69. name: "no limit on tablespace",
  70. dbclientFn: func(db *sql.DB, s string, logger *zap.Logger) dbClient {
  71. if s == tablespaceMaxSpaceSQL {
  72. return &fakeDbClient{Responses: [][]metricRow{
  73. {
  74. {"TABLESPACE_NAME": "SYS", "VALUE": "1024"},
  75. {"TABLESPACE_NAME": "FOO", "VALUE": ""},
  76. },
  77. }}
  78. }
  79. return &fakeDbClient{Responses: [][]metricRow{
  80. queryResponses[s],
  81. }}
  82. },
  83. },
  84. {
  85. name: "bad value on tablespace",
  86. dbclientFn: func(db *sql.DB, s string, logger *zap.Logger) dbClient {
  87. if s == tablespaceMaxSpaceSQL {
  88. return &fakeDbClient{Responses: [][]metricRow{
  89. {
  90. {"TABLESPACE_NAME": "SYS", "VALUE": "1024"},
  91. {"TABLESPACE_NAME": "FOO", "VALUE": "ert"},
  92. },
  93. }}
  94. }
  95. return &fakeDbClient{Responses: [][]metricRow{
  96. queryResponses[s],
  97. }}
  98. },
  99. errWanted: `failed to parse int64 for OracledbTablespaceSizeLimit, value was ert: strconv.ParseInt: parsing "ert": invalid syntax`,
  100. },
  101. }
  102. for _, test := range tests {
  103. t.Run(test.name, func(t *testing.T) {
  104. cfg := metadata.DefaultMetricsBuilderConfig()
  105. cfg.Metrics.OracledbConsistentGets.Enabled = true
  106. cfg.Metrics.OracledbDbBlockGets.Enabled = true
  107. scrpr := scraper{
  108. logger: zap.NewNop(),
  109. mb: metadata.NewMetricsBuilder(cfg, receivertest.NewNopCreateSettings()),
  110. dbProviderFunc: func() (*sql.DB, error) {
  111. return nil, nil
  112. },
  113. clientProviderFunc: test.dbclientFn,
  114. id: component.ID{},
  115. metricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
  116. }
  117. err := scrpr.start(context.Background(), componenttest.NewNopHost())
  118. defer func() {
  119. assert.NoError(t, scrpr.shutdown(context.Background()))
  120. }()
  121. require.NoError(t, err)
  122. m, err := scrpr.scrape(context.Background())
  123. if test.errWanted != "" {
  124. require.True(t, scrapererror.IsPartialScrapeError(err))
  125. require.EqualError(t, err, test.errWanted)
  126. } else {
  127. require.NoError(t, err)
  128. assert.Equal(t, 18, m.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().Len())
  129. }
  130. name, ok := m.ResourceMetrics().At(0).Resource().Attributes().Get("oracledb.instance.name")
  131. assert.True(t, ok)
  132. assert.Equal(t, "", name.Str())
  133. var found pmetric.Metric
  134. for i := 0; i < m.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().Len(); i++ {
  135. metric := m.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(i)
  136. if metric.Name() == "oracledb.consistent_gets" {
  137. found = metric
  138. break
  139. }
  140. }
  141. assert.Equal(t, int64(78944), found.Sum().DataPoints().At(0).IntValue())
  142. })
  143. }
  144. }