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

Commit

Permalink
gRPC implementation of mock2 collector
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-krolik committed Sep 13, 2016
1 parent 9a05d66 commit 0922dec
Show file tree
Hide file tree
Showing 7 changed files with 639 additions and 0 deletions.
23 changes: 23 additions & 0 deletions plugin/collector/snap-plugin-collector-mock2-grpc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!--
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 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).
49 changes: 49 additions & 0 deletions plugin/collector/snap-plugin-collector-mock2-grpc/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 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),
)
}
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 2015 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 plugin/collector/snap-plugin-collector-mock2-grpc/main_test.go
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 2015 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 plugin/collector/snap-plugin-collector-mock2-grpc/mock/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!--
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 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).
192 changes: 192 additions & 0 deletions plugin/collector/snap-plugin-collector-mock2-grpc/mock/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 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.NewNamespace()
for j, ne := range mts[i].Namespace {
if ne.IsDynamic() {
ns = ns.AddDynamicElement(ne.Name, ne.Description)
ns[j].Value = host
} else {
ns = ns.AddStaticElement(ne.Value)
}
}
// 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)
}
Loading

0 comments on commit 0922dec

Please sign in to comment.