Skip to content

Commit 668912e

Browse files
committed
add structured rpc logging
1 parent e9138bf commit 668912e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

pubsub.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,35 @@ type RPC struct {
262262
from peer.ID
263263
}
264264

265+
// LogValue implements slog.LogValuer.
266+
func (rpc *RPC) LogValue() slog.Value {
267+
// Messages
268+
var msgs []any
269+
for _, msg := range rpc.Publish {
270+
msgs = append(msgs, slog.Group(
271+
"message",
272+
slog.Any("topic", msg.Topic),
273+
slog.Any("dataPrefix", msg.Data[0:min(len(msg.Data), 32)]),
274+
slog.Any("dataLen", len(msg.Data)),
275+
))
276+
}
277+
278+
var fields []slog.Attr
279+
if len(msgs) > 0 {
280+
fields = append(fields, slog.Group("publish", msgs...))
281+
}
282+
if rpc.Control != nil {
283+
fields = append(fields, slog.Any("control", rpc.Control))
284+
}
285+
if rpc.Subscriptions != nil {
286+
fields = append(fields, slog.Any("subscriptions", rpc.Subscriptions))
287+
}
288+
if rpc.Partial != nil {
289+
fields = append(fields, slog.Any("Partial", rpc.Partial))
290+
}
291+
return slog.GroupValue(fields...)
292+
}
293+
265294
// split splits the given RPC If a sub RPC is too large and can't be split
266295
// further (e.g. Message data is bigger than the RPC limit), then it will be
267296
// returned as an oversized RPC. The caller should filter out oversized RPCs.

0 commit comments

Comments
 (0)