Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
dealt with more places that use slash as the separator of namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
candysmurf committed Oct 13, 2016
1 parent 85b60c9 commit e79d5fd
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 51 deletions.
7 changes: 5 additions & 2 deletions cmd/snapctl/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
14 changes: 2 additions & 12 deletions core/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
package core

import (
"fmt"
"testing"

"github.com/intelsdi-x/snap/pkg/stringutils"
. "github.com/smartystreets/goconvey/convey"
)

Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions mgmt/rest/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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]
}
Expand Down
13 changes: 2 additions & 11 deletions mgmt/rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
13 changes: 13 additions & 0 deletions pkg/stringutils/string.go
Original file line number Diff line number Diff line change
@@ -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
}
23 changes: 19 additions & 4 deletions plugin/collector/snap-plugin-collector-mock2-grpc/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,24 +189,24 @@ 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{}
for _, m := range mts {
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")

This comment has been minimized.

Copy link
@korservick

korservick May 11, 2017

This unicode simbols strongly nedded?

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)
})
})
Expand All @@ -215,21 +215,21 @@ 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{}
for _, m := range mts {
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)
})
})
Expand Down
13 changes: 2 additions & 11 deletions scheduler/wmap/wmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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),
Expand All @@ -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
}
Expand Down

0 comments on commit e79d5fd

Please sign in to comment.