-
Notifications
You must be signed in to change notification settings - Fork 30
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 support for zap v1.x APIs #11
Conversation
@@ -9,10 +9,9 @@ import: | |||
- package: github.com/spacemonkeygo/monotime | |||
- package: github.com/uber/uber-licence | |||
- package: github.com/uber-go/zap |
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 its a good idea to pin to ^1 to avoid breaking changes if they release 2.x.x
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.
Done.
internal/log/log.go
Outdated
if c.LogSink != "" { | ||
sink, err := os.OpenFile(c.LogSink, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0644) | ||
_, err := os.OpenFile(c.LogSink, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0644) |
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.
You don't need this code if you add the file path to config.OutputPaths. The config.Build()
will then end up calling zap.Open() on the file.
collector/collector_test.go
Outdated
@@ -28,12 +28,14 @@ import ( | |||
|
|||
"github.com/spacemonkeygo/monotime" | |||
"github.com/stretchr/testify/assert" | |||
"github.com/uber-go/zap" | |||
"github.com/uber/arachne/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.
github.com/uber/arachne
are local includes that should be separate from the externals
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.
They should still be separate from standard libs, e.g. "net", "sync" right? Please see next diff.
internal/tcp/tcp.go
Outdated
@@ -522,7 +524,8 @@ func Receiver( | |||
// Received SYN+ACK (Open port) | |||
logger.Debug("Received", | |||
zap.String("flag", "SYN ACK"), | |||
zap.Object("port", pkt.srcPort)) | |||
zap.String("src_address", fromAddrStr), | |||
zap.Any("src_port", pkt.srcPort)) |
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.
zap.Uint16
otherwise these use reflection
(comment repeats throughout)
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 docs actually over state things:
zap.Any
is just a big type assertion https://github.com/uber-go/zap/blob/master/field.go#L233- the final case there uses reflection, but everything else has a much faster path
- now, this is micro slow because it needs to allocate an
uint16
on the heap to pass it as interface{} (until go 1.9) - so yeah, probably stop using
zap.Any
in hot paths for sure, but the reason isn't primarily "reflection" in this case
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.
collector/collector.go
Outdated
@@ -514,7 +516,8 @@ func printTableEntry( | |||
logger.Info("Result", | |||
zap.String("host", targetHost), | |||
zap.Int("source_port", int(srcPort)), |
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 is supported natively
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.
Fixed!
Looks good |
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.
Initial skim, will finish read through later.
bootstrap.go
Outdated
@@ -41,7 +41,12 @@ func Run(ec *config.Extended, opts ...Option) { | |||
err error | |||
) | |||
|
|||
bootstrapLogger := zap.New(zap.NewJSONEncoder()) | |||
bl, _ := zap.NewProduction() |
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.
You really should check that error and panic or call core log.Fatal(err)
on it; I'd suggest the latter:
import coreLog "log"
bl, err := zap.NewProduction()
if err != nil { coreLog.Fatal(err) }
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.
Done. Thanks!
internal/log/log.go
Outdated
bootstrapLogger.Error("Log level provided", zap.Error(err)) | ||
} | ||
|
||
config := zap.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.
Why not take this all the way back into your config shape? Compatibility? Any plans to do so in the future?
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.
Done. zap.Config now created in config.Get() and passed to log.CreateLogger()
internal/log/log.go
Outdated
output), | ||
l, err := config.Build() | ||
if err != nil { | ||
return nil, errors.New("failed to create logger") |
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.
don't hide the reason why, at least do a fmt.Errorf("failed to create loger: %v", err)
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.
preferably using github.com/pkg/errors
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 err message is not hidden, but displayed by the calling function:
logger, err := log.CreateLogger(&gl.App.Logging, d.ArachneService, osHostname, gl.App.PIDPath, util.RemovePID, *(gl.CLI.Foreground), bootstrapLogger) if err != nil { bootstrapLogger.Error("unable to initialize Arachne Logger", zap.Error(err)) os.Exit(1) }
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.
Now using github.com/pkg/errors
globally.
internal/tcp/tcp.go
Outdated
@@ -522,7 +524,8 @@ func Receiver( | |||
// Received SYN+ACK (Open port) | |||
logger.Debug("Received", | |||
zap.String("flag", "SYN ACK"), | |||
zap.Object("port", pkt.srcPort)) | |||
zap.String("src_address", fromAddrStr), | |||
zap.Any("src_port", pkt.srcPort)) |
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 docs actually over state things:
zap.Any
is just a big type assertion https://github.com/uber-go/zap/blob/master/field.go#L233- the final case there uses reflection, but everything else has a much faster path
- now, this is micro slow because it needs to allocate an
uint16
on the heap to pass it as interface{} (until go 1.9) - so yeah, probably stop using
zap.Any
in hot paths for sure, but the reason isn't primarily "reflection" in this case
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.
Meant to accept with last batch of comments.
bootstrap.go
Outdated
if err != nil { | ||
bootstrapLogger.Fatal("unable to initialize Arachne Logger", zap.Error(err)) | ||
bootstrapLogger.Error("unable to initialize Arachne Logger", zap.Error(err)) | ||
os.Exit(1) |
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.
why not bootstrapLogger.Fatal
?
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.
Reverted to Fatal
zap.InfoLevel, | ||
zap.DiscardOutput, | ||
) | ||
l, err := zap.NewDevelopment() |
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.
you may want to look into using zap's test support, but that's mostly for when you want to make assertions about "these sorts of logs happened"
glide.yaml
Outdated
@@ -4,12 +4,16 @@ import: | |||
subpackages: | |||
- statsd | |||
- package: github.com/fatih/color | |||
- package: github.com/hashicorp/go-multierror |
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.
consider switching to go.uber.org/multierr
( http://go.uber.org/multierr )
// Get the file size | ||
stat, err := file.Stat() | ||
if err != nil { | ||
logger.Error("failed to read the FileInfo structure of the log file", |
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.
If you're going to return a multi-error, why do you also need to log about each of them? The caller should be able to log the multi-error after all this is done?
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.
ResetLogFiles() now does not return anything. It's returned error was not previously used anyways.
internal/tcp/tcp.go
Outdated
if err != nil { | ||
logger.Debug("failed to send out", zap.String("flag", flag), srcZap, dstZap) | ||
logger.Debug("failed to send out", lf...) |
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.
sounds like more of a warn or error log? but this was debug before, so not important I suppose
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.
Converted to Error
. Thanks.
Before opening your pull request, please make sure that you have:
make test
); and finally,make lint
).Thanks for your contribution!