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

Commit

Permalink
Merge pull request #596 from danielscottt/issue-526
Browse files Browse the repository at this point in the history
fix #526: adds lookup for GetMetricVersions
  • Loading branch information
pittma committed Dec 5, 2015
2 parents fc06d42 + 51ac1bf commit e104776
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 102 deletions.
6 changes: 5 additions & 1 deletion cmd/snapctl/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,11 @@ func printFields(tw *tabwriter.Writer, indent bool, width int, fields ...interfa
argArray = append(argArray, strings.Repeat(" ", width))
}
for i, field := range fields {
argArray = append(argArray, field)
if field != nil {
argArray = append(argArray, field)
} else {
argArray = append(argArray, "")
}
if i < (len(fields) - 1) {
argArray = append(argArray, "\t")
}
Expand Down
49 changes: 15 additions & 34 deletions cmd/snapctl/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func listMetrics(ctx *cli.Context) {

/*
NAMESPACE VERSION
/intel/mock/foo 1,2
/intel/mock/bar 1
/intel/mock/foo 1,2
/intel/mock/bar 1
*/
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
metsByVer := make(map[string][]string)
Expand All @@ -78,26 +78,23 @@ func listMetrics(ctx *cli.Context) {
}

func getMetric(ctx *cli.Context) {
ns := ctx.String("metric-namespace")
ver := ctx.Int("metric-version")
if ns == "" {
fmt.Println("namespace is required")
if !ctx.IsSet("metric-namespace") || !ctx.IsSet("metric-version") {
fmt.Println("namespace and version are required")
fmt.Println("")
cli.ShowCommandHelp(ctx, ctx.Command.Name)
return
}
if ver == 0 {
ver = -1
}
metrics := pClient.FetchMetrics(ns, ver)
if metrics.Err != nil {
fmt.Println(metrics.Err)
ns := ctx.String("metric-namespace")
ver := ctx.Int("metric-version")
metric := pClient.GetMetric(ns, ver)
if metric.Err != nil {
fmt.Println(metric.Err)
return
}
metric := metrics.Catalog[0]

/*
NAMESPACE VERSION LAST ADVERTISED TIME
/intel/mock/foo 2 Wed, 09 Sep 2015 10:01:04 PDT
/intel/mock/foo 2 Wed, 09 Sep 2015 10:01:04 PDT
Rules for collecting /intel/mock/foo:
Expand All @@ -109,28 +106,12 @@ func getMetric(ctx *cli.Context) {

w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
printFields(w, false, 0, "NAMESPACE", "VERSION", "LAST ADVERTISED TIME")
printFields(w, false, 0, metric.Namespace, metric.Version, time.Unix(metric.LastAdvertisedTimestamp, 0).Format(time.RFC1123))
printFields(w, false, 0, metric.Metric.Namespace, metric.Metric.Version, time.Unix(metric.Metric.LastAdvertisedTimestamp, 0).Format(time.RFC1123))
w.Flush()
fmt.Printf("\n Rules for collecting %s:\n\n", metric.Namespace)
fmt.Printf("\n Rules for collecting %s:\n\n", metric.Metric.Namespace)
printFields(w, true, 6, "NAME", "TYPE", "DEFAULT", "REQUIRED", "MINIMUM", "MAXIMUM")
for _, rule := range metric.Policy {
var def_, min_, max_ interface{}
if rule.Default == nil {
def_ = ""
} else {
def_ = rule.Default
}
if rule.Minimum == nil {
min_ = ""
} else {
min_ = rule.Minimum
}
if rule.Maximum == nil {
max_ = ""
} else {
max_ = rule.Maximum
}
printFields(w, true, 6, rule.Name, rule.Type, def_, rule.Required, min_, max_)
for _, rule := range metric.Metric.Policy {
printFields(w, true, 6, rule.Name, rule.Type, rule.Default, rule.Required, rule.Minimum, rule.Maximum)
}
w.Flush()
}
34 changes: 27 additions & 7 deletions control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,17 @@ type managesPlugins interface {
}

type catalogsMetrics interface {
Get([]string, int) (*metricType, serror.SnapError)
Get([]string, int) (*metricType, error)
Add(*metricType)
AddLoadedMetricType(*loadedPlugin, core.Metric)
RmUnloadedPluginMetrics(lp *loadedPlugin)
Fetch([]string) ([]*metricType, serror.SnapError)
GetVersions([]string) ([]*metricType, error)
Fetch([]string) ([]*metricType, error)
Item() (string, []*metricType)
Next() bool
Subscribe([]string, int) serror.SnapError
Unsubscribe([]string, int) serror.SnapError
GetPlugin([]string, int) (*loadedPlugin, serror.SnapError)
Subscribe([]string, int) error
Unsubscribe([]string, int) error
GetPlugin([]string, int) (*loadedPlugin, error)
}

type managesSigning interface {
Expand Down Expand Up @@ -491,7 +492,10 @@ func (p *pluginControl) validateMetricTypeSubscription(mt core.RequestedMetric,

m, err := p.metricCatalog.Get(mt.Namespace(), mt.Version())
if err != nil {
serrs = append(serrs, err)
serrs = append(serrs, serror.New(err, map[string]interface{}{
"name": core.JoinNamespace(mt.Namespace()),
"version": mt.Version(),
}))
return nil, serrs
}

Expand Down Expand Up @@ -551,7 +555,10 @@ func (p *pluginControl) gatherCollectors(mts []core.Metric) ([]core.Plugin, []se
for _, mt := range mts {
m, err := p.metricCatalog.Get(mt.Namespace(), mt.Version())
if err != nil {
serrs = append(serrs, err)
serrs = append(serrs, serror.New(err, map[string]interface{}{
"name": core.JoinNamespace(mt.Namespace()),
"version": mt.Version(),
}))
continue
}
// if the metric subscription is to version -1, we need to carry
Expand Down Expand Up @@ -786,6 +793,19 @@ func (p *pluginControl) GetMetric(ns []string, ver int) (core.CatalogedMetric, e
return p.metricCatalog.Get(ns, ver)
}

func (p *pluginControl) GetMetricVersions(ns []string) ([]core.CatalogedMetric, error) {
mts, err := p.metricCatalog.GetVersions(ns)
if err != nil {
return nil, err
}

rmts := make([]core.CatalogedMetric, len(mts))
for i, m := range mts {
rmts[i] = m
}
return rmts, nil
}

func (p *pluginControl) MetricExists(mns []string, ver int) bool {
_, err := p.metricCatalog.Get(mns, ver)
if err == nil {
Expand Down
14 changes: 9 additions & 5 deletions control/control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ type mc struct {
e int
}

func (m *mc) Fetch(ns []string) ([]*metricType, serror.SnapError) {
func (m *mc) Fetch(ns []string) ([]*metricType, error) {
if m.e == 2 {
return nil, serror.New(errors.New("test"))
}
Expand All @@ -567,11 +567,15 @@ func (m *mc) resolvePlugin(mns []string, ver int) (*loadedPlugin, error) {
return nil, nil
}

func (m *mc) GetPlugin([]string, int) (*loadedPlugin, serror.SnapError) {
func (m *mc) GetPlugin([]string, int) (*loadedPlugin, error) {
return nil, nil
}

func (m *mc) Get(ns []string, ver int) (*metricType, serror.SnapError) {
func (m *mc) GetVersions([]string) ([]*metricType, error) {
return nil, nil
}

func (m *mc) Get(ns []string, ver int) (*metricType, error) {
if m.e == 1 {
return &metricType{
policy: &mockCDProc{},
Expand All @@ -580,14 +584,14 @@ func (m *mc) Get(ns []string, ver int) (*metricType, serror.SnapError) {
return nil, serror.New(errorMetricNotFound(ns))
}

func (m *mc) Subscribe(ns []string, ver int) serror.SnapError {
func (m *mc) Subscribe(ns []string, ver int) error {
if ns[0] == "nf" {
return serror.New(errorMetricNotFound(ns))
}
return nil
}

func (m *mc) Unsubscribe(ns []string, ver int) serror.SnapError {
func (m *mc) Unsubscribe(ns []string, ver int) error {
if ns[0] == "nf" {
return serror.New(errorMetricNotFound(ns))
}
Expand Down
49 changes: 30 additions & 19 deletions control/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,23 @@ func (mc *metricCatalog) Add(m *metricType) {
mc.tree.Add(m)
}

// Get retrieves a loadedPlugin given a namespace and version.
// Get retrieves a metric given a namespace and version.
// If provided a version of -1 the latest plugin will be returned.
func (mc *metricCatalog) Get(ns []string, version int) (*metricType, serror.SnapError) {
func (mc *metricCatalog) Get(ns []string, version int) (*metricType, error) {
mc.mutex.Lock()
defer mc.mutex.Unlock()
return mc.get(ns, version)
}

// GetVersions retrieves all versions of a given metric namespace.
func (mc *metricCatalog) GetVersions(ns []string) ([]*metricType, error) {
mc.mutex.Lock()
defer mc.mutex.Unlock()
return mc.getVersions(ns)
}

// Fetch transactionally retrieves all metrics which fall under namespace ns
func (mc *metricCatalog) Fetch(ns []string) ([]*metricType, serror.SnapError) {
func (mc *metricCatalog) Fetch(ns []string) ([]*metricType, error) {
mc.mutex.Lock()
defer mc.mutex.Unlock()

Expand Down Expand Up @@ -260,7 +267,7 @@ func (mc *metricCatalog) Next() bool {
}

// Subscribe atomically increments a metric's subscription count in the table.
func (mc *metricCatalog) Subscribe(ns []string, version int) serror.SnapError {
func (mc *metricCatalog) Subscribe(ns []string, version int) error {
mc.mutex.Lock()
defer mc.mutex.Unlock()

Expand All @@ -274,7 +281,7 @@ func (mc *metricCatalog) Subscribe(ns []string, version int) serror.SnapError {
}

// Unsubscribe atomically decrements a metric's count in the table
func (mc *metricCatalog) Unsubscribe(ns []string, version int) serror.SnapError {
func (mc *metricCatalog) Unsubscribe(ns []string, version int) error {
mc.mutex.Lock()
defer mc.mutex.Unlock()

Expand All @@ -286,39 +293,43 @@ func (mc *metricCatalog) Unsubscribe(ns []string, version int) serror.SnapError
return m.Unsubscribe()
}

func (mc *metricCatalog) GetPlugin(mns []string, ver int) (*loadedPlugin, serror.SnapError) {
func (mc *metricCatalog) GetPlugin(mns []string, ver int) (*loadedPlugin, error) {
m, err := mc.Get(mns, ver)
if err != nil {
return nil, err
}
return m.Plugin, nil
}

func (mc *metricCatalog) get(ns []string, ver int) (*metricType, serror.SnapError) {
mts, err := mc.tree.Get(ns)
func (mc *metricCatalog) get(ns []string, ver int) (*metricType, error) {
mts, err := mc.getVersions(ns)
if err != nil {
return nil, err
}
if mts == nil {
return nil, serror.New(errMetricNotFound)
}

// a version IS given
if ver > 0 {
l, err := getVersion(mts, ver)
if err != nil {
se := serror.New(errorMetricNotFound(ns, ver))
se.SetFields(map[string]interface{}{
"name": core.JoinNamespace(ns),
"version": ver,
})
return nil, se
return nil, errorMetricNotFound(ns, ver)
}
return l, nil
}
// ver is less than or equal to 0 get the latest
return getLatest(mts), nil
}

func (mc *metricCatalog) getVersions(ns []string) ([]*metricType, error) {
mts, err := mc.tree.Get(ns)
if err != nil {
return nil, err
}
if mts == nil {
return nil, errMetricNotFound
}
return mts, nil
}

func getMetricKey(metric []string) string {
return strings.Join(metric, ".")
}
Expand All @@ -343,11 +354,11 @@ func appendIfMissing(keys []string, ns string) []string {
return append(keys, ns)
}

func getVersion(c []*metricType, ver int) (*metricType, serror.SnapError) {
func getVersion(c []*metricType, ver int) (*metricType, error) {
for _, m := range c {
if m.Plugin.Version() == ver {
return m, nil
}
}
return nil, serror.New(errMetricNotFound)
return nil, errMetricNotFound
}
5 changes: 0 additions & 5 deletions control/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ func TestMetricCatalog(t *testing.T) {
mc.Add(m35)
_, err := mc.Get([]string{"foo", "bar"}, 7)
So(err.Error(), ShouldContainSubstring, "Metric not found:")
So(err.Fields()["name"], ShouldEqual, "/foo/bar")
So(err.Fields()["version"], ShouldEqual, 7)
})
})
Convey("metricCatalog.Table()", t, func() {
Expand All @@ -164,7 +162,6 @@ func TestMetricCatalog(t *testing.T) {
_mt, err := mc.Get(ns, -1)
So(_mt, ShouldBeNil)
So(err.Error(), ShouldContainSubstring, "Metric not found:")
So(err.Fields()["name"], ShouldEqual, "/test")
})
})
Convey("metricCatalog.Next()", t, func() {
Expand Down Expand Up @@ -243,7 +240,6 @@ func TestSubscribe(t *testing.T) {
Convey("then it returns an error", func() {
err := mc.Subscribe([]string{"test4"}, -1)
So(err.Error(), ShouldContainSubstring, "Metric not found:")
So(err.Fields()["name"], ShouldEqual, "/test4")
})
})
Convey("when the metric is in the table", t, func() {
Expand Down Expand Up @@ -289,7 +285,6 @@ func TestUnsubscribe(t *testing.T) {
Convey("then it returns metric not found error", func() {
err := mc.Unsubscribe([]string{"test4"}, -1)
So(err.Error(), ShouldContainSubstring, "Metric not found:")
So(err.Fields()["name"], ShouldEqual, "/test4")
})
})
Convey("when the metric's count is already 0", t, func() {
Expand Down
Loading

0 comments on commit e104776

Please sign in to comment.