config_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package hostmetricsreceiver
  4. import (
  5. "path/filepath"
  6. "testing"
  7. "time"
  8. "github.com/shirou/gopsutil/v3/common"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. "go.opentelemetry.io/collector/component"
  12. "go.opentelemetry.io/collector/otelcol/otelcoltest"
  13. "go.opentelemetry.io/collector/receiver/scraperhelper"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset"
  15. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal"
  16. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/metadata"
  17. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/cpuscraper"
  18. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/diskscraper"
  19. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper"
  20. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/loadscraper"
  21. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/memoryscraper"
  22. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/networkscraper"
  23. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/pagingscraper"
  24. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processesscraper"
  25. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper"
  26. )
  27. func TestLoadConfig(t *testing.T) {
  28. factories, err := otelcoltest.NopFactories()
  29. require.NoError(t, err)
  30. factory := NewFactory()
  31. factories.Receivers[metadata.Type] = factory
  32. cfg, err := otelcoltest.LoadConfigAndValidate(filepath.Join("testdata", "config.yaml"), factories)
  33. require.NoError(t, err)
  34. require.NotNil(t, cfg)
  35. assert.Equal(t, len(cfg.Receivers), 2)
  36. r0 := cfg.Receivers[component.NewID(metadata.Type)]
  37. defaultConfigCPUScraper := factory.CreateDefaultConfig()
  38. defaultConfigCPUScraper.(*Config).Scrapers = map[string]internal.Config{
  39. cpuscraper.TypeStr: func() internal.Config {
  40. cfg := (&cpuscraper.Factory{}).CreateDefaultConfig()
  41. cfg.SetEnvMap(common.EnvMap{})
  42. return cfg
  43. }(),
  44. }
  45. assert.Equal(t, defaultConfigCPUScraper, r0)
  46. r1 := cfg.Receivers[component.NewIDWithName(metadata.Type, "customname")].(*Config)
  47. expectedConfig := &Config{
  48. ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
  49. CollectionInterval: 30 * time.Second,
  50. InitialDelay: time.Second,
  51. },
  52. Scrapers: map[string]internal.Config{
  53. cpuscraper.TypeStr: func() internal.Config {
  54. cfg := (&cpuscraper.Factory{}).CreateDefaultConfig()
  55. cfg.SetEnvMap(common.EnvMap{})
  56. return cfg
  57. }(),
  58. diskscraper.TypeStr: func() internal.Config {
  59. cfg := (&diskscraper.Factory{}).CreateDefaultConfig()
  60. cfg.SetEnvMap(common.EnvMap{})
  61. return cfg
  62. }(),
  63. loadscraper.TypeStr: (func() internal.Config {
  64. cfg := (&loadscraper.Factory{}).CreateDefaultConfig()
  65. cfg.(*loadscraper.Config).CPUAverage = true
  66. cfg.SetEnvMap(common.EnvMap{})
  67. return cfg
  68. })(),
  69. filesystemscraper.TypeStr: func() internal.Config {
  70. cfg := (&filesystemscraper.Factory{}).CreateDefaultConfig()
  71. cfg.SetEnvMap(common.EnvMap{})
  72. return cfg
  73. }(),
  74. memoryscraper.TypeStr: func() internal.Config {
  75. cfg := (&memoryscraper.Factory{}).CreateDefaultConfig()
  76. cfg.SetEnvMap(common.EnvMap{})
  77. return cfg
  78. }(),
  79. networkscraper.TypeStr: (func() internal.Config {
  80. cfg := (&networkscraper.Factory{}).CreateDefaultConfig()
  81. cfg.(*networkscraper.Config).Include = networkscraper.MatchConfig{
  82. Interfaces: []string{"test1"},
  83. Config: filterset.Config{MatchType: "strict"},
  84. }
  85. cfg.SetEnvMap(common.EnvMap{})
  86. return cfg
  87. })(),
  88. processesscraper.TypeStr: func() internal.Config {
  89. cfg := (&processesscraper.Factory{}).CreateDefaultConfig()
  90. cfg.SetEnvMap(common.EnvMap{})
  91. return cfg
  92. }(),
  93. pagingscraper.TypeStr: func() internal.Config {
  94. cfg := (&pagingscraper.Factory{}).CreateDefaultConfig()
  95. cfg.SetEnvMap(common.EnvMap{})
  96. return cfg
  97. }(),
  98. processscraper.TypeStr: (func() internal.Config {
  99. cfg := (&processscraper.Factory{}).CreateDefaultConfig()
  100. cfg.(*processscraper.Config).Include = processscraper.MatchConfig{
  101. Names: []string{"test2", "test3"},
  102. Config: filterset.Config{MatchType: "regexp"},
  103. }
  104. cfg.SetEnvMap(common.EnvMap{})
  105. return cfg
  106. })(),
  107. },
  108. }
  109. assert.Equal(t, expectedConfig, r1)
  110. }
  111. func TestLoadInvalidConfig_NoScrapers(t *testing.T) {
  112. factories, err := otelcoltest.NopFactories()
  113. require.NoError(t, err)
  114. factory := NewFactory()
  115. factories.Receivers[metadata.Type] = factory
  116. _, err = otelcoltest.LoadConfigAndValidate(filepath.Join("testdata", "config-noscrapers.yaml"), factories)
  117. require.Contains(t, err.Error(), "must specify at least one scraper when using hostmetrics receiver")
  118. }
  119. func TestLoadInvalidConfig_InvalidScraperKey(t *testing.T) {
  120. factories, err := otelcoltest.NopFactories()
  121. require.NoError(t, err)
  122. factory := NewFactory()
  123. factories.Receivers[metadata.Type] = factory
  124. _, err = otelcoltest.LoadConfigAndValidate(filepath.Join("testdata", "config-invalidscraperkey.yaml"), factories)
  125. require.Contains(t, err.Error(), "error reading configuration for \"hostmetrics\": invalid scraper key: invalidscraperkey")
  126. }