zapjournald/zapjournald.go

173 lines
4.7 KiB
Go
Raw Normal View History

package zapjournald
import (
"fmt"
"github.com/ssgreg/journald"
"go.uber.org/zap/zapcore"
"golang.org/x/exp/maps"
)
// Core for zapjournald.
//
// Implements zapcore.LevelEnabler and zapcore.Core interfaces.
type Core struct {
zapcore.LevelEnabler
encoder zapcore.Encoder
j *journald.Journal
// field names, which will be stored in journald structure
storedFieldNames map[string]struct{}
// journald fields, which are always present in current core context
contextStructuredFields map[string]interface{}
}
func NewCore(enab zapcore.LevelEnabler, encoder zapcore.Encoder, journal *journald.Journal, journalFields []string) *Core {
journalFieldsMap := make(map[string]struct{})
for _, field := range journalFields {
journalFieldsMap[field] = struct{}{}
}
return &Core{
LevelEnabler: enab,
encoder: encoder,
j: journal,
storedFieldNames: journalFieldsMap,
contextStructuredFields: make(map[string]interface{}),
}
}
// With adds structured context to the Core.
func (core *Core) With(fields []zapcore.Field) zapcore.Core {
clone := core.clone()
for _, field := range fields {
field.AddTo(clone.encoder)
clone.contextStructuredFields[field.Key] = getFieldValue(field)
}
return clone
}
// Check determines whether the supplied Entry should be logged (using the
// embedded LevelEnabler and possibly some extra logic). If the entry
// should be logged, the Core adds itself to the CheckedEntry and returns
// the result.
//
// Callers must use Check before calling Write.
func (core *Core) Check(entry zapcore.Entry, checked *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if core.Enabled(entry.Level) {
return checked.AddCore(entry, core)
}
return checked
}
// Write serializes the Entry and any Fields supplied at the log site and
// writes them to their destination.
//
// If called, Write should always log the Entry and Fields; it should not
// replicate the logic of Check.
func (core *Core) Write(entry zapcore.Entry, fields []zapcore.Field) error {
prio, err := zapLevelToJournald(entry.Level)
if err != nil {
return err
}
// Generate the message.
buffer, err := core.encoder.EncodeEntry(entry, fields)
if err != nil {
return fmt.Errorf("failed to encode log entry: %w", err)
}
[#12] Free internal encoder buffer ``` goos: linux goarch: amd64 pkg: git.frostfs.info/TrueCloudLab/zapjournald cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz │ old │ 1 │ │ sec/op │ sec/op vs base │ Logger/standard/no_fields-8 435.9n ± 4% 450.3n ± 2% +3.30% (p=0.041 n=10) Logger/standard/application_fields-8 629.0n ± 8% 610.4n ± 1% -2.96% (p=0.002 n=10) Logger/standard/journald_fields-8 626.8n ± 5% 595.8n ± 5% -4.95% (p=0.029 n=10) Logger/journald/no_fields-8 2.097µ ± 3% 1.809µ ± 4% -13.73% (p=0.000 n=10) Logger/journald/application_fields-8 2.348µ ± 5% 2.037µ ± 5% -13.27% (p=0.000 n=10) Logger/journald/journald_fields-8 2.396µ ± 4% 2.054µ ± 4% -14.26% (p=0.000 n=10) geomean 1.125µ 1.036µ -7.88% │ old │ 1 │ │ B/op │ B/op vs base │ Logger/standard/no_fields-8 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ Logger/standard/application_fields-8 128.0 ± 0% 128.0 ± 0% ~ (p=1.000 n=10) ¹ Logger/standard/journald_fields-8 64.00 ± 0% 64.00 ± 0% ~ (p=1.000 n=10) ¹ Logger/journald/no_fields-8 2.210Ki ± 0% 1.178Ki ± 0% -46.71% (p=0.000 n=10) Logger/journald/application_fields-8 2.491Ki ± 0% 1.459Ki ± 0% -41.43% (p=0.000 n=10) Logger/journald/journald_fields-8 2.476Ki ± 0% 1.443Ki ± 0% -41.70% (p=0.000 n=10) geomean ² -24.72% ² ¹ all samples are equal ² summaries must be >0 to compute geomean │ old │ 1 │ │ allocs/op │ allocs/op vs base │ Logger/standard/no_fields-8 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ Logger/standard/application_fields-8 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ Logger/standard/journald_fields-8 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ Logger/journald/no_fields-8 31.00 ± 0% 29.00 ± 0% -6.45% (p=0.000 n=10) Logger/journald/application_fields-8 32.00 ± 0% 30.00 ± 0% -6.25% (p=0.000 n=10) Logger/journald/journald_fields-8 33.00 ± 0% 31.00 ± 0% -6.06% (p=0.000 n=10) geomean ² -3.18% ² ¹ all samples are equal ² summaries must be >0 to compute geomean ``` Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
2023-10-18 08:29:55 +00:00
defer buffer.Free()
message := buffer.String()
structuredFields := maps.Clone(core.contextStructuredFields)
for _, field := range fields {
if _, isJournalField := core.storedFieldNames[field.Key]; isJournalField {
structuredFields[field.Key] = getFieldValue(field)
}
}
// Write the message.
return core.j.Send(message, prio, structuredFields)
}
// Sync flushes buffered logs (not used).
func (core *Core) Sync() error {
return nil
}
// clone returns clone of core.
func (core *Core) clone() *Core {
return &Core{
LevelEnabler: core.LevelEnabler,
encoder: core.encoder.Clone(),
j: core.j,
storedFieldNames: maps.Clone(core.storedFieldNames),
contextStructuredFields: maps.Clone(core.contextStructuredFields),
}
}
// getFieldValue returns underlying value stored in zapcore.Field.
func getFieldValue(f zapcore.Field) interface{} {
switch f.Type {
case zapcore.ArrayMarshalerType,
zapcore.ObjectMarshalerType,
zapcore.InlineMarshalerType,
zapcore.BinaryType,
zapcore.ByteStringType,
zapcore.Complex128Type,
zapcore.Complex64Type,
zapcore.TimeFullType,
zapcore.ReflectType,
zapcore.NamespaceType,
zapcore.StringerType,
zapcore.ErrorType,
zapcore.SkipType:
return f.Interface
case zapcore.DurationType,
zapcore.Float64Type,
zapcore.Float32Type,
zapcore.Int64Type,
zapcore.Int32Type,
zapcore.Int16Type,
zapcore.Int8Type,
zapcore.Uint64Type,
zapcore.Uint32Type,
zapcore.Uint16Type,
zapcore.Uint8Type,
zapcore.UintptrType,
zapcore.BoolType:
return f.Integer
case zapcore.StringType:
return f.String
case zapcore.TimeType:
if f.Interface != nil {
// for example: zap.Time("k", time.Unix(100900, 0).In(time.UTC)) - will produce: "100900000000000 UTC" (result in nanoseconds)
return fmt.Sprintf("%d %v", f.Integer, f.Interface)
}
return f.Integer
default:
panic(fmt.Sprintf("unknown field type: %v", f))
}
}
func zapLevelToJournald(l zapcore.Level) (journald.Priority, error) {
switch l {
case zapcore.DebugLevel:
return journald.PriorityDebug, nil
case zapcore.InfoLevel:
return journald.PriorityInfo, nil
case zapcore.WarnLevel:
return journald.PriorityWarning, nil
case zapcore.ErrorLevel:
return journald.PriorityErr, nil
case zapcore.DPanicLevel:
return journald.PriorityCrit, nil
case zapcore.PanicLevel:
return journald.PriorityCrit, nil
case zapcore.FatalLevel:
return journald.PriorityCrit, nil
default:
return 0, fmt.Errorf("unknown log level: %v", l)
}
}