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

implement zap for cni #1933

Merged
merged 20 commits into from
Jul 3, 2023
Merged

implement zap for cni #1933

merged 20 commits into from
Jul 3, 2023

Conversation

estebancams
Copy link
Contributor

@estebancams estebancams commented Apr 27, 2023

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:

@estebancams estebancams requested a review from a team as a code owner April 27, 2023 18:31
@estebancams estebancams requested review from thatmattlong and removed request for a team April 27, 2023 18:31
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",
Copy link
Member

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

agree ^

Comment on lines 32 to 33
MaxSizeInMB: 5,
MaxBackups: 8,
Copy link
Member

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?

Copy link
Contributor Author

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

@estebancams estebancams force-pushed the estebanca/implement-zap-for-cni branch from 030ad17 to e9fd707 Compare May 17, 2023 15:49
@estebancams estebancams requested a review from a team as a code owner May 17, 2023 15:49
@estebancams estebancams force-pushed the estebanca/implement-zap-for-cni branch from e9fd707 to 5e58cfd Compare May 17, 2023 16:17
@estebancams estebancams changed the title wip: implement zap for cni implement zap for cni May 17, 2023
@estebancams estebancams force-pushed the estebanca/implement-zap-for-cni branch 3 times, most recently from b9d5917 to 69823dc Compare May 20, 2023 13:50
@estebancams estebancams requested a review from matmerr as a code owner May 20, 2023 13:50
@estebancams estebancams force-pushed the estebanca/implement-zap-for-cni branch from 69823dc to 0e4c7c7 Compare May 20, 2023 13:53
@estebancams
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@estebancams estebancams force-pushed the estebanca/implement-zap-for-cni branch 4 times, most recently from 19764d3 to d9ba6be Compare May 22, 2023 20:14
@estebancams estebancams force-pushed the estebanca/implement-zap-for-cni branch from d9ba6be to a675994 Compare May 31, 2023 19:01
var Logger *zap.Logger

func New(cfg *Config) (func(), error) {
Logger = newFileLogger(cfg)
Copy link
Member

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?

Copy link
Contributor Author

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

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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


var Logger *zap.Logger

func New(cfg *Config) (func(), error) {
Copy link
Member

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?

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

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

Choose a reason for hiding this comment

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

zap.String for infraContainerid?

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

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),
Copy link
Member

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

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

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

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The whole object


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

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

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


return nil
}

// This function for sending CNI metrics to telemetry service
func logAndSendEvent(plugin *NetPlugin, msg string) {
log.Printf(msg)
log.Logger.Info(msg)
Copy link
Member

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?

Copy link
Contributor Author

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

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 err := log.SetTargetLogDirectory(log.TargetLogfile, ""); err != nil {
loggerCfg := &log.Config{
Level: zapcore.DebugLevel,
LogPath: log.LogPath + "azure-vnet.log",
Copy link
Member

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"

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

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

@tamilmani1989 tamilmani1989 Jun 2, 2023

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?

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

@estebancams estebancams force-pushed the estebanca/implement-zap-for-cni branch from a675994 to 6c81eb1 Compare June 4, 2023 15:47
tamilmani1989
tamilmani1989 previously approved these changes Jun 6, 2023
@tamilmani1989
Copy link
Member

Thanks for this PR @estebancams

@estebancams estebancams requested a review from rbtr June 28, 2023 14:53
@estebancams estebancams force-pushed the estebanca/implement-zap-for-cni branch from 9b4e169 to 8f3e413 Compare June 30, 2023 18:28
@estebancams
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@tamilmani1989 tamilmani1989 merged commit 04566d5 into master Jul 3, 2023
@tamilmani1989 tamilmani1989 deleted the estebanca/implement-zap-for-cni branch July 3, 2023 19:01
@tamilmani1989
Copy link
Member

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

paulyufan2 pushed a commit that referenced this pull request Jul 7, 2023
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cni Related to CNI.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants