config.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package mongodbreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbreceiver"
  4. import (
  5. "errors"
  6. "fmt"
  7. "strings"
  8. "time"
  9. "go.mongodb.org/mongo-driver/mongo/options"
  10. "go.opentelemetry.io/collector/config/confignet"
  11. "go.opentelemetry.io/collector/config/configopaque"
  12. "go.opentelemetry.io/collector/config/configtls"
  13. "go.opentelemetry.io/collector/receiver/scraperhelper"
  14. "go.uber.org/multierr"
  15. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbreceiver/internal/metadata"
  16. )
  17. type Config struct {
  18. scraperhelper.ScraperControllerSettings `mapstructure:",squash"`
  19. configtls.TLSClientSetting `mapstructure:"tls,omitempty"`
  20. // MetricsBuilderConfig defines which metrics/attributes to enable for the scraper
  21. metadata.MetricsBuilderConfig `mapstructure:",squash"`
  22. Hosts []confignet.NetAddr `mapstructure:"hosts"`
  23. Username string `mapstructure:"username"`
  24. Password configopaque.String `mapstructure:"password"`
  25. ReplicaSet string `mapstructure:"replica_set,omitempty"`
  26. Timeout time.Duration `mapstructure:"timeout"`
  27. }
  28. func (c *Config) Validate() error {
  29. if len(c.Hosts) == 0 {
  30. return errors.New("no hosts were specified in the config")
  31. }
  32. var err error
  33. for _, host := range c.Hosts {
  34. if host.Endpoint == "" {
  35. err = multierr.Append(err, errors.New("no endpoint specified for one of the hosts"))
  36. }
  37. }
  38. if c.Username != "" && c.Password == "" {
  39. err = multierr.Append(err, errors.New("username provided without password"))
  40. } else if c.Username == "" && c.Password != "" {
  41. err = multierr.Append(err, errors.New("password provided without user"))
  42. }
  43. if _, tlsErr := c.LoadTLSConfig(); tlsErr != nil {
  44. err = multierr.Append(err, fmt.Errorf("error loading tls configuration: %w", tlsErr))
  45. }
  46. return err
  47. }
  48. func (c *Config) ClientOptions() *options.ClientOptions {
  49. clientOptions := options.Client()
  50. connString := fmt.Sprintf("mongodb://%s", strings.Join(c.hostlist(), ","))
  51. clientOptions.ApplyURI(connString)
  52. if c.Timeout > 0 {
  53. clientOptions.SetConnectTimeout(c.Timeout)
  54. }
  55. tlsConfig, err := c.LoadTLSConfig()
  56. if err == nil && tlsConfig != nil {
  57. clientOptions.SetTLSConfig(tlsConfig)
  58. }
  59. if c.ReplicaSet != "" {
  60. clientOptions.SetReplicaSet(c.ReplicaSet)
  61. }
  62. if c.Username != "" && c.Password != "" {
  63. clientOptions.SetAuth(options.Credential{
  64. Username: c.Username,
  65. Password: string(c.Password),
  66. })
  67. }
  68. return clientOptions
  69. }
  70. func (c *Config) hostlist() []string {
  71. var hosts []string
  72. for _, ep := range c.Hosts {
  73. hosts = append(hosts, ep.Endpoint)
  74. }
  75. return hosts
  76. }