diff --git a/cmd/snapctl/metric.go b/cmd/snapctl/metric.go index bacdb150b..044a73297 100644 --- a/cmd/snapctl/metric.go +++ b/cmd/snapctl/metric.go @@ -31,6 +31,8 @@ import ( "github.com/codegangsta/cli" "github.com/intelsdi-x/snap/mgmt/rest/client" "github.com/intelsdi-x/snap/mgmt/rest/rbody" + + "github.com/intelsdi-x/snap/pkg/stringutils" ) func listMetrics(ctx *cli.Context) error { @@ -189,11 +191,12 @@ func getMetric(ctx *cli.Context) error { func getNamespace(mt *rbody.Metric) string { ns := mt.Namespace if mt.Dynamic { - slice := strings.Split(ns, "/") + fc := stringutils.GetFirstChar(ns) + slice := strings.Split(ns, fc) for _, v := range mt.DynamicElements { slice[v.Index+1] = "[" + v.Name + "]" } - ns = strings.Join(slice, "/") + ns = strings.Join(slice, fc) } return ns } diff --git a/core/metric_test.go b/core/metric_test.go index ce75e4468..12b2d175e 100644 --- a/core/metric_test.go +++ b/core/metric_test.go @@ -3,9 +3,9 @@ package core import ( - "fmt" "testing" + "github.com/intelsdi-x/snap/pkg/stringutils" . "github.com/smartystreets/goconvey/convey" ) @@ -14,23 +14,13 @@ func TestMetricSeparator(t *testing.T) { Convey("Test namespace separator", t, func() { for _, c := range tc { Convey("namespace "+c.input.String(), func() { - firstChar := getFirstChar(c.input.String()) + firstChar := stringutils.GetFirstChar(c.input.String()) So(firstChar, ShouldEqual, c.expected) }) } }) } -// GetFirstChar returns the first character from the input string. -func getFirstChar(s string) string { - firstChar := "" - for _, r := range s { - firstChar = fmt.Sprintf("%c", r) - break - } - return firstChar -} - type testCase struct { input Namespace expected string diff --git a/mgmt/rest/metric.go b/mgmt/rest/metric.go index 87ed6d204..b0863a0a4 100644 --- a/mgmt/rest/metric.go +++ b/mgmt/rest/metric.go @@ -31,6 +31,7 @@ import ( "github.com/intelsdi-x/snap/core" "github.com/intelsdi-x/snap/mgmt/rest/rbody" + "github.com/intelsdi-x/snap/pkg/stringutils" ) func (s *Server) getMetrics(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { @@ -51,8 +52,9 @@ func (s *Server) getMetrics(w http.ResponseWriter, r *http.Request, _ httprouter return } } - // strip the leading '/' and split on the remaining '/' - ns := strings.Split(strings.TrimLeft(ns_query, "/"), "/") + // strip the leading char and split on the remaining. + fc := stringutils.GetFirstChar(ns_query) + ns := strings.Split(strings.TrimLeft(ns_query, fc), fc) if ns[len(ns)-1] == "*" { ns = ns[:len(ns)-1] } diff --git a/mgmt/rest/server.go b/mgmt/rest/server.go index ab1543690..f35ab723a 100644 --- a/mgmt/rest/server.go +++ b/mgmt/rest/server.go @@ -41,6 +41,7 @@ import ( "github.com/intelsdi-x/snap/mgmt/rest/rbody" "github.com/intelsdi-x/snap/mgmt/tribe/agreement" cschedule "github.com/intelsdi-x/snap/pkg/schedule" + "github.com/intelsdi-x/snap/pkg/stringutils" "github.com/intelsdi-x/snap/scheduler/wmap" ) @@ -516,17 +517,7 @@ func respond(code int, b rbody.Body, w http.ResponseWriter) { } func parseNamespace(ns string) []string { - fc := getFirstChar(ns) + fc := stringutils.GetFirstChar(ns) ns = strings.Trim(ns, fc) return strings.Split(ns, fc) } - -// GetFirstChar returns the first character from the input string. -func getFirstChar(s string) string { - firstChar := "" - for _, r := range s { - firstChar = fmt.Sprintf("%c", r) - break - } - return firstChar -} diff --git a/pkg/stringutils/string.go b/pkg/stringutils/string.go new file mode 100644 index 000000000..14a9e72f6 --- /dev/null +++ b/pkg/stringutils/string.go @@ -0,0 +1,13 @@ +package stringutils + +import "fmt" + +// GetFirstChar returns the first character from the input string. +func GetFirstChar(s string) string { + firstChar := "" + for _, r := range s { + firstChar = fmt.Sprintf("%c", r) + break + } + return firstChar +} diff --git a/plugin/collector/snap-plugin-collector-mock2-grpc/mock/mock.go b/plugin/collector/snap-plugin-collector-mock2-grpc/mock/mock.go index be3378ce3..eea5708b0 100644 --- a/plugin/collector/snap-plugin-collector-mock2-grpc/mock/mock.go +++ b/plugin/collector/snap-plugin-collector-mock2-grpc/mock/mock.go @@ -113,30 +113,45 @@ func (f *Mock) GetMetricTypes(cfg plugin.Config) ([]plugin.Metric, error) { } if _, err := cfg.GetBool("test"); err == nil { mts = append(mts, plugin.Metric{ - Namespace: plugin.NewNamespace("intel", "mock", "test"), + Namespace: plugin.NewNamespace("intel", "mock", "test%>"), Description: "mock description", Unit: "mock unit", }) } if _, err := cfg.GetBool("test-less"); err != nil { mts = append(mts, plugin.Metric{ - Namespace: plugin.NewNamespace("intel", "mock", "foo"), + Namespace: plugin.NewNamespace("intel", "mock", "/foo=㊽"), Description: "mock description", Unit: "mock unit", }) } mts = append(mts, plugin.Metric{ - Namespace: plugin.NewNamespace("intel", "mock", "bar"), + Namespace: plugin.NewNamespace("intel", "mock", "/bar⽔"), Description: "mock description", Unit: "mock unit", }) mts = append(mts, plugin.Metric{ Namespace: plugin.NewNamespace("intel", "mock"). AddDynamicElement("host", "name of the host"). - AddStaticElement("baz"), + AddStaticElement("/baz⽔"), Description: "mock description", Unit: "mock unit", }) + mts = append(mts, plugin.Metric{ + Namespace: plugin.NewNamespace("intel", "mock"). + AddDynamicElement("host", "name of the host"). + AddStaticElements("baz㊽", "/bar⽔"), + Description: "mock description", + Unit: "mock unit", + }) + mts = append(mts, plugin.Metric{ + Namespace: plugin.NewNamespace("intel", "mock"). + AddDynamicElement("host", "name of the host"). + AddStaticElements("baz㊽", "|barᵹÄ☍"), + Description: "mock description", + Unit: "mock unit", + }) + return mts, nil } diff --git a/plugin/collector/snap-plugin-collector-mock2-grpc/mock/mock_medium_test.go b/plugin/collector/snap-plugin-collector-mock2-grpc/mock/mock_medium_test.go index 4d6e5a42e..f03cfe846 100644 --- a/plugin/collector/snap-plugin-collector-mock2-grpc/mock/mock_medium_test.go +++ b/plugin/collector/snap-plugin-collector-mock2-grpc/mock/mock_medium_test.go @@ -189,7 +189,7 @@ func TestGetMetricTypes(t *testing.T) { mts, err := newPlg.GetMetricTypes(cfg) So(err, ShouldBeNil) - So(len(mts), ShouldEqual, 4) + So(len(mts), ShouldEqual, 6) Convey("checking namespaces", func() { metricNames := []string{} @@ -197,16 +197,16 @@ func TestGetMetricTypes(t *testing.T) { metricNames = append(metricNames, m.Namespace.String()) } - ns := plugin.NewNamespace("intel", "mock", "test") + ns := plugin.NewNamespace("intel", "mock", "test%>") So(str.Contains(metricNames, ns.String()), ShouldBeTrue) - ns = plugin.NewNamespace("intel", "mock", "foo") + ns = plugin.NewNamespace("intel", "mock", "/foo=㊽") So(str.Contains(metricNames, ns.String()), ShouldBeTrue) - ns = plugin.NewNamespace("intel", "mock", "bar") + ns = plugin.NewNamespace("intel", "mock", "/bar⽔") So(str.Contains(metricNames, ns.String()), ShouldBeTrue) - ns = plugin.NewNamespace("intel", "mock").AddDynamicElement("host", "name of the host").AddStaticElement("baz") + ns = plugin.NewNamespace("intel", "mock").AddDynamicElement("host", "name of the host").AddStaticElements("baz㊽", "/bar⽔") So(str.Contains(metricNames, ns.String()), ShouldBeTrue) }) }) @@ -215,7 +215,7 @@ func TestGetMetricTypes(t *testing.T) { mts, err := newPlg.GetMetricTypes(plugin.Config{}) So(err, ShouldBeNil) - So(len(mts), ShouldEqual, 3) + So(len(mts), ShouldEqual, 5) Convey("checking namespaces", func() { metricNames := []string{} @@ -223,13 +223,13 @@ func TestGetMetricTypes(t *testing.T) { metricNames = append(metricNames, m.Namespace.String()) } - ns := plugin.NewNamespace("intel", "mock", "foo") + ns := plugin.NewNamespace("intel", "mock", "/foo=㊽") So(str.Contains(metricNames, ns.String()), ShouldBeTrue) - ns = plugin.NewNamespace("intel", "mock", "bar") + ns = plugin.NewNamespace("intel", "mock", "/bar⽔") So(str.Contains(metricNames, ns.String()), ShouldBeTrue) - ns = plugin.NewNamespace("intel", "mock").AddDynamicElement("host", "name of the host").AddStaticElement("baz") + ns = plugin.NewNamespace("intel", "mock").AddDynamicElement("host", "name of the host").AddStaticElements("baz㊽", "|barᵹÄ☍") So(str.Contains(metricNames, ns.String()), ShouldBeTrue) }) }) diff --git a/scheduler/wmap/wmap.go b/scheduler/wmap/wmap.go index e5790c6af..56a359066 100644 --- a/scheduler/wmap/wmap.go +++ b/scheduler/wmap/wmap.go @@ -29,6 +29,7 @@ import ( "github.com/intelsdi-x/snap/core/cdata" "github.com/intelsdi-x/snap/core/ctypes" + "github.com/intelsdi-x/snap/pkg/stringutils" ) var ( @@ -216,7 +217,7 @@ func (c *CollectWorkflowMapNode) GetMetrics() []Metric { for k, v := range c.Metrics { // Identify the character to split on by peaking // at the first character of each metric. - firstChar := getFirstChar(k) + firstChar := stringutils.GetFirstChar(k) ns := strings.Trim(k, firstChar) metrics[i] = Metric{ namespace: strings.Split(ns, firstChar), @@ -227,16 +228,6 @@ func (c *CollectWorkflowMapNode) GetMetrics() []Metric { return metrics } -// GetFirstChar returns the first character from the input string. -func getFirstChar(s string) string { - firstChar := "" - for _, r := range s { - firstChar = fmt.Sprintf("%c", r) - break - } - return firstChar -} - func (c *CollectWorkflowMapNode) GetTags() map[string]map[string]string { return c.Tags }