From 772cbc68a6fb592592f86b1181beb235a8c9a99a Mon Sep 17 00:00:00 2001 From: "zeyuan.li" <50520979+li-zeyuan@users.noreply.github.com> Date: Fri, 19 Apr 2024 22:53:50 +0800 Subject: [PATCH] [pkg/stanza] when the 'jsonArrayParserFeatureGate' is disabled, the func 'Build' returns an error (#32501) **Description:** bug: - collector launch fail with '--feature-gates=logs.jsonParserArray' how to fix: 1. `func init()` always register operator to `operator.DefaultRegistry`. 2. when the `jsonArrayParserFeatureGate` is disabled, the func `Build` returns an error. **Link to tracking Issue:** https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/32313 --------- Co-authored-by: Daniel Jaglowski --- .chloggen/fix-jsonarrayparser-build.yaml | 27 +++++++++++++++ pkg/stanza/go.mod | 3 ++ .../operator/parser/jsonarray/config.go | 9 +++-- .../operator/parser/jsonarray/config_test.go | 34 ++++++++++++++++++- .../operator/parser/jsonarray/parser_test.go | 13 +++++++ processor/logstransformprocessor/go.mod | 2 ++ receiver/azureeventhubreceiver/go.mod | 2 ++ receiver/filelogreceiver/go.mod | 2 ++ receiver/journaldreceiver/go.mod | 2 ++ receiver/namedpipereceiver/go.mod | 2 ++ receiver/otlpjsonfilereceiver/go.mod | 2 ++ receiver/sqlqueryreceiver/go.mod | 2 ++ receiver/syslogreceiver/go.mod | 2 ++ receiver/tcplogreceiver/go.mod | 2 ++ receiver/udplogreceiver/go.mod | 2 ++ receiver/windowseventlogreceiver/go.mod | 2 ++ 16 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 .chloggen/fix-jsonarrayparser-build.yaml diff --git a/.chloggen/fix-jsonarrayparser-build.yaml b/.chloggen/fix-jsonarrayparser-build.yaml new file mode 100644 index 000000000000..ee60ae9677d9 --- /dev/null +++ b/.chloggen/fix-jsonarrayparser-build.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: pkg/stanza + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix race condition which prevented `jsonArrayParserFeatureGate` from working correctly. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [32313] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/pkg/stanza/go.mod b/pkg/stanza/go.mod index 2c3982578147..50cf6de2ba71 100644 --- a/pkg/stanza/go.mod +++ b/pkg/stanza/go.mod @@ -11,6 +11,7 @@ require ( github.com/jpillora/backoff v1.0.0 github.com/json-iterator/go v1.1.12 github.com/open-telemetry/opentelemetry-collector-contrib/extension/storage v0.98.0 + github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.98.0 github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.98.0 github.com/stretchr/testify v1.9.0 github.com/valyala/fastjson v1.6.4 @@ -78,6 +79,8 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/extension/stor replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal => ../../internal/coreinternal +replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => ../../internal/common + retract ( v0.76.2 v0.76.1 diff --git a/pkg/stanza/operator/parser/jsonarray/config.go b/pkg/stanza/operator/parser/jsonarray/config.go index f046aed64299..f11a6b2f71f2 100644 --- a/pkg/stanza/operator/parser/jsonarray/config.go +++ b/pkg/stanza/operator/parser/jsonarray/config.go @@ -3,6 +3,7 @@ package jsonarray // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/parser/jsonarray" import ( + "fmt" "strings" "github.com/valyala/fastjson" @@ -26,9 +27,7 @@ var jsonArrayParserFeatureGate = featuregate.GlobalRegistry().MustRegister( ) func init() { - if jsonArrayParserFeatureGate.IsEnabled() { - operator.Register(operatorType, func() operator.Builder { return NewConfig() }) - } + operator.Register(operatorType, func() operator.Builder { return NewConfig() }) } // NewConfig creates a new json array parser config with default values @@ -51,6 +50,10 @@ type Config struct { // Build will build a json array parser operator. func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) { + if !jsonArrayParserFeatureGate.IsEnabled() { + return nil, fmt.Errorf("%s operator disabled", operatorType) + } + parserOperator, err := c.ParserConfig.Build(logger) if err != nil { return nil, err diff --git a/pkg/stanza/operator/parser/jsonarray/config_test.go b/pkg/stanza/operator/parser/jsonarray/config_test.go index 59352ef3761c..30b1364630da 100644 --- a/pkg/stanza/operator/parser/jsonarray/config_test.go +++ b/pkg/stanza/operator/parser/jsonarray/config_test.go @@ -6,13 +6,17 @@ import ( "path/filepath" "testing" + "github.com/stretchr/testify/require" + + commontestutil "github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/testutil" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/operatortest" + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/testutil" ) func TestConfig(t *testing.T) { - // Manually adding operator to the Registry as its behind a feature gate + // Manually adding operator to the Registry as it's behind a feature gate operator.Register(operatorType, func() operator.Builder { return NewConfig() }) operatortest.ConfigUnmarshalTests{ @@ -65,3 +69,31 @@ func TestConfig(t *testing.T) { }, }.Run(t) } + +func TestBuildWithFeatureGate(t *testing.T) { + cases := []struct { + name string + isFeatureGateEnable bool + onErr string + }{ + {"jsonarray_enabled", true, ""}, + {"jsonarray_disabled", false, "operator disabled"}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + if c.isFeatureGateEnable { + defer commontestutil.SetFeatureGateForTest(t, jsonArrayParserFeatureGate, true)() + } + + buildFunc, ok := operator.Lookup(operatorType) + require.True(t, ok) + + _, err := buildFunc().Build(testutil.Logger(t)) + if err != nil { + require.Contains(t, err.Error(), c.onErr) + } + }) + } + +} diff --git a/pkg/stanza/operator/parser/jsonarray/parser_test.go b/pkg/stanza/operator/parser/jsonarray/parser_test.go index 8700651e3175..6a95d464c07d 100644 --- a/pkg/stanza/operator/parser/jsonarray/parser_test.go +++ b/pkg/stanza/operator/parser/jsonarray/parser_test.go @@ -9,12 +9,15 @@ import ( "github.com/stretchr/testify/require" + commontestutil "github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/testutil" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/testutil" ) func newTestParser(t *testing.T) *Parser { + defer commontestutil.SetFeatureGateForTest(t, jsonArrayParserFeatureGate, true)() + cfg := NewConfigWithID("test") op, err := cfg.Build(testutil.Logger(t)) require.NoError(t, err) @@ -22,6 +25,8 @@ func newTestParser(t *testing.T) *Parser { } func TestParserBuildFailure(t *testing.T) { + defer commontestutil.SetFeatureGateForTest(t, jsonArrayParserFeatureGate, true)() + cfg := NewConfigWithID("test") cfg.OnError = "invalid_on_error" _, err := cfg.Build(testutil.Logger(t)) @@ -37,6 +42,8 @@ func TestParserInvalidType(t *testing.T) { } func TestParserByteFailureHeadersMismatch(t *testing.T) { + defer commontestutil.SetFeatureGateForTest(t, jsonArrayParserFeatureGate, true)() + cfg := NewConfigWithID("test") cfg.Header = "name,sev,msg" op, err := cfg.Build(testutil.Logger(t)) @@ -48,6 +55,8 @@ func TestParserByteFailureHeadersMismatch(t *testing.T) { } func TestParserJarray(t *testing.T) { + defer commontestutil.SetFeatureGateForTest(t, jsonArrayParserFeatureGate, true)() + cases := []struct { name string configure func(*Config) @@ -268,6 +277,8 @@ func TestParserJarray(t *testing.T) { } func TestParserJarrayMultiline(t *testing.T) { + defer commontestutil.SetFeatureGateForTest(t, jsonArrayParserFeatureGate, true)() + cases := []struct { name string input string @@ -374,6 +385,8 @@ dd","eeee"]`, } func TestBuildParserJarray(t *testing.T) { + defer commontestutil.SetFeatureGateForTest(t, jsonArrayParserFeatureGate, true)() + newBasicParser := func() *Config { cfg := NewConfigWithID("test") cfg.OutputIDs = []string{"test"} diff --git a/processor/logstransformprocessor/go.mod b/processor/logstransformprocessor/go.mod index 31c62455c1b1..41f488e8b74f 100644 --- a/processor/logstransformprocessor/go.mod +++ b/processor/logstransformprocessor/go.mod @@ -85,3 +85,5 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil => ../../pkg/pdatautil replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden + +replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => ../../internal/common diff --git a/receiver/azureeventhubreceiver/go.mod b/receiver/azureeventhubreceiver/go.mod index d157366d7cb9..e4e805c7c12d 100644 --- a/receiver/azureeventhubreceiver/go.mod +++ b/receiver/azureeventhubreceiver/go.mod @@ -145,3 +145,5 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/share replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/azure => ../../pkg/translator/azure replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden + +replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => ../../internal/common diff --git a/receiver/filelogreceiver/go.mod b/receiver/filelogreceiver/go.mod index 02339bbd7e9d..7447d894aaf6 100644 --- a/receiver/filelogreceiver/go.mod +++ b/receiver/filelogreceiver/go.mod @@ -84,3 +84,5 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../../pkg/pdatatest replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden + +replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => ../../internal/common diff --git a/receiver/journaldreceiver/go.mod b/receiver/journaldreceiver/go.mod index 2eabce4f648e..75e761d645a8 100644 --- a/receiver/journaldreceiver/go.mod +++ b/receiver/journaldreceiver/go.mod @@ -81,3 +81,5 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../../pkg/pdatatest replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden + +replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => ../../internal/common diff --git a/receiver/namedpipereceiver/go.mod b/receiver/namedpipereceiver/go.mod index 9864e66dcdff..6dff0706a0a0 100644 --- a/receiver/namedpipereceiver/go.mod +++ b/receiver/namedpipereceiver/go.mod @@ -76,3 +76,5 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../../pkg/pdatatest replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden + +replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => ../../internal/common diff --git a/receiver/otlpjsonfilereceiver/go.mod b/receiver/otlpjsonfilereceiver/go.mod index 3b641e8ad815..2fa54b715512 100644 --- a/receiver/otlpjsonfilereceiver/go.mod +++ b/receiver/otlpjsonfilereceiver/go.mod @@ -84,3 +84,5 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil => ../../pkg/pdatautil replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden + +replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => ../../internal/common diff --git a/receiver/sqlqueryreceiver/go.mod b/receiver/sqlqueryreceiver/go.mod index 2615aaa5ab09..febe76ec65d2 100644 --- a/receiver/sqlqueryreceiver/go.mod +++ b/receiver/sqlqueryreceiver/go.mod @@ -181,3 +181,5 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza => replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/sqlquery => ../../internal/sqlquery + +replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => ../../internal/common diff --git a/receiver/syslogreceiver/go.mod b/receiver/syslogreceiver/go.mod index e7796f3458a1..8ab1bdc0caf6 100644 --- a/receiver/syslogreceiver/go.mod +++ b/receiver/syslogreceiver/go.mod @@ -85,3 +85,5 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../../pkg/pdatatest replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden + +replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => ../../internal/common diff --git a/receiver/tcplogreceiver/go.mod b/receiver/tcplogreceiver/go.mod index c2cc880f7817..c7a566ef0c63 100644 --- a/receiver/tcplogreceiver/go.mod +++ b/receiver/tcplogreceiver/go.mod @@ -85,3 +85,5 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../../pkg/pdatatest replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden + +replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => ../../internal/common diff --git a/receiver/udplogreceiver/go.mod b/receiver/udplogreceiver/go.mod index f80990534e53..7ceaf834266d 100644 --- a/receiver/udplogreceiver/go.mod +++ b/receiver/udplogreceiver/go.mod @@ -81,3 +81,5 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../../pkg/pdatatest replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden + +replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => ../../internal/common diff --git a/receiver/windowseventlogreceiver/go.mod b/receiver/windowseventlogreceiver/go.mod index 8fc5f70ea124..b66c470ec912 100644 --- a/receiver/windowseventlogreceiver/go.mod +++ b/receiver/windowseventlogreceiver/go.mod @@ -81,3 +81,5 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../../pkg/pdatatest replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden + +replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => ../../internal/common