configs.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package configschema // import "github.com/open-telemetry/opentelemetry-collector-contrib/cmd/configschema"
  4. import (
  5. "fmt"
  6. "go.opentelemetry.io/collector/component"
  7. "go.opentelemetry.io/collector/otelcol"
  8. )
  9. const (
  10. receiver = "receiver"
  11. extension = "extension"
  12. processor = "processor"
  13. exporter = "exporter"
  14. connector = "connector"
  15. )
  16. // CfgInfo contains a component config instance, as well as its group name and
  17. // type.
  18. type CfgInfo struct {
  19. // the name of the component group, e.g. "receiver"
  20. Group string
  21. // the component type, e.g. "otlpreceiver.Config"
  22. Type component.Type
  23. // an instance of the component's configuration struct
  24. CfgInstance any
  25. }
  26. // GetAllCfgInfos accepts a Factories struct, then creates and returns a CfgInfo
  27. // for each of its components.
  28. func GetAllCfgInfos(components otelcol.Factories) []CfgInfo {
  29. out := make([]CfgInfo, len(components.Receivers)+len(components.Extensions)+len(components.Processors)+len(components.Exporters)+len(components.Connectors))
  30. i := 0
  31. for _, f := range components.Receivers {
  32. out[i] = CfgInfo{
  33. Type: f.Type(),
  34. Group: receiver,
  35. CfgInstance: f.CreateDefaultConfig(),
  36. }
  37. i++
  38. }
  39. for _, f := range components.Extensions {
  40. out[i] = CfgInfo{
  41. Type: f.Type(),
  42. Group: extension,
  43. CfgInstance: f.CreateDefaultConfig(),
  44. }
  45. i++
  46. }
  47. for _, f := range components.Processors {
  48. out[i] = CfgInfo{
  49. Type: f.Type(),
  50. Group: processor,
  51. CfgInstance: f.CreateDefaultConfig(),
  52. }
  53. i++
  54. }
  55. for _, f := range components.Exporters {
  56. out[i] = CfgInfo{
  57. Type: f.Type(),
  58. Group: exporter,
  59. CfgInstance: f.CreateDefaultConfig(),
  60. }
  61. i++
  62. }
  63. for _, f := range components.Connectors {
  64. out[i] = CfgInfo{
  65. Type: f.Type(),
  66. Group: connector,
  67. CfgInstance: f.CreateDefaultConfig(),
  68. }
  69. i++
  70. }
  71. return out
  72. }
  73. // GetCfgInfo accepts a Factories struct, then creates and returns the default
  74. // config for the component specified by the passed-in componentType and
  75. // componentName.
  76. func GetCfgInfo(components otelcol.Factories, componentType, componentName string) (CfgInfo, error) {
  77. t := component.Type(componentName)
  78. switch componentType {
  79. case receiver:
  80. f := components.Receivers[t]
  81. if f == nil {
  82. return CfgInfo{}, fmt.Errorf("unknown %s name %q", componentType, componentName)
  83. }
  84. return CfgInfo{
  85. Type: f.Type(),
  86. Group: componentType,
  87. CfgInstance: f.CreateDefaultConfig(),
  88. }, nil
  89. case processor:
  90. f := components.Processors[t]
  91. if f == nil {
  92. return CfgInfo{}, fmt.Errorf("unknown %s name %q", componentType, componentName)
  93. }
  94. return CfgInfo{
  95. Type: f.Type(),
  96. Group: componentType,
  97. CfgInstance: f.CreateDefaultConfig(),
  98. }, nil
  99. case exporter:
  100. f := components.Exporters[t]
  101. if f == nil {
  102. return CfgInfo{}, fmt.Errorf("unknown %s name %q", componentType, componentName)
  103. }
  104. return CfgInfo{
  105. Type: f.Type(),
  106. Group: componentType,
  107. CfgInstance: f.CreateDefaultConfig(),
  108. }, nil
  109. case connector:
  110. f := components.Connectors[t]
  111. if f == nil {
  112. return CfgInfo{}, fmt.Errorf("unknown %s name %q", componentType, componentName)
  113. }
  114. return CfgInfo{
  115. Type: f.Type(),
  116. Group: componentType,
  117. CfgInstance: f.CreateDefaultConfig(),
  118. }, nil
  119. case extension:
  120. f := components.Extensions[t]
  121. if f == nil {
  122. return CfgInfo{}, fmt.Errorf("unknown %s name %q", componentType, componentName)
  123. }
  124. return CfgInfo{
  125. Type: f.Type(),
  126. Group: componentType,
  127. CfgInstance: f.CreateDefaultConfig(),
  128. }, nil
  129. }
  130. return CfgInfo{}, fmt.Errorf("unknown component type %q", componentType)
  131. }