Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Support for validate subcommand for otel configuration #4023

Merged
merged 27 commits into from
Feb 5, 2024

Conversation

michalpristas
Copy link
Contributor

@michalpristas michalpristas commented Jan 5, 2024

performs validation of config passed to the command using flags supported also in otel subcommand (--config and --set)

@michalpristas michalpristas added enhancement New feature or request Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team Team:Elastic-Agent Label for the Agent team backport-skip skip-changelog labels Jan 5, 2024
@michalpristas michalpristas self-assigned this Jan 5, 2024
@michalpristas michalpristas requested a review from a team as a code owner January 5, 2024 13:37
@elasticmachine
Copy link
Contributor

Pinging @elastic/elastic-agent (Team:Elastic-Agent)

@elasticmachine
Copy link
Contributor

Pinging @elastic/elastic-agent-control-plane (Team:Elastic-Agent-Control-Plane)

@michalpristas
Copy link
Contributor Author

UT for logic not for boilerplate results in low soundqube rating

@ycombinator
Copy link
Contributor

With this PR, we are adding a top-level sub-command to the Agent, so users will execute elastic-agent validate. But this command only supports OTel config validation, which could be confusing.

I really think we need to implement #3903 first and move this validate sub-command under that otel sub-command for clarity, so users will execute elastic-agent otel validate. If, in the future, we support validation for OTel and native Agent configuration both, we can promote the validate sub-command back to the top-level.

@@ -0,0 +1,301 @@
###################### Agent Configuration Example #########################
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about removing all the comments from this file, so this file also becomes more like the otel.yml file? Just thinking that the comments are not adding much value to test fixture files.

@ycombinator
Copy link
Contributor

With this PR, the syntax for running OTel configuration validation becomes elastic-agent validate. However, the syntax for validating OTel configuration with the vanilla OTel collector is otelcol validate --config /path/to/first.yml --config /path/to/first.yml ....

To ease migration, I think we'd want Agent (with embedded OTel Collector) to be as much of a drop-in replacement for the vanilla OTel collector. So I think we need to support the same syntax, that is, something like elastic-agent validate --config /path/to/first.yml --config /path/to/first.yml ..., or better yet, elastic-agent otel validate --config /path/to/first.yml --config /path/to/first.yml ....

@michalpristas
Copy link
Contributor Author

once i introduce otel subcommand i will move this under otel subcommand and add multiple files and other flags. for now i prefer to stay with flags we have and not support multiple files using --config as normal agent does not understand that.

the order in my mind is
#4047 to go in
introduce otel
bring this even with normal collector CLI

Co-authored-by: Shaunak Kashyap <ycombinator@gmail.com>
@ycombinator
Copy link
Contributor

I built Agent with this PR, extracted it into a folder, and ran ./elastic-agent validate. I got this error:

"/private/tmp/elastic-agent-8.13.0-SNAPSHOT-darwin-aarch64/elastic-agent.yml" is not an otel config. file should be named 'otel.yml', 'otlp.yml' or 'otelcol.yml'

There is no /private/tmp/elastic-agent-8.13.0-SNAPSHOT-darwin-aarch64/elastic-agent.yml file mentioned in the error message:

$ ls -l /private/tmp/elastic-agent-8.13.0-SNAPSHOT-darwin-aarch64/elastic-agent.yml
ls: /private/tmp/elastic-agent-8.13.0-SNAPSHOT-darwin-aarch64/elastic-agent.yml: No such file or directory

And there is actually an otel.yml file in that folder:

$ ls -l /private/tmp/elastic-agent-8.13.0-SNAPSHOT-darwin-aarch64/otel.yml
-rw-------  1 shaunak  wheel  425 Jan 10 05:29 /private/tmp/elastic-agent-8.13.0-SNAPSHOT-darwin-aarch64/otel.yml

@michalpristas
Copy link
Contributor Author

elastic-agent.yml is a default config value if not provided by --config flag. may be it

@michalpristas
Copy link
Contributor Author

fixed

Comment on lines 35 to 37
if runAsOtel := otel.IsOtelConfig(ctx, cfgFile); !runAsOtel {
return fmt.Errorf("%q is not an otel config. file should be named 'otel.yml', 'otlp.yml' or 'otelcol.yml'", cfgFile)
}
Copy link
Contributor

@ycombinator ycombinator Jan 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this PR is merged but before we change elastic-agent validate to elastic-agent otel validate, the user experience would be that elastic-agent validate only works for OTel configurations. To me, this is a bit awkward as validate is a top-level sub-command and, therefore, suggests that it can validate both OTel and Agent configurations.

So my suggestion for this PR would be to return early from this function with nil if otel.IsOtelConfig(ctx, cfgFile) returns false and also make DefaultConfigName the default value for the -c/--config global CLI options.

Once we have the follow up PR to move elastic-agent validate to elastic-agent otel validate, it would make sense to error out if otel.IsOtelConfig(ctx, cfgFile) returns false.

Co-authored-by: Shaunak Kashyap <ycombinator@gmail.com>
@blakerouse
Copy link
Contributor

It would be really nice to see this do a simple validation of the elastic-agent.yml as well. Providing a nice clean path to validate both. Just an idea, not a block or anything.

@pierrehilbert
Copy link
Contributor

@michalpristas should we put this back on draft for now?

@michalpristas
Copy link
Contributor Author

michalpristas commented Feb 2, 2024

@pierrehilbert after otel subcommand went in, this is up to date and ready for review

@ycombinator
Copy link
Contributor

ycombinator commented Feb 2, 2024

If I run ./elastic-agent otel validate (without any --config options), the command succeeds with a 0 exit code.

As comparison, if I run the OTel Collector binary's validate sub-command without any --config options, that command fails like so:

$ ./bin/otelcorecol_darwin_arm64 validate
Error: at least one config flag must be provided
2024/02/02 04:18:29 collector server run finished with error: at least one config flag must be provided

Would it be possible to mimic the OTel Collector binary's behavior in ./elastic-agent otel validate? Just trying to make this command a drop-in replacement for the native OTel Collector binary as much as possible.

Copy link
Contributor

@ycombinator ycombinator left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from my request/suggestion in #4023 (comment), which I would say is a nice to have, this PR LGTM.

@@ -133,6 +133,11 @@ func TestOtelFileProcessing(t *testing.T) {
`"stringValue":"system.log"`, // system.log is being processed
})

// check `elastic-agent otel validate` command works for otel config
out, err := fixture.Exec(ctx, []string{"otel", "validate", "--config", cfgFilePath})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to see a test (here or elsewhere) that the validate command actually detects errors in otel configuration files.

That is the real value of the validate command, lets make sure it works. This test would pass even if we never called the otel validate function and just parsed the command line arguments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is now addressed at e432f98

Copy link
Contributor Author

@michalpristas michalpristas Feb 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i pulled it outside into separate function for readability but kept within same test to spare some resources

Copy link

Quality Gate failed Quality Gate failed

Failed conditions

9.1% 9.1% Coverage on New Code (required ≥ 40%)

See analysis details on SonarQube

@michalpristas michalpristas requested a review from cmacknz February 2, 2024 20:57
@cmacknz
Copy link
Member

cmacknz commented Feb 2, 2024

LGTM, fine to force merge this. Will wait until Michal is back online before doing so.

@cmacknz cmacknz merged commit 6f7a116 into elastic:main Feb 5, 2024
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-skip enhancement New feature or request skip-changelog Team:Elastic-Agent Label for the Agent team Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants