-
Notifications
You must be signed in to change notification settings - Fork 242
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
implement zap for cni #1933
implement zap for cni #1933
Conversation
cni/ipam/ipam.go
Outdated
|
||
defer func() { log.Printf("[cni-ipam] ADD command completed with result:%+v err:%v.", result, err) }() | ||
defer func() { | ||
log.Logger.Info("[cni-ipam] ADD command completed", |
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'd argue that even this [cni-ipam]
sigil should be a zap field. I've seen "service" or "component" names for that.
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.
agree ^
cni/ipam/plugin/main.go
Outdated
MaxSizeInMB: 5, | ||
MaxBackups: 8, |
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.
Shouldn't these come from some 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.
At the moment standard logger is not taking them from config. We can have a separated task to uniform the approach, but by now I'll follow the current way. I added constants to be neater
030ad17
to
e9fd707
Compare
e9fd707
to
5e58cfd
Compare
b9d5917
to
69823dc
Compare
69823dc
to
0e4c7c7
Compare
/azp run |
Azure Pipelines successfully started running 2 pipeline(s). |
19764d3
to
d9ba6be
Compare
d9ba6be
to
a675994
Compare
var Logger *zap.Logger | ||
|
||
func New(cfg *Config) (func(), error) { | ||
Logger = newFileLogger(cfg) |
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 know today cni logs only to file... if in future, if we wanted to log to file and also send to logcollector/geneva, how can we expand this?
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 allows to register hooks, so you don't need to further instrument the code for that extra logging. Whatever you log to zap, will be logged to geneva through a hook, that can be used to do the proper transformations and implementation
That's why we needed to go to Zap in the first place
cni/ipam/ipam.go
Outdated
defer func() { log.Printf("[cni-ipam] DEL command completed with err:%v.", err) }() | ||
defer func() { | ||
log.Logger.Info("[cni-ipam] DEL command completed", | ||
zap.Any("error", 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.
if err is nil, does zap.Any handle it?
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, Any chooses the best way to output the field based on its type.
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 use zap.Any
when we know the type
cni/log/logger.go
Outdated
|
||
var Logger *zap.Logger | ||
|
||
func New(cfg *Config) (func(), error) { |
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.
can you add brief comment for this function on what it does and what it returns?
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
cni/network/invoker_cns.go
Outdated
@@ -305,11 +315,15 @@ func (invoker *CNSIPAMInvoker) Delete(address *net.IPNet, nwCfg *cni.NetworkConf | |||
|
|||
if err = invoker.cnsClient.ReleaseIPAddress(context.TODO(), ipConfig); err != nil { | |||
// if the old API fails as well then we just return the error | |||
log.Errorf("Failed to release IP address from CNS using ReleaseIPAddress with infracontainerid %s. error: %v", ipConfigs.InfraContainerID, err) | |||
log.Logger.Error("Failed to release IP address from CNS using ReleaseIPAddress ", | |||
zap.Any("infracontainerid", ipConfigs.InfraContainerID), |
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.String for infraContainerid?
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
cni/network/invoker_cns.go
Outdated
return errors.Wrap(err, fmt.Sprintf("failed to release IP %v using ReleaseIPAddress with err ", ipConfig.DesiredIPAddress)+"%w") | ||
} | ||
} else { | ||
log.Errorf("Failed to release IP address with infracontainerid %s from CNS error: %v", ipConfigs.InfraContainerID, err) | ||
log.Logger.Error("Failed to release IP address", | ||
zap.Any("infracontainerid", ipConfigs.InfraContainerID), |
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.
same as above - zap.String
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
@@ -249,13 +255,14 @@ func (m *Multitenancy) getNetworkContainersInternal( | |||
return nil, []net.IPNet{}, fmt.Errorf("%w", err) | |||
} | |||
|
|||
log.Printf("Network config received from cns %+v", ncConfigs) | |||
log.Logger.Info("Network config received from cns", zap.Any("nconfig", ncConfigs)) |
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.
does this print ncConfig structure with field names or just values?
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 whole object
cni/network/multitenancy.go
Outdated
|
||
subnetPrefixes := []net.IPNet{} | ||
for i := 0; i < len(ncConfigs); i++ { | ||
subnetPrefix := m.netioshim.GetInterfaceSubnetWithSpecificIP(ncConfigs[i].PrimaryInterfaceIdentifier) | ||
if subnetPrefix == nil { | ||
log.Printf("%w %s", errIfaceNotFound, ncConfigs[i].PrimaryInterfaceIdentifier) | ||
log.Logger.Error(errIfaceNotFound.Error(), | ||
zap.String("interface", ncConfigs[i].PrimaryInterfaceIdentifier)) |
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.
let's name this as nodeIP instead of interface
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
cni/network/network.go
Outdated
|
||
return nil | ||
} | ||
|
||
// This function for sending CNI metrics to telemetry service | ||
func logAndSendEvent(plugin *NetPlugin, msg string) { | ||
log.Printf(msg) | ||
log.Logger.Info(msg) |
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.
should we change at the place where we format string?
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 true, good cactch
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
cni/network/plugin/main.go
Outdated
if err := log.SetTargetLogDirectory(log.TargetLogfile, ""); err != nil { | ||
loggerCfg := &log.Config{ | ||
Level: zapcore.DebugLevel, | ||
LogPath: log.LogPath + "azure-vnet.log", |
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.
can it be LogPath: log.LogPath + name + ".log"
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
cni/ipam/ipam.go
Outdated
@@ -61,22 +62,22 @@ func (plugin *ipamPlugin) Start(config *common.PluginConfig) error { | |||
// Initialize base plugin. | |||
err := plugin.Initialize(config) | |||
if err != nil { | |||
log.Printf("[cni-ipam] Failed to initialize base plugin, err:%v.", err) | |||
log.Logger.Error("[cni-ipam] Failed to initialize base plugin.", zap.String("error", err.Error())) |
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.
using zap.Any elsewhere for logging error. can we use same here?
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
a675994
to
6c81eb1
Compare
Thanks for this PR @estebancams |
Co-authored-by: Timothy J. Raymond <[email protected]>
Co-authored-by: Evan Baker <[email protected]>
9b4e169
to
8f3e413
Compare
/azp run |
Azure Pipelines successfully started running 3 pipeline(s). |
aks-engine tests are failing due to npm pod crash which is unrelated to this change. Also those pipelines succeeded for this PR in prior run, so bypassing those pipeline tests |
* feat: added logger package and replaced old log initialization for the new one * feat: changed all log lines to new zap logger * fix: typo * Update azure-ipam/logger/logger.go Co-authored-by: Timothy J. Raymond <[email protected]> * fix: adding constants to describe logger rotations constraints * Renamed logger New method * Replaced logAndSend method by log 1st and send then so we can use zap fields * minor fixes * added logger init for tests * replaced Any by Error * gci ipam_test * fixed govet errors * moved component to a zap field * fixed linit issues * added log mock * fix: gci * fix: added context for logger teardown * Update cni/log/logger.go Co-authored-by: Evan Baker <[email protected]> * moved logger init mock function * fix: lint findings --------- Co-authored-by: Esteban Capillo <[email protected]> Co-authored-by: Timothy J. Raymond <[email protected]> Co-authored-by: Evan Baker <[email protected]>
Reason for Change:
This migrates from golang's simple log interface (through our shim) to zap for structured logging and as a necessary step to work on etw logging later (through hooks, which the old logger does not implement)
Issue Fixed:
Requirements:
Notes: