Commit 96f18205 authored by Olivier Bourdon's avatar Olivier Bourdon

Rework on dynamic metrics

Added more wildcards combinations (like for SMART collector pluign)
Added more tests for better coverage
parent 521ae79b
......@@ -158,38 +158,91 @@ func (p *dfCollector) CollectMetrics(mts []plugin.MetricType) ([]plugin.MetricTy
for _, m := range mts {
ns := m.Namespace()
lns := len(ns)
if lns < 5 {
return nil, fmt.Errorf("Wrong namespace length %d", lns)
if lns < 4 {
return nil, fmt.Errorf("Wrong namespace length %d: should be at least 4", lns)
}
if ns[lns-2].Value == "*" {
for _, dfm := range dfms {
kind := ns[lns-1].Value
ns1 := core.NewNamespace(createNamespace(dfm.MountPoint, kind)...)
ns1[len(ns1)-2].Name = ns[lns-2].Name
metric := plugin.MetricType{
Timestamp_: curTime,
Namespace_: ns1,
// We can request all metrics for all devices in one shot
// using namespace /intel/procfs/filesystem/*
if lns == 4 {
if ns[lns-1].Value != "*" {
return nil, fmt.Errorf("Namespace should contain wildcard")
}
for _, kind := range metricsKind {
for _, dfm := range dfms {
metric := createMetric(
core.NewNamespace(
createNamespace(dfm.MountPoint, kind)...),
curTime)
fillMetric(kind, dfm, &metric)
metrics = append(metrics, metric)
}
fillMetric(kind, dfm, &metric)
metrics = append(metrics, metric)
}
} else {
for _, dfm := range dfms {
if ns[lns-2].Value == dfm.MountPoint {
metric := plugin.MetricType{
Timestamp_: curTime,
Namespace_: ns,
} else if ns[lns-2].Value == "*" {
// namespace /intel/procfs/filesystem/*/<metric>
kind := ns[lns-1].Value
// <metric> is also wildcard => get them all
if kind == "*" {
for _, skind := range metricsKind {
for _, dfm := range dfms {
metric := createMetric(
core.NewNamespace(
createNamespace(dfm.MountPoint, skind)...),
curTime)
fillMetric(skind, dfm, &metric)
metrics = append(metrics, metric)
}
kind := ns[lns-1].Value
}
} else {
// <metric> is not wildcard => getonly matching metrics
for _, dfm := range dfms {
metric := createMetric(
core.NewNamespace(
createNamespace(dfm.MountPoint, kind)...),
curTime)
fillMetric(kind, dfm, &metric)
metrics = append(metrics, metric)
}
}
} else {
// namespace /intel/procfs/filesystem/<fs>/<metric>
kind := ns[lns-1].Value
// <metric> is also wildcard => get them all
if kind == "*" {
for _, skind := range metricsKind {
for _, dfm := range dfms {
if ns[lns-2].Value == dfm.MountPoint {
metric := createMetric(
core.NewNamespace(
createNamespace(dfm.MountPoint, skind)...),
curTime)
fillMetric(skind, dfm, &metric)
metrics = append(metrics, metric)
}
}
}
} else {
for _, dfm := range dfms {
if ns[lns-2].Value == dfm.MountPoint {
metric := createMetric(ns, curTime)
fillMetric(kind, dfm, &metric)
metrics = append(metrics, metric)
}
}
}
}
}
return metrics, nil
}
func createMetric(ns core.Namespace, curTime time.Time) plugin.MetricType {
metric := plugin.MetricType{
Timestamp_: curTime,
Namespace_: ns,
}
ns[len(ns)-2].Name = nsType
return metric
}
// Function to fill metric with proper (computed) value
func fillMetric(kind string, dfm dfMetric, metric *plugin.MetricType) {
switch kind {
......
......@@ -157,6 +157,21 @@ func (dfp *DfPluginSuite) TestCollectMetrics() {
})
})
Convey("When list of metrics is requested with bad namespace", func() {
mts := []plugin.MetricType{
plugin.MetricType{
Namespace_: core.NewNamespace("intel", "procfs", "filesystem", "rootfs"),
},
}
metrics, err := dfPlg.CollectMetrics(mts)
Convey("Then error should be reported", func() {
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "Namespace should contain wildcard")
So(metrics, ShouldBeNil)
})
})
Convey("When list of specific metrics is requested", func() {
mts := []plugin.MetricType{
plugin.MetricType{
......@@ -223,6 +238,98 @@ func (dfp *DfPluginSuite) TestCollectMetrics() {
})
})
Convey("When all available dynamic metrics are requested for given mountpoint", func() {
mts := []plugin.MetricType{
plugin.MetricType{
Namespace_: core.NewNamespace("intel", "procfs", "filesystem", "rootfs", "*"),
},
}
metrics, err := dfPlg.CollectMetrics(mts)
Convey("Then no error should be reported", func() {
So(err, ShouldBeNil)
So(metrics, ShouldNotBeNil)
})
Convey("Then proper metrics are returned", func() {
metvals := map[string]interface{}{}
for _, m := range metrics {
stat := strings.Join(m.Namespace().Strings()[3:], "/")
So(stat, ShouldStartWith, "rootfs")
metvals[stat] = m.Data()
}
So(len(metrics), ShouldEqual, 14)
val, ok := metvals["rootfs/space_free"]
So(ok, ShouldBeTrue)
So(val, ShouldNotBeNil)
})
})
Convey("When all available dynamic metrics are requested for all mountpoints", func() {
mts := []plugin.MetricType{
plugin.MetricType{
Namespace_: core.NewNamespace("intel", "procfs", "filesystem", "*", "*"),
},
}
metrics, err := dfPlg.CollectMetrics(mts)
Convey("Then no error should be reported", func() {
So(err, ShouldBeNil)
So(metrics, ShouldNotBeNil)
})
Convey("Then proper metrics are returned", func() {
metvals := map[string]interface{}{}
for _, m := range metrics {
stat := strings.Join(m.Namespace().Strings()[3:], "/")
metvals[stat] = m.Data()
}
So(len(metrics), ShouldEqual, 28)
val, ok := metvals["rootfs/space_free"]
So(ok, ShouldBeTrue)
So(val, ShouldNotBeNil)
val, ok = metvals["big/space_free"]
So(ok, ShouldBeTrue)
So(val, ShouldNotBeNil)
})
})
Convey("When list of all available dynamic metrics is requested", func() {
mts := []plugin.MetricType{
plugin.MetricType{
Namespace_: core.NewNamespace("intel", "procfs", "filesystem", "*"),
},
}
metrics, err := dfPlg.CollectMetrics(mts)
Convey("Then no error should be reported", func() {
So(err, ShouldBeNil)
So(metrics, ShouldNotBeNil)
})
Convey("Then proper metrics are returned", func() {
metvals := map[string]interface{}{}
for _, m := range metrics {
stat := strings.Join(m.Namespace().Strings()[3:], "/")
metvals[stat] = m.Data()
}
So(len(metrics), ShouldEqual, 28)
val, ok := metvals["rootfs/space_free"]
So(ok, ShouldBeTrue)
So(val, ShouldNotBeNil)
val, ok = metvals["big/space_free"]
So(ok, ShouldBeTrue)
So(val, ShouldNotBeNil)
})
})
Convey("When calling twice", func() {
mts := []plugin.MetricType{
plugin.MetricType{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment