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 support for zap v1.x APIs #11

Merged
merged 12 commits into from
May 5, 2017
Merged

Add support for zap v1.x APIs #11

merged 12 commits into from
May 5, 2017

Conversation

Vasilis
Copy link
Contributor

@Vasilis Vasilis commented Apr 29, 2017

Before opening your pull request, please make sure that you have:

Thanks for your contribution!

@@ -9,10 +9,9 @@ import:
- package: github.com/spacemonkeygo/monotime
- package: github.com/uber/uber-licence
- package: github.com/uber-go/zap
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

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)
Copy link
Contributor

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.

@@ -28,12 +28,14 @@ import (

"github.com/spacemonkeygo/monotime"
"github.com/stretchr/testify/assert"
"github.com/uber-go/zap"
"github.com/uber/arachne/config"
Copy link

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

Copy link
Contributor Author

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.

@@ -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))
Copy link

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)

Copy link

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed! Thank you, @billf and @jcorbin

@@ -514,7 +516,8 @@ func printTableEntry(
logger.Info("Result",
zap.String("host", targetHost),
zap.Int("source_port", int(srcPort)),
Copy link

Choose a reason for hiding this comment

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

this is supported natively

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed!

@henridevieux
Copy link
Contributor

Looks good

Copy link

@jcorbin jcorbin left a 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()
Copy link

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) }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Thanks!

bootstrapLogger.Error("Log level provided", zap.Error(err))
}

config := zap.Config{
Copy link

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?

Copy link
Contributor Author

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()

output),
l, err := config.Build()
if err != nil {
return nil, errors.New("failed to create logger")
Copy link

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)

Copy link

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

Copy link
Contributor Author

@Vasilis Vasilis May 3, 2017

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) }

Copy link
Contributor Author

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.

@@ -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))
Copy link

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

Copy link

@jcorbin jcorbin left a 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)
Copy link

Choose a reason for hiding this comment

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

why not bootstrapLogger.Fatal ?

Copy link
Contributor Author

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()
Copy link

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
Copy link

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",
Copy link

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?

Copy link
Contributor Author

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.

if err != nil {
logger.Debug("failed to send out", zap.String("flag", flag), srcZap, dstZap)
logger.Debug("failed to send out", lf...)
Copy link

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Converted to Error. Thanks.

@Vasilis Vasilis merged commit 49fea49 into master May 5, 2017
@Vasilis Vasilis deleted the zap branch May 5, 2017 22:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants