config.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package googlecloudpubsubreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/googlecloudpubsubreceiver"
  4. import (
  5. "fmt"
  6. "regexp"
  7. "go.opentelemetry.io/collector/exporter/exporterhelper"
  8. )
  9. var subscriptionMatcher = regexp.MustCompile(`projects/[a-z][a-z0-9\-]*/subscriptions/`)
  10. type Config struct {
  11. // Google Cloud Project ID where the Pubsub client will connect to
  12. ProjectID string `mapstructure:"project"`
  13. // User agent that will be used by the Pubsub client to connect to the service
  14. UserAgent string `mapstructure:"user_agent"`
  15. // Override of the Pubsub Endpoint, leave empty for the default endpoint
  16. Endpoint string `mapstructure:"endpoint"`
  17. // Only has effect if Endpoint is not ""
  18. Insecure bool `mapstructure:"insecure"`
  19. // Timeout for all API calls. If not set, defaults to 12 seconds.
  20. exporterhelper.TimeoutSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct.
  21. // The fully qualified resource name of the Pubsub subscription
  22. Subscription string `mapstructure:"subscription"`
  23. // Lock down the encoding of the payload, leave empty for attribute based detection
  24. Encoding string `mapstructure:"encoding"`
  25. // Lock down the compression of the payload, leave empty for attribute based detection
  26. Compression string `mapstructure:"compression"`
  27. // The client id that will be used by Pubsub to make load balancing decisions
  28. ClientID string `mapstructure:"client_id"`
  29. }
  30. func (config *Config) validateForLog() error {
  31. err := config.validate()
  32. if err != nil {
  33. return err
  34. }
  35. switch config.Encoding {
  36. case "":
  37. case "otlp_proto_log":
  38. case "raw_text":
  39. case "raw_json":
  40. default:
  41. return fmt.Errorf("log encoding %v is not supported. supported encoding formats include [otlp_proto_log,raw_text,raw_json]", config.Encoding)
  42. }
  43. return nil
  44. }
  45. func (config *Config) validateForTrace() error {
  46. err := config.validate()
  47. if err != nil {
  48. return err
  49. }
  50. switch config.Encoding {
  51. case "":
  52. case "otlp_proto_trace":
  53. default:
  54. return fmt.Errorf("trace encoding %v is not supported. supported encoding formats include [otlp_proto_trace]", config.Encoding)
  55. }
  56. return nil
  57. }
  58. func (config *Config) validateForMetric() error {
  59. err := config.validate()
  60. if err != nil {
  61. return err
  62. }
  63. switch config.Encoding {
  64. case "":
  65. case "otlp_proto_metric":
  66. default:
  67. return fmt.Errorf("metric encoding %v is not supported. supported encoding formats include [otlp_proto_metric]", config.Encoding)
  68. }
  69. return nil
  70. }
  71. func (config *Config) validate() error {
  72. if !subscriptionMatcher.MatchString(config.Subscription) {
  73. return fmt.Errorf("subscription '%s' is not a valid format, use 'projects/<project_id>/subscriptions/<name>'", config.Subscription)
  74. }
  75. switch config.Compression {
  76. case "":
  77. case "gzip":
  78. default:
  79. return fmt.Errorf("compression %v is not supported. supported compression formats include [gzip]", config.Compression)
  80. }
  81. return nil
  82. }