Skip to content

Commit 2f88c4e

Browse files
authored
skip logging redacted fields with zero values (#1338)
* skip logging redacted fields with zero values * skip nil messages * Create good-jeans-check.md
1 parent 5a6eb7f commit 2f88c4e

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed

.changeset/good-jeans-check.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@livekit/protocol": patch
3+
---
4+
5+
skip logging redacted fields with zero values

logger/proto.go

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,20 @@ type protoMarshaller struct {
4848
}
4949

5050
func (p protoMarshaller) MarshalLogObject(e zapcore.ObjectEncoder) error {
51+
if !p.m.IsValid() {
52+
return nil
53+
}
54+
5155
fields := p.m.Descriptor().Fields()
5256
for i := 0; i < fields.Len(); i++ {
5357
f := fields.Get(i)
5458
k := f.JSONName()
5559
v := p.m.Get(f)
5660

61+
if protoFieldIsZero(f, v) {
62+
continue
63+
}
64+
5765
if proto.HasExtension(f.Options(), logger.E_Redact) {
5866
e.AddString(k, marshalRedacted(f, v))
5967
continue
@@ -131,35 +139,21 @@ func (p protoListMarshaller) MarshalLogArray(e zapcore.ArrayEncoder) error {
131139
func marshalProtoField(k string, f protoreflect.FieldDescriptor, v protoreflect.Value, e zapcore.ObjectEncoder) {
132140
switch f.Kind() {
133141
case protoreflect.BoolKind:
134-
if b := v.Bool(); b {
135-
e.AddBool(k, b)
136-
}
142+
e.AddBool(k, v.Bool())
137143
case protoreflect.EnumKind:
138144
e.AddString(k, marshalProtoEnum(f, v))
139145
case protoreflect.Int32Kind, protoreflect.Int64Kind, protoreflect.Sint32Kind, protoreflect.Sint64Kind, protoreflect.Sfixed32Kind, protoreflect.Sfixed64Kind:
140-
if n := v.Int(); n != 0 {
141-
e.AddInt64(k, n)
142-
}
146+
e.AddInt64(k, v.Int())
143147
case protoreflect.Uint32Kind, protoreflect.Uint64Kind, protoreflect.Fixed32Kind, protoreflect.Fixed64Kind:
144-
if n := v.Uint(); n != 0 {
145-
e.AddUint64(k, n)
146-
}
148+
e.AddUint64(k, v.Uint())
147149
case protoreflect.FloatKind, protoreflect.DoubleKind:
148-
if n := v.Float(); n != 0 {
149-
e.AddFloat64(k, n)
150-
}
150+
e.AddFloat64(k, v.Float())
151151
case protoreflect.StringKind:
152-
if s := v.String(); s != "" {
153-
e.AddString(k, s)
154-
}
152+
e.AddString(k, v.String())
155153
case protoreflect.BytesKind:
156-
if b := v.Bytes(); len(b) != 0 {
157-
e.AddString(k, marshalProtoBytes(b))
158-
}
154+
e.AddString(k, marshalProtoBytes(v.Bytes()))
159155
case protoreflect.MessageKind:
160-
if m := v.Message(); m.IsValid() {
161-
e.AddObject(k, protoMarshaller{m})
162-
}
156+
e.AddObject(k, protoMarshaller{v.Message()})
163157
}
164158
}
165159

@@ -217,10 +211,6 @@ func (d redactTemplateData) TextName() string {
217211
}
218212

219213
func (d redactTemplateData) Size() string {
220-
if !d.v.IsValid() {
221-
return "0"
222-
}
223-
224214
msg := dynamicpb.NewMessage(d.f.ContainingMessage())
225215

226216
switch {
@@ -243,3 +233,33 @@ func (d redactTemplateData) Size() string {
243233

244234
return strconv.Itoa(proto.Size(msg.Interface()))
245235
}
236+
237+
func protoFieldIsZero(f protoreflect.FieldDescriptor, v protoreflect.Value) bool {
238+
if f.IsList() {
239+
l := v.List()
240+
return l == nil || l.Len() == 0
241+
}
242+
if f.IsMap() {
243+
m := v.Map()
244+
return m == nil || m.Len() == 0
245+
}
246+
switch f.Kind() {
247+
case protoreflect.BoolKind:
248+
return !v.Bool()
249+
case protoreflect.EnumKind:
250+
return false
251+
case protoreflect.Int32Kind, protoreflect.Int64Kind, protoreflect.Sint32Kind, protoreflect.Sint64Kind, protoreflect.Sfixed32Kind, protoreflect.Sfixed64Kind:
252+
return v.Int() == 0
253+
case protoreflect.Uint32Kind, protoreflect.Uint64Kind, protoreflect.Fixed32Kind, protoreflect.Fixed64Kind:
254+
return v.Uint() == 0
255+
case protoreflect.FloatKind, protoreflect.DoubleKind:
256+
return v.Float() == 0
257+
case protoreflect.StringKind:
258+
return v.String() == ""
259+
case protoreflect.BytesKind:
260+
return len(v.Bytes()) == 0
261+
case protoreflect.MessageKind:
262+
return !v.Message().IsValid()
263+
}
264+
return true
265+
}

0 commit comments

Comments
 (0)