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