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

Add logging.with_fields to beatreceiver config #42961

Closed
wants to merge 18 commits into from

Conversation

khushijain21
Copy link
Contributor

@khushijain21 khushijain21 commented Feb 28, 2025

Proposed commit message

It adds with_fields config to logging section of the beatreceiver - which allows for additional structured context to the logs emitted by beat process
For https://github.com/elastic/ingest-dev/issues/5139

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in CHANGELOG.next.asciidoc or CHANGELOG-developer.next.asciidoc.

Related issues

@khushijain21 khushijain21 requested review from a team as code owners February 28, 2025 11:19
@botelastic botelastic bot added the needs_team Indicates that the issue/PR needs a Team:* label label Feb 28, 2025
Copy link
Contributor

mergify bot commented Feb 28, 2025

This pull request does not have a backport label.
If this is a bug or security fix, could you label this PR @khushijain21? 🙏.
For such, you'll need to label your PR with:

  • The upcoming major version of the Elastic Stack
  • The upcoming minor version of the Elastic Stack (if you're not pushing a breaking change)

To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-8./d is the label to automatically backport to the 8./d branch. /d is the digit
  • backport-active-all is the label that automatically backports to all active branches.
  • backport-active-8 is the label that automatically backports to all active minor branches for the 8 major.
  • backport-active-9 is the label that automatically backports to all active minor branches for the 9 major.

@khushijain21 khushijain21 marked this pull request as draft February 28, 2025 11:20
@khushijain21 khushijain21 marked this pull request as ready for review February 28, 2025 12:54
Copy link
Member

@mauri870 mauri870 left a comment

Choose a reason for hiding this comment

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

I had already a PR to bump elastic-agent-libs as part of a fix to all active branches at #42804.

If you do not mind, I would like to include the update to libs in that PR, in that case you might have to wait in order to merge this.

@khushijain21 khushijain21 added the Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team label Mar 3, 2025
@elasticmachine
Copy link
Collaborator

Pinging @elastic/elastic-agent-data-plane (Team:Elastic-Agent-Data-Plane)

@botelastic botelastic bot removed the needs_team Indicates that the issue/PR needs a Team:* label label Mar 3, 2025
@khushijain21 khushijain21 requested review from a team as code owners March 3, 2025 06:10
@khushijain21 khushijain21 requested a review from a team as a code owner March 3, 2025 06:10
@@ -346,6 +346,14 @@ func NewBeatReceiver(settings Settings, receiverConfig map[string]interface{}, u
return nil, fmt.Errorf("error unpacking beats logging config: %w\n%v", err, b.Config.Logging)
}

if logpConfig.WithFields != nil {
var fields = []zapcore.Field{}
for key, value := range logpConfig.WithFields {
Copy link
Member

Choose a reason for hiding this comment

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

You should be able to write a test that ensures setting this produces logs with the expected fields. There are several system tests that run a beat and inspect the log files you could look at.

Copy link
Contributor Author

@khushijain21 khushijain21 Mar 4, 2025

Choose a reason for hiding this comment

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

Since this is restricted to beatreceivers - will use the existing tests in elastic-agent to ensure this setting.

Related PR on elastic-agent: elastic/elastic-agent#7062

Copy link
Member

Choose a reason for hiding this comment

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

This is a Beats specific change in beat.go, it doesn't make sense to me to test it in agent because a PR in agent could never break this.

Agent will rely on this feature, but the elastic agent repository shouldn't be responsible for testing that it works on its own.

One way to think about this is a series of contracts that must be upheld:

  1. Beat logs include arbitrary custom fields with logging.with_fields is set. You can specifically test that the component fields can be injected here.
  2. That agent sets the logging.with_fields configuration of each beat receiver when its own log level changes.

Those two things tested independently should guarantee this works properly.

@khushijain21 khushijain21 changed the title Add logging.with_fields to beat config Add logging.with_fields to beatreceiver config Mar 4, 2025
@pierrehilbert pierrehilbert added Team:obs-ds-hosted-services Label for the Observability Hosted Services team Team:Security-Linux Platform Linux Platform Team in Security Solution Team:Security-Windows Platform Windows Platform Team in Security Solution Team:Security-Deployment and Devices Deployment and Devices Team in Security Solution labels Mar 4, 2025
@elasticmachine
Copy link
Collaborator

Pinging @elastic/sec-linux-platform (Team:Security-Linux Platform)

@elasticmachine
Copy link
Collaborator

Pinging @elastic/obs-ds-hosted-services (Team:obs-ds-hosted-services)

@elasticmachine
Copy link
Collaborator

Pinging @elastic/sec-deployment-and-devices (Team:Security-Deployment and Devices)

@elasticmachine
Copy link
Collaborator

Pinging @elastic/sec-windows-platform (Team:Security-Windows Platform)

Copy link
Contributor

mergify bot commented Mar 4, 2025

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b poc upstream/poc
git merge upstream/main
git push upstream poc

@khushijain21
Copy link
Contributor Author

/test

@khushijain21
Copy link
Contributor Author

/test

@@ -348,6 +348,14 @@ func NewBeatReceiver(settings Settings, receiverConfig map[string]interface{}, u
return nil, fmt.Errorf("error unpacking beats logging config: %w\n%v", err, b.Config.Logging)
}

if logpConfig.WithFields != nil {
var fields = []zapcore.Field{}
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree this works, but I'm wondering why logp.ConfigureWithCore isn't doing this? It is the logp config that has the option, I would just expect it to happen there, much like the selectors get set there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad, you are right. I will raise close this and open a separate PR on elastic-agent-libs. Thank you for pointing this

@khushijain21
Copy link
Contributor Author

Closed this in favor of elastic/elastic-agent-libs#290

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team Team:obs-ds-hosted-services Label for the Observability Hosted Services team Team:Security-Deployment and Devices Deployment and Devices Team in Security Solution Team:Security-Linux Platform Linux Platform Team in Security Solution Team:Security-Windows Platform Windows Platform Team in Security Solution
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants