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

Commit fbde67a

Browse files
committed
gRPC version of mock-file publisher
1 parent 0b228f5 commit fbde67a

6 files changed

Lines changed: 303 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!--
2+
http://www.apache.org/licenses/LICENSE-2.0.txt
3+
4+
5+
Copyright 2015 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+
-->
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
http://www.apache.org/licenses/LICENSE-2.0.txt
3+
4+
5+
Copyright 2015 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 file
21+
22+
import (
23+
"bufio"
24+
"fmt"
25+
"os"
26+
"strings"
27+
28+
log "github.com/Sirupsen/logrus"
29+
30+
"github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin"
31+
)
32+
33+
const (
34+
debug = "debug"
35+
)
36+
37+
type filePublisher struct {
38+
}
39+
40+
func NewFilePublisher() *filePublisher {
41+
return &filePublisher{}
42+
}
43+
44+
func (f *filePublisher) Publish(metrics []plugin.Metric, config plugin.Config) error {
45+
log.SetFormatter(&log.TextFormatter{DisableTimestamp: true})
46+
if _, err := config.GetBool(debug); err == nil {
47+
log.SetLevel(log.DebugLevel)
48+
} else {
49+
log.SetLevel(log.InfoLevel)
50+
}
51+
log.Debug("publishing started")
52+
53+
filename, err := config.GetString("file")
54+
if err != nil {
55+
log.Error(err)
56+
return err
57+
}
58+
59+
file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
60+
defer file.Close()
61+
if err != nil {
62+
log.Error(err)
63+
return err
64+
}
65+
log.WithFields(log.Fields{
66+
"file": filename,
67+
"metrics-published-count": len(metrics),
68+
}).Debug("metrics published")
69+
w := bufio.NewWriter(file)
70+
for _, m := range metrics {
71+
formattedTags := formatMetricTagsAsString(m.Tags)
72+
w.WriteString(fmt.Sprintf("%v|%v|%v|%v\n", m.Timestamp, m.Namespace, m.Data, formattedTags))
73+
}
74+
w.Flush()
75+
76+
return nil
77+
}
78+
79+
func (f *filePublisher) GetConfigPolicy() (plugin.ConfigPolicy, error) {
80+
policy := plugin.NewConfigPolicy()
81+
rule1, err := plugin.NewStringRule("file", true)
82+
handleErr(err)
83+
84+
rule2, err := plugin.NewBoolRule(debug, false)
85+
handleErr(err)
86+
87+
policy.AddStringRule([]string{""}, rule1)
88+
policy.AddBoolRule([]string{}, rule2)
89+
90+
return *policy, nil
91+
}
92+
93+
// formatMetricTagsAsString returns metric's tags as a string in the following format tagKey:tagValue where the next tags are separated by semicolon
94+
func formatMetricTagsAsString(metricTags map[string]string) string {
95+
var tags string
96+
for tag, value := range metricTags {
97+
tags += fmt.Sprintf("%s:%s; ", tag, value)
98+
}
99+
// trim the last semicolon
100+
tags = strings.TrimSuffix(tags, "; ")
101+
102+
return "tags[" + tags + "]"
103+
}
104+
105+
func handleErr(e error) {
106+
if e != nil {
107+
panic(e)
108+
}
109+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 file
23+
24+
import (
25+
"os"
26+
"testing"
27+
"time"
28+
29+
"github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin"
30+
31+
. "github.com/smartystreets/goconvey/convey"
32+
)
33+
34+
func TestFilePublish(t *testing.T) {
35+
metrics := []plugin.Metric{plugin.Metric{Namespace: plugin.NewNamespace("foo"), Timestamp: time.Now(), Data: 99, Tags: nil}}
36+
config := plugin.Config{"file": "/tmp/pub.out"}
37+
Convey("TestFilePublish", t, func() {
38+
fp := NewFilePublisher()
39+
So(fp, ShouldNotBeNil)
40+
err := fp.Publish(metrics, config)
41+
So(err, ShouldBeNil)
42+
filename, _ := config.GetString("file")
43+
_, err = os.Stat(filename)
44+
So(err, ShouldBeNil)
45+
})
46+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
http://www.apache.org/licenses/LICENSE-2.0.txt
3+
4+
5+
Copyright 2015 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+
"github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin"
24+
"github.com/intelsdi-x/snap/plugin/publisher/snap-plugin-publisher-mock-file-grpc/file"
25+
)
26+
27+
const (
28+
pluginName = "mock-file-grpc"
29+
pluginVersion = 1
30+
)
31+
32+
func main() {
33+
plugin.StartPublisher(file.NewFilePublisher(), pluginName, pluginVersion)
34+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// +build small
2+
3+
/*
4+
http://www.apache.org/licenses/LICENSE-2.0.txt
5+
6+
7+
Copyright 2015 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+
"testing"
27+
28+
. "github.com/smartystreets/goconvey/convey"
29+
)
30+
31+
func TestMain(t *testing.T) {
32+
Convey("ensure plugin loads and responds", t, func() {
33+
os.Args = []string{"", "{\"NoDaemon\": true}"}
34+
So(func() { main() }, ShouldNotPanic)
35+
})
36+
}
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 2015 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-publisher-mock-file"
37+
PluginType = "publisher"
38+
SnapPath = os.Getenv("SNAP_PATH")
39+
PluginPath = path.Join(SnapPath, "plugin", PluginName)
40+
)
41+
42+
func TestFilePublisherLoad(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+
47+
Convey("make sure plugin has been built", t, func() {
48+
err := helper.CheckPluginBuilt(SnapPath, PluginName)
49+
So(err, ShouldBeNil)
50+
51+
Convey("ensure plugin loads and responds", func() {
52+
c := control.New(control.GetDefaultConfig())
53+
c.Start()
54+
rp, _ := core.NewRequestedPlugin(PluginPath)
55+
_, err := c.Load(rp)
56+
57+
So(err, ShouldBeNil)
58+
})
59+
})
60+
}

0 commit comments

Comments
 (0)