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

Commit b3778cf

Browse files
authored
Merge pull request #1202 from marcin-krolik/kromar-mock2-grpc
gRPC implementation of mock2 collector
2 parents 8918200 + 5d31ecb commit b3778cf

File tree

8 files changed

+633
-0
lines changed

8 files changed

+633
-0
lines changed

glide.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,4 @@ import:
5151
version: 0032a855ba5c8a3c8e0d71c2deef354b70af1584
5252
- package: gopkg.in/yaml.v2
5353
version: c1cd2254a6dd314c9d73c338c12688c9325d85c6
54+
- package: github.com/intelsdi-x/snap-plugin-lib-go
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!--
2+
http://www.apache.org/licenses/LICENSE-2.0.txt
3+
4+
5+
Copyright 2016 Intel Corporation
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
-->
19+
#PLEASE NOTE: These are not example plugins
20+
21+
The contents of `plugin/` are used as part of our testing framework. If you are looking for examples of how to write a plugin for Snap, review the [Plugin Authoring documentation](/docs/PLUGIN_AUTHORING.md).
22+
23+
Curious what plugins are under development? See the `plugin-wishlist` label in [our issue backlog](https://github.com/intelsdi-x/snap/labels/plugin-wishlist).
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
http://www.apache.org/licenses/LICENSE-2.0.txt
3+
4+
5+
Copyright 2016 Intel Corporation
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package main
21+
22+
import (
23+
"time"
24+
25+
// Import the snap plugin library
26+
"github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin"
27+
// Import our collector plugin implementation
28+
"github.com/intelsdi-x/snap/plugin/collector/snap-plugin-collector-mock2-grpc/mock"
29+
)
30+
31+
const (
32+
pluginName = "mock-grpc"
33+
pluginVersion = 1
34+
)
35+
36+
func main() {
37+
// Provided:
38+
// the definition of the plugin metadata
39+
// the implementation satisfying plugin.CollectorPlugin
40+
41+
// Start a collector
42+
plugin.StartCollector(
43+
new(mock.Mock),
44+
pluginName,
45+
pluginVersion,
46+
plugin.CacheTTL(100*time.Millisecond),
47+
plugin.RoutingStrategy(plugin.StickyRouter),
48+
)
49+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// +build small
2+
3+
/*
4+
http://www.apache.org/licenses/LICENSE-2.0.txt
5+
6+
7+
Copyright 2016 Intel Corporation
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
http://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
*/
21+
22+
package main
23+
24+
import (
25+
"testing"
26+
27+
. "github.com/smartystreets/goconvey/convey"
28+
)
29+
30+
func TestMain(t *testing.T) {
31+
Convey("ensure plugin loads and responds", t, func() {
32+
So(func() { main() }, ShouldNotPanic)
33+
})
34+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// +build legacy
2+
3+
/*
4+
http://www.apache.org/licenses/LICENSE-2.0.txt
5+
6+
7+
Copyright 2016 Intel Corporation
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
http://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
*/
21+
22+
package main
23+
24+
import (
25+
"os"
26+
"path"
27+
"testing"
28+
29+
"github.com/intelsdi-x/snap/control"
30+
"github.com/intelsdi-x/snap/core"
31+
"github.com/intelsdi-x/snap/plugin/helper"
32+
. "github.com/smartystreets/goconvey/convey"
33+
)
34+
35+
var (
36+
PluginName = "snap-plugin-collector-mock2-grpc"
37+
PluginType = "collector"
38+
SnapPath = os.ExpandEnv(os.Getenv("SNAP_PATH"))
39+
PluginPath = path.Join(SnapPath, "plugin", PluginName)
40+
)
41+
42+
func TestMockPluginLoad(t *testing.T) {
43+
// These tests only work if SNAP_PATH is known.
44+
// It is the responsibility of the testing framework to
45+
// build the plugins first into the build dir.
46+
Convey("make sure plugin has been built", t, func() {
47+
err := helper.CheckPluginBuilt(SnapPath, PluginName)
48+
So(err, ShouldBeNil)
49+
50+
Convey("ensure plugin loads and responds", func() {
51+
c := control.New(control.GetDefaultConfig())
52+
c.Start()
53+
rp, _ := core.NewRequestedPlugin(PluginPath)
54+
_, err := c.Load(rp)
55+
56+
So(err, ShouldBeNil)
57+
})
58+
59+
})
60+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!--
2+
http://www.apache.org/licenses/LICENSE-2.0.txt
3+
4+
5+
Copyright 2016 Intel Corporation
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
-->
19+
#PLEASE NOTE: These are not example plugins
20+
21+
The contents of `plugin/` are used as part of our testing framework. If you are looking for examples of how to write a plugin for Snap, review the [Plugin Authoring documentation](/docs/PLUGIN_AUTHORING.md).
22+
23+
Curious what plugins are under development? See the `plugin-wishlist` label in [our issue backlog](https://github.com/intelsdi-x/snap/labels/plugin-wishlist).
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/*
2+
http://www.apache.org/licenses/LICENSE-2.0.txt
3+
4+
5+
Copyright 2016 Intel Corporation
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package mock
21+
22+
import (
23+
"fmt"
24+
"log"
25+
"math/rand"
26+
"time"
27+
28+
"github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin"
29+
)
30+
31+
// Mock collector implementation used for testing
32+
type Mock struct {
33+
}
34+
35+
// list of available hosts
36+
var availableHosts = getAllHostnames()
37+
38+
// CollectMetrics collects metrics for testing
39+
func (f *Mock) CollectMetrics(mts []plugin.Metric) ([]plugin.Metric, error) {
40+
for _, p := range mts {
41+
log.Printf("collecting %+v\n", p)
42+
}
43+
44+
rand.Seed(time.Now().UTC().UnixNano())
45+
metrics := []plugin.Metric{}
46+
for i := range mts {
47+
if _, err := mts[i].Config.GetBool("long_print"); err == nil {
48+
letterBytes := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
49+
longLine := []byte{}
50+
for i := 0; i < 8193; i++ {
51+
longLine = append(longLine, letterBytes[rand.Intn(len(letterBytes))])
52+
}
53+
fmt.Println(string(longLine))
54+
}
55+
if _, err := mts[i].Config.GetBool("panic"); err == nil {
56+
panic("Oops!")
57+
}
58+
59+
if isDynamic, _ := mts[i].Namespace.IsDynamic(); isDynamic {
60+
requestedHosts := []string{}
61+
62+
if mts[i].Namespace[2].Value == "*" {
63+
// when dynamic element is not specified (equals an asterisk)
64+
// then consider all available hosts as requested hosts
65+
requestedHosts = append(requestedHosts, availableHosts...)
66+
} else {
67+
// when the dynamic element is specified
68+
// then consider this specified host as requested hosts
69+
host := mts[i].Namespace[2].Value
70+
71+
// check if specified host is available in system
72+
if contains(availableHosts, host) {
73+
requestedHosts = append(requestedHosts, host)
74+
} else {
75+
return nil, fmt.Errorf("requested hostname `%s` is not available (list of available hosts: %s)", host, availableHosts)
76+
}
77+
78+
}
79+
// collect data for each of requested hosts
80+
for _, host := range requestedHosts {
81+
//generate random data
82+
data := randInt(65, 90) + 1000
83+
// prepare namespace as a copy of incoming dynamic namespace,
84+
// but with the set value of dynamic element
85+
ns := plugin.CopyNamespace(mts[i].Namespace)
86+
ns[2].Value = host
87+
// metric with set data, ns, timestamp and the version of the plugin
88+
mt := plugin.Metric{
89+
Data: data,
90+
Namespace: ns,
91+
Timestamp: time.Now(),
92+
Unit: mts[i].Unit,
93+
Version: mts[i].Version,
94+
}
95+
metrics = append(metrics, mt)
96+
}
97+
98+
} else {
99+
data := randInt(65, 90) + 1000
100+
mts[i].Data = data
101+
mts[i].Timestamp = time.Now()
102+
metrics = append(metrics, mts[i])
103+
}
104+
}
105+
return metrics, nil
106+
}
107+
108+
// GetMetricTypes returns metric types for testing
109+
func (f *Mock) GetMetricTypes(cfg plugin.Config) ([]plugin.Metric, error) {
110+
mts := []plugin.Metric{}
111+
if _, err := cfg.GetBool("test-fail"); err == nil {
112+
return mts, fmt.Errorf("testing")
113+
}
114+
if _, err := cfg.GetBool("test"); err == nil {
115+
mts = append(mts, plugin.Metric{
116+
Namespace: plugin.NewNamespace("intel", "mock", "test"),
117+
Description: "mock description",
118+
Unit: "mock unit",
119+
})
120+
}
121+
if _, err := cfg.GetBool("test-less"); err != nil {
122+
mts = append(mts, plugin.Metric{
123+
Namespace: plugin.NewNamespace("intel", "mock", "foo"),
124+
Description: "mock description",
125+
Unit: "mock unit",
126+
})
127+
}
128+
mts = append(mts, plugin.Metric{
129+
Namespace: plugin.NewNamespace("intel", "mock", "bar"),
130+
Description: "mock description",
131+
Unit: "mock unit",
132+
})
133+
mts = append(mts, plugin.Metric{
134+
Namespace: plugin.NewNamespace("intel", "mock").
135+
AddDynamicElement("host", "name of the host").
136+
AddStaticElement("baz"),
137+
Description: "mock description",
138+
Unit: "mock unit",
139+
})
140+
return mts, nil
141+
}
142+
143+
// GetConfigPolicy returns a ConfigPolicy for testing
144+
func (f *Mock) GetConfigPolicy() (plugin.ConfigPolicy, error) {
145+
p := plugin.NewConfigPolicy()
146+
147+
rule1, err := plugin.NewStringRule("name", false, plugin.SetDefaultString("bob"))
148+
if err != nil {
149+
return *p, err
150+
}
151+
152+
rule2, err := plugin.NewStringRule("password", true)
153+
if err != nil {
154+
return *p, err
155+
}
156+
157+
p.AddStringRule([]string{"intel", "mock", "foo"}, rule1)
158+
p.AddStringRule([]string{"intel", "mock", "foo"}, rule2)
159+
160+
return *p, nil
161+
}
162+
163+
// contains reports whether a given item is found in a slice
164+
func contains(slice []string, item string) bool {
165+
for _, s := range slice {
166+
if s == item {
167+
return true
168+
}
169+
}
170+
return false
171+
}
172+
173+
// getAllHostnames returns all available hostnames ('host0', 'host1', ..., 'host9')
174+
func getAllHostnames() []string {
175+
res := []string{}
176+
for j := 0; j < 10; j++ {
177+
res = append(res, fmt.Sprintf("host%d", j))
178+
}
179+
return res
180+
}
181+
182+
// random number generator
183+
func randInt(min int, max int) int {
184+
return min + rand.Intn(max-min)
185+
}

0 commit comments

Comments
 (0)