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

Migrate Netflow code to the logp logging library #42270

Open
wants to merge 7 commits into
base: 8.x
Choose a base branch
from

Conversation

jrmolin
Copy link

@jrmolin jrmolin commented Jan 8, 2025

Proposed commit message

[Netflow] Migrate from log to the logp logging library

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.

Disruptive User Impact

Author's Checklist

  • [ ]

How to test this PR locally

Related issues

Use cases

Screenshots

Logs

@jrmolin jrmolin added cleanup Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team Team:Security-Deployment and Devices Deployment and Devices Team in Security Solution labels Jan 8, 2025
@botelastic botelastic bot added needs_team Indicates that the issue/PR needs a Team:* label and removed needs_team Indicates that the issue/PR needs a Team:* label labels Jan 8, 2025
@mergify mergify bot assigned jrmolin Jan 8, 2025
@jrmolin jrmolin marked this pull request as ready for review January 14, 2025 17:26
@jrmolin jrmolin requested a review from a team as a code owner January 14, 2025 17:26
@elasticmachine
Copy link
Collaborator

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

@elasticmachine
Copy link
Collaborator

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

@@ -30,7 +30,7 @@ type Config struct {

var defaultCfg = Config{
protocols: []string{},
logOutput: io.Discard,
logOutput: logp.L().Named("netflow"),
Copy link
Member

Choose a reason for hiding this comment

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

The godoc for Defaults() says "log output is discarded".

With this line, it is hard to guarantee that is true because initialization order can affect logp.L(). The global logger may or may not be initialized at the time at which this call is executed.

I didn't look at how this is used, but maybe it's better to separate the logger from the decoder config concept. And require that users of the decoders pass their own parent *logp.Logger at construction time.

@jrmolin jrmolin requested a review from andrewkroh January 29, 2025 15:03
@stefans-elastic
Copy link
Contributor

just curious - why is this PR to branch 8.x and not main?

@jrmolin
Copy link
Author

jrmolin commented Jan 31, 2025

I was trying to get this into 8.18, @stefans-elastic

@jrmolin
Copy link
Author

jrmolin commented Jan 31, 2025

what should I do now?

Copy link
Member

@andrewkroh andrewkroh left a comment

Choose a reason for hiding this comment

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

It looks like the code doesn't build successfully at the moment.

This PR should target the main branch, then automation will open a PR to backport this into the 8.x branch if you have applied the backport-8.x label.

sharedTemplates: false,
withCache: false,
}

// Defaults returns a configuration object with defaults settings:
// - no protocols are enabled.
// - log output is discarded
Copy link
Member

@andrewkroh andrewkroh Jan 31, 2025

Choose a reason for hiding this comment

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

This statement needs updated given the changes.

"github.com/stretchr/testify/assert"

"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/template"
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/test"
)

var logger = log.New(io.Discard, "", 0)
var logerr = logp.DevelopmentSetup(logp.ToDiscardOutput())
Copy link
Member

Choose a reason for hiding this comment

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

This logerr is never used. It looks like you want to initialize the logger globally. For this, you could do

func init() {
	logp.TestingSetup()
}

or call that in the specific test functions where you expect the logging facility to be setup.

"github.com/stretchr/testify/assert"

"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/template"
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/test"
)

var logger = log.New(io.Discard, "", 0)
var logerr = logp.DevelopmentSetup(logp.ToDiscardOutput())
var glogger = logp.NewLogger("")
Copy link
Member

Choose a reason for hiding this comment

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

This glogger doesn't need to exist. Any place in this file that uses it can be replaced either logp.NewLogger(name) or just logp.L().

@@ -53,7 +51,7 @@ func (c *Config) WithProtocols(protos ...string) *Config {
}

// WithLogOutput sets the output io.Writer for logging.
func (c *Config) WithLogOutput(output io.Writer) *Config {
func (c *Config) WithLogOutput(output *logp.Logger) *Config {
Copy link
Member

Choose a reason for hiding this comment

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

I think there should only be one way to set the logger. Currently you can set it at construction through Defaults(logger), but you can also call this afterwards. I think this should be removed now that you changed Defaults().

}

func New(config config.Config) protocol.Protocol {
return NewProtocol(ProtocolID, &templateV1, readV1Header, log.New(config.LogOutput(), LogPrefix, 0))
return NewProtocol(ProtocolID, &templateV1, readV1Header, logp.L().Named(LogPrefix))
Copy link
Member

@andrewkroh andrewkroh Jan 31, 2025

Choose a reason for hiding this comment

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

This should utilize the "parent" logger as in config.LogOutput().Named(ProtocolName).

(this applies to other protocols as well)

flowTemplate *template.Template
version uint16
readHeader ReadHeaderFn
}

func init() {
protocol.Registry.Register(ProtocolName, New)
_ := protocol.Registry.Register(ProtocolName, New)
Copy link
Member

@andrewkroh andrewkroh Jan 31, 2025

Choose a reason for hiding this comment

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

What's the goal with changing this? Appease the linter? Personally, I would wrap this in panic() because if we accidentally registered two of the same protocols, then we made a big mistake, and this should fail hard during an integration test.

(this applies to other protocols as well)

Copy link
Author

Choose a reason for hiding this comment

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

oh? sure

Copy link
Author

Choose a reason for hiding this comment

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

yes, to appease the linter. I'll make all these edits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleanup Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team Team:Security-Deployment and Devices Deployment and Devices Team in Security Solution
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants