config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package googlecloudspannerreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/googlecloudspannerreceiver"
  4. import (
  5. "errors"
  6. "fmt"
  7. "go.opentelemetry.io/collector/receiver/scraperhelper"
  8. )
  9. const (
  10. minCollectionIntervalSeconds = 60
  11. maxTopMetricsQueryMaxRows = 100
  12. )
  13. type Config struct {
  14. scraperhelper.ScraperControllerSettings `mapstructure:",squash"`
  15. TopMetricsQueryMaxRows int `mapstructure:"top_metrics_query_max_rows"`
  16. BackfillEnabled bool `mapstructure:"backfill_enabled"`
  17. CardinalityTotalLimit int `mapstructure:"cardinality_total_limit"`
  18. Projects []Project `mapstructure:"projects"`
  19. HideTopnLockstatsRowrangestartkey bool `mapstructure:"hide_topn_lockstats_rowrangestartkey"`
  20. TruncateText bool `mapstructure:"truncate_text"`
  21. }
  22. type Project struct {
  23. ID string `mapstructure:"project_id"`
  24. ServiceAccountKey string `mapstructure:"service_account_key"`
  25. Instances []Instance `mapstructure:"instances"`
  26. }
  27. type Instance struct {
  28. ID string `mapstructure:"instance_id"`
  29. Databases []string `mapstructure:"databases"`
  30. }
  31. func (config *Config) Validate() error {
  32. if config.CollectionInterval.Seconds() < minCollectionIntervalSeconds {
  33. return fmt.Errorf("\"collection_interval\" must be not lower than %v seconds, current value is %v seconds", minCollectionIntervalSeconds, config.CollectionInterval.Seconds())
  34. }
  35. if config.TopMetricsQueryMaxRows <= 0 {
  36. return fmt.Errorf("\"top_metrics_query_max_rows\" must be positive: %v", config.TopMetricsQueryMaxRows)
  37. }
  38. if config.TopMetricsQueryMaxRows > maxTopMetricsQueryMaxRows {
  39. return fmt.Errorf("\"top_metrics_query_max_rows\" must be not greater than %v, current value is %v", maxTopMetricsQueryMaxRows, config.TopMetricsQueryMaxRows)
  40. }
  41. if config.CardinalityTotalLimit < 0 {
  42. return fmt.Errorf("\"cardinality_total_limit\" must be not negative, current value is %v", config.CardinalityTotalLimit)
  43. }
  44. if len(config.Projects) == 0 {
  45. return errors.New("missing required field \"projects\" or its value is empty")
  46. }
  47. for _, project := range config.Projects {
  48. if err := project.Validate(); err != nil {
  49. return err
  50. }
  51. }
  52. return nil
  53. }
  54. func (project Project) Validate() error {
  55. if project.ID == "" {
  56. return errors.New(`field "project_id" is required and cannot be empty for project configuration`)
  57. }
  58. if len(project.Instances) == 0 {
  59. return errors.New("field \"instances\" is required and cannot be empty for project configuration")
  60. }
  61. for _, instance := range project.Instances {
  62. if err := instance.Validate(); err != nil {
  63. return err
  64. }
  65. }
  66. return nil
  67. }
  68. func (instance Instance) Validate() error {
  69. if instance.ID == "" {
  70. return errors.New("field \"instance_id\" is required and cannot be empty for instance configuration")
  71. }
  72. if len(instance.Databases) == 0 {
  73. return errors.New("field \"databases\" is required and cannot be empty for instance configuration")
  74. }
  75. for _, database := range instance.Databases {
  76. if database == "" {
  77. return errors.New("field \"databases\" contains empty database names")
  78. }
  79. }
  80. return nil
  81. }