diff --git a/dev-tools/mage/gotest.go b/dev-tools/mage/gotest.go index 6d6590a1c31b..de7db7e3825a 100644 --- a/dev-tools/mage/gotest.go +++ b/dev-tools/mage/gotest.go @@ -471,6 +471,13 @@ func BuildSystemTestBinary() error { return BuildSystemTestGoBinary(DefaultTestBinaryArgs()) } +// BuildSystemTestOTelBinary builds beat binary that includes otel. +func BuildSystemTestOTelBinary() error { + args := DefaultTestBinaryArgs() + args.ExtraFlags = []string{"-tags", "otelbeat"} + return BuildSystemTestGoBinary(args) +} + // BuildSystemTestGoBinary build a binary for testing that is instrumented for // testing and measuring code coverage. The binary is only instrumented for // coverage when TEST_COVERAGE=true (default is false). diff --git a/x-pack/filebeat/cmd/otel_test.go b/x-pack/filebeat/cmd/otel_test.go deleted file mode 100644 index f50a0bfc39af..000000000000 --- a/x-pack/filebeat/cmd/otel_test.go +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one -// or more contributor license agreements. Licensed under the Elastic License; -// you may not use this file except in compliance with the Elastic License. - -package cmd - -import ( - "context" - "path/filepath" - "testing" - "time" - - "github.com/stretchr/testify/require" - - "github.com/elastic/beats/v7/x-pack/libbeat/common/otelbeat" -) - -func TestOtel(t *testing.T) { - rootDir, _ := filepath.Abs("../filebeat-otel.yml") - - // Create the command - cmd := otelbeat.OTelCmd("filebeat") - cmd.SetArgs([]string{"otel", "--config", rootDir}) - - // Set up a context with a timeout to avoid indefinite blocking - ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) - defer cancel() - - // Run the command in a goroutine - errCh := make(chan error, 1) - go func() { - err := cmd.Execute() - errCh <- err - }() - - // Wait for 15s to check there were no errors in starting the otel collector - select { - case err := <-errCh: - // Assert no error occurred - require.NoError(t, err, "cmd.RunE should not return an error") - case <-ctx.Done(): - return - } -} diff --git a/x-pack/filebeat/cmd/otelcmd_disabled.go b/x-pack/filebeat/cmd/otelcmd_disabled.go new file mode 100644 index 000000000000..af7e1424dd0a --- /dev/null +++ b/x-pack/filebeat/cmd/otelcmd_disabled.go @@ -0,0 +1,15 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +//go:build !otelbeat + +package cmd + +import ( + cmd "github.com/elastic/beats/v7/libbeat/cmd" +) + +func addOTelCommand(command *cmd.BeatsRootCmd) { + // No-op +} diff --git a/x-pack/filebeat/cmd/otelcmd_enabled.go b/x-pack/filebeat/cmd/otelcmd_enabled.go new file mode 100644 index 000000000000..09bf09cdade2 --- /dev/null +++ b/x-pack/filebeat/cmd/otelcmd_enabled.go @@ -0,0 +1,17 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +//go:build otelbeat + +package cmd + +import ( + fbcmd "github.com/elastic/beats/v7/filebeat/cmd" + cmd "github.com/elastic/beats/v7/libbeat/cmd" + "github.com/elastic/beats/v7/x-pack/libbeat/common/otelbeat" +) + +func addOTelCommand(command *cmd.BeatsRootCmd) { + command.AddCommand(otelbeat.OTelCmd(fbcmd.Name)) +} diff --git a/x-pack/filebeat/cmd/root.go b/x-pack/filebeat/cmd/root.go index 1e3ad48c2a02..9b44ebb55960 100644 --- a/x-pack/filebeat/cmd/root.go +++ b/x-pack/filebeat/cmd/root.go @@ -17,7 +17,6 @@ import ( "github.com/elastic/beats/v7/libbeat/publisher/processing" "github.com/elastic/beats/v7/x-pack/filebeat/include" inputs "github.com/elastic/beats/v7/x-pack/filebeat/input/default-inputs" - "github.com/elastic/beats/v7/x-pack/libbeat/common/otelbeat" "github.com/elastic/beats/v7/x-pack/libbeat/management" // Register the includes. @@ -41,7 +40,7 @@ func Filebeat() *cmd.BeatsRootCmd { command.PersistentPreRun = func(cmd *cobra.Command, args []string) { management.ConfigTransform.SetTransform(filebeatCfg) } - command.AddCommand(otelbeat.OTelCmd(fbcmd.Name)) + addOTelCommand(command) return command } diff --git a/x-pack/filebeat/magefile.go b/x-pack/filebeat/magefile.go index cd1cff476781..91fb608c31d5 100644 --- a/x-pack/filebeat/magefile.go +++ b/x-pack/filebeat/magefile.go @@ -43,6 +43,13 @@ func Build() error { return devtools.Build(devtools.DefaultBuildArgs()) } +// BuildOTel builds the Beat binary with OTel sub command +func BuildOTel() error { + args := devtools.DefaultBuildArgs() + args.ExtraFlags = append(args.ExtraFlags, "-tags", "otelbeat") + return devtools.Build(args) +} + // BuildSystemTestBinary builds a binary instrumented for use with Python system tests. func BuildSystemTestBinary() error { return devtools.BuildSystemTestBinary() @@ -168,13 +175,15 @@ func IntegTest() { // GoIntegTest starts the docker containers and executes the Go integration tests. func GoIntegTest(ctx context.Context) error { - mg.Deps(BuildSystemTestBinary) + // build integration test binary with otel sub command + devtools.BuildSystemTestOTelBinary() return devtools.GoIntegTestFromHost(ctx, devtools.DefaultGoTestIntegrationFromHostArgs()) } // GoFIPSOnlyIntegTest starts the docker containers and executes the Go integration tests with GODEBUG=fips140=only set. func GoFIPSOnlyIntegTest(ctx context.Context) error { - mg.Deps(BuildSystemTestBinary) + // build integration test binary with otel sub command + devtools.BuildSystemTestOTelBinary() return devtools.GoIntegTestFromHost(ctx, devtools.FIPSOnlyGoTestIntegrationFromHostArgs()) }