Skip to content

Commit ca8da2f

Browse files
authored
fix: zapetw core With fields (#3433)
fix: zapetw core with fields Signed-off-by: Evan Baker <[email protected]>
1 parent 2451ac1 commit ca8da2f

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

cni/log/logger_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func etwCore(loggingLevel zapcore.Level) (zapcore.Core, error) {
2626
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
2727
jsonEncoder := zapcore.NewJSONEncoder(encoderConfig)
2828

29-
etwcore, err := zapetw.NewETWCore(etwCNIEventName, jsonEncoder, loggingLevel)
29+
etwcore, _, err := zapetw.New("ACN-Monitoring", etwCNIEventName, jsonEncoder, loggingLevel)
3030
if err != nil {
3131
return nil, errors.Wrap(err, "failed to create ETW core")
3232
}

cns/logger/cnslogger_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func getPlatformCores(loggingLevel zapcore.Level, encoder zapcore.Encoder) (zapc
1919
}
2020

2121
func getETWCore(loggingLevel zapcore.Level, encoder zapcore.Encoder) (zapcore.Core, error) {
22-
etwcore, err := zapetw.NewETWCore(etwCNSEventName, encoder, loggingLevel)
22+
etwcore, _, err := zapetw.New("ACN-Monitoring", etwCNSEventName, encoder, loggingLevel)
2323
if err != nil {
2424
return nil, errors.Wrap(err, "failed to create ETW core")
2525
}

zapetw/core_windows.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,45 @@ import (
99
// <product_name>-<component_name>
1010
const providername = "ACN-Monitoring"
1111

12-
type ETWCore struct {
12+
type Core struct {
1313
provider *etw.Provider
1414
eventName string
1515
encoder zapcore.Encoder
16-
fields []zapcore.Field
1716
zapcore.LevelEnabler
1817
}
1918

20-
func NewETWCore(eventName string, encoder zapcore.Encoder, levelEnabler zapcore.LevelEnabler) (*ETWCore, error) {
21-
provider, err := etw.NewProviderWithOptions(providername)
19+
func New(providerName, eventName string, encoder zapcore.Encoder, levelEnabler zapcore.LevelEnabler) (zapcore.Core, func(), error) {
20+
provider, err := etw.NewProviderWithOptions(providerName)
2221
if err != nil {
23-
return nil, errors.Wrap(err, "failed to create ETW provider")
22+
return nil, func() { _ = provider.Close() }, errors.Wrap(err, "failed to create ETW provider")
2423
}
25-
return &ETWCore{
24+
return &Core{
2625
provider: provider,
2726
eventName: eventName,
2827
encoder: encoder,
2928
LevelEnabler: levelEnabler,
30-
}, nil
29+
}, func() { _ = provider.Close() }, nil
3130
}
3231

33-
func (core *ETWCore) With(fields []zapcore.Field) zapcore.Core {
34-
return &ETWCore{
35-
provider: core.provider,
36-
eventName: core.eventName,
37-
encoder: core.encoder,
38-
LevelEnabler: core.LevelEnabler,
39-
fields: append(core.fields, fields...),
32+
func (core *Core) With(fields []zapcore.Field) zapcore.Core {
33+
clone := core.clone()
34+
for i := range fields {
35+
fields[i].AddTo(clone.encoder)
4036
}
37+
return clone
4138
}
4239

4340
// Check is an implementation of the zapcore.Core interface's Check method.
4441
// Check determines whether the logger core is enabled at the supplied zapcore.Entry's Level.
4542
// If enabled, it adds the core to the CheckedEntry and returns it, otherwise returns the CheckedEntry unchanged.
46-
func (core *ETWCore) Check(entry zapcore.Entry, checkedEntry *zapcore.CheckedEntry) *zapcore.CheckedEntry {
43+
func (core *Core) Check(entry zapcore.Entry, checkedEntry *zapcore.CheckedEntry) *zapcore.CheckedEntry {
4744
if core.Enabled(entry.Level) {
4845
return checkedEntry.AddCore(entry, core)
4946
}
5047
return checkedEntry
5148
}
5249

53-
func (core *ETWCore) Write(entry zapcore.Entry, fields []zapcore.Field) error {
50+
func (core *Core) Write(entry zapcore.Entry, fields []zapcore.Field) error {
5451
etwLevel := zapLevelToETWLevel(entry.Level)
5552

5653
buffer, err := core.encoder.EncodeEntry(entry, fields)
@@ -70,10 +67,19 @@ func (core *ETWCore) Write(entry zapcore.Entry, fields []zapcore.Field) error {
7067
return nil
7168
}
7269

73-
func (core *ETWCore) Sync() error {
70+
func (core *Core) Sync() error {
7471
return nil
7572
}
7673

74+
func (core *Core) clone() *Core {
75+
return &Core{
76+
provider: core.provider,
77+
eventName: core.eventName,
78+
encoder: core.encoder.Clone(),
79+
LevelEnabler: core.LevelEnabler,
80+
}
81+
}
82+
7783
func zapLevelToETWLevel(level zapcore.Level) etw.Level {
7884
switch level {
7985
case zapcore.DebugLevel:

0 commit comments

Comments
 (0)