-
Notifications
You must be signed in to change notification settings - Fork 86
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 to zap logger #223
Conversation
895c1fb
to
ee4cba7
Compare
@templecloud do you have any insight into why the e2e tests are failing (I've merged your branch and rebased on top)? |
pkg/log/log.go
Outdated
) | ||
|
||
func init() { | ||
flag.StringVar(&lvlString, "log-level", lvlString, "Adjusts the level of the logs that will be omitted.") |
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 can be cleaned up.
var (
lvl = zapcore.InfoLevel
logJSON = true
config *zap.Config
mu sync.Mutex
)
func init() {
flag.Var(&lvl, "log-level", "Adjusts the level of the logs that will be omitted.")
flag.BoolVar(&logJSON, "log-json", logJSON, "Log in json format.")
}
....
config.Level.SetLevel(lvl)
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.
zapcore.Level
is a int8
though. Doesn't that required --log-level=0
vs --log-level=info
?
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.
That's why it's a Var
. You'd pass strings. The level implements the flag interface https://github.com/uber-go/zap/blob/master/zapcore/level.go
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.
TIL there's a flag interface. Cheers 👍
pkg/log/log.go
Outdated
cfg = zap.NewProductionConfig() | ||
} | ||
|
||
if len(logfilePath) > 0 { |
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.
how does rotation work?
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 mistakenly presumed filebeat would handle that for us. I guess I should add support for log rotation via https://github.com/natefinch/lumberjack?
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.
potentially? I was more so just curious how it works. If OKE is handling it with filebeat then that's fine.
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.
As far as I understand it Filebeat doesn't actually support log rotation though hence my suggestion we do it at the application level.
pkg/oci/client/compute.go
Outdated
@@ -106,14 +105,19 @@ func (c *client) GetPrimaryVNICForInstance(ctx context.Context, compartmentID, i | |||
|
|||
for _, attachment := range resp.Items { | |||
if attachment.LifecycleState != core.VnicAttachmentLifecycleStateAttached { | |||
glog.Infof("VNIC attachment %q for instance %q has a state of %q (not %q)", | |||
*attachment.Id, instanceID, attachment.LifecycleState, core.VnicAttachmentLifecycleStateAttached) | |||
c.logger.With( |
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 should create a context logger at the beginning of this method call with instance (and maybe compartment id) as fields.
pkg/oci/client/compute.go
Outdated
"vnicAttachmentID", *attachment.Id, | ||
"expectedLifecycleState", core.VnicAttachmentLifecycleStateAttached, | ||
"actualLifecycleState", attachment.LifecycleState, | ||
).Info("VNIC attachment in unexpected lifecycle state") |
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.
personally i'd just have the log msg as "VNIC attachment is not in attached state" and remove the expected msg.
pkg/oci/instances.go
Outdated
@@ -96,7 +96,7 @@ func (cp *CloudProvider) NodeAddresses(ctx context.Context, name types.NodeName) | |||
// nodeaddresses are being queried. i.e. local metadata services cannot be used | |||
// in this method to obtain nodeaddresses. | |||
func (cp *CloudProvider) NodeAddressesByProviderID(ctx context.Context, providerID string) ([]api.NodeAddress, error) { | |||
glog.V(4).Infof("NodeAddressesByProviderID(%q) called", providerID) | |||
cp.logger.Debugf("NodeAddressesByProviderID(%q) called", providerID) |
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.
These types of log messages are largely useless imo. It'd be more useful to say
cp.logger.With("providerID", providerID).Debug("fetching node addresses by provider id")
pkg/oci/load_balancer.go
Outdated
@@ -201,9 +202,10 @@ func (cp *CloudProvider) readSSLSecret(svc *v1.Service) (string, string, error) | |||
// balancer, if it doesn't already exist. | |||
func (cp *CloudProvider) ensureSSLCertificate(ctx context.Context, lb *loadbalancer.LoadBalancer, spec *LBSpec) error { | |||
name := spec.SSLConfig.Name | |||
logger := cp.logger.With("certificateName", name, "loadbalancerName", *lb.DisplayName) |
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 feel we should change this to always use ocid and not display names.
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 believe we always add the ocid as soon as we have it; just including display name as we often have that prior to getting an ocid. Thoughts?
pkg/oci/load_balancer.go
Outdated
@@ -267,7 +270,7 @@ func (cp *CloudProvider) createLoadBalancer(ctx context.Context, spec *LBSpec) ( | |||
Certificates: certs, | |||
} | |||
|
|||
glog.V(4).Infof("CreateLoadBalancerDetails: %#v", details.String()) | |||
logger.With("details", details).Debug("CreateLoadBalancerDetails") |
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 gonna be a HUGE log message.... What if the cluster has 500+ nodes? Heck even 20 nodes would be a lot for this haha.
pkg/oci/load_balancer.go
Outdated
@@ -283,16 +286,16 @@ func (cp *CloudProvider) createLoadBalancer(ctx context.Context, spec *LBSpec) ( | |||
return nil, errors.Wrapf(err, "get load balancer %q", *wr.LoadBalancerId) | |||
} | |||
|
|||
glog.Infof("Created load balancer %q with OCID %q", *lb.DisplayName, *lb.Id) | |||
logger.With("loadbalancerID", *lb.Id).Info("Loadbalancer created") |
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.
load balancer
pkg/oci/load_balancer.go
Outdated
@@ -364,7 +367,7 @@ func (cp *CloudProvider) updateLoadBalancer(ctx context.Context, lb *loadbalance | |||
return errors.Wrap(err, "get subnets for nodes") | |||
} | |||
|
|||
actions := sortAndCombineActions(backendSetActions, listenerActions) | |||
actions := sortAndCombineActions(cp.logger, backendSetActions, listenerActions) |
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 and the one above should be context loggers I think w/ at a very minimum the lb id / service name/namespace / something.
pkg/oci/load_balancer.go
Outdated
@@ -403,7 +406,7 @@ func (cp *CloudProvider) updateBackendSet(ctx context.Context, lbID string, acti | |||
ports = action.Ports | |||
) | |||
|
|||
glog.V(2).Infof("Applying %q action on backend set %q for lb %q (ports=%+v)", action.Type(), action.Name(), lbID, ports) | |||
cp.logger.Infof("Applying %q action on backend set %q for lb %q (ports=%+v)", action.Type(), action.Name(), lbID, ports) |
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.
action stuff should be fields.
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.
lb id should be on the logger already. it gets to be super complex/annoying if we need to add the same fields every where down the call stack.
what happens to the existing glog flags around files that never worked? |
@jhorwit2 Do you think it's worth adding no-op flags for the old glog flags for compat. or given they never worked can we just drop them (with a note in the CHANGELOG)? |
pkg/oci/ccm.go
Outdated
cp, err := buildConfigurationProvider(config) | ||
logger := logutil.Logger() | ||
defer logger.Sync() | ||
zap.ReplaceGlobals(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.
Replacing it here means there will be logs emitted not following this logger since the CCM itself logs plus other various global things.
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.
Would you suggest swapping out the logger in func main()
then?
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
@jhorwit2 My attempt at moving the logger override |
Awaiting a +1 review from @jhorwit2 |
@prydie do you know why? that seems odd. |
@jhorwit2 I was previously creating a new logger in both I'm not overly keen on how I'm getting that reference now but it's working:
|
Setting env vars in the form LOG_VAR_field_name=value now adds fields fields to the logger (e.g. zap.String("field_name", "value") in this example).
pkg/logging/logging.go
Outdated
@@ -61,7 +63,15 @@ func Logger() *zap.Logger { | |||
cfg = zap.NewProductionConfig() | |||
} | |||
|
|||
options := []zap.Option{zap.AddStacktrace(zapcore.FatalLevel)} | |||
// Extract log fields from environment variables. |
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 need for this? Won't filebeat/fluentd/< insert log shipper > do that for you?
lgtm |
Actually @prydie could you please update the README for docs on this? |
@jhorwit2 I'm going to merge and create an issue for a broader effort to improve documentation. |
Migrates logging from to
go.uber.org/zap
fromgithub.com/golang/glog
and adds the following flags: