From 2a879472449cded937df3243062906c176e75dec Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Wed, 25 May 2022 21:10:08 +0200 Subject: [PATCH] feat: Add constant 'algorithm' to the mock plugin (#11188) --- plugins/inputs/mock/README.md | 4 ++++ plugins/inputs/mock/mock.go | 10 ++++++++++ plugins/inputs/mock/mock_test.go | 25 +++++++++++++++++++++++++ plugins/inputs/mock/sample.conf | 3 +++ 4 files changed, 42 insertions(+) diff --git a/plugins/inputs/mock/README.md b/plugins/inputs/mock/README.md index cbfe0bd991343..df2fef829d913 100644 --- a/plugins/inputs/mock/README.md +++ b/plugins/inputs/mock/README.md @@ -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 @@ -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 diff --git a/plugins/inputs/mock/mock.go b/plugins/inputs/mock/mock.go index fe5a7c71a6792..920fd23b82aeb 100644 --- a/plugins/inputs/mock/mock.go +++ b/plugins/inputs/mock/mock.go @@ -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"` @@ -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 diff --git a/plugins/inputs/mock/mock_test.go b/plugins/inputs/mock/mock_test.go index a497568114441..070829bc1fd62 100644 --- a/plugins/inputs/mock/mock_test.go +++ b/plugins/inputs/mock/mock_test.go @@ -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, @@ -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}, @@ -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) diff --git a/plugins/inputs/mock/sample.conf b/plugins/inputs/mock/sample.conf index ba0efab422419..03feadf79e3c9 100644 --- a/plugins/inputs/mock/sample.conf +++ b/plugins/inputs/mock/sample.conf @@ -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