This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1202 from marcin-krolik/kromar-mock2-grpc
gRPC implementation of mock2 collector
- Loading branch information
Showing
8 changed files
with
633 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
plugin/collector/snap-plugin-collector-mock2-grpc/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!-- | ||
http://www.apache.org/licenses/LICENSE-2.0.txt | ||
Copyright 2016 Intel Corporation | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
#PLEASE NOTE: These are not example plugins | ||
|
||
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). | ||
|
||
Curious what plugins are under development? See the `plugin-wishlist` label in [our issue backlog](https://github.com/intelsdi-x/snap/labels/plugin-wishlist). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
http://www.apache.org/licenses/LICENSE-2.0.txt | ||
Copyright 2016 Intel Corporation | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"time" | ||
|
||
// Import the snap plugin library | ||
"github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin" | ||
// Import our collector plugin implementation | ||
"github.com/intelsdi-x/snap/plugin/collector/snap-plugin-collector-mock2-grpc/mock" | ||
) | ||
|
||
const ( | ||
pluginName = "mock-grpc" | ||
pluginVersion = 1 | ||
) | ||
|
||
func main() { | ||
// Provided: | ||
// the definition of the plugin metadata | ||
// the implementation satisfying plugin.CollectorPlugin | ||
|
||
// Start a collector | ||
plugin.StartCollector( | ||
new(mock.Mock), | ||
pluginName, | ||
pluginVersion, | ||
plugin.CacheTTL(100*time.Millisecond), | ||
plugin.RoutingStrategy(plugin.StickyRouter), | ||
) | ||
} |
34 changes: 34 additions & 0 deletions
34
plugin/collector/snap-plugin-collector-mock2-grpc/main_small_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// +build small | ||
|
||
/* | ||
http://www.apache.org/licenses/LICENSE-2.0.txt | ||
Copyright 2016 Intel Corporation | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestMain(t *testing.T) { | ||
Convey("ensure plugin loads and responds", t, func() { | ||
So(func() { main() }, ShouldNotPanic) | ||
}) | ||
} |
60 changes: 60 additions & 0 deletions
60
plugin/collector/snap-plugin-collector-mock2-grpc/main_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// +build legacy | ||
|
||
/* | ||
http://www.apache.org/licenses/LICENSE-2.0.txt | ||
Copyright 2016 Intel Corporation | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/intelsdi-x/snap/control" | ||
"github.com/intelsdi-x/snap/core" | ||
"github.com/intelsdi-x/snap/plugin/helper" | ||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
var ( | ||
PluginName = "snap-plugin-collector-mock2-grpc" | ||
PluginType = "collector" | ||
SnapPath = os.ExpandEnv(os.Getenv("SNAP_PATH")) | ||
PluginPath = path.Join(SnapPath, "plugin", PluginName) | ||
) | ||
|
||
func TestMockPluginLoad(t *testing.T) { | ||
// These tests only work if SNAP_PATH is known. | ||
// It is the responsibility of the testing framework to | ||
// build the plugins first into the build dir. | ||
Convey("make sure plugin has been built", t, func() { | ||
err := helper.CheckPluginBuilt(SnapPath, PluginName) | ||
So(err, ShouldBeNil) | ||
|
||
Convey("ensure plugin loads and responds", func() { | ||
c := control.New(control.GetDefaultConfig()) | ||
c.Start() | ||
rp, _ := core.NewRequestedPlugin(PluginPath) | ||
_, err := c.Load(rp) | ||
|
||
So(err, ShouldBeNil) | ||
}) | ||
|
||
}) | ||
} |
23 changes: 23 additions & 0 deletions
23
plugin/collector/snap-plugin-collector-mock2-grpc/mock/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!-- | ||
http://www.apache.org/licenses/LICENSE-2.0.txt | ||
Copyright 2016 Intel Corporation | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
#PLEASE NOTE: These are not example plugins | ||
|
||
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). | ||
|
||
Curious what plugins are under development? See the `plugin-wishlist` label in [our issue backlog](https://github.com/intelsdi-x/snap/labels/plugin-wishlist). |
185 changes: 185 additions & 0 deletions
185
plugin/collector/snap-plugin-collector-mock2-grpc/mock/mock.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/* | ||
http://www.apache.org/licenses/LICENSE-2.0.txt | ||
Copyright 2016 Intel Corporation | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package mock | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"math/rand" | ||
"time" | ||
|
||
"github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin" | ||
) | ||
|
||
// Mock collector implementation used for testing | ||
type Mock struct { | ||
} | ||
|
||
// list of available hosts | ||
var availableHosts = getAllHostnames() | ||
|
||
// CollectMetrics collects metrics for testing | ||
func (f *Mock) CollectMetrics(mts []plugin.Metric) ([]plugin.Metric, error) { | ||
for _, p := range mts { | ||
log.Printf("collecting %+v\n", p) | ||
} | ||
|
||
rand.Seed(time.Now().UTC().UnixNano()) | ||
metrics := []plugin.Metric{} | ||
for i := range mts { | ||
if _, err := mts[i].Config.GetBool("long_print"); err == nil { | ||
letterBytes := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
longLine := []byte{} | ||
for i := 0; i < 8193; i++ { | ||
longLine = append(longLine, letterBytes[rand.Intn(len(letterBytes))]) | ||
} | ||
fmt.Println(string(longLine)) | ||
} | ||
if _, err := mts[i].Config.GetBool("panic"); err == nil { | ||
panic("Oops!") | ||
} | ||
|
||
if isDynamic, _ := mts[i].Namespace.IsDynamic(); isDynamic { | ||
requestedHosts := []string{} | ||
|
||
if mts[i].Namespace[2].Value == "*" { | ||
// when dynamic element is not specified (equals an asterisk) | ||
// then consider all available hosts as requested hosts | ||
requestedHosts = append(requestedHosts, availableHosts...) | ||
} else { | ||
// when the dynamic element is specified | ||
// then consider this specified host as requested hosts | ||
host := mts[i].Namespace[2].Value | ||
|
||
// check if specified host is available in system | ||
if contains(availableHosts, host) { | ||
requestedHosts = append(requestedHosts, host) | ||
} else { | ||
return nil, fmt.Errorf("requested hostname `%s` is not available (list of available hosts: %s)", host, availableHosts) | ||
} | ||
|
||
} | ||
// collect data for each of requested hosts | ||
for _, host := range requestedHosts { | ||
//generate random data | ||
data := randInt(65, 90) + 1000 | ||
// prepare namespace as a copy of incoming dynamic namespace, | ||
// but with the set value of dynamic element | ||
ns := plugin.CopyNamespace(mts[i].Namespace) | ||
ns[2].Value = host | ||
// metric with set data, ns, timestamp and the version of the plugin | ||
mt := plugin.Metric{ | ||
Data: data, | ||
Namespace: ns, | ||
Timestamp: time.Now(), | ||
Unit: mts[i].Unit, | ||
Version: mts[i].Version, | ||
} | ||
metrics = append(metrics, mt) | ||
} | ||
|
||
} else { | ||
data := randInt(65, 90) + 1000 | ||
mts[i].Data = data | ||
mts[i].Timestamp = time.Now() | ||
metrics = append(metrics, mts[i]) | ||
} | ||
} | ||
return metrics, nil | ||
} | ||
|
||
// GetMetricTypes returns metric types for testing | ||
func (f *Mock) GetMetricTypes(cfg plugin.Config) ([]plugin.Metric, error) { | ||
mts := []plugin.Metric{} | ||
if _, err := cfg.GetBool("test-fail"); err == nil { | ||
return mts, fmt.Errorf("testing") | ||
} | ||
if _, err := cfg.GetBool("test"); err == nil { | ||
mts = append(mts, plugin.Metric{ | ||
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"), | ||
Description: "mock description", | ||
Unit: "mock unit", | ||
}) | ||
} | ||
mts = append(mts, plugin.Metric{ | ||
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"), | ||
Description: "mock description", | ||
Unit: "mock unit", | ||
}) | ||
return mts, nil | ||
} | ||
|
||
// GetConfigPolicy returns a ConfigPolicy for testing | ||
func (f *Mock) GetConfigPolicy() (plugin.ConfigPolicy, error) { | ||
p := plugin.NewConfigPolicy() | ||
|
||
rule1, err := plugin.NewStringRule("name", false, plugin.SetDefaultString("bob")) | ||
if err != nil { | ||
return *p, err | ||
} | ||
|
||
rule2, err := plugin.NewStringRule("password", true) | ||
if err != nil { | ||
return *p, err | ||
} | ||
|
||
p.AddStringRule([]string{"intel", "mock", "foo"}, rule1) | ||
p.AddStringRule([]string{"intel", "mock", "foo"}, rule2) | ||
|
||
return *p, nil | ||
} | ||
|
||
// contains reports whether a given item is found in a slice | ||
func contains(slice []string, item string) bool { | ||
for _, s := range slice { | ||
if s == item { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// getAllHostnames returns all available hostnames ('host0', 'host1', ..., 'host9') | ||
func getAllHostnames() []string { | ||
res := []string{} | ||
for j := 0; j < 10; j++ { | ||
res = append(res, fmt.Sprintf("host%d", j)) | ||
} | ||
return res | ||
} | ||
|
||
// random number generator | ||
func randInt(min int, max int) int { | ||
return min + rand.Intn(max-min) | ||
} |
Oops, something went wrong.