Skip to content

Commit cc754e6

Browse files
nwntNont
andauthored
Minor refactor to remove duplication (#761)
Signed-off-by: Nont <[email protected]> Co-authored-by: Nont <[email protected]>
1 parent 51cebef commit cc754e6

File tree

1 file changed

+33
-59
lines changed

1 file changed

+33
-59
lines changed

interceptors/logging/interceptors.go

Lines changed: 33 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -72,37 +72,24 @@ func (c *reporter) PostMsgSend(payload any, err error, duration time.Duration) {
7272
if err != nil || !has(c.opts.loggableEvents, PayloadSent) {
7373
return
7474
}
75+
callType := "response"
7576
if c.CallMeta.IsClient {
76-
p, ok := payload.(proto.Message)
77-
if !ok {
78-
c.logger.Log(
79-
c.ctx,
80-
LevelError,
81-
"payload is not a google.golang.org/protobuf/proto.Message; programmatic error?",
82-
fields.AppendUnique(Fields{"grpc.request.type", fmt.Sprintf("%T", payload)})...,
83-
)
84-
return
85-
}
86-
87-
fields = fields.AppendUnique(Fields{"grpc.send.duration", duration.String(), "grpc.request.content", p})
88-
fields = fields.AppendUnique(c.opts.durationFieldFunc(duration))
89-
c.logger.Log(c.ctx, logLvl, "request sent", fields...)
90-
} else {
91-
p, ok := payload.(proto.Message)
92-
if !ok {
93-
c.logger.Log(
94-
c.ctx,
95-
LevelError,
96-
"payload is not a google.golang.org/protobuf/proto.Message; programmatic error?",
97-
fields.AppendUnique(Fields{"grpc.response.type", fmt.Sprintf("%T", payload)})...,
98-
)
99-
return
100-
}
101-
102-
fields = fields.AppendUnique(Fields{"grpc.send.duration", duration.String(), "grpc.response.content", p})
103-
fields = fields.AppendUnique(c.opts.durationFieldFunc(duration))
104-
c.logger.Log(c.ctx, logLvl, "response sent", fields...)
77+
callType = "request"
10578
}
79+
p, ok := payload.(proto.Message)
80+
if !ok {
81+
c.logger.Log(
82+
c.ctx,
83+
LevelError,
84+
"payload is not a google.golang.org/protobuf/proto.Message; programmatic error?",
85+
fields.AppendUnique(Fields{fmt.Sprintf("grpc.%s.type", callType), fmt.Sprintf("%T", payload)})...,
86+
)
87+
return
88+
}
89+
90+
fields = fields.AppendUnique(Fields{"grpc.send.duration", duration.String(), fmt.Sprintf("grpc.%s.content", callType), p})
91+
fields = fields.AppendUnique(c.opts.durationFieldFunc(duration))
92+
c.logger.Log(c.ctx, logLvl, fmt.Sprintf("%s sent", callType), fields...)
10693
}
10794

10895
func (c *reporter) PostMsgReceive(payload any, err error, duration time.Duration) {
@@ -126,37 +113,24 @@ func (c *reporter) PostMsgReceive(payload any, err error, duration time.Duration
126113
if err != nil || !has(c.opts.loggableEvents, PayloadReceived) {
127114
return
128115
}
129-
if !c.CallMeta.IsClient {
130-
p, ok := payload.(proto.Message)
131-
if !ok {
132-
c.logger.Log(
133-
c.ctx,
134-
LevelError,
135-
"payload is not a google.golang.org/protobuf/proto.Message; programmatic error?",
136-
fields.AppendUnique(Fields{"grpc.request.type", fmt.Sprintf("%T", payload)})...,
137-
)
138-
return
139-
}
140-
141-
fields = fields.AppendUnique(Fields{"grpc.recv.duration", duration.String(), "grpc.request.content", p})
142-
fields = fields.AppendUnique(c.opts.durationFieldFunc(duration))
143-
c.logger.Log(c.ctx, logLvl, "request received", fields...)
144-
} else {
145-
p, ok := payload.(proto.Message)
146-
if !ok {
147-
c.logger.Log(
148-
c.ctx,
149-
LevelError,
150-
"payload is not a google.golang.org/protobuf/proto.Message; programmatic error?",
151-
fields.AppendUnique(Fields{"grpc.response.type", fmt.Sprintf("%T", payload)})...,
152-
)
153-
return
154-
}
155-
156-
fields = fields.AppendUnique(Fields{"grpc.recv.duration", duration.String(), "grpc.response.content", p})
157-
fields = fields.AppendUnique(c.opts.durationFieldFunc(duration))
158-
c.logger.Log(c.ctx, logLvl, "response received", fields...)
116+
callType := "request"
117+
if c.CallMeta.IsClient {
118+
callType = "response"
159119
}
120+
p, ok := payload.(proto.Message)
121+
if !ok {
122+
c.logger.Log(
123+
c.ctx,
124+
LevelError,
125+
"payload is not a google.golang.org/protouf/proto.Message; programmatic error?",
126+
fields.AppendUnique(Fields{fmt.Sprintf("grpc.%s.type", callType), fmt.Sprintf("%T", payload)})...,
127+
)
128+
return
129+
}
130+
131+
fields = fields.AppendUnique(Fields{"grpc.recv.duration", duration.String(), fmt.Sprintf("grpc.%s.content", callType), p})
132+
fields = fields.AppendUnique(c.opts.durationFieldFunc(duration))
133+
c.logger.Log(c.ctx, logLvl, fmt.Sprintf("%s received", callType), fields...)
160134
}
161135

162136
func reportable(logger Logger, opts *options) interceptors.CommonReportableFunc {

0 commit comments

Comments
 (0)