From 0f264d64ce6d4b54c97f0dab8b4d372c623af3ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20=C5=BBak?= Date: Wed, 25 Oct 2023 23:02:46 +0200 Subject: [PATCH 1/7] chore(linters): Fix findings found by testifylint: error-is-as (#14180) --- plugins/inputs/beat/beat_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/inputs/beat/beat_test.go b/plugins/inputs/beat/beat_test.go index 433e8fcd61337..2ca2ffba0a149 100644 --- a/plugins/inputs/beat/beat_test.go +++ b/plugins/inputs/beat/beat_test.go @@ -44,7 +44,7 @@ func Test_BeatStats(t *testing.T) { fakeServer.Start() defer fakeServer.Close() - require.NoError(t, err, beatTest.Gather(&beat6StatsAccumulator)) + require.NoError(t, beatTest.Gather(&beat6StatsAccumulator)) beat6StatsAccumulator.AssertContainsTaggedFields( t, From 12065fd15212ae0f11d7ebc31183b230f5146525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20=C5=BBak?= Date: Wed, 25 Oct 2023 23:04:00 +0200 Subject: [PATCH 2/7] chore(linters): Fix findings found by testifylint: require-error (#14181) --- agent/accumulator_test.go | 16 ++-- filter/filter_test.go | 42 +++++------ internal/templating/engine_test.go | 7 +- models/running_input_test.go | 7 +- .../inputs/cloud_pubsub/cloud_pubsub_test.go | 3 +- plugins/inputs/intel_rdt/publisher_test.go | 75 ++++++++++--------- plugins/inputs/nginx_plus/nginx_plus_test.go | 4 +- plugins/inputs/p4runtime/p4runtime_test.go | 7 +- .../outputs/postgresql/table_manager_test.go | 47 ++++++------ selfstat/selfstat_test.go | 44 +++++------ testutil/accumulator.go | 12 +-- 11 files changed, 130 insertions(+), 134 deletions(-) diff --git a/agent/accumulator_test.go b/agent/accumulator_test.go index 38a7e047c9bcc..d8a697eaf0152 100644 --- a/agent/accumulator_test.go +++ b/agent/accumulator_test.go @@ -8,10 +8,10 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" + "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/models" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestAddFields(t *testing.T) { @@ -61,12 +61,12 @@ func TestAccAddError(t *testing.T) { errs := bytes.Split(errBuf.Bytes(), []byte{'\n'}) require.Len(t, errs, 4) // 4 because of trailing newline - assert.Contains(t, string(errs[0]), "TestPlugin") - assert.Contains(t, string(errs[0]), "foo") - assert.Contains(t, string(errs[1]), "TestPlugin") - assert.Contains(t, string(errs[1]), "bar") - assert.Contains(t, string(errs[2]), "TestPlugin") - assert.Contains(t, string(errs[2]), "baz") + require.Contains(t, string(errs[0]), "TestPlugin") + require.Contains(t, string(errs[0]), "foo") + require.Contains(t, string(errs[1]), "TestPlugin") + require.Contains(t, string(errs[1]), "bar") + require.Contains(t, string(errs[2]), "TestPlugin") + require.Contains(t, string(errs[2]), "baz") } func TestSetPrecision(t *testing.T) { diff --git a/filter/filter_test.go b/filter/filter_test.go index 18ebcd795b0ef..78918948a8998 100644 --- a/filter/filter_test.go +++ b/filter/filter_test.go @@ -3,38 +3,38 @@ package filter import ( "testing" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestCompile(t *testing.T) { f, err := Compile([]string{}) - assert.NoError(t, err) - assert.Nil(t, f) + require.NoError(t, err) + require.Nil(t, f) f, err = Compile([]string{"cpu"}) - assert.NoError(t, err) - assert.True(t, f.Match("cpu")) - assert.False(t, f.Match("cpu0")) - assert.False(t, f.Match("mem")) + require.NoError(t, err) + require.True(t, f.Match("cpu")) + require.False(t, f.Match("cpu0")) + require.False(t, f.Match("mem")) f, err = Compile([]string{"cpu*"}) - assert.NoError(t, err) - assert.True(t, f.Match("cpu")) - assert.True(t, f.Match("cpu0")) - assert.False(t, f.Match("mem")) + require.NoError(t, err) + require.True(t, f.Match("cpu")) + require.True(t, f.Match("cpu0")) + require.False(t, f.Match("mem")) f, err = Compile([]string{"cpu", "mem"}) - assert.NoError(t, err) - assert.True(t, f.Match("cpu")) - assert.False(t, f.Match("cpu0")) - assert.True(t, f.Match("mem")) + require.NoError(t, err) + require.True(t, f.Match("cpu")) + require.False(t, f.Match("cpu0")) + require.True(t, f.Match("mem")) f, err = Compile([]string{"cpu", "mem", "net*"}) - assert.NoError(t, err) - assert.True(t, f.Match("cpu")) - assert.False(t, f.Match("cpu0")) - assert.True(t, f.Match("mem")) - assert.True(t, f.Match("network")) + require.NoError(t, err) + require.True(t, f.Match("cpu")) + require.False(t, f.Match("cpu0")) + require.True(t, f.Match("mem")) + require.True(t, f.Match("network")) } func TestIncludeExclude(t *testing.T) { @@ -52,7 +52,7 @@ func TestIncludeExclude(t *testing.T) { } } - assert.Equal(t, []string{"best", "timeseries", "ever"}, tags) + require.Equal(t, []string{"best", "timeseries", "ever"}, tags) } var benchbool bool diff --git a/internal/templating/engine_test.go b/internal/templating/engine_test.go index 0dfcb89d8d823..1246db24de997 100644 --- a/internal/templating/engine_test.go +++ b/internal/templating/engine_test.go @@ -3,7 +3,6 @@ package templating import ( "testing" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -69,9 +68,9 @@ func TestEngineWithWildcardTemplate(t *testing.T) { measurement, tags, field, err := engine.Apply(testCase.line) require.NoError(t, err) - assert.Equal(t, testCase.measurement, measurement) - assert.Equal(t, testCase.field, field) - assert.Equal(t, testCase.tags, tags) + require.Equal(t, testCase.measurement, measurement) + require.Equal(t, testCase.field, field) + require.Equal(t, testCase.tags, tags) }) } } diff --git a/models/running_input_test.go b/models/running_input_test.go index eff0425d10c3e..877cc2bd39908 100644 --- a/models/running_input_test.go +++ b/models/running_input_test.go @@ -4,13 +4,12 @@ import ( "testing" "time" - "github.com/influxdata/telegraf/selfstat" + "github.com/stretchr/testify/require" "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/metric" + "github.com/influxdata/telegraf/selfstat" "github.com/influxdata/telegraf/testutil" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestMakeMetricFilterAfterApplyingGlobalTags(t *testing.T) { @@ -127,7 +126,7 @@ func TestMakeMetricFilteredOut(t *testing.T) { Filter: Filter{NamePass: []string{"foobar"}}, }) - assert.NoError(t, ri.Config.Filter.Compile()) + require.NoError(t, ri.Config.Filter.Compile()) m := metric.New("RITest", map[string]string{}, diff --git a/plugins/inputs/cloud_pubsub/cloud_pubsub_test.go b/plugins/inputs/cloud_pubsub/cloud_pubsub_test.go index a63fed21d3696..6da36a2c5f2f1 100644 --- a/plugins/inputs/cloud_pubsub/cloud_pubsub_test.go +++ b/plugins/inputs/cloud_pubsub/cloud_pubsub_test.go @@ -5,7 +5,6 @@ import ( "errors" "testing" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/influxdata/telegraf/internal" @@ -152,7 +151,7 @@ func TestRunGzipDecode(t *testing.T) { } sub.messages <- msg acc.Wait(1) - assert.Equal(t, acc.NFields(), 1) + require.Equal(t, acc.NFields(), 1) metric := acc.Metrics[0] validateTestInfluxMetric(t, metric) } diff --git a/plugins/inputs/intel_rdt/publisher_test.go b/plugins/inputs/intel_rdt/publisher_test.go index 844873db08910..d8c51323a750e 100644 --- a/plugins/inputs/intel_rdt/publisher_test.go +++ b/plugins/inputs/intel_rdt/publisher_test.go @@ -7,8 +7,9 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" + "github.com/influxdata/telegraf/testutil" - "github.com/stretchr/testify/assert" ) var metricsValues = map[string]float64{ @@ -40,25 +41,25 @@ func TestParseCoresMeasurement(t *testing.T) { result, err := parseCoresMeasurement(measurement) - assert.Nil(t, err) - assert.Equal(t, expectedCores, result.cores) - assert.Equal(t, expectedTimestamp, result.time) - assert.Equal(t, result.values[0], metricsValues["IPC"]) - assert.Equal(t, result.values[1], metricsValues["LLC_Misses"]) - assert.Equal(t, result.values[2], metricsValues["LLC"]) - assert.Equal(t, result.values[3], metricsValues["MBL"]) - assert.Equal(t, result.values[4], metricsValues["MBR"]) - assert.Equal(t, result.values[5], metricsValues["MBT"]) + require.Nil(t, err) + require.Equal(t, expectedCores, result.cores) + require.Equal(t, expectedTimestamp, result.time) + require.Equal(t, result.values[0], metricsValues["IPC"]) + require.Equal(t, result.values[1], metricsValues["LLC_Misses"]) + require.Equal(t, result.values[2], metricsValues["LLC"]) + require.Equal(t, result.values[3], metricsValues["MBL"]) + require.Equal(t, result.values[4], metricsValues["MBR"]) + require.Equal(t, result.values[5], metricsValues["MBT"]) }) t.Run("not valid measurement string", func(t *testing.T) { measurement := "not, valid, measurement" result, err := parseCoresMeasurement(measurement) - assert.NotNil(t, err) - assert.Equal(t, "", result.cores) - assert.Nil(t, result.values) - assert.Equal(t, time.Time{}, result.time) + require.NotNil(t, err) + require.Equal(t, "", result.cores) + require.Nil(t, result.values) + require.Equal(t, time.Time{}, result.time) }) t.Run("not valid values string", func(t *testing.T) { measurement := fmt.Sprintf("%s,%s,%s,%s,%f,%f,%f,%f", @@ -73,10 +74,10 @@ func TestParseCoresMeasurement(t *testing.T) { result, err := parseCoresMeasurement(measurement) - assert.NotNil(t, err) - assert.Equal(t, "", result.cores) - assert.Nil(t, result.values) - assert.Equal(t, time.Time{}, result.time) + require.NotNil(t, err) + require.Equal(t, "", result.cores) + require.Nil(t, result.values) + require.Equal(t, time.Time{}, result.time) }) t.Run("not valid timestamp format", func(t *testing.T) { invalidTimestamp := "2020-08-12-21 13:34:" @@ -92,10 +93,10 @@ func TestParseCoresMeasurement(t *testing.T) { result, err := parseCoresMeasurement(measurement) - assert.NotNil(t, err) - assert.Equal(t, "", result.cores) - assert.Nil(t, result.values) - assert.Equal(t, time.Time{}, result.time) + require.NotNil(t, err) + require.Equal(t, "", result.cores) + require.Nil(t, result.values) + require.Equal(t, time.Time{}, result.time) }) } @@ -126,16 +127,16 @@ func TestParseProcessesMeasurement(t *testing.T) { } result, err := parseProcessesMeasurement(newMeasurement) - assert.Nil(t, err) - assert.Equal(t, processName, result.process) - assert.Equal(t, expectedCores, result.cores) - assert.Equal(t, expectedTimestamp, result.time) - assert.Equal(t, result.values[0], metricsValues["IPC"]) - assert.Equal(t, result.values[1], metricsValues["LLC_Misses"]) - assert.Equal(t, result.values[2], metricsValues["LLC"]) - assert.Equal(t, result.values[3], metricsValues["MBL"]) - assert.Equal(t, result.values[4], metricsValues["MBR"]) - assert.Equal(t, result.values[5], metricsValues["MBT"]) + require.Nil(t, err) + require.Equal(t, processName, result.process) + require.Equal(t, expectedCores, result.cores) + require.Equal(t, expectedTimestamp, result.time) + require.Equal(t, result.values[0], metricsValues["IPC"]) + require.Equal(t, result.values[1], metricsValues["LLC_Misses"]) + require.Equal(t, result.values[2], metricsValues["LLC"]) + require.Equal(t, result.values[3], metricsValues["MBL"]) + require.Equal(t, result.values[4], metricsValues["MBR"]) + require.Equal(t, result.values[5], metricsValues["MBT"]) }) invalidTimestamp := "2020-20-20-31" @@ -185,11 +186,11 @@ func TestParseProcessesMeasurement(t *testing.T) { } result, err := parseProcessesMeasurement(newMeasurement) - assert.NotNil(t, err) - assert.Equal(t, "", result.process) - assert.Equal(t, "", result.cores) - assert.Nil(t, result.values) - assert.Equal(t, time.Time{}, result.time) + require.NotNil(t, err) + require.Equal(t, "", result.process) + require.Equal(t, "", result.cores) + require.Nil(t, result.values) + require.Equal(t, time.Time{}, result.time) }) } } diff --git a/plugins/inputs/nginx_plus/nginx_plus_test.go b/plugins/inputs/nginx_plus/nginx_plus_test.go index 36fe5a2dce8f6..24aa708d567d1 100644 --- a/plugins/inputs/nginx_plus/nginx_plus_test.go +++ b/plugins/inputs/nginx_plus/nginx_plus_test.go @@ -8,9 +8,9 @@ import ( "net/url" "testing" - "github.com/influxdata/telegraf/testutil" - //"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/influxdata/telegraf/testutil" ) const sampleStatusResponse = ` diff --git a/plugins/inputs/p4runtime/p4runtime_test.go b/plugins/inputs/p4runtime/p4runtime_test.go index 0348b37a2ec4d..fe1c3f89dfc92 100644 --- a/plugins/inputs/p4runtime/p4runtime_test.go +++ b/plugins/inputs/p4runtime/p4runtime_test.go @@ -10,7 +10,6 @@ import ( p4ConfigV1 "github.com/p4lang/p4runtime/go/p4/config/v1" p4v1 "github.com/p4lang/p4runtime/go/p4/v1" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" @@ -568,7 +567,7 @@ func TestFailReadCounterEntryFromEntry(t *testing.T) { var acc testutil.Accumulator require.NoError(t, plugin.Gather(&acc)) - assert.Equal( + require.Equal( t, acc.Errors[0], errors.New("reading counter entry from entry table_entry:<> failed"), @@ -615,7 +614,7 @@ func TestFailReadAllEntries(t *testing.T) { var acc testutil.Accumulator require.NoError(t, plugin.Gather(&acc)) - assert.Equal( + require.Equal( t, acc.Errors[0], fmt.Errorf("reading counter entries with ID=1111 failed with error: %w", @@ -640,7 +639,7 @@ func TestFilterCounterNamesInclude(t *testing.T) { counterNamesInclude := []string{"bar"} filteredCounters := filterCounters(counters, counterNamesInclude) - assert.Equal( + require.Equal( t, filteredCounters, []*p4ConfigV1.Counter{ diff --git a/plugins/outputs/postgresql/table_manager_test.go b/plugins/outputs/postgresql/table_manager_test.go index ef110ce1e1ff0..baaa1f112135a 100644 --- a/plugins/outputs/postgresql/table_manager_test.go +++ b/plugins/outputs/postgresql/table_manager_test.go @@ -4,7 +4,6 @@ import ( "strings" "testing" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/influxdata/telegraf" @@ -35,11 +34,11 @@ func TestTableManagerIntegration_EnsureStructure(t *testing.T) { nil, ) require.NoError(t, err) - assert.Empty(t, missingCols) + require.Empty(t, missingCols) tblCols := p.tableManager.table(t.Name()).columns - assert.EqualValues(t, cols[0], tblCols["foo"]) - assert.EqualValues(t, cols[1], tblCols["baz"]) + require.EqualValues(t, cols[0], tblCols["foo"]) + require.EqualValues(t, cols[1], tblCols["baz"]) } func TestTableManagerIntegration_EnsureStructure_alter(t *testing.T) { @@ -78,12 +77,12 @@ func TestTableManagerIntegration_EnsureStructure_alter(t *testing.T) { nil, ) require.NoError(t, err) - assert.Empty(t, missingCols) + require.Empty(t, missingCols) tblCols := p.tableManager.table(t.Name()).columns - assert.EqualValues(t, cols[0], tblCols["foo"]) - assert.EqualValues(t, cols[1], tblCols["bar"]) - assert.EqualValues(t, cols[2], tblCols["baz"]) + require.EqualValues(t, cols[0], tblCols["foo"]) + require.EqualValues(t, cols[1], tblCols["bar"]) + require.EqualValues(t, cols[2], tblCols["baz"]) } func TestTableManagerIntegration_EnsureStructure_overflowTableName(t *testing.T) { @@ -109,8 +108,8 @@ func TestTableManagerIntegration_EnsureStructure_overflowTableName(t *testing.T) nil, ) require.Error(t, err) - assert.Contains(t, err.Error(), "table name too long") - assert.False(t, isTempError(err)) + require.Contains(t, err.Error(), "table name too long") + require.False(t, isTempError(err)) } func TestTableManagerIntegration_EnsureStructure_overflowTagName(t *testing.T) { @@ -137,7 +136,7 @@ func TestTableManagerIntegration_EnsureStructure_overflowTagName(t *testing.T) { nil, ) require.Error(t, err) - assert.False(t, isTempError(err)) + require.False(t, isTempError(err)) } func TestTableManagerIntegration_EnsureStructure_overflowFieldName(t *testing.T) { @@ -164,8 +163,8 @@ func TestTableManagerIntegration_EnsureStructure_overflowFieldName(t *testing.T) nil, ) require.NoError(t, err) - assert.Len(t, missingCols, 1) - assert.Equal(t, cols[1], missingCols[0]) + require.Len(t, missingCols, 1) + require.Equal(t, cols[1], missingCols[0]) } func TestTableManagerIntegration_getColumns(t *testing.T) { @@ -198,8 +197,8 @@ func TestTableManagerIntegration_getColumns(t *testing.T) { curCols, err := p.tableManager.getColumns(ctx, p.db, t.Name()) require.NoError(t, err) - assert.EqualValues(t, cols[0], curCols["foo"]) - assert.EqualValues(t, cols[1], curCols["baz"]) + require.EqualValues(t, cols[0], curCols["foo"]) + require.EqualValues(t, cols[1], curCols["baz"]) } func TestTableManagerIntegration_MatchSource(t *testing.T) { @@ -217,8 +216,8 @@ func TestTableManagerIntegration_MatchSource(t *testing.T) { tsrc := NewTableSources(p.Postgresql, metrics)[t.Name()] require.NoError(t, p.tableManager.MatchSource(ctx, p.db, tsrc)) - assert.Contains(t, p.tableManager.table(t.Name()+p.TagTableSuffix).columns, "tag") - assert.Contains(t, p.tableManager.table(t.Name()).columns, "a") + require.Contains(t, p.tableManager.table(t.Name()+p.TagTableSuffix).columns, "tag") + require.Contains(t, p.tableManager.table(t.Name()).columns, "a") } func TestTableManagerIntegration_MatchSource_UnsignedIntegers(t *testing.T) { @@ -243,7 +242,7 @@ func TestTableManagerIntegration_MatchSource_UnsignedIntegers(t *testing.T) { tsrc := NewTableSources(p.Postgresql, metrics)[t.Name()] require.NoError(t, p.tableManager.MatchSource(ctx, p.db, tsrc)) - assert.Equal(t, PgUint8, p.tableManager.table(t.Name()).columns["a"].Type) + require.Equal(t, PgUint8, p.tableManager.table(t.Name()).columns["a"].Type) } func TestTableManagerIntegration_noCreateTable(t *testing.T) { @@ -321,7 +320,7 @@ func TestTableManagerIntegration_noAlterMissingTag(t *testing.T) { } tsrc = NewTableSources(p.Postgresql, metrics)[t.Name()] require.NoError(t, p.tableManager.MatchSource(ctx, p.db, tsrc)) - assert.NotContains(t, tsrc.ColumnNames(), "bar") + require.NotContains(t, tsrc.ColumnNames(), "bar") } // Verify that when using foreign tags and alter statements are disabled and a metric comes in with a new tag key, that @@ -349,7 +348,7 @@ func TestTableManagerIntegration_noAlterMissingTagTableTag(t *testing.T) { tsrc = NewTableSources(p.Postgresql, metrics)[t.Name()] ttsrc := NewTagTableSource(tsrc) require.NoError(t, p.tableManager.MatchSource(ctx, p.db, tsrc)) - assert.NotContains(t, ttsrc.ColumnNames(), "bar") + require.NotContains(t, ttsrc.ColumnNames(), "bar") } // Verify that when using foreign tags and alter statements generate a permanent error and a metric comes in with a new @@ -379,7 +378,7 @@ func TestTableManagerIntegration_badAlterTagTable(t *testing.T) { tsrc = NewTableSources(p.Postgresql, metrics)[t.Name()] ttsrc := NewTagTableSource(tsrc) require.NoError(t, p.tableManager.MatchSource(ctx, p.db, tsrc)) - assert.NotContains(t, ttsrc.ColumnNames(), "bar") + require.NotContains(t, ttsrc.ColumnNames(), "bar") } // verify that when alter statements are disabled and a metric comes in with a new field key, that the field is omitted. @@ -404,7 +403,7 @@ func TestTableManagerIntegration_noAlterMissingField(t *testing.T) { } tsrc = NewTableSources(p.Postgresql, metrics)[t.Name()] require.NoError(t, p.tableManager.MatchSource(ctx, p.db, tsrc)) - assert.NotContains(t, tsrc.ColumnNames(), "b") + require.NotContains(t, tsrc.ColumnNames(), "b") } // verify that when alter statements generate a permanent error and a metric comes in with a new field key, that the field is omitted. @@ -431,7 +430,7 @@ func TestTableManagerIntegration_badAlterField(t *testing.T) { } tsrc = NewTableSources(p.Postgresql, metrics)[t.Name()] require.NoError(t, p.tableManager.MatchSource(ctx, p.db, tsrc)) - assert.NotContains(t, tsrc.ColumnNames(), "b") + require.NotContains(t, tsrc.ColumnNames(), "b") } func TestTableManager_addColumnTemplates(t *testing.T) { @@ -468,7 +467,7 @@ func TestTableManager_addColumnTemplates(t *testing.T) { } } - assert.Equal(t, 1, stmtCount) + require.Equal(t, 1, stmtCount) } func TestTableManager_TimeWithTimezone(t *testing.T) { diff --git a/selfstat/selfstat_test.go b/selfstat/selfstat_test.go index 3d590bb96006c..f3b0b0c231a9b 100644 --- a/selfstat/selfstat_test.go +++ b/selfstat/selfstat_test.go @@ -4,9 +4,9 @@ import ( "sync" "testing" - "github.com/influxdata/telegraf/testutil" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/influxdata/telegraf/testutil" ) var ( @@ -51,29 +51,29 @@ func TestRegisterAndIncrAndSet(t *testing.T) { defer testCleanup() s1 := Register("test", "test_field1", map[string]string{"test": "foo"}) s2 := Register("test", "test_field2", map[string]string{"test": "foo"}) - assert.Equal(t, int64(0), s1.Get()) + require.Equal(t, int64(0), s1.Get()) s1.Incr(10) s1.Incr(5) - assert.Equal(t, int64(15), s1.Get()) + require.Equal(t, int64(15), s1.Get()) s1.Set(12) - assert.Equal(t, int64(12), s1.Get()) + require.Equal(t, int64(12), s1.Get()) s1.Incr(-2) - assert.Equal(t, int64(10), s1.Get()) + require.Equal(t, int64(10), s1.Get()) s2.Set(101) - assert.Equal(t, int64(101), s2.Get()) + require.Equal(t, int64(101), s2.Get()) // make sure that the same field returns the same metric // this one should be the same as s2. foo := Register("test", "test_field2", map[string]string{"test": "foo"}) - assert.Equal(t, int64(101), foo.Get()) + require.Equal(t, int64(101), foo.Get()) // check that tags are consistent - assert.Equal(t, map[string]string{"test": "foo"}, foo.Tags()) - assert.Equal(t, "internal_test", foo.Name()) + require.Equal(t, map[string]string{"test": "foo"}, foo.Tags()) + require.Equal(t, "internal_test", foo.Name()) } func TestRegisterTimingAndIncrAndSet(t *testing.T) { @@ -81,31 +81,31 @@ func TestRegisterTimingAndIncrAndSet(t *testing.T) { defer testCleanup() s1 := RegisterTiming("test", "test_field1_ns", map[string]string{"test": "foo"}) s2 := RegisterTiming("test", "test_field2_ns", map[string]string{"test": "foo"}) - assert.Equal(t, int64(0), s1.Get()) + require.Equal(t, int64(0), s1.Get()) s1.Incr(10) s1.Incr(5) - assert.Equal(t, int64(7), s1.Get()) + require.Equal(t, int64(7), s1.Get()) // previous value is used on subsequent calls to Get() - assert.Equal(t, int64(7), s1.Get()) + require.Equal(t, int64(7), s1.Get()) s1.Set(12) - assert.Equal(t, int64(12), s1.Get()) + require.Equal(t, int64(12), s1.Get()) s1.Incr(-2) - assert.Equal(t, int64(-2), s1.Get()) + require.Equal(t, int64(-2), s1.Get()) s2.Set(101) - assert.Equal(t, int64(101), s2.Get()) + require.Equal(t, int64(101), s2.Get()) // make sure that the same field returns the same metric // this one should be the same as s2. foo := RegisterTiming("test", "test_field2_ns", map[string]string{"test": "foo"}) - assert.Equal(t, int64(101), foo.Get()) + require.Equal(t, int64(101), foo.Get()) // check that tags are consistent - assert.Equal(t, map[string]string{"test": "foo"}, foo.Tags()) - assert.Equal(t, "internal_test", foo.Name()) + require.Equal(t, map[string]string{"test": "foo"}, foo.Tags()) + require.Equal(t, "internal_test", foo.Name()) } func TestStatKeyConsistency(t *testing.T) { @@ -131,14 +131,14 @@ func TestRegisterMetricsAndVerify(t *testing.T) { s2 := RegisterTiming("test_timing", "test_field2_ns", map[string]string{"test": "foo"}) s1.Incr(10) s2.Incr(15) - assert.Len(t, Metrics(), 1) + require.Len(t, Metrics(), 1) // register two more metrics with different keys s3 := RegisterTiming("test_timing", "test_field1_ns", map[string]string{"test": "bar"}) s4 := RegisterTiming("test_timing", "test_field2_ns", map[string]string{"test": "baz"}) s3.Incr(10) s4.Incr(15) - assert.Len(t, Metrics(), 3) + require.Len(t, Metrics(), 3) // register some non-timing metrics s5 := Register("test", "test_field1", map[string]string{"test": "bar"}) @@ -147,7 +147,7 @@ func TestRegisterMetricsAndVerify(t *testing.T) { s5.Incr(10) s5.Incr(18) s6.Incr(15) - assert.Len(t, Metrics(), 5) + require.Len(t, Metrics(), 5) acc := testutil.Accumulator{} acc.AddMetrics(Metrics()) diff --git a/testutil/accumulator.go b/testutil/accumulator.go index 559efef443870..05b97d0f6090e 100644 --- a/testutil/accumulator.go +++ b/testutil/accumulator.go @@ -9,7 +9,7 @@ import ( "testing" "time" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/influxdata/telegraf" ) @@ -369,7 +369,7 @@ func (a *Accumulator) AssertContainsTaggedFields( } } msg := fmt.Sprintf("unknown measurement %q with tags %v", measurement, tags) - assert.Fail(t, msg) + require.Fail(t, msg) } func (a *Accumulator) AssertDoesNotContainsTaggedFields( @@ -389,7 +389,7 @@ func (a *Accumulator) AssertDoesNotContainsTaggedFields( msg := fmt.Sprintf( "found measurement %s with tagged fields (tags %v) which should not be there", measurement, tags) - assert.Fail(t, msg) + require.Fail(t, msg) } } } @@ -402,12 +402,12 @@ func (a *Accumulator) AssertContainsFields( defer a.Unlock() for _, p := range a.Metrics { if p.Measurement == measurement { - assert.Equal(t, fields, p.Fields) + require.Equal(t, fields, p.Fields) return } } msg := fmt.Sprintf("unknown measurement %q", measurement) - assert.Fail(t, msg) + require.Fail(t, msg) } func (a *Accumulator) HasPoint( @@ -441,7 +441,7 @@ func (a *Accumulator) AssertDoesNotContainMeasurement(t *testing.T, measurement for _, p := range a.Metrics { if p.Measurement == measurement { msg := fmt.Sprintf("found unexpected measurement %s", measurement) - assert.Fail(t, msg) + require.Fail(t, msg) } } } From 97f47c11ddde5810fa5a420d8ccdf4769a44ded5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20=C5=BBak?= Date: Wed, 25 Oct 2023 23:09:13 +0200 Subject: [PATCH 3/7] chore(linters): Fix findings found by testifylint: empty (#14179) --- agent/agent_test.go | 4 +- models/buffer_test.go | 7 ++-- models/filter_test.go | 2 +- models/running_output_test.go | 42 +++++++++---------- .../aggregators/histogram/histogram_test.go | 2 +- plugins/inputs/aurora/aurora_test.go | 17 ++++---- plugins/inputs/cassandra/cassandra_test.go | 2 +- plugins/inputs/clickhouse/clickhouse_test.go | 4 +- plugins/inputs/couchbase/couchbase_test.go | 4 +- plugins/inputs/dpdk/dpdk_connector_test.go | 10 ++--- plugins/inputs/dpdk/dpdk_test.go | 14 +++---- plugins/inputs/example/example_test.go | 4 +- .../google_cloud_storage_test.go | 2 +- plugins/inputs/http/http_test.go | 2 +- .../influxdb_listener_test.go | 2 +- .../intel_powerstat/intel_powerstat_test.go | 22 +++++----- plugins/inputs/intel_powerstat/rapl_test.go | 2 +- plugins/inputs/interrupts/interrupts_test.go | 2 +- plugins/inputs/jolokia/jolokia_test.go | 4 +- .../kafka_consumer_legacy_integration_test.go | 2 +- .../inputs/nsq_consumer/nsq_consumer_test.go | 2 +- plugins/inputs/prometheus/kubernetes_test.go | 2 +- plugins/inputs/rabbitmq/rabbitmq_test.go | 7 ++-- plugins/inputs/radius/radius_test.go | 2 +- plugins/inputs/sql/sql_test.go | 6 +-- plugins/inputs/statsd/statsd_test.go | 4 +- plugins/inputs/tacacs/tacacs_test.go | 2 +- plugins/inputs/uwsgi/uwsgi_test.go | 5 ++- plugins/inputs/varnish/varnish_test.go | 2 +- plugins/inputs/vsphere/vsphere_test.go | 8 ++-- .../inputs/win_services/win_services_test.go | 2 +- plugins/inputs/x509_cert/x509_cert_test.go | 4 +- plugins/inputs/xtremio/xtremio_test.go | 2 +- .../azure_monitor/azure_monitor_test.go | 4 +- plugins/outputs/cloudwatch/cloudwatch_test.go | 2 +- plugins/outputs/groundwork/groundwork_test.go | 2 +- plugins/outputs/http/http_test.go | 4 +- plugins/outputs/loki/loki_test.go | 2 +- plugins/outputs/riemann/riemann_test.go | 8 ++-- plugins/outputs/wavefront/wavefront_test.go | 2 +- plugins/parsers/csv/parser_test.go | 6 +-- plugins/parsers/dropwizard/parser_test.go | 5 +-- .../parsers/form_urlencoded/parser_test.go | 4 +- plugins/parsers/json/parser_test.go | 8 ++-- plugins/parsers/json_v2/parser_test.go | 5 ++- plugins/parsers/xpath/parser_test.go | 2 +- plugins/processors/aws/ec2/ec2_test.go | 19 +++++---- .../processors/converter/converter_test.go | 5 ++- plugins/processors/dedup/dedup_test.go | 10 ++--- plugins/processors/ifname/ttl_cache_test.go | 2 +- .../processors/reverse_dns/rdnscache_test.go | 2 +- plugins/serializers/msgpack/msgpack_test.go | 8 ++-- 52 files changed, 151 insertions(+), 147 deletions(-) diff --git a/agent/agent_test.go b/agent/agent_test.go index c7d3fea98a0ff..c0ae841ba3aee 100644 --- a/agent/agent_test.go +++ b/agent/agent_test.go @@ -43,7 +43,7 @@ func TestAgent_LoadPlugin(t *testing.T) { err = c.LoadConfig("../config/testdata/telegraf-agent.toml") require.NoError(t, err) a = NewAgent(c) - require.Equal(t, 0, len(a.Config.Inputs)) + require.Empty(t, a.Config.Inputs) c = config.NewConfig() c.InputFilters = []string{"mysql", "foo"} @@ -94,7 +94,7 @@ func TestAgent_LoadOutput(t *testing.T) { err = c.LoadConfig("../config/testdata/telegraf-agent.toml") require.NoError(t, err) a = NewAgent(c) - require.Equal(t, 0, len(a.Config.Outputs)) + require.Empty(t, a.Config.Outputs) c = config.NewConfig() c.OutputFilters = []string{"influxdb", "foo"} diff --git a/models/buffer_test.go b/models/buffer_test.go index d830ac91c6dd9..276b5c47cabbb 100644 --- a/models/buffer_test.go +++ b/models/buffer_test.go @@ -4,10 +4,11 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" + "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/metric" "github.com/influxdata/telegraf/testutil" - "github.com/stretchr/testify/require" ) type MockMetric struct { @@ -95,14 +96,14 @@ func TestBuffer_BatchLenZero(t *testing.T) { b := setup(NewBuffer("test", "", 5)) batch := b.Batch(0) - require.Len(t, batch, 0) + require.Empty(t, batch) } func TestBuffer_BatchLenBufferEmpty(t *testing.T) { b := setup(NewBuffer("test", "", 5)) batch := b.Batch(2) - require.Len(t, batch, 0) + require.Empty(t, batch) } func TestBuffer_BatchLenUnderfill(t *testing.T) { diff --git a/models/filter_test.go b/models/filter_test.go index dc3084375e712..80f976809f72b 100644 --- a/models/filter_test.go +++ b/models/filter_test.go @@ -89,7 +89,7 @@ func TestFilter_ApplyDeleteAllFields(t *testing.T) { require.NoError(t, err) require.True(t, selected) f.Modify(m) - require.Len(t, m.FieldList(), 0) + require.Empty(t, m.FieldList()) } func TestFilter_Empty(t *testing.T) { diff --git a/models/running_output_test.go b/models/running_output_test.go index 422568069d734..c12028ebc9a61 100644 --- a/models/running_output_test.go +++ b/models/running_output_test.go @@ -94,7 +94,7 @@ func TestRunningOutput_DropFilter(t *testing.T) { for _, metric := range next5 { ro.AddMetric(metric) } - require.Len(t, m.Metrics(), 0) + require.Empty(t, m.Metrics()) err := ro.Write() require.NoError(t, err) @@ -119,7 +119,7 @@ func TestRunningOutput_PassFilter(t *testing.T) { for _, metric := range next5 { ro.AddMetric(metric) } - require.Len(t, m.Metrics(), 0) + require.Empty(t, m.Metrics()) err := ro.Write() require.NoError(t, err) @@ -139,7 +139,7 @@ func TestRunningOutput_TagIncludeNoMatch(t *testing.T) { ro := NewRunningOutput(m, conf, 1000, 10000) ro.AddMetric(testutil.TestMetric(101, "metric1")) - require.Len(t, m.Metrics(), 0) + require.Empty(t, m.Metrics()) err := ro.Write() require.NoError(t, err) @@ -160,12 +160,12 @@ func TestRunningOutput_TagExcludeMatch(t *testing.T) { ro := NewRunningOutput(m, conf, 1000, 10000) ro.AddMetric(testutil.TestMetric(101, "metric1")) - require.Len(t, m.Metrics(), 0) + require.Empty(t, m.Metrics()) err := ro.Write() require.NoError(t, err) require.Len(t, m.Metrics(), 1) - require.Len(t, m.Metrics()[0].Tags(), 0) + require.Empty(t, m.Metrics()[0].Tags()) } // Test that tags are properly Excluded @@ -181,7 +181,7 @@ func TestRunningOutput_TagExcludeNoMatch(t *testing.T) { ro := NewRunningOutput(m, conf, 1000, 10000) ro.AddMetric(testutil.TestMetric(101, "metric1")) - require.Len(t, m.Metrics(), 0) + require.Empty(t, m.Metrics()) err := ro.Write() require.NoError(t, err) @@ -202,7 +202,7 @@ func TestRunningOutput_TagIncludeMatch(t *testing.T) { ro := NewRunningOutput(m, conf, 1000, 10000) ro.AddMetric(testutil.TestMetric(101, "metric1")) - require.Len(t, m.Metrics(), 0) + require.Empty(t, m.Metrics()) err := ro.Write() require.NoError(t, err) @@ -220,7 +220,7 @@ func TestRunningOutput_NameOverride(t *testing.T) { ro := NewRunningOutput(m, conf, 1000, 10000) ro.AddMetric(testutil.TestMetric(101, "metric1")) - require.Len(t, m.Metrics(), 0) + require.Empty(t, m.Metrics()) err := ro.Write() require.NoError(t, err) @@ -238,7 +238,7 @@ func TestRunningOutput_NamePrefix(t *testing.T) { ro := NewRunningOutput(m, conf, 1000, 10000) ro.AddMetric(testutil.TestMetric(101, "metric1")) - require.Len(t, m.Metrics(), 0) + require.Empty(t, m.Metrics()) err := ro.Write() require.NoError(t, err) @@ -256,7 +256,7 @@ func TestRunningOutput_NameSuffix(t *testing.T) { ro := NewRunningOutput(m, conf, 1000, 10000) ro.AddMetric(testutil.TestMetric(101, "metric1")) - require.Len(t, m.Metrics(), 0) + require.Empty(t, m.Metrics()) err := ro.Write() require.NoError(t, err) @@ -279,7 +279,7 @@ func TestRunningOutputDefault(t *testing.T) { for _, metric := range next5 { ro.AddMetric(metric) } - require.Len(t, m.Metrics(), 0) + require.Empty(t, m.Metrics()) err := ro.Write() require.NoError(t, err) @@ -303,13 +303,13 @@ func TestRunningOutputWriteFail(t *testing.T) { ro.AddMetric(metric) } // no successful flush yet - require.Len(t, m.Metrics(), 0) + require.Empty(t, m.Metrics()) // manual write fails err := ro.Write() require.Error(t, err) // no successful flush yet - require.Len(t, m.Metrics(), 0) + require.Empty(t, m.Metrics()) m.failWrite = false err = ro.Write() @@ -333,13 +333,13 @@ func TestRunningOutputWriteFailOrder(t *testing.T) { ro.AddMetric(metric) } // no successful flush yet - require.Len(t, m.Metrics(), 0) + require.Empty(t, m.Metrics()) // Write fails err := ro.Write() require.Error(t, err) // no successful flush yet - require.Len(t, m.Metrics(), 0) + require.Empty(t, m.Metrics()) m.failWrite = false // add 5 more metrics @@ -374,7 +374,7 @@ func TestRunningOutputWriteFailOrder2(t *testing.T) { err := ro.Write() require.Error(t, err) // no successful flush yet - require.Len(t, m.Metrics(), 0) + require.Empty(t, m.Metrics()) // add 5 metrics for _, metric := range next5 { @@ -384,7 +384,7 @@ func TestRunningOutputWriteFailOrder2(t *testing.T) { err = ro.Write() require.Error(t, err) // no successful flush yet - require.Len(t, m.Metrics(), 0) + require.Empty(t, m.Metrics()) // add 5 metrics for _, metric := range first5 { @@ -394,7 +394,7 @@ func TestRunningOutputWriteFailOrder2(t *testing.T) { err = ro.Write() require.Error(t, err) // no successful flush yet - require.Len(t, m.Metrics(), 0) + require.Empty(t, m.Metrics()) // add 5 metrics for _, metric := range next5 { @@ -404,7 +404,7 @@ func TestRunningOutputWriteFailOrder2(t *testing.T) { err = ro.Write() require.Error(t, err) // no successful flush yet - require.Len(t, m.Metrics(), 0) + require.Empty(t, m.Metrics()) m.failWrite = false err = ro.Write() @@ -435,13 +435,13 @@ func TestRunningOutputWriteFailOrder3(t *testing.T) { ro.AddMetric(metric) } // no successful flush yet - require.Len(t, m.Metrics(), 0) + require.Empty(t, m.Metrics()) // Write fails err := ro.Write() require.Error(t, err) // no successful flush yet - require.Len(t, m.Metrics(), 0) + require.Empty(t, m.Metrics()) // add and attempt to write a single metric: ro.AddMetric(next5[0]) diff --git a/plugins/aggregators/histogram/histogram_test.go b/plugins/aggregators/histogram/histogram_test.go index 9ba9fc9e41c3a..8a7b2ce22e7c7 100644 --- a/plugins/aggregators/histogram/histogram_test.go +++ b/plugins/aggregators/histogram/histogram_test.go @@ -128,7 +128,7 @@ func TestHistogramPushOnUpdate(t *testing.T) { acc.ClearMetrics() histogram.Push(acc) - require.Len(t, acc.Metrics, 0, "Incorrect number of metrics") + require.Empty(t, acc.Metrics, "Incorrect number of metrics") histogram.Add(firstMetric2) histogram.Push(acc) diff --git a/plugins/inputs/aurora/aurora_test.go b/plugins/inputs/aurora/aurora_test.go index e22488929e545..0cab51e6cfeea 100644 --- a/plugins/inputs/aurora/aurora_test.go +++ b/plugins/inputs/aurora/aurora_test.go @@ -7,8 +7,9 @@ import ( "net/url" "testing" - "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/require" + + "github.com/influxdata/telegraf/testutil" ) type ( @@ -77,7 +78,7 @@ func TestAurora(t *testing.T) { check: func(t *testing.T, err error, acc *testutil.Accumulator) { require.NoError(t, err) require.NoError(t, acc.FirstError()) - require.Equal(t, 0, len(acc.Metrics)) + require.Empty(t, acc.Metrics) }, }, { @@ -93,7 +94,7 @@ func TestAurora(t *testing.T) { check: func(t *testing.T, err error, acc *testutil.Accumulator) { require.NoError(t, err) require.NoError(t, acc.FirstError()) - require.Equal(t, 0, len(acc.Metrics)) + require.Empty(t, acc.Metrics) }, }, { @@ -112,7 +113,7 @@ func TestAurora(t *testing.T) { check: func(t *testing.T, err error, acc *testutil.Accumulator) { require.NoError(t, err) require.NoError(t, acc.FirstError()) - require.Equal(t, 0, len(acc.Metrics)) + require.Empty(t, acc.Metrics) }, }, { @@ -132,7 +133,7 @@ func TestAurora(t *testing.T) { check: func(t *testing.T, err error, acc *testutil.Accumulator) { require.NoError(t, err) require.Error(t, acc.FirstError()) - require.Equal(t, 0, len(acc.Metrics)) + require.Empty(t, acc.Metrics) }, }, { @@ -152,7 +153,7 @@ func TestAurora(t *testing.T) { check: func(t *testing.T, err error, acc *testutil.Accumulator) { require.NoError(t, err) require.Error(t, acc.FirstError()) - require.Equal(t, 0, len(acc.Metrics)) + require.Empty(t, acc.Metrics) }, }, { @@ -169,7 +170,7 @@ func TestAurora(t *testing.T) { check: func(t *testing.T, err error, acc *testutil.Accumulator) { require.NoError(t, err) require.Error(t, acc.FirstError()) - require.Equal(t, 0, len(acc.Metrics)) + require.Empty(t, acc.Metrics) }, }, { @@ -188,7 +189,7 @@ func TestAurora(t *testing.T) { check: func(t *testing.T, err error, acc *testutil.Accumulator) { require.NoError(t, err) require.Error(t, acc.FirstError()) - require.Equal(t, 0, len(acc.Metrics)) + require.Empty(t, acc.Metrics) }, }, } diff --git a/plugins/inputs/cassandra/cassandra_test.go b/plugins/inputs/cassandra/cassandra_test.go index 791686297f41e..e7d725bef05cc 100644 --- a/plugins/inputs/cassandra/cassandra_test.go +++ b/plugins/inputs/cassandra/cassandra_test.go @@ -190,7 +190,7 @@ func TestHttp404(t *testing.T) { err := acc.GatherError(jolokia.Gather) require.Error(t, err) - require.Equal(t, 0, len(acc.Metrics)) + require.Empty(t, acc.Metrics) require.Contains(t, err.Error(), "has status code 404") } diff --git a/plugins/inputs/clickhouse/clickhouse_test.go b/plugins/inputs/clickhouse/clickhouse_test.go index b342e6872c37c..101e74616a9d0 100644 --- a/plugins/inputs/clickhouse/clickhouse_test.go +++ b/plugins/inputs/clickhouse/clickhouse_test.go @@ -516,7 +516,7 @@ func TestWrongJSONMarshalling(t *testing.T) { defer ts.Close() require.NoError(t, ch.Gather(acc)) - require.Equal(t, 0, len(acc.Metrics)) + require.Empty(t, acc.Metrics) allMeasurements := []string{ "clickhouse_events", "clickhouse_metrics", @@ -549,7 +549,7 @@ func TestOfflineServer(t *testing.T) { ) require.NoError(t, ch.Gather(acc)) - require.Equal(t, 0, len(acc.Metrics)) + require.Empty(t, acc.Metrics) allMeasurements := []string{ "clickhouse_events", "clickhouse_metrics", diff --git a/plugins/inputs/couchbase/couchbase_test.go b/plugins/inputs/couchbase/couchbase_test.go index 758dac4711ecd..21f2d2fc2b34a 100644 --- a/plugins/inputs/couchbase/couchbase_test.go +++ b/plugins/inputs/couchbase/couchbase_test.go @@ -166,7 +166,7 @@ func TestGatherNodeOnly(t *testing.T) { var acc testutil.Accumulator require.NoError(t, cb.gatherServer(&acc, faker.URL)) - require.Equal(t, 0, len(acc.Errors)) + require.Empty(t, acc.Errors) require.Equal(t, 7, len(acc.Metrics)) acc.AssertDoesNotContainMeasurement(t, "couchbase_bucket") } @@ -197,7 +197,7 @@ func TestGatherFailover(t *testing.T) { var acc testutil.Accumulator require.NoError(t, cb.gatherServer(&acc, faker.URL)) - require.Equal(t, 0, len(acc.Errors)) + require.Empty(t, acc.Errors) require.Equal(t, 8, len(acc.Metrics)) var metric *testutil.Metric diff --git a/plugins/inputs/dpdk/dpdk_connector_test.go b/plugins/inputs/dpdk/dpdk_connector_test.go index ddffc0cb42140..19841e034ebd7 100644 --- a/plugins/inputs/dpdk/dpdk_connector_test.go +++ b/plugins/inputs/dpdk/dpdk_connector_test.go @@ -127,7 +127,7 @@ func Test_getCommandResponse(t *testing.T) { require.Error(t, err) require.Contains(t, err.Error(), "failed to get connection to execute \"/\" command") - require.Equal(t, 0, len(buf)) + require.Empty(t, buf) }) t.Run("should return error if failed to set timeout duration", func(t *testing.T) { @@ -139,7 +139,7 @@ func Test_getCommandResponse(t *testing.T) { require.Error(t, err) require.Contains(t, err.Error(), "deadline error") - require.Equal(t, 0, len(buf)) + require.Empty(t, buf) }) t.Run("should return error if timeout occurred during Write operation", func(t *testing.T) { @@ -153,7 +153,7 @@ func Test_getCommandResponse(t *testing.T) { require.Error(t, err) require.Contains(t, err.Error(), "write timeout") - require.Equal(t, 0, len(buf)) + require.Empty(t, buf) }) t.Run("should return error if timeout occurred during Read operation", func(t *testing.T) { @@ -165,7 +165,7 @@ func Test_getCommandResponse(t *testing.T) { require.Error(t, err) require.Contains(t, err.Error(), "read timeout") - require.Equal(t, 0, len(buf)) + require.Empty(t, buf) }) t.Run("should return error if got empty response", func(t *testing.T) { @@ -176,7 +176,7 @@ func Test_getCommandResponse(t *testing.T) { buf, err := dpdk.connector.getCommandResponse(command) require.Error(t, err) - require.Equal(t, 0, len(buf)) + require.Empty(t, buf) require.Contains(t, err.Error(), "got empty response during execution of") }) } diff --git a/plugins/inputs/dpdk/dpdk_test.go b/plugins/inputs/dpdk/dpdk_test.go index dada767f714b2..c2683b62c5dff 100644 --- a/plugins/inputs/dpdk/dpdk_test.go +++ b/plugins/inputs/dpdk/dpdk_test.go @@ -177,7 +177,7 @@ func Test_processCommand(t *testing.T) { dpdk.processCommand(mockAcc, "/") - require.Equal(t, 0, len(mockAcc.Errors)) + require.Empty(t, mockAcc.Errors) }) t.Run("if received a non-JSON object then should return error", func(t *testing.T) { @@ -249,7 +249,7 @@ func Test_getCommandsAndParamsCombinations(t *testing.T) { commands := dpdk.gatherCommands(mockAcc) require.ElementsMatch(t, commands, expectedCommands) - require.Equal(t, 0, len(mockAcc.Errors)) + require.Empty(t, mockAcc.Errors) }) t.Run("when 1 rawdev command is enabled, then 2*numberOfIds new commands should be appended", func(t *testing.T) { @@ -265,7 +265,7 @@ func Test_getCommandsAndParamsCombinations(t *testing.T) { commands := dpdk.gatherCommands(mockAcc) require.ElementsMatch(t, commands, expectedCommands) - require.Equal(t, 0, len(mockAcc.Errors)) + require.Empty(t, mockAcc.Errors) }) t.Run("when 2 ethdev commands are enabled but one command is disabled, then numberOfIds new commands should be appended", func(t *testing.T) { @@ -282,7 +282,7 @@ func Test_getCommandsAndParamsCombinations(t *testing.T) { commands := dpdk.gatherCommands(mockAcc) require.ElementsMatch(t, commands, expectedCommands) - require.Equal(t, 0, len(mockAcc.Errors)) + require.Empty(t, mockAcc.Errors) }) t.Run("when ethdev commands are enabled but params fetching command returns error then error should be logged in accumulator", func(t *testing.T) { @@ -296,7 +296,7 @@ func Test_getCommandsAndParamsCombinations(t *testing.T) { dpdk.AdditionalCommands = []string{} commands := dpdk.gatherCommands(mockAcc) - require.Equal(t, 0, len(commands)) + require.Empty(t, commands) require.Equal(t, 1, len(mockAcc.Errors)) }) } @@ -311,7 +311,7 @@ func Test_Gather(t *testing.T) { err := dpdk.Gather(mockAcc) require.NoError(t, err) - require.Equal(t, 0, len(mockAcc.Errors)) + require.Empty(t, mockAcc.Errors) expected := []telegraf.Metric{ testutil.MustMetric( @@ -339,7 +339,7 @@ func Test_Gather(t *testing.T) { err := dpdk.Gather(mockAcc) require.NoError(t, err) - require.Equal(t, 0, len(mockAcc.Errors)) + require.Empty(t, mockAcc.Errors) expected := []telegraf.Metric{ testutil.MustMetric( diff --git a/plugins/inputs/example/example_test.go b/plugins/inputs/example/example_test.go index 1c3b4b0a5e66e..d744449ba9613 100644 --- a/plugins/inputs/example/example_test.go +++ b/plugins/inputs/example/example_test.go @@ -218,7 +218,7 @@ func TestFixedValue(t *testing.T) { // Call gather and check no error occurs. In case you use acc.AddError() somewhere // in your code, it is not sufficient to only check the return value of Gather(). require.NoError(t, tt.plugin.Gather(&acc)) - require.Len(t, acc.Errors, 0, "found errors accumulated by acc.AddError()") + require.Empty(t, acc.Errors, "found errors accumulated by acc.AddError()") // Wait for the expected number of metrics to avoid flaky tests due to // race conditions. @@ -311,7 +311,7 @@ func TestRandomValue(t *testing.T) { // Call gather and check no error occurs. In case you use acc.AddError() somewhere // in your code, it is not sufficient to only check the return value of Gather(). require.NoError(t, tt.plugin.Gather(&acc)) - require.Len(t, acc.Errors, 0, "found errors accumulated by acc.AddError()") + require.Empty(t, acc.Errors, "found errors accumulated by acc.AddError()") // Wait for the expected number of metrics to avoid flaky tests due to // race conditions. diff --git a/plugins/inputs/google_cloud_storage/google_cloud_storage_test.go b/plugins/inputs/google_cloud_storage/google_cloud_storage_test.go index 6a3fa1af1e028..dff29a6416aaf 100644 --- a/plugins/inputs/google_cloud_storage/google_cloud_storage_test.go +++ b/plugins/inputs/google_cloud_storage/google_cloud_storage_test.go @@ -176,7 +176,7 @@ func TestRunGatherIterationWithPages(t *testing.T) { emptyAcc := &testutil.Accumulator{} require.NoError(t, gcs.Gather(emptyAcc)) - require.Equal(t, 0, len(emptyAcc.Metrics)) + require.Empty(t, emptyAcc.Metrics) } func createParser() telegraf.Parser { diff --git a/plugins/inputs/http/http_test.go b/plugins/inputs/http/http_test.go index bd1204110dfca..4eff3eee390d0 100644 --- a/plugins/inputs/http/http_test.go +++ b/plugins/inputs/http/http_test.go @@ -336,7 +336,7 @@ func TestOAuthClientCredentialsGrant(t *testing.T) { Log: testutil.Logger{}, }, handler: func(t *testing.T, w http.ResponseWriter, r *http.Request) { - require.Len(t, r.Header["Authorization"], 0) + require.Empty(t, r.Header["Authorization"]) w.WriteHeader(http.StatusOK) }, }, diff --git a/plugins/inputs/influxdb_listener/influxdb_listener_test.go b/plugins/inputs/influxdb_listener/influxdb_listener_test.go index 1f629f6a2b72b..ac5a5f0921372 100644 --- a/plugins/inputs/influxdb_listener/influxdb_listener_test.go +++ b/plugins/inputs/influxdb_listener/influxdb_listener_test.go @@ -729,7 +729,7 @@ func TestPing(t *testing.T) { resp, err := http.Post(createURL(listener, "http", "/ping", ""), "", nil) require.NoError(t, err) require.Equal(t, "1.0", resp.Header["X-Influxdb-Version"][0]) - require.Len(t, resp.Header["Content-Type"], 0) + require.Empty(t, resp.Header["Content-Type"]) require.NoError(t, resp.Body.Close()) require.EqualValues(t, 204, resp.StatusCode) }) diff --git a/plugins/inputs/intel_powerstat/intel_powerstat_test.go b/plugins/inputs/intel_powerstat/intel_powerstat_test.go index 65ca0d691df28..fc501a288800e 100644 --- a/plugins/inputs/intel_powerstat/intel_powerstat_test.go +++ b/plugins/inputs/intel_powerstat/intel_powerstat_test.go @@ -60,7 +60,7 @@ func TestInitPlugin(t *testing.T) { // In case of an error when fetching cpu cores plugin should proceed with execution. require.NoError(t, power.Init()) mockServices.fs.AssertCalled(t, "getStringsMatchingPatternOnPath", mock.Anything) - require.Equal(t, 0, len(power.msr.getCPUCoresData())) + require.Empty(t, power.msr.getCPUCoresData()) } func TestParseCPUMetricsConfig(t *testing.T) { @@ -142,14 +142,14 @@ func TestAddGlobalMetricsNegative(t *testing.T) { On("retrieveAndCalculateData", mock.Anything).Return(errors.New("error while calculating data")).Times(len(raplDataMap)) power.addGlobalMetrics(&acc) - require.Equal(t, 0, len(acc.GetTelegrafMetrics())) + require.Empty(t, acc.GetTelegrafMetrics()) mockServices.rapl.AssertNumberOfCalls(t, "retrieveAndCalculateData", len(raplDataMap)) mockServices.rapl.On("initializeRaplData", mock.Anything).Once(). On("getRaplData").Return(make(map[string]*raplData)).Once() power.addGlobalMetrics(&acc) - require.Equal(t, 0, len(acc.GetTelegrafMetrics())) + require.Empty(t, acc.GetTelegrafMetrics()) mockServices.rapl.AssertNotCalled(t, "retrieveAndCalculateData") mockServices.rapl.On("initializeRaplData", mock.Anything).Once(). @@ -201,7 +201,7 @@ func TestAddMetricsForSingleCoreNegative(t *testing.T) { power.addMetricsForSingleCore(core, &acc, &wg) wg.Wait() - require.Equal(t, 0, len(acc.GetTelegrafMetrics())) + require.Empty(t, acc.GetTelegrafMetrics()) } func TestAddCPUFrequencyMetric(t *testing.T) { @@ -217,7 +217,7 @@ func TestAddCPUFrequencyMetric(t *testing.T) { Return(float64(0), errors.New("error on reading file")).Once() power.addCPUFrequencyMetric(cpuID, &acc) - require.Equal(t, 0, len(acc.GetTelegrafMetrics())) + require.Empty(t, acc.GetTelegrafMetrics()) mockServices.msr.On("retrieveCPUFrequencyForCore", mock.Anything).Return(frequency, nil).Once() @@ -306,7 +306,7 @@ func TestAddC6StateResidencyMetric(t *testing.T) { preparedData[cpuID].timeStampCounterDelta = 0 power.addCPUC6StateResidencyMetric(cpuID, &acc) - require.Equal(t, 0, len(acc.GetTelegrafMetrics())) + require.Empty(t, acc.GetTelegrafMetrics()) } func TestAddC0StateResidencyMetric(t *testing.T) { @@ -335,7 +335,7 @@ func TestAddC0StateResidencyMetric(t *testing.T) { acc.ClearMetrics() preparedData[cpuID].timeStampCounterDelta = 0 power.addCPUC0StateResidencyMetric(cpuID, &acc) - require.Equal(t, 0, len(acc.GetTelegrafMetrics())) + require.Empty(t, acc.GetTelegrafMetrics()) } func TestAddProcessorBusyFrequencyMetric(t *testing.T) { @@ -355,7 +355,7 @@ func TestAddProcessorBusyFrequencyMetric(t *testing.T) { acc.ClearMetrics() preparedData[cpuID].mperfDelta = 0 power.addCPUBusyFrequencyMetric(cpuID, &acc) - require.Equal(t, 0, len(acc.GetTelegrafMetrics())) + require.Empty(t, acc.GetTelegrafMetrics()) } func TestAddC1StateResidencyMetric(t *testing.T) { @@ -381,7 +381,7 @@ func TestAddC1StateResidencyMetric(t *testing.T) { acc.ClearMetrics() preparedData[cpuID].timeStampCounterDelta = 0 power.addCPUC1StateResidencyMetric(cpuID, &acc) - require.Equal(t, 0, len(acc.GetTelegrafMetrics())) + require.Empty(t, acc.GetTelegrafMetrics()) } func TestAddThermalDesignPowerMetric(t *testing.T) { @@ -395,7 +395,7 @@ func TestAddThermalDesignPowerMetric(t *testing.T) { On("getConstraintMaxPowerWatts", mock.Anything).Return(maxPower, nil).Once() power.addThermalDesignPowerMetric(sockets[0], &acc) - require.Equal(t, 0, len(acc.GetTelegrafMetrics())) + require.Empty(t, acc.GetTelegrafMetrics()) power.addThermalDesignPowerMetric(sockets[0], &acc) require.Equal(t, 1, len(acc.GetTelegrafMetrics())) @@ -827,7 +827,7 @@ func TestAddCPUBaseFreq(t *testing.T) { p.addCPUBaseFreq(tt.socketID, &acc) actual := acc.GetTelegrafMetrics() if !tt.metricExpected { - require.Len(t, actual, 0) + require.Empty(t, actual) return } diff --git a/plugins/inputs/intel_powerstat/rapl_test.go b/plugins/inputs/intel_powerstat/rapl_test.go index f89330bd55779..74a86a5251ded 100644 --- a/plugins/inputs/intel_powerstat/rapl_test.go +++ b/plugins/inputs/intel_powerstat/rapl_test.go @@ -37,7 +37,7 @@ func TestPrepareData(t *testing.T) { Return(nil, errors.New("missing RAPL")).Once() rapl.prepareData() fsMock.AssertCalled(t, "getStringsMatchingPatternOnPath", mock.Anything) - require.Equal(t, 0, len(rapl.getRaplData())) + require.Empty(t, rapl.getRaplData()) } func TestFindDramFolders(t *testing.T) { diff --git a/plugins/inputs/interrupts/interrupts_test.go b/plugins/inputs/interrupts/interrupts_test.go index 95f9c8b1d4bd4..3abb14b14de1a 100644 --- a/plugins/inputs/interrupts/interrupts_test.go +++ b/plugins/inputs/interrupts/interrupts_test.go @@ -39,7 +39,7 @@ func setup(t *testing.T, irqString string, cpuAsTags bool) (*testutil.Accumulato f := bytes.NewBufferString(irqString) irqs, err := parseInterrupts(f) require.Equal(t, nil, err) - require.NotEqual(t, 0, len(irqs)) + require.NotEmpty(t, irqs) acc := new(testutil.Accumulator) reportMetrics("soft_interrupts", irqs, acc, cpuAsTags) diff --git a/plugins/inputs/jolokia/jolokia_test.go b/plugins/inputs/jolokia/jolokia_test.go index 48a76963f2885..22ea9465c98f0 100644 --- a/plugins/inputs/jolokia/jolokia_test.go +++ b/plugins/inputs/jolokia/jolokia_test.go @@ -231,7 +231,7 @@ func TestHttp404(t *testing.T) { err := acc.GatherError(jolokia.Gather) require.Error(t, err) - require.Equal(t, 0, len(acc.Metrics)) + require.Empty(t, acc.Metrics) require.Contains(t, err.Error(), "has status code 404") } @@ -244,6 +244,6 @@ func TestHttpInvalidJson(t *testing.T) { err := acc.GatherError(jolokia.Gather) require.Error(t, err) - require.Equal(t, 0, len(acc.Metrics)) + require.Empty(t, acc.Metrics) require.Contains(t, err.Error(), "error decoding JSON response") } diff --git a/plugins/inputs/kafka_consumer_legacy/kafka_consumer_legacy_integration_test.go b/plugins/inputs/kafka_consumer_legacy/kafka_consumer_legacy_integration_test.go index 77d4832d3bdd9..1cc05f0ef976d 100644 --- a/plugins/inputs/kafka_consumer_legacy/kafka_consumer_legacy_integration_test.go +++ b/plugins/inputs/kafka_consumer_legacy/kafka_consumer_legacy_integration_test.go @@ -51,7 +51,7 @@ func TestReadsMetricsFromKafkaIntegration(t *testing.T) { var acc testutil.Accumulator // Sanity check - require.Equal(t, 0, len(acc.Metrics), "There should not be any points") + require.Empty(t, acc.Metrics, "There should not be any points") if err := k.Start(&acc); err != nil { t.Fatal(err.Error()) } else { diff --git a/plugins/inputs/nsq_consumer/nsq_consumer_test.go b/plugins/inputs/nsq_consumer/nsq_consumer_test.go index 6ae8bcd533438..d2e3b23d1ed4c 100644 --- a/plugins/inputs/nsq_consumer/nsq_consumer_test.go +++ b/plugins/inputs/nsq_consumer/nsq_consumer_test.go @@ -53,7 +53,7 @@ func TestReadsMetricsFromNSQ(t *testing.T) { require.NoError(t, p.Init()) consumer.SetParser(p) var acc testutil.Accumulator - require.Len(t, acc.Metrics, 0, "There should not be any points") + require.Empty(t, acc.Metrics, "There should not be any points") require.NoError(t, consumer.Start(&acc)) waitForPoint(&acc, t) diff --git a/plugins/inputs/prometheus/kubernetes_test.go b/plugins/inputs/prometheus/kubernetes_test.go index 8e6d099ca519c..daff0ebbcc7c7 100644 --- a/plugins/inputs/prometheus/kubernetes_test.go +++ b/plugins/inputs/prometheus/kubernetes_test.go @@ -181,7 +181,7 @@ func TestDeletePods(t *testing.T) { podID, _ := cache.MetaNamespaceKeyFunc(p) unregisterPod(PodID(podID), prom) - require.Equal(t, 0, len(prom.kubernetesPods)) + require.Empty(t, prom.kubernetesPods) } func TestKeepDefaultNamespaceLabelName(t *testing.T) { diff --git a/plugins/inputs/rabbitmq/rabbitmq_test.go b/plugins/inputs/rabbitmq/rabbitmq_test.go index e006236eba2bf..bf77afc87a775 100644 --- a/plugins/inputs/rabbitmq/rabbitmq_test.go +++ b/plugins/inputs/rabbitmq/rabbitmq_test.go @@ -5,14 +5,13 @@ import ( "net/http" "net/http/httptest" "os" + "testing" "time" - "testing" + "github.com/stretchr/testify/require" "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/testutil" - - "github.com/stretchr/testify/require" ) func TestRabbitMQGeneratesMetricsSet1(t *testing.T) { @@ -221,7 +220,7 @@ func TestRabbitMQGeneratesMetricsSet1(t *testing.T) { require.NoError(t, plugin.Gather(acc)) acc.Wait(len(expected)) - require.Len(t, acc.Errors, 0) + require.Empty(t, acc.Errors) testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime(), testutil.SortMetrics()) } diff --git a/plugins/inputs/radius/radius_test.go b/plugins/inputs/radius/radius_test.go index 9ce40f1b95ae1..cc8e7bdff2381 100644 --- a/plugins/inputs/radius/radius_test.go +++ b/plugins/inputs/radius/radius_test.go @@ -179,7 +179,7 @@ func TestRadiusIntegration(t *testing.T) { // Gather require.NoError(t, plugin.Gather(&acc)) - require.Len(t, acc.Errors, 0) + require.Empty(t, acc.Errors) if !acc.HasMeasurement("radius") { t.Errorf("acc.HasMeasurement: expected radius") diff --git a/plugins/inputs/sql/sql_test.go b/plugins/inputs/sql/sql_test.go index 0291ec85f1f1d..6f434dff27f20 100644 --- a/plugins/inputs/sql/sql_test.go +++ b/plugins/inputs/sql/sql_test.go @@ -116,7 +116,7 @@ func TestMariaDBIntegration(t *testing.T) { // Gather require.NoError(t, plugin.Gather(&acc)) - require.Len(t, acc.Errors, 0) + require.Empty(t, acc.Errors) // Stopping the plugin plugin.Stop() @@ -215,7 +215,7 @@ func TestPostgreSQLIntegration(t *testing.T) { // Gather require.NoError(t, plugin.Gather(&acc)) - require.Len(t, acc.Errors, 0) + require.Empty(t, acc.Errors) // Stopping the plugin plugin.Stop() @@ -310,7 +310,7 @@ func TestClickHouseIntegration(t *testing.T) { // Gather require.NoError(t, plugin.Gather(&acc)) - require.Len(t, acc.Errors, 0) + require.Empty(t, acc.Errors) // Stopping the plugin plugin.Stop() diff --git a/plugins/inputs/statsd/statsd_test.go b/plugins/inputs/statsd/statsd_test.go index f6d2358d3feba..8c405108e33dd 100644 --- a/plugins/inputs/statsd/statsd_test.go +++ b/plugins/inputs/statsd/statsd_test.go @@ -1564,7 +1564,7 @@ func TestParse_Timings_Delete(t *testing.T) { require.NoError(t, s.Gather(fakeacc)) - require.Lenf(t, s.timings, 0, "All timings should have been deleted, found %d", len(s.timings)) + require.Emptyf(t, s.timings, "All timings should have been deleted, found %d", len(s.timings)) } // Tests the delete_gauges option @@ -1840,7 +1840,7 @@ func TestUdpFillQueue(t *testing.T) { defer plugin.Stop() errs := logger.Errors() - require.Lenf(t, errs, 0, "got errors: %v", errs) + require.Emptyf(t, errs, "got errors: %v", errs) } func TestParse_Ints(t *testing.T) { diff --git a/plugins/inputs/tacacs/tacacs_test.go b/plugins/inputs/tacacs/tacacs_test.go index 10d630b36b095..9b808fdd065d1 100644 --- a/plugins/inputs/tacacs/tacacs_test.go +++ b/plugins/inputs/tacacs/tacacs_test.go @@ -220,7 +220,7 @@ func TestTacacsLocal(t *testing.T) { require.NoError(t, plugin.Gather(&acc)) if tt.errContains == "" { - require.Len(t, acc.Errors, 0) + require.Empty(t, acc.Errors) require.True(t, acc.HasMeasurement("tacacs")) require.True(t, acc.HasTag("tacacs", "source")) require.Equal(t, srvLocal, acc.TagValue("tacacs", "source")) diff --git a/plugins/inputs/uwsgi/uwsgi_test.go b/plugins/inputs/uwsgi/uwsgi_test.go index d1ed75ec9efc1..51fe2fe1380dd 100644 --- a/plugins/inputs/uwsgi/uwsgi_test.go +++ b/plugins/inputs/uwsgi/uwsgi_test.go @@ -6,10 +6,11 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" + "github.com/influxdata/telegraf/config" "github.com/influxdata/telegraf/plugins/inputs/uwsgi" "github.com/influxdata/telegraf/testutil" - "github.com/stretchr/testify/require" ) func TestBasic(t *testing.T) { @@ -125,7 +126,7 @@ func TestBasic(t *testing.T) { } var acc testutil.Accumulator require.NoError(t, plugin.Gather(&acc)) - require.Equal(t, 0, len(acc.Errors)) + require.Empty(t, acc.Errors) } func TestInvalidJSON(t *testing.T) { diff --git a/plugins/inputs/varnish/varnish_test.go b/plugins/inputs/varnish/varnish_test.go index f671f76a28a9b..8cdae37d7f983 100644 --- a/plugins/inputs/varnish/varnish_test.go +++ b/plugins/inputs/varnish/varnish_test.go @@ -536,7 +536,7 @@ func TestVersions(t *testing.T) { require.NoError(t, server.Init()) acc := &testutil.Accumulator{} - require.Equal(t, 0, len(acc.Metrics)) + require.Empty(t, acc.Metrics) type testConfig struct { jsonFile string diff --git a/plugins/inputs/vsphere/vsphere_test.go b/plugins/inputs/vsphere/vsphere_test.go index b4ef8b47e502b..4e444fb960a8e 100644 --- a/plugins/inputs/vsphere/vsphere_test.go +++ b/plugins/inputs/vsphere/vsphere_test.go @@ -344,7 +344,7 @@ func TestFinder(t *testing.T) { } vm = []mo.VirtualMachine{} require.NoError(t, rf.FindAll(ctx, &vm)) - require.Equal(t, 0, len(vm)) + require.Empty(t, vm) rf = ResourceFilter{ finder: &f, @@ -354,7 +354,7 @@ func TestFinder(t *testing.T) { } vm = []mo.VirtualMachine{} require.NoError(t, rf.FindAll(ctx, &vm)) - require.Equal(t, 0, len(vm)) + require.Empty(t, vm) rf = ResourceFilter{ finder: &f, @@ -513,8 +513,8 @@ func testCollection(t *testing.T, excludeClusters bool) { require.NoError(t, v.Start(&acc)) defer v.Stop() require.NoError(t, v.Gather(&acc)) - require.Equal(t, 0, len(acc.Errors), fmt.Sprintf("Errors found: %s", acc.Errors)) - require.Greater(t, len(acc.Metrics), 0, "No metrics were collected") + require.Empty(t, acc.Errors, fmt.Sprintf("Errors found: %s", acc.Errors)) + require.NotEmpty(t, acc.Metrics, "No metrics were collected") cache := make(map[string]string) client, err := v.endpoints[0].clientFactory.GetClient(context.Background()) require.NoError(t, err) diff --git a/plugins/inputs/win_services/win_services_test.go b/plugins/inputs/win_services/win_services_test.go index 4305e6ec80526..f2b2312a98fc9 100644 --- a/plugins/inputs/win_services/win_services_test.go +++ b/plugins/inputs/win_services/win_services_test.go @@ -201,7 +201,7 @@ func TestGatherContainsTag(t *testing.T) { var acc1 testutil.Accumulator require.NoError(t, winServices.Gather(&acc1)) - require.Len(t, acc1.Errors, 0, "There should be no errors after gather") + require.Empty(t, acc1.Errors, "There should be no errors after gather") for _, s := range testSimpleData[0].services { fields := make(map[string]interface{}) diff --git a/plugins/inputs/x509_cert/x509_cert_test.go b/plugins/inputs/x509_cert/x509_cert_test.go index cd174a6098cb3..bed7a7f6b04ca 100644 --- a/plugins/inputs/x509_cert/x509_cert_test.go +++ b/plugins/inputs/x509_cert/x509_cert_test.go @@ -330,7 +330,7 @@ func TestGatherUDPCertIntegration(t *testing.T) { var acc testutil.Accumulator require.NoError(t, m.Gather(&acc)) - require.Len(t, acc.Errors, 0) + require.Empty(t, acc.Errors) require.True(t, acc.HasMeasurement("x509_cert")) require.True(t, acc.HasTag("x509_cert", "ocsp_stapled")) } @@ -350,7 +350,7 @@ func TestGatherTCPCert(t *testing.T) { var acc testutil.Accumulator require.NoError(t, m.Gather(&acc)) - require.Len(t, acc.Errors, 0) + require.Empty(t, acc.Errors) require.True(t, acc.HasMeasurement("x509_cert")) } diff --git a/plugins/inputs/xtremio/xtremio_test.go b/plugins/inputs/xtremio/xtremio_test.go index 2269e4ed576dc..81aa54c9675d5 100644 --- a/plugins/inputs/xtremio/xtremio_test.go +++ b/plugins/inputs/xtremio/xtremio_test.go @@ -143,7 +143,7 @@ func TestFixedValue(t *testing.T) { tt.plugin.Log = testutil.Logger{} require.NoError(t, tt.plugin.Init()) require.NoError(t, tt.plugin.Gather(&acc)) - require.Len(t, acc.Errors, 0, "found errors accumulated by acc.AddError()") + require.Empty(t, acc.Errors, "found errors accumulated by acc.AddError()") acc.Wait(len(tt.expected)) testutil.RequireMetricsEqual(t, tt.expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime()) }) diff --git a/plugins/outputs/azure_monitor/azure_monitor_test.go b/plugins/outputs/azure_monitor/azure_monitor_test.go index 139a0789393e1..b954859094ad8 100644 --- a/plugins/outputs/azure_monitor/azure_monitor_test.go +++ b/plugins/outputs/azure_monitor/azure_monitor_test.go @@ -47,7 +47,7 @@ func TestAggregate(t *testing.T) { pushTime: time.Unix(3600, 0), check: func(t *testing.T, plugin *AzureMonitor, metrics []telegraf.Metric) { require.Equal(t, int64(1), plugin.MetricOutsideWindow.Get()) - require.Len(t, metrics, 0) + require.Empty(t, metrics) }, }, { @@ -70,7 +70,7 @@ func TestAggregate(t *testing.T) { addTime: time.Unix(0, 0), pushTime: time.Unix(0, 0), check: func(t *testing.T, plugin *AzureMonitor, metrics []telegraf.Metric) { - require.Len(t, metrics, 0) + require.Empty(t, metrics) }, }, { diff --git a/plugins/outputs/cloudwatch/cloudwatch_test.go b/plugins/outputs/cloudwatch/cloudwatch_test.go index 0dc2614ae59f4..89e31c038829a 100644 --- a/plugins/outputs/cloudwatch/cloudwatch_test.go +++ b/plugins/outputs/cloudwatch/cloudwatch_test.go @@ -72,7 +72,7 @@ func TestBuildMetricDatums(t *testing.T) { } for _, point := range invalidMetrics { datums := BuildMetricDatum(false, false, point) - require.Equal(t, 0, len(datums), fmt.Sprintf("Valid point should not create a Datum {value: %v}", point)) + require.Empty(t, datums, fmt.Sprintf("Valid point should not create a Datum {value: %v}", point)) } statisticMetric := metric.New( diff --git a/plugins/outputs/groundwork/groundwork_test.go b/plugins/outputs/groundwork/groundwork_test.go index 111ccfdf9e35f..91755971914d0 100644 --- a/plugins/outputs/groundwork/groundwork_test.go +++ b/plugins/outputs/groundwork/groundwork_test.go @@ -44,7 +44,7 @@ func TestWriteWithDefaults(t *testing.T) { require.Equal(t, transit.MonitorStatus("SERVICE_OK"), obj.Resources[0].Services[0].Status) require.Equal(t, "IntMetric", obj.Resources[0].Services[0].Name) require.Equal(t, int64(42), *obj.Resources[0].Services[0].Metrics[0].Value.IntegerValue) - require.Equal(t, 0, len(obj.Groups)) + require.Empty(t, obj.Groups) _, err = fmt.Fprintln(w, "OK") require.NoError(t, err) diff --git a/plugins/outputs/http/http_test.go b/plugins/outputs/http/http_test.go index 4418073a9978a..cd1a6e408a1b0 100644 --- a/plugins/outputs/http/http_test.go +++ b/plugins/outputs/http/http_test.go @@ -468,7 +468,7 @@ func TestOAuthClientCredentialsGrant(t *testing.T) { URL: u.String(), }, handler: func(t *testing.T, w http.ResponseWriter, r *http.Request) { - require.Len(t, r.Header["Authorization"], 0) + require.Empty(t, r.Header["Authorization"]) w.WriteHeader(http.StatusOK) }, }, @@ -578,7 +578,7 @@ func TestOAuthAuthorizationCodeGrant(t *testing.T) { URL: u.String(), }, handler: func(t *testing.T, w http.ResponseWriter, r *http.Request) { - require.Len(t, r.Header["Authorization"], 0) + require.Empty(t, r.Header["Authorization"]) w.WriteHeader(http.StatusOK) }, }, diff --git a/plugins/outputs/loki/loki_test.go b/plugins/outputs/loki/loki_test.go index b7a10ee471795..1de4297d2ceb5 100644 --- a/plugins/outputs/loki/loki_test.go +++ b/plugins/outputs/loki/loki_test.go @@ -356,7 +356,7 @@ func TestOAuthClientCredentialsGrant(t *testing.T) { Domain: u.String(), }, handler: func(t *testing.T, w http.ResponseWriter, r *http.Request) { - require.Len(t, r.Header["Authorization"], 0) + require.Empty(t, r.Header["Authorization"]) w.WriteHeader(http.StatusOK) }, }, diff --git a/plugins/outputs/riemann/riemann_test.go b/plugins/outputs/riemann/riemann_test.go index b56fb33e114be..e775f6f1a442d 100644 --- a/plugins/outputs/riemann/riemann_test.go +++ b/plugins/outputs/riemann/riemann_test.go @@ -4,11 +4,11 @@ import ( "testing" "time" - "github.com/influxdata/telegraf/testutil" - "github.com/amir/raidman" - "github.com/influxdata/telegraf/metric" "github.com/stretchr/testify/require" + + "github.com/influxdata/telegraf/metric" + "github.com/influxdata/telegraf/testutil" ) func TestAttributes(t *testing.T) { @@ -146,7 +146,7 @@ func TestStateEvents(t *testing.T) { events := r.buildRiemannEvents(m) // no event should be present - require.Len(t, events, 0) + require.Empty(t, events) // enable string metrics as event states r.StringAsState = true diff --git a/plugins/outputs/wavefront/wavefront_test.go b/plugins/outputs/wavefront/wavefront_test.go index afa1c8a3aa250..b8f032b97216c 100644 --- a/plugins/outputs/wavefront/wavefront_test.go +++ b/plugins/outputs/wavefront/wavefront_test.go @@ -439,7 +439,7 @@ func TestMakeAuthOptions(t *testing.T) { noAuthOptionsWavefront := outputs.Outputs["wavefront"]().(*Wavefront) options, err = noAuthOptionsWavefront.makeAuthOptions() require.NoError(t, err) - require.Equal(t, 0, len(options)) + require.Empty(t, options) } // Benchmarks to test performance of string replacement via Regex and Sanitize diff --git a/plugins/parsers/csv/parser_test.go b/plugins/parsers/csv/parser_test.go index d8dbcbe02cc54..c5d8265f25ca4 100644 --- a/plugins/parsers/csv/parser_test.go +++ b/plugins/parsers/csv/parser_test.go @@ -509,7 +509,7 @@ func TestParseStream(t *testing.T) { metrics, err := p.Parse([]byte(csvHeader)) require.NoError(t, err) - require.Len(t, metrics, 0) + require.Empty(t, metrics) m, err := p.ParseLine(csvBody) require.NoError(t, err) testutil.RequireMetricEqual(t, @@ -539,7 +539,7 @@ func TestParseLineMultiMetricErrorMessage(t *testing.T) { metrics, err := p.Parse([]byte(csvHeader)) require.NoError(t, err) - require.Len(t, metrics, 0) + require.Empty(t, metrics) m, err := p.ParseLine(csvOneRow) require.NoError(t, err) testutil.RequireMetricEqual(t, @@ -870,7 +870,7 @@ func TestParseMetadataSeparators(t *testing.T) { err = p.Init() require.NoError(t, err) require.Len(t, p.metadataSeparatorList, 4) - require.Len(t, p.MetadataTrimSet, 0) + require.Empty(t, p.MetadataTrimSet) require.Equal(t, p.metadataSeparatorList, metadataPattern{":=", ",", "=", ":"}) p = &Parser{ ColumnNames: []string{"a", "b"}, diff --git a/plugins/parsers/dropwizard/parser_test.go b/plugins/parsers/dropwizard/parser_test.go index 1d0565c7a5fa3..e8eb56c87b28a 100644 --- a/plugins/parsers/dropwizard/parser_test.go +++ b/plugins/parsers/dropwizard/parser_test.go @@ -5,12 +5,11 @@ import ( "testing" "time" - "github.com/influxdata/telegraf/testutil" - "github.com/stretchr/testify/require" "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/metric" + "github.com/influxdata/telegraf/testutil" ) // validEmptyJSON is a valid dropwizard json document, but without any metrics @@ -32,7 +31,7 @@ func TestParseValidEmptyJSON(t *testing.T) { // Most basic vanilla test metrics, err := parser.Parse([]byte(validEmptyJSON)) require.NoError(t, err) - require.Len(t, metrics, 0) + require.Empty(t, metrics) } // validCounterJSON is a valid dropwizard json document containing one counter diff --git a/plugins/parsers/form_urlencoded/parser_test.go b/plugins/parsers/form_urlencoded/parser_test.go index 931d5a4caeea8..45daadad078ee 100644 --- a/plugins/parsers/form_urlencoded/parser_test.go +++ b/plugins/parsers/form_urlencoded/parser_test.go @@ -133,7 +133,7 @@ func TestParseInvalidFormDataError(t *testing.T) { metrics, err := parser.Parse([]byte(notEscapedProperlyFormData)) require.Error(t, err) - require.Len(t, metrics, 0) + require.Empty(t, metrics) } func TestParseInvalidFormDataEmptyKey(t *testing.T) { @@ -168,5 +168,5 @@ func TestParseInvalidFormDataEmptyString(t *testing.T) { metrics, err := parser.Parse([]byte(emptyFormData)) require.NoError(t, err) - require.Len(t, metrics, 0) + require.Empty(t, metrics) } diff --git a/plugins/parsers/json/parser_test.go b/plugins/parsers/json/parser_test.go index f2fe66914b616..3f6b1ecbb64e9 100644 --- a/plugins/parsers/json/parser_test.go +++ b/plugins/parsers/json/parser_test.go @@ -116,12 +116,12 @@ func TestParseValidJSON(t *testing.T) { // Test that whitespace only will parse as an empty list of metrics metrics, err = parser.Parse([]byte("\n\t")) require.NoError(t, err) - require.Len(t, metrics, 0) + require.Empty(t, metrics) // Test that an empty string will parse as an empty list of metrics metrics, err = parser.Parse([]byte("")) require.NoError(t, err) - require.Len(t, metrics, 0) + require.Empty(t, metrics) } func TestParseLineValidJSON(t *testing.T) { @@ -784,7 +784,7 @@ func TestTimeErrors(t *testing.T) { metrics, err := parser.Parse([]byte(testString)) require.Error(t, err) - require.Equal(t, 0, len(metrics)) + require.Empty(t, metrics) testString2 := `{ "a": 5, @@ -804,7 +804,7 @@ func TestTimeErrors(t *testing.T) { metrics, err = parser.Parse([]byte(testString2)) require.Error(t, err) - require.Equal(t, 0, len(metrics)) + require.Empty(t, metrics) require.Equal(t, fmt.Errorf("JSON time key could not be found"), err) } diff --git a/plugins/parsers/json_v2/parser_test.go b/plugins/parsers/json_v2/parser_test.go index 576adbdf475bf..b0749d9922855 100644 --- a/plugins/parsers/json_v2/parser_test.go +++ b/plugins/parsers/json_v2/parser_test.go @@ -8,13 +8,14 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" + "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/config" "github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs/file" "github.com/influxdata/telegraf/plugins/parsers/influx" "github.com/influxdata/telegraf/testutil" - "github.com/stretchr/testify/require" ) func TestMultipleConfigs(t *testing.T) { @@ -22,7 +23,7 @@ func TestMultipleConfigs(t *testing.T) { folders, err := os.ReadDir("testdata") require.NoError(t, err) // Make sure testdata contains data - require.Greater(t, len(folders), 0) + require.NotEmpty(t, folders) // Setup influx parser for parsing the expected metrics parser := &influx.Parser{} diff --git a/plugins/parsers/xpath/parser_test.go b/plugins/parsers/xpath/parser_test.go index 752276c6ffbf5..e8ba234ae3a39 100644 --- a/plugins/parsers/xpath/parser_test.go +++ b/plugins/parsers/xpath/parser_test.go @@ -1320,7 +1320,7 @@ func TestTestCases(t *testing.T) { require.Len(t, input, 1) filefields := strings.Fields(input[0]) - require.GreaterOrEqual(t, len(filefields), 1) + require.NotEmpty(t, filefields) datafile := filepath.FromSlash(filefields[0]) fileformat := "" if len(filefields) > 1 { diff --git a/plugins/processors/aws/ec2/ec2_test.go b/plugins/processors/aws/ec2/ec2_test.go index 03dfa9df1bfbd..385f535f91b8b 100644 --- a/plugins/processors/aws/ec2/ec2_test.go +++ b/plugins/processors/aws/ec2/ec2_test.go @@ -4,9 +4,10 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" + "github.com/influxdata/telegraf/config" "github.com/influxdata/telegraf/testutil" - "github.com/stretchr/testify/require" ) func TestBasicStartup(t *testing.T) { @@ -16,8 +17,8 @@ func TestBasicStartup(t *testing.T) { acc := &testutil.Accumulator{} require.NoError(t, p.Init()) - require.Len(t, acc.GetTelegrafMetrics(), 0) - require.Len(t, acc.Errors, 0) + require.Empty(t, acc.GetTelegrafMetrics()) + require.Empty(t, acc.Errors) } func TestBasicStartupWithEC2Tags(t *testing.T) { @@ -28,8 +29,8 @@ func TestBasicStartupWithEC2Tags(t *testing.T) { acc := &testutil.Accumulator{} require.NoError(t, p.Init()) - require.Len(t, acc.GetTelegrafMetrics(), 0) - require.Len(t, acc.Errors, 0) + require.Empty(t, acc.GetTelegrafMetrics()) + require.Empty(t, acc.Errors) } func TestBasicStartupWithCacheTTL(t *testing.T) { @@ -40,8 +41,8 @@ func TestBasicStartupWithCacheTTL(t *testing.T) { acc := &testutil.Accumulator{} require.NoError(t, p.Init()) - require.Len(t, acc.GetTelegrafMetrics(), 0) - require.Len(t, acc.Errors, 0) + require.Empty(t, acc.GetTelegrafMetrics()) + require.Empty(t, acc.Errors) } func TestBasicStartupWithTagCacheSize(t *testing.T) { @@ -52,8 +53,8 @@ func TestBasicStartupWithTagCacheSize(t *testing.T) { acc := &testutil.Accumulator{} require.NoError(t, p.Init()) - require.Len(t, acc.GetTelegrafMetrics(), 0) - require.Len(t, acc.Errors, 0) + require.Empty(t, acc.GetTelegrafMetrics()) + require.Empty(t, acc.Errors) } func TestBasicInitNoTagsReturnAnError(t *testing.T) { diff --git a/plugins/processors/converter/converter_test.go b/plugins/processors/converter/converter_test.go index 09628a2c899cd..2d21ca9a14ca5 100644 --- a/plugins/processors/converter/converter_test.go +++ b/plugins/processors/converter/converter_test.go @@ -5,9 +5,10 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" + "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/testutil" - "github.com/stretchr/testify/require" ) func TestConverter(t *testing.T) { @@ -702,7 +703,7 @@ func TestMultipleTimestamps(t *testing.T) { result := c.Apply(input) require.Len(t, result, 1) - require.Len(t, result[0].TagList(), 0) + require.Empty(t, result[0].TagList()) require.Len(t, result[0].FieldList(), 1) } diff --git a/plugins/processors/dedup/dedup_test.go b/plugins/processors/dedup/dedup_test.go index 7a2904689ed8a..c8a78b76ecaf6 100644 --- a/plugins/processors/dedup/dedup_test.go +++ b/plugins/processors/dedup/dedup_test.go @@ -34,7 +34,7 @@ func assertCacheRefresh(t *testing.T, proc *Dedup, item telegraf.Metric) { id := item.HashID() name := item.Name() // cache is not empty - require.NotEqual(t, 0, len(proc.Cache)) + require.NotEmpty(t, proc.Cache) // cache has metric with proper id cache, present := proc.Cache[id] require.True(t, present) @@ -53,7 +53,7 @@ func assertCacheHit(t *testing.T, proc *Dedup, item telegraf.Metric) { id := item.HashID() name := item.Name() // cache is not empty - require.NotEqual(t, 0, len(proc.Cache)) + require.NotEmpty(t, proc.Cache) // cache has metric with proper id cache, present := proc.Cache[id] require.True(t, present) @@ -70,7 +70,7 @@ func assertCacheHit(t *testing.T, proc *Dedup, item telegraf.Metric) { func assertMetricPassed(t *testing.T, target []telegraf.Metric, source telegraf.Metric) { // target is not empty - require.NotEqual(t, 0, len(target)) + require.NotEmpty(t, target) // target has metric with proper name require.Equal(t, metricName, target[0].Name()) // target metric has proper field @@ -85,7 +85,7 @@ func assertMetricPassed(t *testing.T, target []telegraf.Metric, source telegraf. func assertMetricSuppressed(t *testing.T, target []telegraf.Metric) { // target is empty - require.Equal(t, 0, len(target)) + require.Empty(t, target) } func TestProcRetainsMetric(t *testing.T) { @@ -156,7 +156,7 @@ func TestCacheShrink(t *testing.T) { source := createMetric(1, time.Now().Add(-1*time.Hour)) deduplicate.Apply(source) - require.Equal(t, 0, len(deduplicate.Cache)) + require.Empty(t, deduplicate.Cache) } func TestSameTimestamp(t *testing.T) { diff --git a/plugins/processors/ifname/ttl_cache_test.go b/plugins/processors/ifname/ttl_cache_test.go index 8ae57d6df9265..5e6cae32e0d3f 100644 --- a/plugins/processors/ifname/ttl_cache_test.go +++ b/plugins/processors/ifname/ttl_cache_test.go @@ -23,7 +23,7 @@ func TestTTLCacheExpire(t *testing.T) { _, ok, _ := c.Get("ones") require.False(t, ok) - require.Len(t, c.lru.m, 0) + require.Empty(t, c.lru.m) require.Equal(t, c.lru.l.Len(), 0) } diff --git a/plugins/processors/reverse_dns/rdnscache_test.go b/plugins/processors/reverse_dns/rdnscache_test.go index b717e64efc4dc..eff64c2e6db01 100644 --- a/plugins/processors/reverse_dns/rdnscache_test.go +++ b/plugins/processors/reverse_dns/rdnscache_test.go @@ -105,7 +105,7 @@ func TestCleanupHappens(t *testing.T) { time.Sleep(ttl) // wait for cache entry to expire. d.cleanup() - require.Len(t, d.expireList, 0) + require.Empty(t, d.expireList) stats := d.Stats() diff --git a/plugins/serializers/msgpack/msgpack_test.go b/plugins/serializers/msgpack/msgpack_test.go index dc0ecf9432699..a8ec169e5db71 100644 --- a/plugins/serializers/msgpack/msgpack_test.go +++ b/plugins/serializers/msgpack/msgpack_test.go @@ -27,7 +27,7 @@ func TestSerializeMetricInt(t *testing.T) { left, err := m2.UnmarshalMsg(buf) require.NoError(t, err) - require.Equal(t, len(left), 0) + require.Empty(t, left) testutil.RequireMetricEqual(t, m, toTelegrafMetric(*m2)) } @@ -44,7 +44,7 @@ func TestSerializeMetricString(t *testing.T) { left, err := m2.UnmarshalMsg(buf) require.NoError(t, err) - require.Equal(t, len(left), 0) + require.Empty(t, left) testutil.RequireMetricEqual(t, m, toTelegrafMetric(*m2)) } @@ -62,7 +62,7 @@ func TestSerializeMultiFields(t *testing.T) { left, err := m2.UnmarshalMsg(buf) require.NoError(t, err) - require.Equal(t, len(left), 0) + require.Empty(t, left) testutil.RequireMetricEqual(t, m, toTelegrafMetric(*m2)) } @@ -81,7 +81,7 @@ func TestSerializeMetricWithEscapes(t *testing.T) { left, err := m2.UnmarshalMsg(buf) require.NoError(t, err) - require.Equal(t, len(left), 0) + require.Empty(t, left) testutil.RequireMetricEqual(t, m, toTelegrafMetric(*m2)) } From 4e84fc8925025e9a3d9a250686f1d2811418fcc5 Mon Sep 17 00:00:00 2001 From: Robert <48691478+ke55ler@users.noreply.github.com> Date: Wed, 25 Oct 2023 23:13:37 +0200 Subject: [PATCH 4/7] feat(socket_listener): Add vsock support to socket listener and writer (#14172) --- docs/LICENSE_OF_DEPENDENCIES.md | 1 + go.mod | 1 + go.sum | 2 + plugins/inputs/socket_listener/README.md | 1 + plugins/inputs/socket_listener/sample.conf | 1 + .../inputs/socket_listener/socket_listener.go | 16 ++++++++ .../inputs/socket_listener/stream_listener.go | 27 +++++++++++++ plugins/outputs/socket_writer/README.md | 1 + plugins/outputs/socket_writer/sample.conf | 1 + .../outputs/socket_writer/socket_writer.go | 40 ++++++++++++++++--- 10 files changed, 85 insertions(+), 6 deletions(-) diff --git a/docs/LICENSE_OF_DEPENDENCIES.md b/docs/LICENSE_OF_DEPENDENCIES.md index 2bc9233fadcfa..1b289f69fc918 100644 --- a/docs/LICENSE_OF_DEPENDENCIES.md +++ b/docs/LICENSE_OF_DEPENDENCIES.md @@ -242,6 +242,7 @@ following works: - github.com/mdlayher/genetlink [MIT License](https://github.com/mdlayher/genetlink/blob/master/LICENSE.md) - github.com/mdlayher/netlink [MIT License](https://github.com/mdlayher/netlink/blob/master/LICENSE.md) - github.com/mdlayher/socket [MIT License](https://github.com/mdlayher/socket/blob/master/LICENSE.md) +- github.com/mdlayher/vsock [MIT License](https://github.com/mdlayher/vsock/blob/master/LICENSE.md) - github.com/microsoft/ApplicationInsights-Go [MIT License](https://github.com/microsoft/ApplicationInsights-Go/blob/master/LICENSE) - github.com/microsoft/go-mssqldb [BSD 3-Clause "New" or "Revised" License](https://github.com/microsoft/go-mssqldb/blob/master/LICENSE.txt) - github.com/miekg/dns [BSD 3-Clause Clear License](https://github.com/miekg/dns/blob/master/LICENSE) diff --git a/go.mod b/go.mod index 53d8c83bf7e8f..3ef999ec389dc 100644 --- a/go.mod +++ b/go.mod @@ -128,6 +128,7 @@ require ( github.com/lxc/lxd v0.0.0-20220920163450-e9b4b514106a github.com/matttproud/golang_protobuf_extensions v1.0.4 github.com/mdlayher/apcupsd v0.0.0-20220319200143-473c7b5f3c6a + github.com/mdlayher/vsock v1.1.1 github.com/microsoft/ApplicationInsights-Go v0.4.4 github.com/microsoft/go-mssqldb v1.5.0 github.com/miekg/dns v1.1.56 diff --git a/go.sum b/go.sum index 46f273582492a..885cb5fbaa97e 100644 --- a/go.sum +++ b/go.sum @@ -1095,6 +1095,8 @@ github.com/mdlayher/socket v0.0.0-20211102153432-57e3fa563ecb/go.mod h1:nFZ1EtZY github.com/mdlayher/socket v0.1.1/go.mod h1:mYV5YIZAfHh4dzDVzI8x8tWLWCliuX8Mon5Awbj+qDs= github.com/mdlayher/socket v0.2.3 h1:XZA2X2TjdOwNoNPVPclRCURoX/hokBY8nkTmRZFEheM= github.com/mdlayher/socket v0.2.3/go.mod h1:bz12/FozYNH/VbvC3q7TRIK/Y6dH1kCKsXaUeXi/FmY= +github.com/mdlayher/vsock v1.1.1 h1:8lFuiXQnmICBrCIIA9PMgVSke6Fg6V4+r0v7r55k88I= +github.com/mdlayher/vsock v1.1.1/go.mod h1:Y43jzcy7KM3QB+/FK15pfqGxDMCMzUXWegEfIbSM18U= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= github.com/mholt/archiver/v3 v3.5.0/go.mod h1:qqTTPUK/HZPFgFQ/TJ3BzvTpF/dPtFVJXdQbCmeMxwc= github.com/microsoft/ApplicationInsights-Go v0.4.4 h1:G4+H9WNs6ygSCe6sUyxRc2U81TI5Es90b2t/MwX5KqY= diff --git a/plugins/inputs/socket_listener/README.md b/plugins/inputs/socket_listener/README.md index 3bc1daa7381d4..77dd2546dc76f 100644 --- a/plugins/inputs/socket_listener/README.md +++ b/plugins/inputs/socket_listener/README.md @@ -42,6 +42,7 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details. # service_address = "udp6://:8094" # service_address = "unix:///tmp/telegraf.sock" # service_address = "unixgram:///tmp/telegraf.sock" + # service_address = "vsock://cid:port" ## Change the file mode bits on unix sockets. These permissions may not be ## respected by some platforms, to safely restrict write permissions it is best diff --git a/plugins/inputs/socket_listener/sample.conf b/plugins/inputs/socket_listener/sample.conf index ed9bf9aaa94ad..40837316d2c19 100644 --- a/plugins/inputs/socket_listener/sample.conf +++ b/plugins/inputs/socket_listener/sample.conf @@ -11,6 +11,7 @@ # service_address = "udp6://:8094" # service_address = "unix:///tmp/telegraf.sock" # service_address = "unixgram:///tmp/telegraf.sock" + # service_address = "vsock://cid:port" ## Change the file mode bits on unix sockets. These permissions may not be ## respected by some platforms, to safely restrict write permissions it is best diff --git a/plugins/inputs/socket_listener/socket_listener.go b/plugins/inputs/socket_listener/socket_listener.go index c4a03314d152b..b57c29c149612 100644 --- a/plugins/inputs/socket_listener/socket_listener.go +++ b/plugins/inputs/socket_listener/socket_listener.go @@ -223,6 +223,22 @@ func (sl *SocketListener) Start(acc telegraf.Accumulator) error { return err } sl.listener = psl + case "vsock": + ssl := &streamListener{ + ReadBufferSize: int(sl.ReadBufferSize), + ReadTimeout: sl.ReadTimeout, + KeepAlivePeriod: sl.KeepAlivePeriod, + MaxConnections: sl.MaxConnections, + Encoding: sl.ContentEncoding, + Splitter: sl.splitter, + Parser: sl.parser, + Log: sl.Log, + } + + if err := ssl.setupVsock(u); err != nil { + return err + } + sl.listener = ssl default: return fmt.Errorf("unknown protocol %q in %q", u.Scheme, sl.ServiceAddress) } diff --git a/plugins/inputs/socket_listener/stream_listener.go b/plugins/inputs/socket_listener/stream_listener.go index 960027c5e4ffe..88a3137c23962 100644 --- a/plugins/inputs/socket_listener/stream_listener.go +++ b/plugins/inputs/socket_listener/stream_listener.go @@ -6,10 +6,12 @@ import ( "errors" "fmt" "io" + "math" "net" "net/url" "os" "strconv" + "strings" "sync" "syscall" "time" @@ -17,6 +19,7 @@ import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/config" "github.com/influxdata/telegraf/internal" + "github.com/mdlayher/vsock" ) type hasSetReadBuffer interface { @@ -83,6 +86,30 @@ func (l *streamListener) setupUnix(u *url.URL, tlsCfg *tls.Config, socketMode st return nil } +func (l *streamListener) setupVsock(u *url.URL) error { + var err error + + addrTuple := strings.SplitN(u.String(), ":", 2) + + // Check address string for containing two tokens + if len(addrTuple) < 2 { + return fmt.Errorf("CID and/or port number missing") + } + // Parse CID and port number from address string both being 32-bit + // source: https://man7.org/linux/man-pages/man7/vsock.7.html + cid, _ := strconv.ParseUint(addrTuple[0], 10, 32) + if (cid >= uint64(math.Pow(2, 32))-1) && (cid <= 0) { + return fmt.Errorf("CID %d is out of range", cid) + } + port, _ := strconv.ParseUint(addrTuple[1], 10, 32) + if (port >= uint64(math.Pow(2, 32))-1) && (port <= 0) { + return fmt.Errorf("Port numner %d is out of range", port) + } + + l.listener, err = vsock.Listen(uint32(port), nil) + return err +} + func (l *streamListener) setupConnection(conn net.Conn) error { if c, ok := conn.(*tls.Conn); ok { conn = c.NetConn() diff --git a/plugins/outputs/socket_writer/README.md b/plugins/outputs/socket_writer/README.md index b65528f2f0ede..4be52e73338f1 100644 --- a/plugins/outputs/socket_writer/README.md +++ b/plugins/outputs/socket_writer/README.md @@ -31,6 +31,7 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details. # address = "udp6://127.0.0.1:8094" # address = "unix:///tmp/telegraf.sock" # address = "unixgram:///tmp/telegraf.sock" + # address = "vsock://cid:port" ## Optional TLS Config # tls_ca = "/etc/telegraf/ca.pem" diff --git a/plugins/outputs/socket_writer/sample.conf b/plugins/outputs/socket_writer/sample.conf index cf84621f4deb6..341bab1543efd 100644 --- a/plugins/outputs/socket_writer/sample.conf +++ b/plugins/outputs/socket_writer/sample.conf @@ -11,6 +11,7 @@ # address = "udp6://127.0.0.1:8094" # address = "unix:///tmp/telegraf.sock" # address = "unixgram:///tmp/telegraf.sock" + # address = "vsock://cid:port" ## Optional TLS Config # tls_ca = "/etc/telegraf/ca.pem" diff --git a/plugins/outputs/socket_writer/socket_writer.go b/plugins/outputs/socket_writer/socket_writer.go index b45fe685da651..a7a64bfa307c4 100644 --- a/plugins/outputs/socket_writer/socket_writer.go +++ b/plugins/outputs/socket_writer/socket_writer.go @@ -6,7 +6,9 @@ import ( _ "embed" "errors" "fmt" + "math" "net" + "strconv" "strings" "time" @@ -16,6 +18,7 @@ import ( tlsint "github.com/influxdata/telegraf/plugins/common/tls" "github.com/influxdata/telegraf/plugins/outputs" "github.com/influxdata/telegraf/plugins/serializers" + "github.com/mdlayher/vsock" ) //go:embed sample.conf @@ -55,13 +58,38 @@ func (sw *SocketWriter) Connect() error { } var c net.Conn - if tlsCfg == nil { - c, err = net.Dial(spl[0], spl[1]) + + if spl[0] == "vsock" { + addrTuple := strings.SplitN(spl[1], ":", 2) + + // Check address string for containing two + if len(addrTuple) < 2 { + return fmt.Errorf("CID and/or port number missing") + } + + // Parse CID and port number from address string both being 32-bit + // source: https://man7.org/linux/man-pages/man7/vsock.7.html + cid, _ := strconv.ParseUint(addrTuple[0], 10, 32) + if (cid >= uint64(math.Pow(2, 32))-1) && (cid <= 0) { + return fmt.Errorf("CID %d is out of range", cid) + } + port, _ := strconv.ParseUint(addrTuple[1], 10, 32) + if (port >= uint64(math.Pow(2, 32))-1) && (port <= 0) { + return fmt.Errorf("Port numner %d is out of range", port) + } + c, err = vsock.Dial(uint32(cid), uint32(port), nil) + if err != nil { + return err + } } else { - c, err = tls.Dial(spl[0], spl[1], tlsCfg) - } - if err != nil { - return err + if tlsCfg == nil { + c, err = net.Dial(spl[0], spl[1]) + } else { + c, err = tls.Dial(spl[0], spl[1], tlsCfg) + } + if err != nil { + return err + } } if err := sw.setKeepAlive(c); err != nil { From 57eb71688f7984ab053284885aee15115d411a53 Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Wed, 25 Oct 2023 23:21:06 +0200 Subject: [PATCH 5/7] fix(inputs.zfs): Parse metrics correctly on FreeBSD 14 (#14176) --- .../testcases/freebsd/freebsd14/expected.out | 16 ++ .../testcases/freebsd/freebsd14/sysctl.json | 159 +++++++++++ .../testcases/freebsd/freebsd14/telegraf.conf | 4 + .../zfs/testcases/freebsd/freebsd14/uname.txt | 1 + .../testcases/freebsd/freebsd14/zdataset.txt | 14 + .../zfs/testcases/freebsd/freebsd14/zpool.txt | 1 + plugins/inputs/zfs/zfs.go | 10 +- plugins/inputs/zfs/zfs_freebsd.go | 264 +++++++++++------- plugins/inputs/zfs/zfs_freebsd_test.go | 12 + 9 files changed, 373 insertions(+), 108 deletions(-) create mode 100644 plugins/inputs/zfs/testcases/freebsd/freebsd14/expected.out create mode 100644 plugins/inputs/zfs/testcases/freebsd/freebsd14/sysctl.json create mode 100644 plugins/inputs/zfs/testcases/freebsd/freebsd14/telegraf.conf create mode 100644 plugins/inputs/zfs/testcases/freebsd/freebsd14/uname.txt create mode 100644 plugins/inputs/zfs/testcases/freebsd/freebsd14/zdataset.txt create mode 100644 plugins/inputs/zfs/testcases/freebsd/freebsd14/zpool.txt diff --git a/plugins/inputs/zfs/testcases/freebsd/freebsd14/expected.out b/plugins/inputs/zfs/testcases/freebsd/freebsd14/expected.out new file mode 100644 index 0000000000000..2e9b581e079a0 --- /dev/null +++ b/plugins/inputs/zfs/testcases/freebsd/freebsd14/expected.out @@ -0,0 +1,16 @@ +zfs_pool,health=ONLINE,pool=zroot allocated=11959578624i,capacity=17i,dedupratio=1,fragmentation=4i,free=55149285376i,size=67108864000i 1698172931876196974 +zfs_dataset,dataset=zroot avail=53069045760i,used=11959443456i,usedds=0i,usedsnap=0i 1698172931876204237 +zfs_dataset,dataset=zroot/ROOT avail=53069045760i,used=4685123584i,usedds=0i,usedsnap=0i 1698172931876207310 +zfs_dataset,dataset=zroot/ROOT/default avail=53069045760i,used=4684689408i,usedds=0i,usedsnap=0i 1698172931876213735 +zfs_dataset,dataset=zroot/home avail=53069045760i,used=7263580160i,usedds=0i,usedsnap=0i 1698172931876217926 +zfs_dataset,dataset=zroot/tmp avail=53069045760i,used=684032i,usedds=0i,usedsnap=0i 1698172931876220720 +zfs_dataset,dataset=zroot/usr avail=53069045760i,used=1724416i,usedds=0i,usedsnap=0i 1698172931876224631 +zfs_dataset,dataset=zroot/usr/obj avail=53069045760i,used=430080i,usedds=0i,usedsnap=0i 1698172931876227424 +zfs_dataset,dataset=zroot/usr/ports avail=53069045760i,used=430080i,usedds=0i,usedsnap=0i 1698172931876230218 +zfs_dataset,dataset=zroot/usr/src avail=53069045760i,used=430080i,usedds=0i,usedsnap=0i 1698172931876233291 +zfs_dataset,dataset=zroot/var avail=53069045760i,used=2269184i,usedds=0i,usedsnap=0i 1698172931876237481 +zfs_dataset,dataset=zroot/var/audit avail=53069045760i,used=438272i,usedds=0i,usedsnap=0i 1698172931876240554 +zfs_dataset,dataset=zroot/var/log avail=53069045760i,used=544768i,usedds=0i,usedsnap=0i 1698172931876243348 +zfs_dataset,dataset=zroot/var/mail avail=53069045760i,used=425984i,usedds=0i,usedsnap=0i 1698172931876246980 +zfs_dataset,dataset=zroot/var/tmp avail=53069045760i,used=425984i,usedds=0i,usedsnap=0i 1698172931876249774 +zfs,datasets=zroot::zroot/ROOT::zroot/ROOT/default::zroot/home::zroot/tmp::zroot/usr::zroot/usr/obj::zroot/usr/ports::zroot/usr/src::zroot/var::zroot/var/audit::zroot/var/log::zroot/var/mail::zroot/var/tmp,pools=zroot zfetchstats_hits=6439i,zfetchstats_io_active=0i,zfetchstats_io_issued=2197i,zfetchstats_max_streams=9411i,zfetchstats_misses=14538i 1698172931876287767 diff --git a/plugins/inputs/zfs/testcases/freebsd/freebsd14/sysctl.json b/plugins/inputs/zfs/testcases/freebsd/freebsd14/sysctl.json new file mode 100644 index 0000000000000..c4aa784002437 --- /dev/null +++ b/plugins/inputs/zfs/testcases/freebsd/freebsd14/sysctl.json @@ -0,0 +1,159 @@ +{ + "archstats": [ + "kstat.zfs.misc.arcstats.abd_chunk_waste_size: 626176", + "kstat.zfs.misc.arcstats.cached_only_in_progress: 0", + "kstat.zfs.misc.arcstats.arc_raw_size: 0", + "kstat.zfs.misc.arcstats.arc_sys_free: 0", + "kstat.zfs.misc.arcstats.arc_need_free: 0", + "kstat.zfs.misc.arcstats.demand_iohit_prescient_prefetch: 1", + "kstat.zfs.misc.arcstats.demand_hit_prescient_prefetch: 14", + "kstat.zfs.misc.arcstats.prescient_prefetch: 15", + "kstat.zfs.misc.arcstats.demand_iohit_predictive_prefetch: 218", + "kstat.zfs.misc.arcstats.demand_hit_predictive_prefetch: 1778", + "kstat.zfs.misc.arcstats.predictive_prefetch: 5099", + "kstat.zfs.misc.arcstats.async_upgrade_sync: 135", + "kstat.zfs.misc.arcstats.arc_dnode_limit: 317881139", + "kstat.zfs.misc.arcstats.arc_meta_used: 80185232", + "kstat.zfs.misc.arcstats.arc_prune: 25", + "kstat.zfs.misc.arcstats.arc_loaned_bytes: 0", + "kstat.zfs.misc.arcstats.arc_tempreserve: 0", + "kstat.zfs.misc.arcstats.arc_no_grow: 0", + "kstat.zfs.misc.arcstats.memory_available_bytes: 3663839232", + "kstat.zfs.misc.arcstats.memory_free_bytes: 3752042496", + "kstat.zfs.misc.arcstats.memory_all_bytes: 4252553216", + "kstat.zfs.misc.arcstats.memory_indirect_count: 0", + "kstat.zfs.misc.arcstats.memory_direct_count: 0", + "kstat.zfs.misc.arcstats.memory_throttle_count: 0", + "kstat.zfs.misc.arcstats.l2_rebuild_log_blks: 0", + "kstat.zfs.misc.arcstats.l2_rebuild_bufs_precached: 0", + "kstat.zfs.misc.arcstats.l2_rebuild_bufs: 0", + "kstat.zfs.misc.arcstats.l2_rebuild_asize: 0", + "kstat.zfs.misc.arcstats.l2_rebuild_size: 0", + "kstat.zfs.misc.arcstats.l2_rebuild_lowmem: 0", + "kstat.zfs.misc.arcstats.l2_rebuild_cksum_lb_errors: 0", + "kstat.zfs.misc.arcstats.l2_rebuild_dh_errors: 0", + "kstat.zfs.misc.arcstats.l2_rebuild_io_errors: 0", + "kstat.zfs.misc.arcstats.l2_rebuild_unsupported: 0", + "kstat.zfs.misc.arcstats.l2_rebuild_success: 0", + "kstat.zfs.misc.arcstats.l2_data_to_meta_ratio: 0", + "kstat.zfs.misc.arcstats.l2_log_blk_count: 0", + "kstat.zfs.misc.arcstats.l2_log_blk_asize: 0", + "kstat.zfs.misc.arcstats.l2_log_blk_avg_asize: 0", + "kstat.zfs.misc.arcstats.l2_log_blk_writes: 0", + "kstat.zfs.misc.arcstats.l2_hdr_size: 0", + "kstat.zfs.misc.arcstats.l2_asize: 0", + "kstat.zfs.misc.arcstats.l2_size: 0", + "kstat.zfs.misc.arcstats.l2_io_error: 0", + "kstat.zfs.misc.arcstats.l2_cksum_bad: 0", + "kstat.zfs.misc.arcstats.l2_abort_lowmem: 0", + "kstat.zfs.misc.arcstats.l2_free_on_write: 0", + "kstat.zfs.misc.arcstats.l2_evict_l1cached: 0", + "kstat.zfs.misc.arcstats.l2_evict_reading: 0", + "kstat.zfs.misc.arcstats.l2_evict_lock_retry: 0", + "kstat.zfs.misc.arcstats.l2_writes_lock_retry: 0", + "kstat.zfs.misc.arcstats.l2_writes_error: 0", + "kstat.zfs.misc.arcstats.l2_writes_done: 0", + "kstat.zfs.misc.arcstats.l2_writes_sent: 0", + "kstat.zfs.misc.arcstats.l2_write_bytes: 0", + "kstat.zfs.misc.arcstats.l2_read_bytes: 0", + "kstat.zfs.misc.arcstats.l2_rw_clash: 0", + "kstat.zfs.misc.arcstats.l2_feeds: 0", + "kstat.zfs.misc.arcstats.l2_bufc_metadata_asize: 0", + "kstat.zfs.misc.arcstats.l2_bufc_data_asize: 0", + "kstat.zfs.misc.arcstats.l2_mfu_asize: 0", + "kstat.zfs.misc.arcstats.l2_mru_asize: 0", + "kstat.zfs.misc.arcstats.l2_prefetch_asize: 0", + "kstat.zfs.misc.arcstats.l2_misses: 0", + "kstat.zfs.misc.arcstats.l2_hits: 0", + "kstat.zfs.misc.arcstats.uncached_evictable_metadata: 0", + "kstat.zfs.misc.arcstats.uncached_evictable_data: 0", + "kstat.zfs.misc.arcstats.uncached_metadata: 0", + "kstat.zfs.misc.arcstats.uncached_data: 0", + "kstat.zfs.misc.arcstats.uncached_size: 0", + "kstat.zfs.misc.arcstats.mfu_ghost_evictable_metadata: 35231232", + "kstat.zfs.misc.arcstats.mfu_ghost_evictable_data: 31780352", + "kstat.zfs.misc.arcstats.mfu_ghost_metadata: 35231232", + "kstat.zfs.misc.arcstats.mfu_ghost_data: 31780352", + "kstat.zfs.misc.arcstats.mfu_ghost_size: 67011584", + "kstat.zfs.misc.arcstats.mfu_evictable_metadata: 648192", + "kstat.zfs.misc.arcstats.mfu_evictable_data: 29985280", + "kstat.zfs.misc.arcstats.mfu_metadata: 9697280", + "kstat.zfs.misc.arcstats.mfu_data: 31448576", + "kstat.zfs.misc.arcstats.mfu_size: 41145856", + "kstat.zfs.misc.arcstats.mru_ghost_evictable_metadata: 27670528", + "kstat.zfs.misc.arcstats.mru_ghost_evictable_data: 39195136", + "kstat.zfs.misc.arcstats.mru_ghost_metadata: 27670528", + "kstat.zfs.misc.arcstats.mru_ghost_data: 39195136", + "kstat.zfs.misc.arcstats.mru_ghost_size: 66865664", + "kstat.zfs.misc.arcstats.mru_evictable_metadata: 4007424", + "kstat.zfs.misc.arcstats.mru_evictable_data: 21310976", + "kstat.zfs.misc.arcstats.mru_metadata: 32238592", + "kstat.zfs.misc.arcstats.mru_data: 25252864", + "kstat.zfs.misc.arcstats.mru_size: 57491456", + "kstat.zfs.misc.arcstats.anon_evictable_metadata: 0", + "kstat.zfs.misc.arcstats.anon_evictable_data: 0", + "kstat.zfs.misc.arcstats.anon_metadata: 0", + "kstat.zfs.misc.arcstats.anon_data: 0", + "kstat.zfs.misc.arcstats.anon_size: 0", + "kstat.zfs.misc.arcstats.other_size: 36376512", + "kstat.zfs.misc.arcstats.bonus_size: 7930560", + "kstat.zfs.misc.arcstats.dnode_size: 20521584", + "kstat.zfs.misc.arcstats.dbuf_size: 7924368", + "kstat.zfs.misc.arcstats.metadata_size: 41935872", + "kstat.zfs.misc.arcstats.data_size: 56701440", + "kstat.zfs.misc.arcstats.hdr_size: 1872848", + "kstat.zfs.misc.arcstats.overhead_size: 29338112", + "kstat.zfs.misc.arcstats.uncompressed_size: 121843712", + "kstat.zfs.misc.arcstats.compressed_size: 69299200", + "kstat.zfs.misc.arcstats.size: 137512848", + "kstat.zfs.misc.arcstats.c_max: 3178811392", + "kstat.zfs.misc.arcstats.c_min: 132892288", + "kstat.zfs.misc.arcstats.c: 176014976", + "kstat.zfs.misc.arcstats.pm: 2306048980", + "kstat.zfs.misc.arcstats.pd: 1302662522", + "kstat.zfs.misc.arcstats.meta: 1110189320", + "kstat.zfs.misc.arcstats.hash_chain_max: 4", + "kstat.zfs.misc.arcstats.hash_chains: 68", + "kstat.zfs.misc.arcstats.hash_collisions: 15034", + "kstat.zfs.misc.arcstats.hash_elements_max: 91215", + "kstat.zfs.misc.arcstats.hash_elements: 7890", + "kstat.zfs.misc.arcstats.evict_l2_skip: 0", + "kstat.zfs.misc.arcstats.evict_l2_ineligible: 65306624", + "kstat.zfs.misc.arcstats.evict_l2_eligible_mru: 1369077760", + "kstat.zfs.misc.arcstats.evict_l2_eligible_mfu: 940835840", + "kstat.zfs.misc.arcstats.evict_l2_eligible: 2309913600", + "kstat.zfs.misc.arcstats.evict_l2_cached: 0", + "kstat.zfs.misc.arcstats.evict_not_enough: 12", + "kstat.zfs.misc.arcstats.evict_skip: 445", + "kstat.zfs.misc.arcstats.access_skip: 0", + "kstat.zfs.misc.arcstats.mutex_miss: 0", + "kstat.zfs.misc.arcstats.deleted: 90151", + "kstat.zfs.misc.arcstats.uncached_hits: 0", + "kstat.zfs.misc.arcstats.mfu_ghost_hits: 6108", + "kstat.zfs.misc.arcstats.mfu_hits: 1240173", + "kstat.zfs.misc.arcstats.mru_ghost_hits: 2692", + "kstat.zfs.misc.arcstats.mru_hits: 233633", + "kstat.zfs.misc.arcstats.prefetch_metadata_misses: 406", + "kstat.zfs.misc.arcstats.prefetch_metadata_iohits: 2315", + "kstat.zfs.misc.arcstats.prefetch_metadata_hits: 196", + "kstat.zfs.misc.arcstats.prefetch_data_misses: 2092", + "kstat.zfs.misc.arcstats.prefetch_data_iohits: 0", + "kstat.zfs.misc.arcstats.prefetch_data_hits: 105", + "kstat.zfs.misc.arcstats.demand_metadata_misses: 12021", + "kstat.zfs.misc.arcstats.demand_metadata_iohits: 87", + "kstat.zfs.misc.arcstats.demand_metadata_hits: 906938", + "kstat.zfs.misc.arcstats.demand_data_misses: 48482", + "kstat.zfs.misc.arcstats.demand_data_iohits: 128", + "kstat.zfs.misc.arcstats.demand_data_hits: 566567", + "kstat.zfs.misc.arcstats.misses: 63001", + "kstat.zfs.misc.arcstats.iohits: 2530", + "kstat.zfs.misc.arcstats.hits: 1473806" + ], + "zfetchstats": [ + "kstat.zfs.misc.zfetchstats.io_active: 0", + "kstat.zfs.misc.zfetchstats.io_issued: 2197", + "kstat.zfs.misc.zfetchstats.max_streams: 9411", + "kstat.zfs.misc.zfetchstats.misses: 14538", + "kstat.zfs.misc.zfetchstats.hits: 6439" + ] +} \ No newline at end of file diff --git a/plugins/inputs/zfs/testcases/freebsd/freebsd14/telegraf.conf b/plugins/inputs/zfs/testcases/freebsd/freebsd14/telegraf.conf new file mode 100644 index 0000000000000..06e527832d14a --- /dev/null +++ b/plugins/inputs/zfs/testcases/freebsd/freebsd14/telegraf.conf @@ -0,0 +1,4 @@ +[[inputs.zfs]] + kstatMetrics = ["arcstats", "zfetchstats", "vdev_cache_stats"] + poolMetrics = true + datasetMetrics = true \ No newline at end of file diff --git a/plugins/inputs/zfs/testcases/freebsd/freebsd14/uname.txt b/plugins/inputs/zfs/testcases/freebsd/freebsd14/uname.txt new file mode 100644 index 0000000000000..5672e83cabe11 --- /dev/null +++ b/plugins/inputs/zfs/testcases/freebsd/freebsd14/uname.txt @@ -0,0 +1 @@ +14.0-RC2 \ No newline at end of file diff --git a/plugins/inputs/zfs/testcases/freebsd/freebsd14/zdataset.txt b/plugins/inputs/zfs/testcases/freebsd/freebsd14/zdataset.txt new file mode 100644 index 0000000000000..2c2599f5fa9f4 --- /dev/null +++ b/plugins/inputs/zfs/testcases/freebsd/freebsd14/zdataset.txt @@ -0,0 +1,14 @@ +zroot 53069045760 11959443456 - - +zroot/ROOT 53069045760 4685123584 - - +zroot/ROOT/default 53069045760 4684689408 - - +zroot/home 53069045760 7263580160 - - +zroot/tmp 53069045760 684032 - - +zroot/usr 53069045760 1724416 - - +zroot/usr/obj 53069045760 430080 - - +zroot/usr/ports 53069045760 430080 - - +zroot/usr/src 53069045760 430080 - - +zroot/var 53069045760 2269184 - - +zroot/var/audit 53069045760 438272 - - +zroot/var/log 53069045760 544768 - - +zroot/var/mail 53069045760 425984 - - +zroot/var/tmp 53069045760 425984 - - diff --git a/plugins/inputs/zfs/testcases/freebsd/freebsd14/zpool.txt b/plugins/inputs/zfs/testcases/freebsd/freebsd14/zpool.txt new file mode 100644 index 0000000000000..0d86c0ce4744b --- /dev/null +++ b/plugins/inputs/zfs/testcases/freebsd/freebsd14/zpool.txt @@ -0,0 +1 @@ +zroot ONLINE 67108864000 11959578624 55149285376 4 17 1.00 diff --git a/plugins/inputs/zfs/zfs.go b/plugins/inputs/zfs/zfs.go index 262cd55d44015..9b135acd9f8de 100644 --- a/plugins/inputs/zfs/zfs.go +++ b/plugins/inputs/zfs/zfs.go @@ -13,16 +13,20 @@ var sampleConfig string type Sysctl func(metric string) ([]string, error) type Zpool func() ([]string, error) type Zdataset func(properties []string) ([]string, error) +type Uname func() (string, error) type Zfs struct { KstatPath string KstatMetrics []string PoolMetrics bool DatasetMetrics bool - sysctl Sysctl //nolint:unused // False positive - this var is used for non-default build tag: freebsd - zpool Zpool //nolint:unused // False positive - this var is used for non-default build tag: freebsd - zdataset Zdataset //nolint:unused // False positive - this var is used for non-default build tag: freebsd Log telegraf.Logger `toml:"-"` + + sysctl Sysctl //nolint:unused // False positive - this var is used for non-default build tag: freebsd + zpool Zpool //nolint:unused // False positive - this var is used for non-default build tag: freebsd + zdataset Zdataset //nolint:unused // False positive - this var is used for non-default build tag: freebsd + uname Uname //nolint:unused // False positive - this var is used for non-default build tag: freebsd + version int64 //nolint:unused // False positive - this var is used for non-default build tag: freebsd } func (*Zfs) SampleConfig() string { diff --git a/plugins/inputs/zfs/zfs_freebsd.go b/plugins/inputs/zfs/zfs_freebsd.go index a15302be49107..4d684aca33974 100644 --- a/plugins/inputs/zfs/zfs_freebsd.go +++ b/plugins/inputs/zfs/zfs_freebsd.go @@ -11,8 +11,84 @@ import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/plugins/inputs" + "golang.org/x/sys/unix" ) +func (z *Zfs) Init() error { + // Determine the kernel version to adapt parsing + release, err := z.uname() + if err != nil { + return fmt.Errorf("determining uname failed: %w", err) + } + parts := strings.SplitN(release, ".", 2) + z.version, err = strconv.ParseInt(parts[0], 10, 64) + if err != nil { + return fmt.Errorf("determining version from %q failed: %w", release, err) + } + + // Setup default metrics if they are not specified. + // Please note that starting from FreeBSD 14 the 'vdev_cache_stats' are + // no longer available. + if len(z.KstatMetrics) == 0 { + if z.version < 14 { + z.KstatMetrics = []string{"arcstats", "zfetchstats", "vdev_cache_stats"} + } else { + z.KstatMetrics = []string{"arcstats", "zfetchstats"} + } + } + + return nil +} + +func (z *Zfs) Gather(acc telegraf.Accumulator) error { + tags := map[string]string{} + + poolNames, err := z.gatherPoolStats(acc) + if err != nil { + return err + } + if poolNames != "" { + tags["pools"] = poolNames + } + + datasetNames, err := z.gatherDatasetStats(acc) + if err != nil { + return err + } + if datasetNames != "" { + tags["datasets"] = datasetNames + } + + // Gather information form the kernel using sysctl + fields := make(map[string]interface{}) + var removeIndices []int + for i, metric := range z.KstatMetrics { + stdout, err := z.sysctl(metric) + if err != nil { + z.Log.Warnf("sysctl for 'kstat.zfs.misc.%s' failed: %v; removing metric", metric, err) + removeIndices = append(removeIndices, i) + continue + } + for _, line := range stdout { + rawData := strings.Split(line, ": ") + key := metric + "_" + strings.Split(rawData[0], ".")[4] + value, _ := strconv.ParseInt(rawData[1], 10, 64) + fields[key] = value + } + } + acc.AddFields("zfs", fields, tags) + + // Remove the invalid kstat metrics + if len(removeIndices) > 0 { + for i := len(removeIndices) - 1; i >= 0; i-- { + idx := removeIndices[i] + z.KstatMetrics = append(z.KstatMetrics[:idx], z.KstatMetrics[idx+1:]...) + } + } + + return nil +} + func (z *Zfs) gatherPoolStats(acc telegraf.Accumulator) (string, error) { lines, err := z.zpool() if err != nil { @@ -22,62 +98,63 @@ func (z *Zfs) gatherPoolStats(acc telegraf.Accumulator) (string, error) { pools := []string{} for _, line := range lines { col := strings.Split(line, "\t") - pools = append(pools, col[0]) } - if z.PoolMetrics { - for _, line := range lines { - col := strings.Split(line, "\t") - if len(col) != 8 { - continue + if !z.PoolMetrics { + return strings.Join(pools, "::"), nil + } + + for _, line := range lines { + col := strings.Split(line, "\t") + if len(col) != 8 { + continue + } + + tags := map[string]string{"pool": col[0], "health": col[1]} + fields := map[string]interface{}{} + + if tags["health"] == "UNAVAIL" { + fields["size"] = int64(0) + } else { + size, err := strconv.ParseInt(col[2], 10, 64) + if err != nil { + return "", fmt.Errorf("Error parsing size: %s", err) } + fields["size"] = size - tags := map[string]string{"pool": col[0], "health": col[1]} - fields := map[string]interface{}{} - - if tags["health"] == "UNAVAIL" { - fields["size"] = int64(0) - } else { - size, err := strconv.ParseInt(col[2], 10, 64) - if err != nil { - return "", fmt.Errorf("Error parsing size: %s", err) - } - fields["size"] = size - - alloc, err := strconv.ParseInt(col[3], 10, 64) - if err != nil { - return "", fmt.Errorf("Error parsing allocation: %s", err) - } - fields["allocated"] = alloc - - free, err := strconv.ParseInt(col[4], 10, 64) - if err != nil { - return "", fmt.Errorf("Error parsing free: %s", err) - } - fields["free"] = free - - frag, err := strconv.ParseInt(strings.TrimSuffix(col[5], "%"), 10, 0) - if err != nil { // This might be - for RO devs - frag = 0 - } - fields["fragmentation"] = frag - - capval, err := strconv.ParseInt(col[6], 10, 0) - if err != nil { - return "", fmt.Errorf("Error parsing capacity: %s", err) - } - fields["capacity"] = capval - - dedup, err := strconv.ParseFloat(strings.TrimSuffix(col[7], "x"), 32) - if err != nil { - return "", fmt.Errorf("Error parsing dedupratio: %s", err) - } - fields["dedupratio"] = dedup + alloc, err := strconv.ParseInt(col[3], 10, 64) + if err != nil { + return "", fmt.Errorf("Error parsing allocation: %s", err) } + fields["allocated"] = alloc - acc.AddFields("zfs_pool", fields, tags) + free, err := strconv.ParseInt(col[4], 10, 64) + if err != nil { + return "", fmt.Errorf("Error parsing free: %s", err) + } + fields["free"] = free + + frag, err := strconv.ParseInt(strings.TrimSuffix(col[5], "%"), 10, 0) + if err != nil { // This might be - for RO devs + frag = 0 + } + fields["fragmentation"] = frag + + capval, err := strconv.ParseInt(col[6], 10, 0) + if err != nil { + return "", fmt.Errorf("Error parsing capacity: %s", err) + } + fields["capacity"] = capval + + dedup, err := strconv.ParseFloat(strings.TrimSuffix(col[7], "x"), 32) + if err != nil { + return "", fmt.Errorf("Error parsing dedupratio: %s", err) + } + fields["dedupratio"] = dedup } + + acc.AddFields("zfs_pool", fields, tags) } return strings.Join(pools, "::"), nil @@ -97,70 +174,37 @@ func (z *Zfs) gatherDatasetStats(acc telegraf.Accumulator) (string, error) { datasets = append(datasets, col[0]) } - if z.DatasetMetrics { - for _, line := range lines { - col := strings.Split(line, "\t") - if len(col) != len(properties) { - z.Log.Warnf("Invalid number of columns for line: %s", line) - continue - } - - tags := map[string]string{"dataset": col[0]} - fields := map[string]interface{}{} - - for i, key := range properties[1:] { - value, err := strconv.ParseInt(col[i+1], 10, 64) - if err != nil { - return "", fmt.Errorf("Error parsing %s %q: %s", key, col[i+1], err) - } - fields[key] = value - } - - acc.AddFields("zfs_dataset", fields, tags) - } + if !z.DatasetMetrics { + return strings.Join(datasets, "::"), nil } - return strings.Join(datasets, "::"), nil -} - -func (z *Zfs) Gather(acc telegraf.Accumulator) error { - kstatMetrics := z.KstatMetrics - if len(kstatMetrics) == 0 { - kstatMetrics = []string{"arcstats", "zfetchstats", "vdev_cache_stats"} - } - - tags := map[string]string{} - poolNames, err := z.gatherPoolStats(acc) - if err != nil { - return err - } - if poolNames != "" { - tags["pools"] = poolNames - } + for _, line := range lines { + col := strings.Split(line, "\t") + if len(col) != len(properties) { + z.Log.Warnf("Invalid number of columns for line: %s", line) + continue + } - datasetNames, err := z.gatherDatasetStats(acc) - if err != nil { - return err - } - if datasetNames != "" { - tags["datasets"] = datasetNames - } + tags := map[string]string{"dataset": col[0]} + fields := map[string]interface{}{} - fields := make(map[string]interface{}) - for _, metric := range kstatMetrics { - stdout, err := z.sysctl(metric) - if err != nil { - return err - } - for _, line := range stdout { - rawData := strings.Split(line, ": ") - key := metric + "_" + strings.Split(rawData[0], ".")[4] - value, _ := strconv.ParseInt(rawData[1], 10, 64) + for i, key := range properties[1:] { + // Treat '-' entries as zero + if col[i+1] == "-" { + fields[key] = int64(0) + continue + } + value, err := strconv.ParseInt(col[i+1], 10, 64) + if err != nil { + return "", fmt.Errorf("Error parsing %s %q: %s", key, col[i+1], err) + } fields[key] = value } + + acc.AddFields("zfs_dataset", fields, tags) } - acc.AddFields("zfs", fields, tags) - return nil + + return strings.Join(datasets, "::"), nil } func run(command string, args ...string) ([]string, error) { @@ -194,12 +238,22 @@ func sysctl(metric string) ([]string, error) { return run("sysctl", []string{"-q", fmt.Sprintf("kstat.zfs.misc.%s", metric)}...) } +func uname() (string, error) { + var info unix.Utsname + if err := unix.Uname(&info); err != nil { + return "", err + } + release := unix.ByteSliceToString(info.Release[:]) + return release, nil +} + func init() { inputs.Add("zfs", func() telegraf.Input { return &Zfs{ sysctl: sysctl, zpool: zpool, zdataset: zdataset, + uname: uname, } }) } diff --git a/plugins/inputs/zfs/zfs_freebsd_test.go b/plugins/inputs/zfs/zfs_freebsd_test.go index f673fe4c9b3e6..401ca553dc5e6 100644 --- a/plugins/inputs/zfs/zfs_freebsd_test.go +++ b/plugins/inputs/zfs/zfs_freebsd_test.go @@ -44,6 +44,7 @@ func TestCases(t *testing.T) { inputSysctlFilename := filepath.Join(testcasePath, "sysctl.json") inputZPoolFilename := filepath.Join(testcasePath, "zpool.txt") inputZDatasetFilename := filepath.Join(testcasePath, "zdataset.txt") + inputUnameFilename := filepath.Join(testcasePath, "uname.txt") expectedFilename := filepath.Join(testcasePath, "expected.out") // Load the input data @@ -51,11 +52,20 @@ func TestCases(t *testing.T) { require.NoError(t, err) var sysctl map[string][]string require.NoError(t, json.Unmarshal(buf, &sysctl)) + zpool, err := testutil.ParseLinesFromFile(inputZPoolFilename) require.NoError(t, err) + zdataset, err := testutil.ParseLinesFromFile(inputZDatasetFilename) require.NoError(t, err) + // Try to read release from file and default to FreeBSD 13 if + // an error occurs. + uname := "13.2-STABLE" + if buf, err := os.ReadFile(inputUnameFilename); err == nil { + uname = string(buf) + } + // Prepare the influx parser for expectations parser := &influx.Parser{} require.NoError(t, parser.Init()) @@ -79,7 +89,9 @@ func TestCases(t *testing.T) { } plugin.zpool = func() ([]string, error) { return zpool, nil } plugin.zdataset = func(_ []string) ([]string, error) { return zdataset, nil } + plugin.uname = func() (string, error) { return uname, nil } plugin.Log = testutil.Logger{} + require.NoError(t, plugin.Init()) // Gather and test var acc testutil.Accumulator From fe3100fb39367d05a522af3a0ad2bbe1d467fcb8 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 25 Oct 2023 16:24:30 -0600 Subject: [PATCH 6/7] chore(deps): Bump google.golang.org/grpc from 1.58.2 to 1.58.3 (#14192) --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 3ef999ec389dc..013e7889b6603 100644 --- a/go.mod +++ b/go.mod @@ -204,7 +204,7 @@ require ( gonum.org/v1/gonum v0.14.0 google.golang.org/api v0.147.0 google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97 - google.golang.org/grpc v1.58.2 + google.golang.org/grpc v1.58.3 google.golang.org/protobuf v1.31.0 gopkg.in/gorethink/gorethink.v3 v3.0.5 gopkg.in/olivere/elastic.v5 v5.0.86 diff --git a/go.sum b/go.sum index 885cb5fbaa97e..997e5d48ebab3 100644 --- a/go.sum +++ b/go.sum @@ -2018,8 +2018,8 @@ google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAG google.golang.org/grpc v1.37.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM= google.golang.org/grpc v1.38.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM= google.golang.org/grpc v1.46.0/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACuMGWk= -google.golang.org/grpc v1.58.2 h1:SXUpjxeVF3FKrTYQI4f4KvbGD5u2xccdYdurwowix5I= -google.golang.org/grpc v1.58.2/go.mod h1:tgX3ZQDlNJGU96V6yHh1T/JeoBQ2TXdr43YbYSsCJk0= +google.golang.org/grpc v1.58.3 h1:BjnpXut1btbtgN/6sp+brB2Kbm2LjNXnidYujAVbSoQ= +google.golang.org/grpc v1.58.3/go.mod h1:tgX3ZQDlNJGU96V6yHh1T/JeoBQ2TXdr43YbYSsCJk0= google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= From 321c5a4070cd46d699826432ab4858224f25001d Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Thu, 26 Oct 2023 01:05:25 +0200 Subject: [PATCH 7/7] test(parsers): Add benchmarks (#14148) --- plugins/parsers/json/parser_test.go | 406 +++++++++++------- plugins/parsers/json_v2/parser_test.go | 65 +++ .../json_v2/testdata/benchmark/benchmark.json | 34 ++ .../json_v2/testdata/benchmark/expected.out | 2 + .../json_v2/testdata/benchmark/input.json | 34 ++ .../json_v2/testdata/benchmark/telegraf.conf | 9 + 6 files changed, 404 insertions(+), 146 deletions(-) create mode 100644 plugins/parsers/json_v2/testdata/benchmark/benchmark.json create mode 100644 plugins/parsers/json_v2/testdata/benchmark/expected.out create mode 100644 plugins/parsers/json_v2/testdata/benchmark/input.json create mode 100644 plugins/parsers/json_v2/testdata/benchmark/telegraf.conf diff --git a/plugins/parsers/json/parser_test.go b/plugins/parsers/json/parser_test.go index 3f6b1ecbb64e9..75d37d73a2742 100644 --- a/plugins/parsers/json/parser_test.go +++ b/plugins/parsers/json/parser_test.go @@ -9,6 +9,7 @@ import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/internal/fuzz" + "github.com/influxdata/telegraf/metric" "github.com/influxdata/telegraf/testutil" ) @@ -76,52 +77,86 @@ const validJSONArrayTags = ` ] ` +const benchmarkData = ` +[ + { + "name": "impression", + "timestamp": 1653643420, + "fields": { + "count_sum": 5 + }, + "tags": { + "key": "12345", + "flagname": "F5", + "host": "1cbbb3796fc2", + "platform": "Java", + "sdkver": "4.9.1", + "value": "false" + } + }, + { + "name": "expression", + "timestamp": 1653646789, + "fields": { + "count_sum": 42 + }, + "tags": { + "key": "67890", + "flagname": "E42", + "host": "klaus", + "platform": "Golang", + "sdkver": "1.18.3", + "value": "true" + } + } +]` + func TestParseValidJSON(t *testing.T) { parser := &Parser{MetricName: "json_test"} require.NoError(t, parser.Init()) // Most basic vanilla test - metrics, err := parser.Parse([]byte(validJSON)) + actual, err := parser.Parse([]byte(validJSON)) require.NoError(t, err) - require.Len(t, metrics, 1) - require.Equal(t, "json_test", metrics[0].Name()) + require.Len(t, actual, 1) + require.Equal(t, "json_test", actual[0].Name()) require.Equal(t, map[string]interface{}{ "a": float64(5), "b_c": float64(6), - }, metrics[0].Fields()) - require.Equal(t, map[string]string{}, metrics[0].Tags()) + }, actual[0].Fields()) + require.Equal(t, map[string]string{}, actual[0].Tags()) // Test that newlines are fine - metrics, err = parser.Parse([]byte(validJSONNewline)) + actual, err = parser.Parse([]byte(validJSONNewline)) require.NoError(t, err) - require.Len(t, metrics, 1) - require.Equal(t, "json_test", metrics[0].Name()) + require.Len(t, actual, 1) + require.Equal(t, "json_test", actual[0].Name()) require.Equal(t, map[string]interface{}{ "d": float64(7), "b_d": float64(8), - }, metrics[0].Fields()) - require.Equal(t, map[string]string{}, metrics[0].Tags()) + }, actual[0].Fields()) + require.Equal(t, map[string]string{}, actual[0].Tags()) // Test that strings without TagKeys defined are ignored - metrics, err = parser.Parse([]byte(validJSONTags)) + actual, err = parser.Parse([]byte(validJSONTags)) require.NoError(t, err) - require.Len(t, metrics, 1) - require.Equal(t, "json_test", metrics[0].Name()) + require.Len(t, actual, 1) + require.Equal(t, "json_test", actual[0].Name()) require.Equal(t, map[string]interface{}{ "a": float64(5), "b_c": float64(6), - }, metrics[0].Fields()) - require.Equal(t, map[string]string{}, metrics[0].Tags()) + }, actual[0].Fields()) + require.Equal(t, map[string]string{}, actual[0].Tags()) - // Test that whitespace only will parse as an empty list of metrics - metrics, err = parser.Parse([]byte("\n\t")) + // Test that whitespace only will parse as an empty list of actual + actual, err = parser.Parse([]byte("\n\t")) require.NoError(t, err) - require.Empty(t, metrics) + require.Empty(t, actual) - // Test that an empty string will parse as an empty list of metrics - metrics, err = parser.Parse([]byte("")) + // Test that an empty string will parse as an empty list of actual + actual, err = parser.Parse([]byte("")) require.NoError(t, err) - require.Empty(t, metrics) + require.Empty(t, actual) } func TestParseLineValidJSON(t *testing.T) { @@ -129,34 +164,34 @@ func TestParseLineValidJSON(t *testing.T) { require.NoError(t, parser.Init()) // Most basic vanilla test - metric, err := parser.ParseLine(validJSON) + actual, err := parser.ParseLine(validJSON) require.NoError(t, err) - require.Equal(t, "json_test", metric.Name()) + require.Equal(t, "json_test", actual.Name()) require.Equal(t, map[string]interface{}{ "a": float64(5), "b_c": float64(6), - }, metric.Fields()) - require.Equal(t, map[string]string{}, metric.Tags()) + }, actual.Fields()) + require.Equal(t, map[string]string{}, actual.Tags()) // Test that newlines are fine - metric, err = parser.ParseLine(validJSONNewline) + actual, err = parser.ParseLine(validJSONNewline) require.NoError(t, err) - require.Equal(t, "json_test", metric.Name()) + require.Equal(t, "json_test", actual.Name()) require.Equal(t, map[string]interface{}{ "d": float64(7), "b_d": float64(8), - }, metric.Fields()) - require.Equal(t, map[string]string{}, metric.Tags()) + }, actual.Fields()) + require.Equal(t, map[string]string{}, actual.Tags()) // Test that strings without TagKeys defined are ignored - metric, err = parser.ParseLine(validJSONTags) + actual, err = parser.ParseLine(validJSONTags) require.NoError(t, err) - require.Equal(t, "json_test", metric.Name()) + require.Equal(t, "json_test", actual.Name()) require.Equal(t, map[string]interface{}{ "a": float64(5), "b_c": float64(6), - }, metric.Fields()) - require.Equal(t, map[string]string{}, metric.Tags()) + }, actual.Fields()) + require.Equal(t, map[string]string{}, actual.Tags()) } func TestParseInvalidJSON(t *testing.T) { @@ -214,15 +249,15 @@ func TestParseWithTagKeys(t *testing.T) { } require.NoError(t, parser.Init()) - metrics, err := parser.Parse([]byte(validJSONTags)) + actual, err := parser.Parse([]byte(validJSONTags)) require.NoError(t, err) - require.Len(t, metrics, 1) - require.Equal(t, "json_test", metrics[0].Name()) + require.Len(t, actual, 1) + require.Equal(t, "json_test", actual[0].Name()) require.Equal(t, map[string]interface{}{ "a": float64(5), "b_c": float64(6), - }, metrics[0].Fields()) - require.Equal(t, map[string]string{}, metrics[0].Tags()) + }, actual[0].Fields()) + require.Equal(t, map[string]string{}, actual[0].Tags()) // Test that single tag key is found and applied parser = &Parser{ @@ -231,17 +266,17 @@ func TestParseWithTagKeys(t *testing.T) { } require.NoError(t, parser.Init()) - metrics, err = parser.Parse([]byte(validJSONTags)) + actual, err = parser.Parse([]byte(validJSONTags)) require.NoError(t, err) - require.Len(t, metrics, 1) - require.Equal(t, "json_test", metrics[0].Name()) + require.Len(t, actual, 1) + require.Equal(t, "json_test", actual[0].Name()) require.Equal(t, map[string]interface{}{ "a": float64(5), "b_c": float64(6), - }, metrics[0].Fields()) + }, actual[0].Fields()) require.Equal(t, map[string]string{ "mytag": "foobar", - }, metrics[0].Tags()) + }, actual[0].Tags()) // Test that both tag keys are found and applied parser = &Parser{ @@ -250,18 +285,18 @@ func TestParseWithTagKeys(t *testing.T) { } require.NoError(t, parser.Init()) - metrics, err = parser.Parse([]byte(validJSONTags)) + actual, err = parser.Parse([]byte(validJSONTags)) require.NoError(t, err) - require.Len(t, metrics, 1) - require.Equal(t, "json_test", metrics[0].Name()) + require.Len(t, actual, 1) + require.Equal(t, "json_test", actual[0].Name()) require.Equal(t, map[string]interface{}{ "a": float64(5), "b_c": float64(6), - }, metrics[0].Fields()) + }, actual[0].Fields()) require.Equal(t, map[string]string{ "mytag": "foobar", "othertag": "baz", - }, metrics[0].Tags()) + }, actual[0].Tags()) } func TestParseLineWithTagKeys(t *testing.T) { @@ -272,14 +307,14 @@ func TestParseLineWithTagKeys(t *testing.T) { } require.NoError(t, parser.Init()) - metric, err := parser.ParseLine(validJSONTags) + actual, err := parser.ParseLine(validJSONTags) require.NoError(t, err) - require.Equal(t, "json_test", metric.Name()) + require.Equal(t, "json_test", actual.Name()) require.Equal(t, map[string]interface{}{ "a": float64(5), "b_c": float64(6), - }, metric.Fields()) - require.Equal(t, map[string]string{}, metric.Tags()) + }, actual.Fields()) + require.Equal(t, map[string]string{}, actual.Tags()) // Test that single tag key is found and applied parser = &Parser{ @@ -288,16 +323,16 @@ func TestParseLineWithTagKeys(t *testing.T) { } require.NoError(t, parser.Init()) - metric, err = parser.ParseLine(validJSONTags) + actual, err = parser.ParseLine(validJSONTags) require.NoError(t, err) - require.Equal(t, "json_test", metric.Name()) + require.Equal(t, "json_test", actual.Name()) require.Equal(t, map[string]interface{}{ "a": float64(5), "b_c": float64(6), - }, metric.Fields()) + }, actual.Fields()) require.Equal(t, map[string]string{ "mytag": "foobar", - }, metric.Tags()) + }, actual.Tags()) // Test that both tag keys are found and applied parser = &Parser{ @@ -306,17 +341,17 @@ func TestParseLineWithTagKeys(t *testing.T) { } require.NoError(t, parser.Init()) - metric, err = parser.ParseLine(validJSONTags) + actual, err = parser.ParseLine(validJSONTags) require.NoError(t, err) - require.Equal(t, "json_test", metric.Name()) + require.Equal(t, "json_test", actual.Name()) require.Equal(t, map[string]interface{}{ "a": float64(5), "b_c": float64(6), - }, metric.Fields()) + }, actual.Fields()) require.Equal(t, map[string]string{ "mytag": "foobar", "othertag": "baz", - }, metric.Tags()) + }, actual.Tags()) } func TestParseValidJSONDefaultTags(t *testing.T) { @@ -328,29 +363,29 @@ func TestParseValidJSONDefaultTags(t *testing.T) { require.NoError(t, parser.Init()) // Most basic vanilla test - metrics, err := parser.Parse([]byte(validJSON)) + actual, err := parser.Parse([]byte(validJSON)) require.NoError(t, err) - require.Len(t, metrics, 1) - require.Equal(t, "json_test", metrics[0].Name()) + require.Len(t, actual, 1) + require.Equal(t, "json_test", actual[0].Name()) require.Equal(t, map[string]interface{}{ "a": float64(5), "b_c": float64(6), - }, metrics[0].Fields()) - require.Equal(t, map[string]string{"t4g": "default"}, metrics[0].Tags()) + }, actual[0].Fields()) + require.Equal(t, map[string]string{"t4g": "default"}, actual[0].Tags()) // Test that tagkeys and default tags are applied - metrics, err = parser.Parse([]byte(validJSONTags)) + actual, err = parser.Parse([]byte(validJSONTags)) require.NoError(t, err) - require.Len(t, metrics, 1) - require.Equal(t, "json_test", metrics[0].Name()) + require.Len(t, actual, 1) + require.Equal(t, "json_test", actual[0].Name()) require.Equal(t, map[string]interface{}{ "a": float64(5), "b_c": float64(6), - }, metrics[0].Fields()) + }, actual[0].Fields()) require.Equal(t, map[string]string{ "t4g": "default", "mytag": "foobar", - }, metrics[0].Tags()) + }, actual[0].Tags()) } // Test that default tags are overridden by tag keys @@ -363,28 +398,28 @@ func TestParseValidJSONDefaultTagsOverride(t *testing.T) { require.NoError(t, parser.Init()) // Most basic vanilla test - metrics, err := parser.Parse([]byte(validJSON)) + actual, err := parser.Parse([]byte(validJSON)) require.NoError(t, err) - require.Len(t, metrics, 1) - require.Equal(t, "json_test", metrics[0].Name()) + require.Len(t, actual, 1) + require.Equal(t, "json_test", actual[0].Name()) require.Equal(t, map[string]interface{}{ "a": float64(5), "b_c": float64(6), - }, metrics[0].Fields()) - require.Equal(t, map[string]string{"mytag": "default"}, metrics[0].Tags()) + }, actual[0].Fields()) + require.Equal(t, map[string]string{"mytag": "default"}, actual[0].Tags()) // Test that tagkeys override default tags - metrics, err = parser.Parse([]byte(validJSONTags)) + actual, err = parser.Parse([]byte(validJSONTags)) require.NoError(t, err) - require.Len(t, metrics, 1) - require.Equal(t, "json_test", metrics[0].Name()) + require.Len(t, actual, 1) + require.Equal(t, "json_test", actual[0].Name()) require.Equal(t, map[string]interface{}{ "a": float64(5), "b_c": float64(6), - }, metrics[0].Fields()) + }, actual[0].Fields()) require.Equal(t, map[string]string{ "mytag": "foobar", - }, metrics[0].Tags()) + }, actual[0].Tags()) } // Test that json arrays can be parsed @@ -393,32 +428,32 @@ func TestParseValidJSONArray(t *testing.T) { require.NoError(t, parser.Init()) // Most basic vanilla test - metrics, err := parser.Parse([]byte(validJSONArray)) + actual, err := parser.Parse([]byte(validJSONArray)) require.NoError(t, err) - require.Len(t, metrics, 1) - require.Equal(t, "json_array_test", metrics[0].Name()) + require.Len(t, actual, 1) + require.Equal(t, "json_array_test", actual[0].Name()) require.Equal(t, map[string]interface{}{ "a": float64(5), "b_c": float64(6), - }, metrics[0].Fields()) - require.Equal(t, map[string]string{}, metrics[0].Tags()) + }, actual[0].Fields()) + require.Equal(t, map[string]string{}, actual[0].Tags()) // Basic multiple datapoints - metrics, err = parser.Parse([]byte(validJSONArrayMultiple)) + actual, err = parser.Parse([]byte(validJSONArrayMultiple)) require.NoError(t, err) - require.Len(t, metrics, 2) - require.Equal(t, "json_array_test", metrics[0].Name()) + require.Len(t, actual, 2) + require.Equal(t, "json_array_test", actual[0].Name()) require.Equal(t, map[string]interface{}{ "a": float64(5), "b_c": float64(6), - }, metrics[0].Fields()) - require.Equal(t, map[string]string{}, metrics[1].Tags()) - require.Equal(t, "json_array_test", metrics[1].Name()) + }, actual[0].Fields()) + require.Equal(t, map[string]string{}, actual[1].Tags()) + require.Equal(t, "json_array_test", actual[1].Name()) require.Equal(t, map[string]interface{}{ "a": float64(7), "b_c": float64(8), - }, metrics[1].Fields()) - require.Equal(t, map[string]string{}, metrics[1].Tags()) + }, actual[1].Fields()) + require.Equal(t, map[string]string{}, actual[1].Tags()) } func TestParseArrayWithTagKeys(t *testing.T) { @@ -429,22 +464,22 @@ func TestParseArrayWithTagKeys(t *testing.T) { } require.NoError(t, parser.Init()) - metrics, err := parser.Parse([]byte(validJSONArrayTags)) + actual, err := parser.Parse([]byte(validJSONArrayTags)) require.NoError(t, err) - require.Len(t, metrics, 2) - require.Equal(t, "json_array_test", metrics[0].Name()) + require.Len(t, actual, 2) + require.Equal(t, "json_array_test", actual[0].Name()) require.Equal(t, map[string]interface{}{ "a": float64(5), "b_c": float64(6), - }, metrics[0].Fields()) - require.Equal(t, map[string]string{}, metrics[0].Tags()) + }, actual[0].Fields()) + require.Equal(t, map[string]string{}, actual[0].Tags()) - require.Equal(t, "json_array_test", metrics[1].Name()) + require.Equal(t, "json_array_test", actual[1].Name()) require.Equal(t, map[string]interface{}{ "a": float64(7), "b_c": float64(8), - }, metrics[1].Fields()) - require.Equal(t, map[string]string{}, metrics[1].Tags()) + }, actual[1].Fields()) + require.Equal(t, map[string]string{}, actual[1].Tags()) // Test that single tag key is found and applied parser = &Parser{ @@ -453,26 +488,26 @@ func TestParseArrayWithTagKeys(t *testing.T) { } require.NoError(t, parser.Init()) - metrics, err = parser.Parse([]byte(validJSONArrayTags)) + actual, err = parser.Parse([]byte(validJSONArrayTags)) require.NoError(t, err) - require.Len(t, metrics, 2) - require.Equal(t, "json_array_test", metrics[0].Name()) + require.Len(t, actual, 2) + require.Equal(t, "json_array_test", actual[0].Name()) require.Equal(t, map[string]interface{}{ "a": float64(5), "b_c": float64(6), - }, metrics[0].Fields()) + }, actual[0].Fields()) require.Equal(t, map[string]string{ "mytag": "foo", - }, metrics[0].Tags()) + }, actual[0].Tags()) - require.Equal(t, "json_array_test", metrics[1].Name()) + require.Equal(t, "json_array_test", actual[1].Name()) require.Equal(t, map[string]interface{}{ "a": float64(7), "b_c": float64(8), - }, metrics[1].Fields()) + }, actual[1].Fields()) require.Equal(t, map[string]string{ "mytag": "bar", - }, metrics[1].Tags()) + }, actual[1].Tags()) // Test that both tag keys are found and applied parser = &Parser{ @@ -481,28 +516,28 @@ func TestParseArrayWithTagKeys(t *testing.T) { } require.NoError(t, parser.Init()) - metrics, err = parser.Parse([]byte(validJSONArrayTags)) + actual, err = parser.Parse([]byte(validJSONArrayTags)) require.NoError(t, err) - require.Len(t, metrics, 2) - require.Equal(t, "json_array_test", metrics[0].Name()) + require.Len(t, actual, 2) + require.Equal(t, "json_array_test", actual[0].Name()) require.Equal(t, map[string]interface{}{ "a": float64(5), "b_c": float64(6), - }, metrics[0].Fields()) + }, actual[0].Fields()) require.Equal(t, map[string]string{ "mytag": "foo", "othertag": "baz", - }, metrics[0].Tags()) + }, actual[0].Tags()) - require.Equal(t, "json_array_test", metrics[1].Name()) + require.Equal(t, "json_array_test", actual[1].Name()) require.Equal(t, map[string]interface{}{ "a": float64(7), "b_c": float64(8), - }, metrics[1].Fields()) + }, actual[1].Fields()) require.Equal(t, map[string]string{ "mytag": "bar", "othertag": "baz", - }, metrics[1].Tags()) + }, actual[1].Tags()) } var jsonBOM = []byte("\xef\xbb\xbf[{\"value\":17}]") @@ -542,10 +577,10 @@ func TestJSONParseNestedArray(t *testing.T) { } require.NoError(t, parser.Init()) - metrics, err := parser.Parse([]byte(testString)) - require.Len(t, metrics, 1) + actual, err := parser.Parse([]byte(testString)) + require.Len(t, actual, 1) require.NoError(t, err) - require.Equal(t, 3, len(metrics[0].Tags())) + require.Equal(t, 3, len(actual[0].Tags())) } func TestJSONQueryErrorOnArray(t *testing.T) { @@ -606,9 +641,9 @@ func TestArrayOfObjects(t *testing.T) { } require.NoError(t, parser.Init()) - metrics, err := parser.Parse([]byte(testString)) + actual, err := parser.Parse([]byte(testString)) require.NoError(t, err) - require.Equal(t, 3, len(metrics)) + require.Equal(t, 3, len(actual)) } func TestUseCaseJSONQuery(t *testing.T) { @@ -634,10 +669,10 @@ func TestUseCaseJSONQuery(t *testing.T) { } require.NoError(t, parser.Init()) - metrics, err := parser.Parse([]byte(testString)) + actual, err := parser.Parse([]byte(testString)) require.NoError(t, err) - require.Equal(t, 3, len(metrics)) - require.Equal(t, metrics[0].Fields()["last"], "Murphy") + require.Equal(t, 3, len(actual)) + require.Equal(t, actual[0].Fields()["last"], "Murphy") } func TestTimeParser(t *testing.T) { @@ -669,10 +704,10 @@ func TestTimeParser(t *testing.T) { } require.NoError(t, parser.Init()) - metrics, err := parser.Parse([]byte(testString)) + actual, err := parser.Parse([]byte(testString)) require.NoError(t, err) - require.Equal(t, 2, len(metrics)) - require.NotEqual(t, metrics[0].Time(), metrics[1].Time()) + require.Equal(t, 2, len(actual)) + require.NotEqual(t, actual[0].Time(), actual[1].Time()) } func TestTimeParserWithTimezone(t *testing.T) { @@ -688,10 +723,10 @@ func TestTimeParserWithTimezone(t *testing.T) { } require.NoError(t, parser.Init()) - metrics, err := parser.Parse([]byte(testString)) + actual, err := parser.Parse([]byte(testString)) require.NoError(t, err) - require.Equal(t, 1, len(metrics)) - require.EqualValues(t, int64(1136405040000000000), metrics[0].Time().UnixNano()) + require.Equal(t, 1, len(actual)) + require.EqualValues(t, int64(1136405040000000000), actual[0].Time().UnixNano()) } func TestUnixTimeParser(t *testing.T) { @@ -723,10 +758,10 @@ func TestUnixTimeParser(t *testing.T) { } require.NoError(t, parser.Init()) - metrics, err := parser.Parse([]byte(testString)) + actual, err := parser.Parse([]byte(testString)) require.NoError(t, err) - require.Equal(t, 2, len(metrics)) - require.NotEqual(t, metrics[0].Time(), metrics[1].Time()) + require.Equal(t, 2, len(actual)) + require.NotEqual(t, actual[0].Time(), actual[1].Time()) } func TestUnixMsTimeParser(t *testing.T) { @@ -758,10 +793,10 @@ func TestUnixMsTimeParser(t *testing.T) { } require.NoError(t, parser.Init()) - metrics, err := parser.Parse([]byte(testString)) + actual, err := parser.Parse([]byte(testString)) require.NoError(t, err) - require.Equal(t, 2, len(metrics)) - require.NotEqual(t, metrics[0].Time(), metrics[1].Time()) + require.Equal(t, 2, len(actual)) + require.NotEqual(t, actual[0].Time(), actual[1].Time()) } func TestTimeErrors(t *testing.T) { @@ -782,9 +817,9 @@ func TestTimeErrors(t *testing.T) { } require.NoError(t, parser.Init()) - metrics, err := parser.Parse([]byte(testString)) + actual, err := parser.Parse([]byte(testString)) require.Error(t, err) - require.Empty(t, metrics) + require.Empty(t, actual) testString2 := `{ "a": 5, @@ -802,9 +837,9 @@ func TestTimeErrors(t *testing.T) { } require.NoError(t, parser.Init()) - metrics, err = parser.Parse([]byte(testString2)) + actual, err = parser.Parse([]byte(testString2)) require.Error(t, err) - require.Empty(t, metrics) + require.Empty(t, actual) require.Equal(t, fmt.Errorf("JSON time key could not be found"), err) } @@ -812,10 +847,10 @@ func TestShareTimestamp(t *testing.T) { parser := &Parser{MetricName: "json_test"} require.NoError(t, parser.Init()) - metrics, err := parser.Parse([]byte(validJSONArrayMultiple)) + actual, err := parser.Parse([]byte(validJSONArrayMultiple)) require.NoError(t, err) - require.Equal(t, 2, len(metrics)) - require.Equal(t, metrics[0].Time(), metrics[1].Time()) + require.Equal(t, 2, len(actual)) + require.Equal(t, actual[0].Time(), actual[1].Time()) } func TestNameKey(t *testing.T) { @@ -832,9 +867,9 @@ func TestNameKey(t *testing.T) { parser := &Parser{NameKey: "b_c"} require.NoError(t, parser.Init()) - metrics, err := parser.Parse([]byte(testString)) + actual, err := parser.Parse([]byte(testString)) require.NoError(t, err) - require.Equal(t, "this is my name", metrics[0].Name()) + require.Equal(t, "this is my name", actual[0].Name()) } func TestParseArrayWithWrongType(t *testing.T) { @@ -1359,6 +1394,85 @@ func TestParseArrayWithWildcardTagKeys(t *testing.T) { } } +func TestBenchmarkData(t *testing.T) { + // Setup the plugin + plugin := &Parser{ + MetricName: "benchmark", + TagKeys: []string{"tags_*"}, + } + require.NoError(t, plugin.Init()) + + expected := []telegraf.Metric{ + metric.New( + "benchmark", + map[string]string{ + "tags_flagname": "F5", + "tags_host": "1cbbb3796fc2", + "tags_key": "12345", + "tags_platform": "Java", + "tags_sdkver": "4.9.1", + "tags_value": "false", + }, + map[string]interface{}{ + "fields_count_sum": float64(5), + "timestamp": float64(1653643420), + }, + time.Unix(0, 0), + ), + metric.New( + "benchmark", + map[string]string{ + "tags_flagname": "E42", + "tags_host": "klaus", + "tags_key": "67890", + "tags_platform": "Golang", + "tags_sdkver": "1.18.3", + "tags_value": "true", + }, + map[string]interface{}{ + "fields_count_sum": float64(42), + "timestamp": float64(1653646789), + }, + time.Unix(0, 0), + ), + } + + // Do the parsing + actual, err := plugin.Parse([]byte(benchmarkData)) + require.NoError(t, err) + testutil.RequireMetricsEqual(t, expected, actual, testutil.IgnoreTime()) +} + +func BenchmarkParsingSequential(b *testing.B) { + // Configure the plugin + plugin := &Parser{ + MetricName: "benchmark", + TagKeys: []string{"tags_*"}, + } + require.NoError(b, plugin.Init()) + + // Do the benchmarking + for n := 0; n < b.N; n++ { + _, _ = plugin.Parse([]byte(benchmarkData)) + } +} + +func BenchmarkParsingParallel(b *testing.B) { + // Configure the plugin + plugin := &Parser{ + MetricName: "benchmark", + TagKeys: []string{"tags_*"}, + } + require.NoError(b, plugin.Init()) + + // Do the benchmarking + b.RunParallel(func(p *testing.PB) { + for p.Next() { + _, _ = plugin.Parse([]byte(benchmarkData)) + } + }) +} + func FuzzParserJSON(f *testing.F) { for _, value := range fuzz.JSONDictionary { f.Add([]byte(value)) diff --git a/plugins/parsers/json_v2/parser_test.go b/plugins/parsers/json_v2/parser_test.go index b0749d9922855..498afd4e51125 100644 --- a/plugins/parsers/json_v2/parser_test.go +++ b/plugins/parsers/json_v2/parser_test.go @@ -15,6 +15,7 @@ import ( "github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs/file" "github.com/influxdata/telegraf/plugins/parsers/influx" + "github.com/influxdata/telegraf/plugins/parsers/json_v2" "github.com/influxdata/telegraf/testutil" ) @@ -34,6 +35,10 @@ func TestMultipleConfigs(t *testing.T) { }) for _, f := range folders { + // Only use directories as those contain test-cases + if !f.IsDir() { + continue + } testdataPath := filepath.Join("testdata", f.Name()) configFilename := filepath.Join(testdataPath, "telegraf.conf") expectedFilename := filepath.Join(testdataPath, "expected.out") @@ -94,3 +99,63 @@ func TestMultipleConfigs(t *testing.T) { }) } } + +func BenchmarkParsingSequential(b *testing.B) { + inputFilename := filepath.Join("testdata", "benchmark", "input.json") + + // Configure the plugin + plugin := &json_v2.Parser{ + Configs: []json_v2.Config{ + { + MeasurementName: "benchmark", + JSONObjects: []json_v2.Object{ + { + Path: "metrics", + DisablePrependKeys: true, + }, + }, + }, + }, + } + require.NoError(b, plugin.Init()) + + // Read the input data + input, err := os.ReadFile(inputFilename) + require.NoError(b, err) + + // Do the benchmarking + for n := 0; n < b.N; n++ { + _, _ = plugin.Parse(input) + } +} + +func BenchmarkParsingParallel(b *testing.B) { + inputFilename := filepath.Join("testdata", "benchmark", "input.json") + + // Configure the plugin + plugin := &json_v2.Parser{ + Configs: []json_v2.Config{ + { + MeasurementName: "benchmark", + JSONObjects: []json_v2.Object{ + { + Path: "metrics", + DisablePrependKeys: true, + }, + }, + }, + }, + } + require.NoError(b, plugin.Init()) + + // Read the input data + input, err := os.ReadFile(inputFilename) + require.NoError(b, err) + + // Do the benchmarking + b.RunParallel(func(p *testing.PB) { + for p.Next() { + _, _ = plugin.Parse(input) + } + }) +} diff --git a/plugins/parsers/json_v2/testdata/benchmark/benchmark.json b/plugins/parsers/json_v2/testdata/benchmark/benchmark.json new file mode 100644 index 0000000000000..4f17cb0962330 --- /dev/null +++ b/plugins/parsers/json_v2/testdata/benchmark/benchmark.json @@ -0,0 +1,34 @@ +{ + "metrics": [ + { + "name": "impression", + "timestamp": 1653643420, + "fields": { + "count_sum": 5 + }, + "tags": { + "key": "12345", + "flagname": "F5", + "host": "1cbbb3796fc2", + "platform": "Java", + "sdkver": "4.9.1", + "value": "false" + } + }, + { + "name": "expression", + "timestamp": 1653646789, + "fields": { + "count_sum": 42 + }, + "tags": { + "key": "67890", + "flagname": "E42", + "host": "klaus", + "platform": "Golang", + "sdkver": "1.18.3", + "value": "true" + } + } + ] +} \ No newline at end of file diff --git a/plugins/parsers/json_v2/testdata/benchmark/expected.out b/plugins/parsers/json_v2/testdata/benchmark/expected.out new file mode 100644 index 0000000000000..3bd3868db3b21 --- /dev/null +++ b/plugins/parsers/json_v2/testdata/benchmark/expected.out @@ -0,0 +1,2 @@ +benchmark count_sum=5,flagname="F5",host="1cbbb3796fc2",key="12345",name="impression",platform="Java",sdkver="4.9.1",timestamp=1653643420,value="false" 1697730835574029794 +benchmark count_sum=42,flagname="E42",host="klaus",key="67890",name="expression",platform="Golang",sdkver="1.18.3",timestamp=1653646789,value="true" 1697730835574029794 diff --git a/plugins/parsers/json_v2/testdata/benchmark/input.json b/plugins/parsers/json_v2/testdata/benchmark/input.json new file mode 100644 index 0000000000000..915ff465c6fcb --- /dev/null +++ b/plugins/parsers/json_v2/testdata/benchmark/input.json @@ -0,0 +1,34 @@ +{ + "metrics": [ + { + "name": "impression", + "timestamp": 1653643420, + "fields": { + "count_sum": 5 + }, + "tags": { + "key": "12345", + "flagname": "F5", + "host": "1cbbb3796fc2", + "platform": "Java", + "sdkver": "4.9.1", + "value": "false" + } + }, + { + "name": "expression", + "timestamp": 1653646789, + "fields": { + "count_sum": 42 + }, + "tags": { + "key": "67890", + "flagname": "E42", + "host": "klaus", + "platform": "Golang", + "sdkver": "1.18.3", + "value": "true" + } + } + ] + } \ No newline at end of file diff --git a/plugins/parsers/json_v2/testdata/benchmark/telegraf.conf b/plugins/parsers/json_v2/testdata/benchmark/telegraf.conf new file mode 100644 index 0000000000000..6c498fbbbe501 --- /dev/null +++ b/plugins/parsers/json_v2/testdata/benchmark/telegraf.conf @@ -0,0 +1,9 @@ +[[inputs.file]] + files = ["./testdata/benchmark/input.json"] + data_format = "json_v2" + + [[inputs.file.json_v2]] + measurement_name = "benchmark" + [[inputs.file.json_v2.object]] + path = 'metrics' + disable_prepend_keys = true