-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
base: 8.x
Are you sure you want to change the base?
Conversation
Pinging @elastic/elastic-agent-control-plane (Team:Elastic-Agent-Control-Plane) |
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"), |
There was a problem hiding this comment.
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.
just curious - why is this PR to branch |
I was trying to get this into 8.18, @stefans-elastic |
what should I do now? |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
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("") |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh? sure
There was a problem hiding this comment.
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.
Proposed commit message
[Netflow] Migrate from log to the logp logging library
Checklist
CHANGELOG.next.asciidoc
orCHANGELOG-developer.next.asciidoc
.Disruptive User Impact
Author's Checklist
How to test this PR locally
Related issues
Use cases
Screenshots
Logs