scraper_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package saphanareceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/saphanareceiver"
  4. import (
  5. "context"
  6. "encoding/json"
  7. "os"
  8. "testing"
  9. "github.com/stretchr/testify/require"
  10. "go.opentelemetry.io/collector/receiver/receivertest"
  11. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
  12. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
  13. )
  14. const fullExpectedMetricsPath = "./testdata/expected_metrics/full.yaml"
  15. const partialExpectedMetricsPath = "./testdata/expected_metrics/mostly_disabled.yaml"
  16. const allQueryMetrics = "./testdata/mocked_queries/all_query_results.json"
  17. const mostlyDisabledQueryMetrics = "./testdata/mocked_queries/mostly_disabled_results.json"
  18. func TestScraper(t *testing.T) {
  19. t.Parallel()
  20. dbWrapper := &testDBWrapper{}
  21. initializeWrapper(t, dbWrapper, allQueryMetrics)
  22. sc, err := newSapHanaScraper(receivertest.NewNopCreateSettings(), createDefaultConfig().(*Config), &testConnectionFactory{dbWrapper})
  23. require.NoError(t, err)
  24. expectedMetrics, err := golden.ReadMetrics(fullExpectedMetricsPath)
  25. require.NoError(t, err)
  26. actualMetrics, err := sc.Scrape(context.Background())
  27. require.NoError(t, err)
  28. require.NoError(t, pmetrictest.CompareMetrics(expectedMetrics, actualMetrics,
  29. pmetrictest.IgnoreResourceMetricsOrder(), pmetrictest.IgnoreMetricDataPointsOrder(),
  30. pmetrictest.IgnoreStartTimestamp(), pmetrictest.IgnoreTimestamp()))
  31. }
  32. func TestDisabledMetrics(t *testing.T) {
  33. t.Parallel()
  34. dbWrapper := &testDBWrapper{}
  35. initializeWrapper(t, dbWrapper, mostlyDisabledQueryMetrics)
  36. cfg := createDefaultConfig().(*Config)
  37. cfg.MetricsBuilderConfig.Metrics.SaphanaAlertCount.Enabled = false
  38. cfg.MetricsBuilderConfig.Metrics.SaphanaBackupLatest.Enabled = false
  39. cfg.MetricsBuilderConfig.Metrics.SaphanaColumnMemoryUsed.Enabled = false
  40. cfg.MetricsBuilderConfig.Metrics.SaphanaComponentMemoryUsed.Enabled = false
  41. cfg.MetricsBuilderConfig.Metrics.SaphanaConnectionCount.Enabled = false
  42. cfg.MetricsBuilderConfig.Metrics.SaphanaCPUUsed.Enabled = false
  43. cfg.MetricsBuilderConfig.Metrics.SaphanaDiskSizeCurrent.Enabled = false
  44. cfg.MetricsBuilderConfig.Metrics.SaphanaHostMemoryCurrent.Enabled = false
  45. cfg.MetricsBuilderConfig.Metrics.SaphanaHostSwapCurrent.Enabled = false
  46. cfg.MetricsBuilderConfig.Metrics.SaphanaInstanceCodeSize.Enabled = false
  47. cfg.MetricsBuilderConfig.Metrics.SaphanaInstanceMemoryCurrent.Enabled = false
  48. cfg.MetricsBuilderConfig.Metrics.SaphanaInstanceMemorySharedAllocated.Enabled = false
  49. cfg.MetricsBuilderConfig.Metrics.SaphanaInstanceMemoryUsedPeak.Enabled = false
  50. cfg.MetricsBuilderConfig.Metrics.SaphanaLicenseExpirationTime.Enabled = false
  51. cfg.MetricsBuilderConfig.Metrics.SaphanaLicenseLimit.Enabled = false
  52. cfg.MetricsBuilderConfig.Metrics.SaphanaLicensePeak.Enabled = false
  53. cfg.MetricsBuilderConfig.Metrics.SaphanaNetworkRequestAverageTime.Enabled = false
  54. cfg.MetricsBuilderConfig.Metrics.SaphanaNetworkRequestCount.Enabled = false
  55. cfg.MetricsBuilderConfig.Metrics.SaphanaNetworkRequestFinishedCount.Enabled = false
  56. cfg.MetricsBuilderConfig.Metrics.SaphanaReplicationAverageTime.Enabled = false
  57. cfg.MetricsBuilderConfig.Metrics.SaphanaReplicationBacklogSize.Enabled = false
  58. cfg.MetricsBuilderConfig.Metrics.SaphanaReplicationBacklogTime.Enabled = false
  59. cfg.MetricsBuilderConfig.Metrics.SaphanaRowStoreMemoryUsed.Enabled = false
  60. cfg.MetricsBuilderConfig.Metrics.SaphanaSchemaMemoryUsedCurrent.Enabled = false
  61. cfg.MetricsBuilderConfig.Metrics.SaphanaSchemaMemoryUsedMax.Enabled = false
  62. cfg.MetricsBuilderConfig.Metrics.SaphanaSchemaOperationCount.Enabled = false
  63. cfg.MetricsBuilderConfig.Metrics.SaphanaSchemaRecordCompressedCount.Enabled = false
  64. cfg.MetricsBuilderConfig.Metrics.SaphanaSchemaRecordCount.Enabled = false
  65. cfg.MetricsBuilderConfig.Metrics.SaphanaServiceCodeSize.Enabled = false
  66. cfg.MetricsBuilderConfig.Metrics.SaphanaServiceCount.Enabled = true // Service Count Enabled
  67. cfg.MetricsBuilderConfig.Metrics.SaphanaServiceMemoryCompactorsAllocated.Enabled = false
  68. cfg.MetricsBuilderConfig.Metrics.SaphanaServiceMemoryCompactorsFreeable.Enabled = false
  69. cfg.MetricsBuilderConfig.Metrics.SaphanaServiceMemoryEffectiveLimit.Enabled = false
  70. cfg.MetricsBuilderConfig.Metrics.SaphanaServiceMemoryHeapCurrent.Enabled = false
  71. cfg.MetricsBuilderConfig.Metrics.SaphanaServiceMemoryLimit.Enabled = false
  72. cfg.MetricsBuilderConfig.Metrics.SaphanaServiceMemorySharedCurrent.Enabled = false
  73. cfg.MetricsBuilderConfig.Metrics.SaphanaServiceMemoryUsed.Enabled = false
  74. cfg.MetricsBuilderConfig.Metrics.SaphanaServiceStackSize.Enabled = false
  75. cfg.MetricsBuilderConfig.Metrics.SaphanaServiceThreadCount.Enabled = true // Service Thread Count Enabled
  76. cfg.MetricsBuilderConfig.Metrics.SaphanaTransactionBlocked.Enabled = false
  77. cfg.MetricsBuilderConfig.Metrics.SaphanaTransactionCount.Enabled = false
  78. cfg.MetricsBuilderConfig.Metrics.SaphanaUptime.Enabled = false
  79. cfg.MetricsBuilderConfig.Metrics.SaphanaVolumeOperationCount.Enabled = false
  80. cfg.MetricsBuilderConfig.Metrics.SaphanaVolumeOperationSize.Enabled = false
  81. cfg.MetricsBuilderConfig.Metrics.SaphanaVolumeOperationTime.Enabled = false
  82. sc, err := newSapHanaScraper(receivertest.NewNopCreateSettings(), cfg, &testConnectionFactory{dbWrapper})
  83. require.NoError(t, err)
  84. expectedMetrics, err := golden.ReadMetrics(partialExpectedMetricsPath)
  85. require.NoError(t, err)
  86. actualMetrics, err := sc.Scrape(context.Background())
  87. require.NoError(t, err)
  88. require.NoError(t, pmetrictest.CompareMetrics(expectedMetrics, actualMetrics,
  89. pmetrictest.IgnoreResourceMetricsOrder(), pmetrictest.IgnoreStartTimestamp(), pmetrictest.IgnoreTimestamp()))
  90. }
  91. type queryJSON struct {
  92. Query string
  93. Result [][]string
  94. }
  95. func initializeWrapper(t *testing.T, w *testDBWrapper, filename string) {
  96. w.On("PingContext").Return(nil)
  97. w.On("Close").Return(nil)
  98. contents, err := os.ReadFile(filename)
  99. require.NoError(t, err)
  100. var queries []queryJSON
  101. err = json.Unmarshal(contents, &queries)
  102. require.NoError(t, err)
  103. for _, query := range queries {
  104. var result [][]*string
  105. for _, providedRow := range query.Result {
  106. var row []*string
  107. for _, val := range providedRow {
  108. if val == "nil" {
  109. row = append(row, nil)
  110. } else {
  111. row = append(row, str(val))
  112. }
  113. }
  114. result = append(result, row)
  115. }
  116. w.mockQueryResult(query.Query, result, nil)
  117. }
  118. }