Skip to content
Snippets Groups Projects
Commit 79d8eefc authored by Semyon Krestyaninov's avatar Semyon Krestyaninov
Browse files

feature: add unused v2 entry encoder

parent d5f28920
No related branches found
No related tags found
No related merge requests found
package zap
import (
"fmt"
"testing"
"git.perx.ru/perxis/perxis-go/pkg/items"
logzap "git.perx.ru/perxis/perxis-go/zap"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func BenchmarkEntryEncoderSimple(b *testing.B) {
fields := []zapcore.Field{
logzap.Event(items.EventCreateItem),
logzap.Object(items.NewItem("WPNN", "9VGP", "GxNv", "W0fl", nil, nil)),
logzap.Caller("/system"),
logzap.Tags("tag1", "tag2", "tag3"),
}
enc := NewEntryEncoder()
for i := 0; i < b.N; i++ {
_, _ = enc.EncodeEntry(zapcore.Entry{Message: fmt.Sprintf("Msg: %d", i)}, fields)
}
}
func BenchmarkEntryEncoderUnknownFields(b *testing.B) {
fields := []zapcore.Field{
logzap.Event(items.EventCreateItem),
logzap.Object(items.NewItem("WPNN", "9VGP", "GxNv", "W0fl", nil, nil)),
logzap.Caller("/system"),
logzap.Tags("tag1", "tag2", "tag3"),
}
for i := 0; i < 1000; i++ {
fields = append(fields, zap.String(fmt.Sprintf("Key: %d", i), fmt.Sprintf("Value: %d", i)))
}
enc := NewEntryEncoder()
for i := 0; i < b.N; i++ {
_, _ = enc.EncodeEntry(zapcore.Entry{Message: fmt.Sprintf("Msg: %d", i)}, fields)
}
}
func BenchmarkEntryEncoderV2Simple(b *testing.B) {
fields := []zapcore.Field{
logzap.Event(items.EventCreateItem),
logzap.Object(items.NewItem("WPNN", "9VGP", "GxNv", "W0fl", nil, nil)),
logzap.Caller("/system"),
logzap.Tags("tag1", "tag2", "tag3"),
}
enc := NewEntryEncoderV2()
for i := 0; i < b.N; i++ {
_, _ = enc.EncodeEntry(zapcore.Entry{Message: fmt.Sprintf("Msg: %d", i)}, fields)
}
}
func BenchmarkEntryEncoderV2UnknownFields(b *testing.B) {
fields := []zapcore.Field{
logzap.Event(items.EventCreateItem),
logzap.Object(items.NewItem("WPNN", "9VGP", "GxNv", "W0fl", nil, nil)),
logzap.Caller("/system"),
logzap.Tags("tag1", "tag2", "tag3"),
}
for i := 0; i < 1000; i++ {
fields = append(fields, zap.String(fmt.Sprintf("Key: %d", i), fmt.Sprintf("Value: %d", i)))
}
enc := NewEntryEncoderV2()
for i := 0; i < b.N; i++ {
_, _ = enc.EncodeEntry(zapcore.Entry{Message: fmt.Sprintf("Msg: %d", i)}, fields)
}
}
package zap
import (
"fmt"
"slices"
oid "git.perx.ru/perxis/perxis-go/id"
"git.perx.ru/perxis/perxis-go/logs"
"git.perx.ru/perxis/perxis-go/pkg/id"
"git.perx.ru/perxis/perxis-go/zap"
"go.uber.org/zap/zapcore"
)
type EncoderV2 interface {
With([]zapcore.Field) EncoderV2
EncodeEntry(zapcore.Entry, []zapcore.Field) (*logs.Entry, error)
}
type entryEncoderV2 struct {
fields []zapcore.Field
}
func NewEntryEncoderV2() EncoderV2 {
return &entryEncoderV2{}
}
func (enc *entryEncoderV2) With(fields []zapcore.Field) EncoderV2 {
return enc.with(fields)
}
func (enc *entryEncoderV2) with(fields []zapcore.Field) *entryEncoderV2 {
return &entryEncoderV2{fields: slices.Concat(enc.fields, fields)}
}
func (enc *entryEncoderV2) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*logs.Entry, error) {
ent := &logs.Entry{
ID: id.GenerateNewID(),
Timestamp: entry.Time,
LogLevel: logs.Level(entry.Level),
Message: entry.Message,
}
clone := enc.with(fields)
for i := range clone.fields {
switch clone.fields[i].Key {
case "category":
ent.Category = clone.fields[i].String
case "component":
ent.Component = clone.fields[i].String
case "event":
ent.Event = clone.fields[i].String
case "object":
ent.ObjectID, _ = clone.fields[i].Interface.(*oid.ObjectId)
case "caller":
ent.CallerID, _ = clone.fields[i].Interface.(*oid.ObjectId)
case "attr":
ent.Attr = clone.fields[i].Interface
case "error":
if err, _ := clone.fields[i].Interface.(error); err != nil {
ent.Message = fmt.Sprintf("%s. Error: %s", ent.Message, err.Error())
}
case "tags":
ent.Tags, _ = clone.fields[i].Interface.(zap.StringArray)
}
}
return ent, nil
}
......@@ -17,7 +17,7 @@ func ContainsChannels(channels ...string) FilterFunc {
return func(entry zapcore.Entry, fields []zapcore.Field) bool {
for _, f := range fields {
if f.Key == channelKey && f.Type == zapcore.SkipType {
for _, v := range f.Interface.(stringArray) {
for _, v := range f.Interface.(StringArray) {
if data.Contains(v, channels) {
return true
}
......
......@@ -10,9 +10,9 @@ import (
"go.uber.org/zap/zapcore"
)
type stringArray []string
type StringArray []string
func (ss stringArray) MarshalLogArray(arr zapcore.ArrayEncoder) error {
func (ss StringArray) MarshalLogArray(arr zapcore.ArrayEncoder) error {
for i := range ss {
arr.AppendString(ss[i])
}
......@@ -24,7 +24,7 @@ func Channels(channels ...string) zap.Field {
return zap.Field{
Key: channelKey,
Type: zapcore.SkipType, // используем тип zapcore.SkipType, чтобы при кодировании поле игнорировалось
Interface: stringArray(channels),
Interface: StringArray(channels),
}
}
......@@ -64,5 +64,5 @@ func Attr(attr any) zap.Field {
}
func Tags(tags ...string) zap.Field {
return zap.Strings("tags", tags)
return zap.Array("tags", StringArray(tags))
}
......@@ -19,8 +19,8 @@ func TestChannels(t *testing.T) {
field zap.Field
want zap.Field
}{
{name: "ok", field: Channels("master"), want: zap.Field{Key: channelKey, Type: zapcore.SkipType, Interface: stringArray{"master"}}},
{name: "invalid", field: Channels(), want: zap.Field{Key: channelKey, Type: zapcore.SkipType, Interface: stringArray(nil)}},
{name: "ok", field: Channels("master"), want: zap.Field{Key: channelKey, Type: zapcore.SkipType, Interface: StringArray{"master"}}},
{name: "invalid", field: Channels(), want: zap.Field{Key: channelKey, Type: zapcore.SkipType, Interface: StringArray(nil)}},
}
for _, tc := range tests {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment