Skip to content

Commit d10e653

Browse files
committed
Validate exporter config based on go tags
Signed-off-by: Arthur Silva Sens <[email protected]>
1 parent 7033f27 commit d10e653

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/alecthomas/kingpin/v2 v2.4.0
77
github.com/coreos/go-systemd/v22 v22.6.0
88
github.com/mdlayher/vsock v1.2.1
9+
github.com/mitchellh/mapstructure v1.5.0
910
github.com/prometheus/common v0.66.1
1011
go.opentelemetry.io/collector/component v1.44.0
1112
go.opentelemetry.io/collector/consumer v1.44.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ github.com/mdlayher/socket v0.4.1 h1:eM9y2/jlbs1M615oshPQOHZzj6R6wMT7bX5NPiQvn2U
4343
github.com/mdlayher/socket v0.4.1/go.mod h1:cAqeGjoufqdxWkD7DkpyS+wcefOtmu5OQ8KuoJGIReA=
4444
github.com/mdlayher/vsock v1.2.1 h1:pC1mTJTvjo1r9n9fbm7S1j04rCgCzhCOS5DY0zqHlnQ=
4545
github.com/mdlayher/vsock v1.2.1/go.mod h1:NRfCibel++DgeMD8z/hP+PPTjlNJsdPOmxcnENvE+SE=
46+
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
47+
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
4648
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
4749
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
4850
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

otlpreceiver/factory.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"context"
1818
"fmt"
1919

20+
"github.com/mitchellh/mapstructure"
2021
"github.com/prometheus/client_golang/prometheus"
2122
"go.opentelemetry.io/collector/component"
2223
"go.opentelemetry.io/collector/consumer"
@@ -34,11 +35,12 @@ type ExporterInitializer interface {
3435
Shutdown(ctx context.Context) error
3536
}
3637

37-
// ConfigUnmarshaler is the interface for unmarshaling exporter-specific configuration.
38+
// ConfigUnmarshaler is the interface for struct-based configuration
39+
// with automatic validation using mapstructure and struct tags.
3840
type ConfigUnmarshaler interface {
39-
// UnmarshalExporterConfig parses the exporter-specific configuration
40-
// from the raw map into a Config instance.
41-
UnmarshalExporterConfig(data map[string]interface{}) (Config, error)
41+
// GetConfigStruct returns a pointer to the config struct that mapstructure
42+
// will populate. The struct should have appropriate mapstructure tags.
43+
GetConfigStruct() Config
4244
}
4345

4446
// FactoryOption is a function that configures a Factory.
@@ -130,12 +132,27 @@ func createMetricsReceiver(
130132
return nil, fmt.Errorf("invalid config type: %T", cfg)
131133
}
132134

133-
// Unmarshal the exporter-specific config
135+
// Unmarshal the exporter-specific config using mapstructure
134136
if len(receiverCfg.ExporterConfig) > 0 {
135-
exporterCfg, err := unmarshaler.UnmarshalExporterConfig(receiverCfg.ExporterConfig)
137+
// Get the config struct from the unmarshaler
138+
exporterCfg := unmarshaler.GetConfigStruct()
139+
140+
// Configure mapstructure for strict validation
141+
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
142+
Result: exporterCfg,
143+
ErrorUnused: true, // Reject unknown fields
144+
WeaklyTypedInput: false, // Strict type checking
145+
TagName: "mapstructure",
146+
})
136147
if err != nil {
137-
return nil, fmt.Errorf("failed to unmarshal exporter config: %w", err)
148+
return nil, fmt.Errorf("failed to create decoder: %w", err)
138149
}
150+
151+
// Decode with automatic validation
152+
if err = decoder.Decode(receiverCfg.ExporterConfig); err != nil {
153+
return nil, fmt.Errorf("configuration validation failed: %w", err)
154+
}
155+
139156
receiverCfg.SetExporterConfig(exporterCfg)
140157
}
141158

0 commit comments

Comments
 (0)