azure-container-networking/zapetw/core_windows.go

93 строки
2.5 KiB
Go
Исходник Обычный вид История

ETW logging implementation in CNI (#2668) * Add ETW support in zap logger for CNI Added a zap WriteSyncer to enable direct ETW logging through zap core, maintaining existing logging structure while to ETW from CNI. * Transform 'zapetw' package into a standalone module for external use * Revert "Transform 'zapetw' package into a standalone module for external use" This reverts commit 63050ed68d353e43ae1b1fedd11b8e99259b7a28. * Relocate EtwWriteSyncer.go to zapetw module for improved organization - Renamed and moved cni/log/ETWZapCore/EtwWriteSyncer.go to zapetw/write_syncer.go. * Applied gofumpt formatting to adhere to style guidelines. * 1. Implemented platform-specific ETW logging enhancements. 2. Refactor ETW initialization into dedicated method and zapetw package. * Changed InitETWLogger method signature for Linux. * Wrapped error messages at each level of the call hierarchy. * Removed punctuation marks from error messages. * Wrapped error messages with errors.wrap method. * Added comments for clarity. * implemented zap.core for ETW. * Fixed lint issues. * Catch errors from etw.writeEvent method. * Renamed provider. * Abstracted etw core creation in logger_windows. Removed unsupported error from logger_linux to keep the behaviour uniform. * renamed unused parameter. * Renamed variable to lower camel case as it is private. Removed additional local reference. * fixed variable name. * Added comment. * Renamed ETW provider, removed application names from the provider name. --------- Co-authored-by: Sivakami Subramaniam <t-sivakamis@microsoft.com>
2024-04-18 21:17:18 +03:00
package zapetw
import (
"github.com/Microsoft/go-winio/pkg/etw"
"github.com/pkg/errors"
"go.uber.org/zap/zapcore"
)
// <product_name>-<component_name>
const providername = "ACN-Monitoring"
type ETWCore struct {
provider *etw.Provider
eventName string
encoder zapcore.Encoder
fields []zapcore.Field
zapcore.LevelEnabler
}
func NewETWCore(eventName string, encoder zapcore.Encoder, levelEnabler zapcore.LevelEnabler) (*ETWCore, error) {
provider, err := etw.NewProviderWithOptions(providername)
if err != nil {
return nil, errors.Wrap(err, "failed to create ETW provider")
}
return &ETWCore{
provider: provider,
eventName: eventName,
encoder: encoder,
LevelEnabler: levelEnabler,
}, nil
}
func (core *ETWCore) With(fields []zapcore.Field) zapcore.Core {
return &ETWCore{
provider: core.provider,
eventName: core.eventName,
encoder: core.encoder,
LevelEnabler: core.LevelEnabler,
fields: append(core.fields, fields...),
}
}
// Check is an implementation of the zapcore.Core interface's Check method.
// Check determines whether the logger core is enabled at the supplied zapcore.Entry's Level.
// If enabled, it adds the core to the CheckedEntry and returns it, otherwise returns the CheckedEntry unchanged.
func (core *ETWCore) Check(entry zapcore.Entry, checkedEntry *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if core.Enabled(entry.Level) {
return checkedEntry.AddCore(entry, core)
}
return checkedEntry
}
func (core *ETWCore) Write(entry zapcore.Entry, fields []zapcore.Field) error {
etwLevel := zapLevelToETWLevel(entry.Level)
buffer, err := core.encoder.EncodeEntry(entry, fields)
if err != nil {
return errors.Wrap(err, "failed to encode entry")
}
err = core.provider.WriteEvent(
core.eventName,
[]etw.EventOpt{etw.WithLevel(etwLevel)},
[]etw.FieldOpt{etw.StringField("Message", buffer.String())},
)
if err != nil {
return errors.Wrap(err, "failed to write event")
}
return nil
}
func (core *ETWCore) Sync() error {
return nil
}
func zapLevelToETWLevel(level zapcore.Level) etw.Level {
switch level {
case zapcore.DebugLevel:
return etw.LevelVerbose // ETW doesn't have a Debug level, so Verbose is used instead.
case zapcore.InfoLevel:
return etw.LevelInfo
case zapcore.WarnLevel:
return etw.LevelWarning
case zapcore.ErrorLevel:
return etw.LevelError
case zapcore.DPanicLevel, zapcore.PanicLevel, zapcore.FatalLevel, zapcore.InvalidLevel:
return etw.LevelCritical
default:
return etw.LevelAlways
}
}