factory.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package snmpreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/snmpreceiver"
  4. import (
  5. "context"
  6. "errors"
  7. "fmt"
  8. "net/url"
  9. "strings"
  10. "go.opentelemetry.io/collector/component"
  11. "go.opentelemetry.io/collector/consumer"
  12. "go.opentelemetry.io/collector/receiver"
  13. "go.opentelemetry.io/collector/receiver/scraperhelper"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/snmpreceiver/internal/metadata"
  15. )
  16. var errConfigNotSNMP = errors.New("config was not a SNMP receiver config")
  17. // NewFactory creates a new receiver factory for SNMP
  18. func NewFactory() receiver.Factory {
  19. return receiver.NewFactory(
  20. metadata.Type,
  21. createDefaultConfig,
  22. receiver.WithMetrics(createMetricsReceiver, metadata.MetricsStability))
  23. }
  24. // createDefaultConfig creates a config for SNMP with as many default values as possible
  25. func createDefaultConfig() component.Config {
  26. return &Config{
  27. ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
  28. CollectionInterval: defaultCollectionInterval,
  29. Timeout: defaultTimeout,
  30. },
  31. Endpoint: defaultEndpoint,
  32. Version: defaultVersion,
  33. Community: defaultCommunity,
  34. SecurityLevel: defaultSecurityLevel,
  35. AuthType: defaultAuthType,
  36. PrivacyType: defaultPrivacyType,
  37. }
  38. }
  39. // createMetricsReceiver creates the metric receiver for SNMP
  40. func createMetricsReceiver(
  41. _ context.Context,
  42. params receiver.CreateSettings,
  43. config component.Config,
  44. consumer consumer.Metrics,
  45. ) (receiver.Metrics, error) {
  46. snmpConfig, ok := config.(*Config)
  47. if !ok {
  48. return nil, errConfigNotSNMP
  49. }
  50. if err := addMissingConfigDefaults(snmpConfig); err != nil {
  51. return nil, fmt.Errorf("failed to validate added config defaults: %w", err)
  52. }
  53. snmpScraper := newScraper(params.Logger, snmpConfig, params)
  54. scraper, err := scraperhelper.NewScraper(metadata.Type, snmpScraper.scrape, scraperhelper.WithStart(snmpScraper.start))
  55. if err != nil {
  56. return nil, err
  57. }
  58. return scraperhelper.NewScraperControllerReceiver(&snmpConfig.ScraperControllerSettings, params, consumer, scraperhelper.AddScraper(scraper))
  59. }
  60. // addMissingConfigDefaults adds any missing config parameters that have defaults
  61. func addMissingConfigDefaults(cfg *Config) error {
  62. // Add the schema prefix to the endpoint if it doesn't contain one
  63. if !strings.Contains(cfg.Endpoint, "://") {
  64. cfg.Endpoint = "udp://" + cfg.Endpoint
  65. }
  66. // Add default port to endpoint if it doesn't contain one
  67. u, err := url.Parse(cfg.Endpoint)
  68. if err == nil && u.Port() == "" {
  69. portSuffix := "161"
  70. if cfg.Endpoint[len(cfg.Endpoint)-1:] != ":" {
  71. portSuffix = ":" + portSuffix
  72. }
  73. cfg.Endpoint += portSuffix
  74. }
  75. // Set defaults for metric configs
  76. for _, metricCfg := range cfg.Metrics {
  77. if metricCfg.Unit == "" {
  78. metricCfg.Unit = "1"
  79. }
  80. if metricCfg.Gauge != nil && metricCfg.Gauge.ValueType == "" {
  81. metricCfg.Gauge.ValueType = "double"
  82. }
  83. if metricCfg.Sum != nil {
  84. if metricCfg.Sum.ValueType == "" {
  85. metricCfg.Sum.ValueType = "double"
  86. }
  87. if metricCfg.Sum.Aggregation == "" {
  88. metricCfg.Sum.Aggregation = "cumulative"
  89. }
  90. }
  91. }
  92. return component.ValidateConfig(cfg)
  93. }