@@ -21,36 +21,58 @@ type Configuration struct {
2121 since string // set only by DSN
2222}
2323
24- func ( s * Source ) UnmarshalConfig ( yamlConfig []byte ) error {
25- config := Configuration {}
24+ func ConfigurationFromYAML ( y []byte ) ( Configuration , error ) {
25+ var cfg Configuration
2626
27- // defaults
28- config .Mode = configuration .TAIL_MODE
27+ if err := yaml .UnmarshalWithOptions (y , & cfg , yaml .Strict ()); err != nil {
28+ return cfg , fmt .Errorf ("cannot parse: %s" , yaml .FormatError (err , false , false ))
29+ }
2930
30- err := yaml .UnmarshalWithOptions (yamlConfig , & config , yaml .Strict ())
31- if err != nil {
32- return fmt .Errorf ("cannot parse journalctl acquisition config: %s" , yaml .FormatError (err , false , false ))
31+ cfg .SetDefaults ()
32+
33+ if err := cfg .Validate (); err != nil {
34+ return cfg , err
3335 }
3436
35- if len (config .Filters ) == 0 {
37+ return cfg , nil
38+ }
39+
40+ func (c * Configuration ) SetDefaults () {
41+ if c .Mode == "" {
42+ c .Mode = configuration .TAIL_MODE
43+ }
44+ }
45+
46+ func (c * Configuration ) Validate () error {
47+ if len (c .Filters ) == 0 {
3648 return errors .New ("journalctl_filter is required" )
3749 }
3850
39- s .config = config
51+ return nil
52+ }
53+
54+ func (s * Source ) UnmarshalConfig (yamlConfig []byte ) error {
55+ cfg , err := ConfigurationFromYAML (yamlConfig )
56+ if err != nil {
57+ return err
58+ }
59+
60+ s .config = cfg
61+
4062 s .setSrc (s .config .Filters )
4163
4264 return nil
4365}
4466
4567func (s * Source ) Configure (_ context.Context , yamlConfig []byte , logger * log.Entry , metricsLevel metrics.AcquisitionMetricsLevel ) error {
46- s .metricsLevel = metricsLevel
47-
4868 if err := s .UnmarshalConfig (yamlConfig ); err != nil {
4969 return err
5070 }
5171
5272 s .setLogger (logger , 0 , s .src )
5373
74+ s .metricsLevel = metricsLevel
75+
5476 return nil
5577}
5678
0 commit comments