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 40
/
Copy pathrand.go
206 lines (178 loc) · 5.78 KB
/
rand.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
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 rand
import (
"fmt"
"math/rand"
"time"
"errors"
"github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin"
"github.com/urfave/cli"
)
var (
strs = []string{
"It is certain",
"It is decidedly so",
"Without a doubt",
"Yes definitely",
"You may rely on it",
"As I see it yes",
"Most likely",
"Outlook good",
"Yes",
"Signs point to yes",
"Reply hazy try again",
"Ask again later",
"Better not tell you now",
"Cannot predict now",
"Concentrate and ask again",
"Don't count on it",
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful",
}
)
var req bool = false
func init() {
rand.Seed(42)
//The required-config flag is added for testing plugin-lib-go.
//When the flag is set, an additional policy will be added in GetConfigPolicy().
//This additional policy has a required field. This simulates
//the situation when a plugin requires a config to load.
plugin.Flags = append(plugin.Flags, cli.BoolFlag{
Name: "required-config",
Hidden: false,
Usage: "Plugin requires config passed in",
Destination: &req,
})
}
// Mock collector implementation used for testing
type RandCollector struct {
}
/* CollectMetrics collects metrics for testing.
CollectMetrics() will be called by Snap when a task that collects one of the metrics returned from this plugins
GetMetricTypes() is started. The input will include a slice of all the metric types being collected.
The output is the collected metrics as plugin.Metric and an error.
*/
func (RandCollector) CollectMetrics(mts []plugin.Metric) ([]plugin.Metric, error) {
metrics := []plugin.Metric{}
for idx, mt := range mts {
mts[idx].Timestamp = time.Now()
if val, err := mt.Config.GetBool("return_error"); err == nil && val {
return nil, errors.New("Houston, we have a problem!")
}
if val, err := mt.Config.GetBool("testbool"); err == nil && val {
continue
}
if mt.Namespace[0].Value == "static" {
var err error
mts[idx].Data, err = mts[idx].Config.GetString("value")
if err != nil {
return nil, fmt.Errorf("Invalid or missing value key.")
}
metrics = append(metrics, mts[idx])
} else if mt.Namespace[len(mt.Namespace)-1].Value == "integer" {
if val, err := mt.Config.GetInt("testint"); err == nil {
mts[idx].Data = val
} else {
mts[idx].Data = rand.Int31()
}
metrics = append(metrics, mts[idx])
} else if mt.Namespace[len(mt.Namespace)-1].Value == "float" {
if val, err := mt.Config.GetFloat("testfloat"); err == nil {
mts[idx].Data = val
} else {
mts[idx].Data = rand.Float64()
}
metrics = append(metrics, mts[idx])
} else if mt.Namespace[len(mt.Namespace)-1].Value == "string" {
if val, err := mt.Config.GetString("teststring"); err == nil {
mts[idx].Data = val
} else {
mts[idx].Data = strs[rand.Intn(len(strs)-1)]
}
metrics = append(metrics, mts[idx])
} else {
return nil, fmt.Errorf("Invalid metric: %v", mt.Namespace.Strings())
}
}
return metrics, nil
}
/*
GetMetricTypes returns metric types for testing.
GetMetricTypes() will be called when your plugin is loaded in order to populate the metric catalog(where snaps stores all
available metrics).
Config info is passed in. This config information would come from global config snap settings.
The metrics returned will be advertised to users who list all the metrics and will become targetable by tasks.
*/
func (RandCollector) GetMetricTypes(cfg plugin.Config) ([]plugin.Metric, error) {
//Simulate throwing error when a config is required but not passed in
if req && len(cfg) < 1 {
return nil, fmt.Errorf("! When -required-config is set you must provide a config. Example: -config '{\"key\":\"kelly\", \"spirit-animal\":\"coatimundi\"}'\n")
}
//Simulate throwing error when dependencies not met
if _, ok := cfg["depsReq"]; ok {
return nil, fmt.Errorf("! Dependency XX required. Run `make deps` to resolve.")
}
metrics := []plugin.Metric{}
vals := []string{"integer", "float", "string"}
for _, val := range vals {
metric := plugin.Metric{
Namespace: plugin.NewNamespace("random", val),
}
metrics = append(metrics, metric)
}
if req {
metrics = append(metrics, plugin.Metric{
Namespace: plugin.NewNamespace("static", "string"),
})
}
return metrics, nil
}
/*
GetConfigPolicy() returns the configPolicy for your plugin.
A config policy is how users can provide configuration info to
plugin. Here you define what sorts of config info your plugin
needs and/or requires.
*/
func (RandCollector) GetConfigPolicy() (plugin.ConfigPolicy, error) {
policy := plugin.NewConfigPolicy()
//The required-config flag is used for testing plugin-lib-go
if req {
policy.AddNewStringRule([]string{"static", "string"},
"value",
true,
)
}
policy.AddNewIntRule([]string{"random", "integer"},
"testint",
false,
plugin.SetMaxInt(1000),
plugin.SetMinInt(0))
policy.AddNewFloatRule([]string{"random", "float"},
"testfloat",
false,
plugin.SetMaxFloat(1000.0),
plugin.SetMinFloat(0.0))
policy.AddNewStringRule([]string{"random", "string"},
"teststring",
false,
plugin.SetDefaultString("some_string"))
policy.AddNewBoolRule([]string{"random"},
"testbool",
false,
plugin.SetDefaultBool(false))
return *policy, nil
}