@@ -17,6 +17,7 @@ package config
1717import (
1818 "fmt"
1919 "github.com/fstab/grok_exporter/config/v2"
20+ v3 "github.com/fstab/grok_exporter/config/v3"
2021 "io/ioutil"
2122 "regexp"
2223 "strconv"
@@ -25,7 +26,7 @@ import (
2526
2627// Example config: See ./example/config.yml
2728
28- func LoadConfigFile (filename string ) (* v2 .Config , string , error ) {
29+ func LoadConfigFile (filename string ) (* v3 .Config , string , error ) {
2930 content , err := ioutil .ReadFile (filename )
3031 if err != nil {
3132 return nil , "" , fmt .Errorf ("Failed to load %v: %v" , filename , err .Error ())
@@ -37,7 +38,7 @@ func LoadConfigFile(filename string) (*v2.Config, string, error) {
3738 return cfg , warn , nil
3839}
3940
40- func LoadConfigString (content []byte ) (* v2 .Config , string , error ) {
41+ func LoadConfigString (content []byte ) (* v3 .Config , string , error ) {
4142 version , warn , err := findVersion (string (content ))
4243 if err != nil {
4344 return nil , warn , err
@@ -47,26 +48,36 @@ func LoadConfigString(content []byte) (*v2.Config, string, error) {
4748}
4849
4950// returns (version, warning, error).
50- // Warning is for deprecating old versions, but as we currently only support version 2 it is currently not used.
5151func findVersion (content string ) (int , string , error ) {
52+ warning := "Configuration version 2 found. This is still supported, but we recommend updating to version 3. Run grok_exporter with the -showconfig command line parameter to automatically convert to version 3 and write the result to the console."
5253 versionExpr := regexp .MustCompile (`"?global"?:\s*"?config_version"?:[\t\f ]*(\S+)` )
5354 versionInfo := versionExpr .FindStringSubmatch (content )
5455 if len (versionInfo ) == 2 {
5556 version , err := strconv .Atoi (strings .TrimSpace (versionInfo [1 ]))
5657 if err != nil {
5758 return 0 , "" , fmt .Errorf ("invalid 'global' configuration: '%v' is not a valid 'config_version'." , versionInfo [1 ])
5859 }
59- return version , "" , nil
60+ if version == 2 {
61+ return version , warning , nil
62+ } else {
63+ return version , "" , nil
64+ }
6065 } else { // no version found
6166 return 0 , "" , fmt .Errorf ("invalid configuration: 'global.config_version' not found." )
6267 }
6368}
6469
65- func unmarshal (content []byte , version int ) (* v2 .Config , error ) {
70+ func unmarshal (content []byte , version int ) (* v3 .Config , error ) {
6671 switch version {
6772 case 2 :
68- return v2 .Unmarshal (content )
73+ v2cfg , err := v2 .Unmarshal (content )
74+ if err != nil {
75+ return nil , err
76+ }
77+ return v3 .Convert (v2cfg ), nil
78+ case 3 :
79+ return v3 .Unmarshal (content )
6980 default :
70- return nil , fmt .Errorf ("global.config_version %v is not supported. " , version )
81+ return nil , fmt .Errorf ("global.config_version %v is not supported" , version )
7182 }
7283}
0 commit comments