Skip to content

Commit 2118622

Browse files
authored
[chore] move service pipeline id validation to main config Validate (#6281)
Signed-off-by: Bogdan <[email protected]> Signed-off-by: Bogdan <[email protected]>
1 parent b33b12a commit 2118622

File tree

5 files changed

+51
-76
lines changed

5 files changed

+51
-76
lines changed

config/moved_config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ func (cfg *Config) validateService() error {
113113
// Check that all pipelines have at least one receiver and one exporter, and they reference
114114
// only configured components.
115115
for pipelineID, pipeline := range cfg.Service.Pipelines {
116+
if pipelineID.Type() != TracesDataType && pipelineID.Type() != MetricsDataType && pipelineID.Type() != LogsDataType {
117+
return fmt.Errorf("unknown pipeline datatype %q for %v", pipelineID.Type(), pipelineID)
118+
}
119+
116120
// Validate pipeline has at least one receiver.
117121
if len(pipeline.Receivers) == 0 {
118122
return fmt.Errorf("pipeline %q must have at least one receiver", pipelineID)

service/config_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,19 @@ func TestConfigValidate(t *testing.T) {
228228
},
229229
expected: fmt.Errorf(`extension "nop" has invalid configuration: %w`, errInvalidExtConfig),
230230
},
231+
{
232+
name: "invalid-service-pipeline-type",
233+
cfgFn: func() *Config {
234+
cfg := generateConfig()
235+
cfg.Service.Pipelines[config.NewComponentID("wrongtype")] = &ConfigServicePipeline{
236+
Receivers: []config.ComponentID{config.NewComponentID("nop")},
237+
Processors: []config.ComponentID{config.NewComponentID("nop")},
238+
Exporters: []config.ComponentID{config.NewComponentID("nop")},
239+
}
240+
return cfg
241+
},
242+
expected: errors.New(`unknown pipeline datatype "wrongtype" for wrongtype`),
243+
},
231244
}
232245

233246
for _, test := range testCases {

service/internal/configunmarshaler/defaultunmarshaler.go

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ const (
4040
errUnmarshalReceiver
4141
errUnmarshalProcessor
4242
errUnmarshalExporter
43-
errUnmarshalService
4443
)
4544

4645
type configError struct {
@@ -64,17 +63,14 @@ const (
6463

6564
// processorsKeyName is the configuration key name for processors section.
6665
processorsKeyName = "processors"
67-
68-
// pipelinesKeyName is the configuration key name for pipelines section.
69-
pipelinesKeyName = "pipelines"
7066
)
7167

7268
type configSettings struct {
7369
Receivers map[config.ComponentID]map[string]interface{} `mapstructure:"receivers"`
7470
Processors map[config.ComponentID]map[string]interface{} `mapstructure:"processors"`
7571
Exporters map[config.ComponentID]map[string]interface{} `mapstructure:"exporters"`
7672
Extensions map[config.ComponentID]map[string]interface{} `mapstructure:"extensions"`
77-
Service map[string]interface{} `mapstructure:"service"`
73+
Service config.Service `mapstructure:"service"`
7874
}
7975

8076
type ConfigUnmarshaler struct{}
@@ -88,17 +84,36 @@ func New() ConfigUnmarshaler {
8884
// Unmarshal the config.Config from a confmap.Conf.
8985
// After the config is unmarshalled, `Validate()` must be called to validate.
9086
func (ConfigUnmarshaler) Unmarshal(v *confmap.Conf, factories component.Factories) (*config.Config, error) {
91-
var cfg config.Config
92-
9387
// Unmarshal top level sections and validate.
94-
rawCfg := configSettings{}
88+
rawCfg := configSettings{
89+
// TODO: Add a component.ServiceFactory to allow this to be defined by the Service.
90+
Service: config.Service{
91+
Telemetry: telemetry.Config{
92+
Logs: telemetry.LogsConfig{
93+
Level: zapcore.InfoLevel,
94+
Development: false,
95+
Encoding: "console",
96+
OutputPaths: []string{"stderr"},
97+
ErrorOutputPaths: []string{"stderr"},
98+
DisableCaller: false,
99+
DisableStacktrace: false,
100+
InitialFields: map[string]interface{}(nil),
101+
},
102+
Metrics: telemetry.MetricsConfig{
103+
Level: configtelemetry.LevelBasic,
104+
Address: ":8888",
105+
},
106+
},
107+
},
108+
}
95109
if err := v.Unmarshal(&rawCfg, confmap.WithErrorUnused()); err != nil {
96110
return nil, configError{
97111
error: fmt.Errorf("error reading top level configuration sections: %w", err),
98112
code: errUnmarshalTopLevelStructure,
99113
}
100114
}
101115

116+
var cfg config.Config
102117
var err error
103118
if cfg.Extensions, err = unmarshalExtensions(rawCfg.Extensions, factories.Extensions); err != nil {
104119
return nil, configError{
@@ -128,12 +143,7 @@ func (ConfigUnmarshaler) Unmarshal(v *confmap.Conf, factories component.Factorie
128143
}
129144
}
130145

131-
if cfg.Service, err = unmarshalService(rawCfg.Service); err != nil {
132-
return nil, configError{
133-
error: err,
134-
code: errUnmarshalService,
135-
}
136-
}
146+
cfg.Service = rawCfg.Service
137147

138148
return &cfg, nil
139149
}
@@ -166,44 +176,6 @@ func unmarshalExtensions(exts map[config.ComponentID]map[string]interface{}, fac
166176
return extensions, nil
167177
}
168178

169-
func unmarshalService(srvRaw map[string]interface{}) (config.Service, error) {
170-
// Setup default telemetry values as in service/logger.go.
171-
// TODO: Add a component.ServiceFactory to allow this to be defined by the Service.
172-
srv := config.Service{
173-
Telemetry: telemetry.Config{
174-
Logs: telemetry.LogsConfig{
175-
Level: zapcore.InfoLevel,
176-
Development: false,
177-
Encoding: "console",
178-
OutputPaths: []string{"stderr"},
179-
ErrorOutputPaths: []string{"stderr"},
180-
DisableCaller: false,
181-
DisableStacktrace: false,
182-
InitialFields: map[string]interface{}(nil),
183-
},
184-
Metrics: defaultServiceTelemetryMetricsSettings(),
185-
},
186-
}
187-
188-
if err := confmap.NewFromStringMap(srvRaw).Unmarshal(&srv, confmap.WithErrorUnused()); err != nil {
189-
return srv, fmt.Errorf("error reading service configuration: %w", err)
190-
}
191-
192-
for id := range srv.Pipelines {
193-
if id.Type() != config.TracesDataType && id.Type() != config.MetricsDataType && id.Type() != config.LogsDataType {
194-
return srv, fmt.Errorf("unknown %q datatype %q for %v", pipelinesKeyName, id.Type(), id)
195-
}
196-
}
197-
return srv, nil
198-
}
199-
200-
func defaultServiceTelemetryMetricsSettings() telemetry.MetricsConfig {
201-
return telemetry.MetricsConfig{
202-
Level: configtelemetry.LevelBasic,
203-
Address: ":8888",
204-
}
205-
}
206-
207179
// LoadReceiver loads a receiver config from componentConfig using the provided factories.
208180
func LoadReceiver(componentConfig *confmap.Conf, id config.ComponentID, factory component.ReceiverFactory) (config.Receiver, error) {
209181
// Create the default config for this receiver.

service/internal/configunmarshaler/defaultunmarshaler_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,44 +107,43 @@ func TestDecodeConfig_Invalid(t *testing.T) {
107107
{name: "invalid-receiver-type", expected: errUnmarshalTopLevelStructure},
108108
{name: "invalid-processor-type", expected: errUnmarshalTopLevelStructure},
109109
{name: "invalid-exporter-type", expected: errUnmarshalTopLevelStructure},
110-
{name: "invalid-pipeline-type", expected: errUnmarshalService},
110+
{name: "invalid-pipeline-type", expected: errUnmarshalTopLevelStructure},
111111

112112
{name: "invalid-extension-name-after-slash", expected: errUnmarshalTopLevelStructure},
113113
{name: "invalid-receiver-name-after-slash", expected: errUnmarshalTopLevelStructure},
114114
{name: "invalid-processor-name-after-slash", expected: errUnmarshalTopLevelStructure},
115115
{name: "invalid-exporter-name-after-slash", expected: errUnmarshalTopLevelStructure},
116-
{name: "invalid-pipeline-name-after-slash", expected: errUnmarshalService},
116+
{name: "invalid-pipeline-name-after-slash", expected: errUnmarshalTopLevelStructure},
117117

118118
{name: "unknown-extension-type", expected: errUnmarshalExtension, expectedMessage: "extensions"},
119119
{name: "unknown-receiver-type", expected: errUnmarshalReceiver, expectedMessage: "receivers"},
120120
{name: "unknown-processor-type", expected: errUnmarshalProcessor, expectedMessage: "processors"},
121121
{name: "unknown-exporter-type", expected: errUnmarshalExporter, expectedMessage: "exporters"},
122-
{name: "unknown-pipeline-type", expected: errUnmarshalService, expectedMessage: "pipelines"},
123122

124123
{name: "duplicate-extension", expected: errUnmarshalTopLevelStructure, expectedMessage: "duplicate name"},
125124
{name: "duplicate-receiver", expected: errUnmarshalTopLevelStructure, expectedMessage: "duplicate name"},
126125
{name: "duplicate-processor", expected: errUnmarshalTopLevelStructure, expectedMessage: "duplicate name"},
127126
{name: "duplicate-exporter", expected: errUnmarshalTopLevelStructure, expectedMessage: "duplicate name"},
128-
{name: "duplicate-pipeline", expected: errUnmarshalService, expectedMessage: "duplicate name"},
127+
{name: "duplicate-pipeline", expected: errUnmarshalTopLevelStructure, expectedMessage: "duplicate name"},
129128

130129
{name: "invalid-top-level-section", expected: errUnmarshalTopLevelStructure, expectedMessage: "top level"},
131130
{name: "invalid-extension-section", expected: errUnmarshalExtension, expectedMessage: "extensions"},
132131
{name: "invalid-receiver-section", expected: errUnmarshalReceiver, expectedMessage: "receivers"},
133132
{name: "invalid-processor-section", expected: errUnmarshalProcessor, expectedMessage: "processors"},
134133
{name: "invalid-exporter-section", expected: errUnmarshalExporter, expectedMessage: "exporters"},
135-
{name: "invalid-service-section", expected: errUnmarshalService},
136-
{name: "invalid-service-extensions-section", expected: errUnmarshalService},
137-
{name: "invalid-pipeline-section", expected: errUnmarshalService, expectedMessage: "pipelines"},
138-
{name: "invalid-sequence-value", expected: errUnmarshalService, expectedMessage: "pipelines"},
134+
{name: "invalid-service-section", expected: errUnmarshalTopLevelStructure},
135+
{name: "invalid-service-extensions-section", expected: errUnmarshalTopLevelStructure},
136+
{name: "invalid-pipeline-section", expected: errUnmarshalTopLevelStructure, expectedMessage: "pipelines"},
137+
{name: "invalid-sequence-value", expected: errUnmarshalTopLevelStructure, expectedMessage: "pipelines"},
139138

140139
{name: "invalid-extension-sub-config", expected: errUnmarshalTopLevelStructure},
141140
{name: "invalid-receiver-sub-config", expected: errUnmarshalTopLevelStructure},
142141
{name: "invalid-processor-sub-config", expected: errUnmarshalTopLevelStructure},
143142
{name: "invalid-exporter-sub-config", expected: errUnmarshalTopLevelStructure},
144-
{name: "invalid-pipeline-sub-config", expected: errUnmarshalService},
143+
{name: "invalid-pipeline-sub-config", expected: errUnmarshalTopLevelStructure},
145144

146-
{name: "invalid-logs-level", expected: errUnmarshalService},
147-
{name: "invalid-metrics-level", expected: errUnmarshalService},
145+
{name: "invalid-logs-level", expected: errUnmarshalTopLevelStructure},
146+
{name: "invalid-metrics-level", expected: errUnmarshalTopLevelStructure},
148147
}
149148

150149
factories, err := componenttest.NopFactories()

service/internal/configunmarshaler/testdata/unknown-pipeline-type.yaml

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)