feat: Add constant 'algorithm' to the mock plugin (#11188)
This commit is contained in:
parent
ae98e7f1f6
commit
2a87947244
|
|
@ -28,6 +28,9 @@ Below is a sample config to generate one of each of the four types:
|
||||||
|
|
||||||
## One or more mock data fields *must* be defined.
|
## One or more mock data fields *must* be defined.
|
||||||
##
|
##
|
||||||
|
## [[inputs.mock.constant]]
|
||||||
|
## name = "constant"
|
||||||
|
## value = value_of_any_type
|
||||||
## [[inputs.mock.random]]
|
## [[inputs.mock.random]]
|
||||||
## name = "rand"
|
## name = "rand"
|
||||||
## min = 1.0
|
## min = 1.0
|
||||||
|
|
@ -50,6 +53,7 @@ Below is a sample config to generate one of each of the four types:
|
||||||
|
|
||||||
The available algorithms for generating mock data include:
|
The available algorithms for generating mock data include:
|
||||||
|
|
||||||
|
* Constant - generate a field with the given value of type string, float, int or bool
|
||||||
* Random Float - generate a random float, inclusive of min and max
|
* Random Float - generate a random float, inclusive of min and max
|
||||||
* Sine Wave - produce a sine wave with a certain amplitude and period
|
* Sine Wave - produce a sine wave with a certain amplitude and period
|
||||||
* Step - always add the step value, negative values accepted
|
* Step - always add the step value, negative values accepted
|
||||||
|
|
|
||||||
|
|
@ -21,12 +21,18 @@ type Mock struct {
|
||||||
MetricName string `toml:"metric_name"`
|
MetricName string `toml:"metric_name"`
|
||||||
Tags map[string]string `toml:"tags"`
|
Tags map[string]string `toml:"tags"`
|
||||||
|
|
||||||
|
Constant []*constant `toml:"constant"`
|
||||||
Random []*random `toml:"random"`
|
Random []*random `toml:"random"`
|
||||||
Step []*step `toml:"step"`
|
Step []*step `toml:"step"`
|
||||||
Stock []*stock `toml:"stock"`
|
Stock []*stock `toml:"stock"`
|
||||||
SineWave []*sineWave `toml:"sine_wave"`
|
SineWave []*sineWave `toml:"sine_wave"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type constant struct {
|
||||||
|
Name string `toml:"name"`
|
||||||
|
Value interface{} `toml:"value"`
|
||||||
|
}
|
||||||
|
|
||||||
type random struct {
|
type random struct {
|
||||||
Name string `toml:"name"`
|
Name string `toml:"name"`
|
||||||
Min float64 `toml:"min"`
|
Min float64 `toml:"min"`
|
||||||
|
|
@ -71,6 +77,10 @@ func (m *Mock) Gather(acc telegraf.Accumulator) error {
|
||||||
m.generateSineWave(fields)
|
m.generateSineWave(fields)
|
||||||
m.generateStep(fields)
|
m.generateStep(fields)
|
||||||
|
|
||||||
|
for _, c := range m.Constant {
|
||||||
|
fields[c.Name] = c.Value
|
||||||
|
}
|
||||||
|
|
||||||
tags := make(map[string]string)
|
tags := make(map[string]string)
|
||||||
for key, value := range m.Tags {
|
for key, value := range m.Tags {
|
||||||
tags[key] = value
|
tags[key] = value
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,22 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGather(t *testing.T) {
|
func TestGather(t *testing.T) {
|
||||||
|
testConstantString := &constant{
|
||||||
|
Name: "constant_string",
|
||||||
|
Value: "a string",
|
||||||
|
}
|
||||||
|
testConstantFloat := &constant{
|
||||||
|
Name: "constant_float",
|
||||||
|
Value: 3.1415,
|
||||||
|
}
|
||||||
|
testConstantInt := &constant{
|
||||||
|
Name: "constant_int",
|
||||||
|
Value: 42,
|
||||||
|
}
|
||||||
|
testConstantBool := &constant{
|
||||||
|
Name: "constant_bool",
|
||||||
|
Value: true,
|
||||||
|
}
|
||||||
testRandom := &random{
|
testRandom := &random{
|
||||||
Name: "random",
|
Name: "random",
|
||||||
Min: 1.0,
|
Min: 1.0,
|
||||||
|
|
@ -38,6 +54,7 @@ func TestGather(t *testing.T) {
|
||||||
MetricName: "test",
|
MetricName: "test",
|
||||||
Tags: tags,
|
Tags: tags,
|
||||||
|
|
||||||
|
Constant: []*constant{testConstantString, testConstantFloat, testConstantInt, testConstantBool},
|
||||||
Random: []*random{testRandom},
|
Random: []*random{testRandom},
|
||||||
SineWave: []*sineWave{testSineWave},
|
SineWave: []*sineWave{testSineWave},
|
||||||
Step: []*step{testStep},
|
Step: []*step{testStep},
|
||||||
|
|
@ -56,6 +73,14 @@ func TestGather(t *testing.T) {
|
||||||
switch k {
|
switch k {
|
||||||
case "abc":
|
case "abc":
|
||||||
require.Equal(t, 50.0, v)
|
require.Equal(t, 50.0, v)
|
||||||
|
case "constant_string":
|
||||||
|
require.Equal(t, testConstantString.Value, v)
|
||||||
|
case "constant_float":
|
||||||
|
require.Equal(t, testConstantFloat.Value, v)
|
||||||
|
case "constant_int":
|
||||||
|
require.Equal(t, testConstantInt.Value, v)
|
||||||
|
case "constant_bool":
|
||||||
|
require.Equal(t, testConstantBool.Value, v)
|
||||||
case "random":
|
case "random":
|
||||||
require.GreaterOrEqual(t, 6.0, v)
|
require.GreaterOrEqual(t, 6.0, v)
|
||||||
require.LessOrEqual(t, 1.0, v)
|
require.LessOrEqual(t, 1.0, v)
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,9 @@
|
||||||
|
|
||||||
## One or more mock data fields *must* be defined.
|
## One or more mock data fields *must* be defined.
|
||||||
##
|
##
|
||||||
|
## [[inputs.mock.constant]]
|
||||||
|
## name = "constant"
|
||||||
|
## value = value_of_any_type
|
||||||
## [[inputs.mock.random]]
|
## [[inputs.mock.random]]
|
||||||
## name = "rand"
|
## name = "rand"
|
||||||
## min = 1.0
|
## min = 1.0
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue