diff options
author | Wolfy-J <[email protected]> | 2019-12-23 16:19:39 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2019-12-23 16:19:39 +0300 |
commit | 23e3ca71347aec2152b17def2bb37d4927a6aebe (patch) | |
tree | 645dc399aec15b414dc40d3942c43b095633e7c6 | |
parent | a11a3a5511a7b986f06c5921932e3438a0a543d7 (diff) |
- the ability to specify config via JSON string
-rw-r--r-- | cmd/rr/cmd/root.go | 4 | ||||
-rw-r--r-- | cmd/util/config.go | 23 | ||||
-rw-r--r-- | service/metrics/rpc_test.go | 76 |
3 files changed, 61 insertions, 42 deletions
diff --git a/cmd/rr/cmd/root.go b/cmd/rr/cmd/root.go index d6929473..515e6419 100644 --- a/cmd/rr/cmd/root.go +++ b/cmd/rr/cmd/root.go @@ -33,6 +33,7 @@ import ( var ( cfgFile, workDir, logFormat string override []string + mergeJson string // Verbose enables verbosity mode (container specific). Verbose bool @@ -73,6 +74,7 @@ func init() { CLI.PersistentFlags().StringVarP(&logFormat, "logFormat", "l", "color", "select log formatter (color, json, plain)") CLI.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is .rr.yaml)") CLI.PersistentFlags().StringVarP(&workDir, "workDir", "w", "", "work directory") + CLI.PersistentFlags().StringVarP(&mergeJson, "jsonConfig", "j", "", "merge json configuration") CLI.PersistentFlags().StringArrayVarP( &override, @@ -89,7 +91,7 @@ func init() { configureLogger(logFormat) - cfg, err := util.LoadConfig(cfgFile, []string{"."}, ".rr", override) + cfg, err := util.LoadConfig(cfgFile, []string{"."}, ".rr", override, mergeJson) if err != nil { Logger.Warnf("config: %s", err) return diff --git a/cmd/util/config.go b/cmd/util/config.go index 0a8d6005..08e01a89 100644 --- a/cmd/util/config.go +++ b/cmd/util/config.go @@ -1,6 +1,7 @@ package util import ( + "bytes" "fmt" "github.com/spf13/viper" "github.com/spiral/roadrunner/service" @@ -30,7 +31,7 @@ func (w *ConfigWrapper) Unmarshal(out interface{}) error { } // LoadConfig config and merge it's values with set of flags. -func LoadConfig(cfgFile string, path []string, name string, flags []string) (*ConfigWrapper, error) { +func LoadConfig(cfgFile string, path []string, name string, flags []string, jsonConfig string) (*ConfigWrapper, error) { cfg := viper.New() if cfgFile != "" { @@ -68,14 +69,13 @@ func LoadConfig(cfgFile string, path []string, name string, flags []string) (*Co // If a cfg file is found, read it in. if err := cfg.ReadInConfig(); err != nil { - if len(flags) == 0 { + if len(flags) == 0 && jsonConfig == "" { return nil, err } } // merge included configs if include, ok := cfg.Get("include").([]interface{}); ok { - for _, file := range include { filename, ok := file.(string) if !ok { @@ -117,6 +117,23 @@ func LoadConfig(cfgFile string, path []string, name string, flags []string) (*Co } } + if jsonConfig != "" { + jConfig := viper.New() + jConfig.AutomaticEnv() + jConfig.SetEnvPrefix("rr") + jConfig.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + + jConfig.SetConfigType("json") + if err := jConfig.ReadConfig(bytes.NewBufferString(jsonConfig)); err != nil { + return nil, err + } + + // merging + if err := cfg.MergeConfigMap(jConfig.AllSettings()); err != nil { + return nil, err + } + } + merged := viper.New() // we have to copy all the merged values into new config in order normalize it (viper bug?) diff --git a/service/metrics/rpc_test.go b/service/metrics/rpc_test.go index 2468c083..3fe48818 100644 --- a/service/metrics/rpc_test.go +++ b/service/metrics/rpc_test.go @@ -88,7 +88,7 @@ func Test_Set_RPC_Vector(t *testing.T) { "type": "gauge", "labels": ["type", "section"] }`, - "2112", + "2113", ) defer c.Stop() @@ -100,7 +100,7 @@ func Test_Set_RPC_Vector(t *testing.T) { }, &ok)) assert.True(t, ok) - out, _, err := get("http://localhost:2112/metrics") + out, _, err := get("http://localhost:2113/metrics") assert.NoError(t, err) assert.Contains(t, out, `user_gauge{section="first",type="core"} 100`) } @@ -112,7 +112,7 @@ func Test_Set_RPC_CollectorError(t *testing.T) { "type": "gauge", "labels": ["type", "section"] }`, - "2112", + "2114", ) defer c.Stop() @@ -131,7 +131,7 @@ func Test_Set_RPC_MetricError(t *testing.T) { "type": "gauge", "labels": ["type", "section"] }`, - "2112", + "2115", ) defer c.Stop() @@ -150,7 +150,7 @@ func Test_Set_RPC_MetricError_2(t *testing.T) { "type": "gauge", "labels": ["type", "section"] }`, - "2112", + "2116", ) defer c.Stop() @@ -168,7 +168,7 @@ func Test_Set_RPC_MetricError_3(t *testing.T) { "type": "histogram", "labels": ["type", "section"] }`, - "2112", + "2117", ) defer c.Stop() @@ -187,7 +187,7 @@ func Test_Sub_RPC(t *testing.T) { `"user_gauge":{ "type": "gauge" }`, - "2113", + "2118", ) defer c.Stop() @@ -204,7 +204,7 @@ func Test_Sub_RPC(t *testing.T) { }, &ok)) assert.True(t, ok) - out, _, err := get("http://localhost:2113/metrics") + out, _, err := get("http://localhost:2118/metrics") assert.NoError(t, err) assert.Contains(t, out, `user_gauge 90`) } @@ -216,7 +216,7 @@ func Test_Sub_RPC_Vector(t *testing.T) { "type": "gauge", "labels": ["type", "section"] }`, - "2114", + "2119", ) defer c.Stop() @@ -235,7 +235,7 @@ func Test_Sub_RPC_Vector(t *testing.T) { }, &ok)) assert.True(t, ok) - out, _, err := get("http://localhost:2114/metrics") + out, _, err := get("http://localhost:2119/metrics") assert.NoError(t, err) assert.Contains(t, out, `user_gauge{section="first",type="core"} 90`) } @@ -247,7 +247,7 @@ func Test_Sub_RPC_CollectorError(t *testing.T) { "type": "gauge", "labels": ["type", "section"] }`, - "2112", + "2120", ) defer c.Stop() @@ -266,7 +266,7 @@ func Test_Sub_RPC_MetricError(t *testing.T) { "type": "gauge", "labels": ["type", "section"] }`, - "2112", + "2121", ) defer c.Stop() @@ -285,7 +285,7 @@ func Test_Sub_RPC_MetricError_2(t *testing.T) { "type": "gauge", "labels": ["type", "section"] }`, - "2112", + "2122", ) defer c.Stop() @@ -303,7 +303,7 @@ func Test_Sub_RPC_MetricError_3(t *testing.T) { "type": "histogram", "labels": ["type", "section"] }`, - "2112", + "2123", ) defer c.Stop() @@ -322,7 +322,7 @@ func Test_Observe_RPC(t *testing.T) { `"user_histogram":{ "type": "histogram" }`, - "2116", + "2124", ) defer c.Stop() @@ -333,7 +333,7 @@ func Test_Observe_RPC(t *testing.T) { }, &ok)) assert.True(t, ok) - out, _, err := get("http://localhost:2116/metrics") + out, _, err := get("http://localhost:2124/metrics") assert.NoError(t, err) assert.Contains(t, out, `user_histogram`) } @@ -345,7 +345,7 @@ func Test_Observe_RPC_Vector(t *testing.T) { "type": "histogram", "labels": ["type", "section"] }`, - "2117", + "2125", ) defer c.Stop() @@ -357,7 +357,7 @@ func Test_Observe_RPC_Vector(t *testing.T) { }, &ok)) assert.True(t, ok) - out, _, err := get("http://localhost:2117/metrics") + out, _, err := get("http://localhost:2125/metrics") assert.NoError(t, err) assert.Contains(t, out, `user_histogram`) } @@ -369,7 +369,7 @@ func Test_Observe_RPC_CollectorError(t *testing.T) { "type": "histogram", "labels": ["type", "section"] }`, - "2112", + "2126", ) defer c.Stop() @@ -388,7 +388,7 @@ func Test_Observe_RPC_MetricError(t *testing.T) { "type": "histogram", "labels": ["type", "section"] }`, - "2112", + "2127", ) defer c.Stop() @@ -407,7 +407,7 @@ func Test_Observe_RPC_MetricError_2(t *testing.T) { "type": "histogram", "labels": ["type", "section"] }`, - "2112", + "2128", ) defer c.Stop() @@ -426,7 +426,7 @@ func Test_Observe2_RPC(t *testing.T) { `"user_histogram":{ "type": "summary" }`, - "2118", + "2129", ) defer c.Stop() @@ -437,7 +437,7 @@ func Test_Observe2_RPC(t *testing.T) { }, &ok)) assert.True(t, ok) - out, _, err := get("http://localhost:2118/metrics") + out, _, err := get("http://localhost:2129/metrics") assert.NoError(t, err) assert.Contains(t, out, `user_histogram`) } @@ -448,7 +448,7 @@ func Test_Observe2_RPC_Invalid(t *testing.T) { `"user_histogram":{ "type": "summary" }`, - "2112", + "2130", ) defer c.Stop() @@ -466,7 +466,7 @@ func Test_Observe2_RPC_Invalid_2(t *testing.T) { `"user_histogram":{ "type": "gauge" }`, - "2112", + "2131", ) defer c.Stop() @@ -484,7 +484,7 @@ func Test_Observe2_RPC_Vector(t *testing.T) { "type": "summary", "labels": ["type", "section"] }`, - "2119", + "2132", ) defer c.Stop() @@ -496,7 +496,7 @@ func Test_Observe2_RPC_Vector(t *testing.T) { }, &ok)) assert.True(t, ok) - out, _, err := get("http://localhost:2119/metrics") + out, _, err := get("http://localhost:2132/metrics") assert.NoError(t, err) assert.Contains(t, out, `user_histogram`) } @@ -508,7 +508,7 @@ func Test_Observe2_RPC_CollectorError(t *testing.T) { "type": "summary", "labels": ["type", "section"] }`, - "2112", + "2133", ) defer c.Stop() @@ -527,7 +527,7 @@ func Test_Observe2_RPC_MetricError(t *testing.T) { "type": "summary", "labels": ["type", "section"] }`, - "2112", + "2134", ) defer c.Stop() @@ -546,7 +546,7 @@ func Test_Observe2_RPC_MetricError_2(t *testing.T) { "type": "summary", "labels": ["type", "section"] }`, - "2112", + "2135", ) defer c.Stop() @@ -564,7 +564,7 @@ func Test_Add_RPC(t *testing.T) { `"user_gauge":{ "type": "counter" }`, - "2120", + "2136", ) defer c.Stop() @@ -575,7 +575,7 @@ func Test_Add_RPC(t *testing.T) { }, &ok)) assert.True(t, ok) - out, _, err := get("http://localhost:2120/metrics") + out, _, err := get("http://localhost:2136/metrics") assert.NoError(t, err) assert.Contains(t, out, `user_gauge 100`) } @@ -587,7 +587,7 @@ func Test_Add_RPC_Vector(t *testing.T) { "type": "counter", "labels": ["type", "section"] }`, - "2121", + "2137", ) defer c.Stop() @@ -599,7 +599,7 @@ func Test_Add_RPC_Vector(t *testing.T) { }, &ok)) assert.True(t, ok) - out, _, err := get("http://localhost:2121/metrics") + out, _, err := get("http://localhost:2137/metrics") assert.NoError(t, err) assert.Contains(t, out, `user_gauge{section="first",type="core"} 100`) } @@ -611,7 +611,7 @@ func Test_Add_RPC_CollectorError(t *testing.T) { "type": "counter", "labels": ["type", "section"] }`, - "2112", + "2138", ) defer c.Stop() @@ -630,7 +630,7 @@ func Test_Add_RPC_MetricError(t *testing.T) { "type": "counter", "labels": ["type", "section"] }`, - "2112", + "2139", ) defer c.Stop() @@ -649,7 +649,7 @@ func Test_Add_RPC_MetricError_2(t *testing.T) { "type": "counter", "labels": ["type", "section"] }`, - "2112", + "2140", ) defer c.Stop() @@ -667,7 +667,7 @@ func Test_Add_RPC_MetricError_3(t *testing.T) { "type": "histogram", "labels": ["type", "section"] }`, - "2112", + "2141", ) defer c.Stop() |