Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add constant 'algorithm' to the mock plugin #11188

Merged
merged 4 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions plugins/inputs/mock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
##
## [[inputs.mock.constant]]
## name = "constant"
## value = value_of_any_type
## [[inputs.mock.random]]
## name = "rand"
## min = 1.0
Expand All @@ -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:

* 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
* Sine Wave - produce a sine wave with a certain amplitude and period
* Step - always add the step value, negative values accepted
Expand Down
10 changes: 10 additions & 0 deletions plugins/inputs/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ type Mock struct {
MetricName string `toml:"metric_name"`
Tags map[string]string `toml:"tags"`

Constant []*constant `toml:"constant"`
Random []*random `toml:"random"`
Step []*step `toml:"step"`
Stock []*stock `toml:"stock"`
SineWave []*sineWave `toml:"sine_wave"`
}

type constant struct {
Name string `toml:"name"`
Value interface{} `toml:"value"`
}

type random struct {
Name string `toml:"name"`
Min float64 `toml:"min"`
Expand Down Expand Up @@ -71,6 +77,10 @@ func (m *Mock) Gather(acc telegraf.Accumulator) error {
m.generateSineWave(fields)
m.generateStep(fields)

for _, c := range m.Constant {
fields[c.Name] = c.Value
}

tags := make(map[string]string)
for key, value := range m.Tags {
tags[key] = value
Expand Down
25 changes: 25 additions & 0 deletions plugins/inputs/mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ import (
)

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{
Name: "random",
Min: 1.0,
Expand Down Expand Up @@ -38,6 +54,7 @@ func TestGather(t *testing.T) {
MetricName: "test",
Tags: tags,

Constant: []*constant{testConstantString, testConstantFloat, testConstantInt, testConstantBool},
Random: []*random{testRandom},
SineWave: []*sineWave{testSineWave},
Step: []*step{testStep},
Expand All @@ -56,6 +73,14 @@ func TestGather(t *testing.T) {
switch k {
case "abc":
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":
require.GreaterOrEqual(t, 6.0, v)
require.LessOrEqual(t, 1.0, v)
Expand Down
3 changes: 3 additions & 0 deletions plugins/inputs/mock/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

## One or more mock data fields *must* be defined.
##
## [[inputs.mock.constant]]
## name = "constant"
## value = value_of_any_type
## [[inputs.mock.random]]
## name = "rand"
## min = 1.0
Expand Down