Skip to content

Commit 390bcef

Browse files
authored
avoid unnecessary logging field creation when payload logging is disabled (#809)
* performance improvements in logging reporter * fix lint
1 parent 748e2b2 commit 390bcef

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

interceptors/logging/interceptors.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ func (c *reporter) PostCall(err error, duration time.Duration) {
5353
}
5454

5555
func (c *reporter) PostMsgSend(payload any, err error, duration time.Duration) {
56+
logStartCall := !c.startCallLogged && has(c.opts.loggableEvents, StartCall)
57+
logPayloadSend := err == nil && has(c.opts.loggableEvents, PayloadSent)
58+
if !logStartCall && !logPayloadSend {
59+
return
60+
}
61+
5662
logLvl := c.opts.levelFunc(c.opts.codeFunc(err))
5763
fields := c.fields.WithUnique(ExtractFields(c.ctx))
5864
if err != nil {
@@ -65,12 +71,12 @@ func (c *reporter) PostMsgSend(payload any, err error, duration time.Duration) {
6571
// fieldsFromCtxFn dups override the existing fields.
6672
fields = c.opts.fieldsFromCtxCallMetaFn(c.ctx, c.CallMeta).AppendUnique(fields)
6773
}
68-
if !c.startCallLogged && has(c.opts.loggableEvents, StartCall) {
74+
if logStartCall {
6975
c.startCallLogged = true
7076
c.logger.Log(c.ctx, logLvl, "started call", fields.AppendUnique(c.opts.durationFieldFunc(duration))...)
7177
}
7278

73-
if err != nil || !has(c.opts.loggableEvents, PayloadSent) {
79+
if !logPayloadSend {
7480
return
7581
}
7682
callType := "response"
@@ -94,6 +100,12 @@ func (c *reporter) PostMsgSend(payload any, err error, duration time.Duration) {
94100
}
95101

96102
func (c *reporter) PostMsgReceive(payload any, err error, duration time.Duration) {
103+
logStartCall := !c.startCallLogged && has(c.opts.loggableEvents, StartCall)
104+
logPayloadReceived := err == nil && has(c.opts.loggableEvents, PayloadReceived)
105+
if !logStartCall && !logPayloadReceived {
106+
return
107+
}
108+
97109
logLvl := c.opts.levelFunc(c.opts.codeFunc(err))
98110
fields := c.fields.WithUnique(ExtractFields(c.ctx))
99111
if err != nil {
@@ -106,12 +118,12 @@ func (c *reporter) PostMsgReceive(payload any, err error, duration time.Duration
106118
// fieldsFromCtxFn dups override the existing fields.
107119
fields = c.opts.fieldsFromCtxCallMetaFn(c.ctx, c.CallMeta).AppendUnique(fields)
108120
}
109-
if !c.startCallLogged && has(c.opts.loggableEvents, StartCall) {
121+
if logStartCall {
110122
c.startCallLogged = true
111123
c.logger.Log(c.ctx, logLvl, "started call", fields.AppendUnique(c.opts.durationFieldFunc(duration))...)
112124
}
113125

114-
if err != nil || !has(c.opts.loggableEvents, PayloadReceived) {
126+
if !logPayloadReceived {
115127
return
116128
}
117129
callType := "request"

0 commit comments

Comments
 (0)