Skip to content

Commit 80906aa

Browse files
authored
refactor(logs): use slog attributes for protocol clients (#763)
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent 65a3bf1 commit 80906aa

File tree

9 files changed

+521
-81
lines changed

9 files changed

+521
-81
lines changed

protocol/blockfetch/client.go

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
8282
func (c *Client) Start() {
8383
c.onceStart.Do(func() {
8484
c.Protocol.Logger().
85-
Debug(fmt.Sprintf("%s: starting client protocol for connection %+v", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr))
85+
Debug("starting client protocol",
86+
"component", "network",
87+
"protocol", ProtocolName,
88+
"connection_id", c.callbackContext.ConnectionId.String(),
89+
)
8690
c.Protocol.Start()
8791
// Start goroutine to cleanup resources on protocol shutdown
8892
go func() {
@@ -97,7 +101,11 @@ func (c *Client) Stop() error {
97101
var err error
98102
c.onceStop.Do(func() {
99103
c.Protocol.Logger().
100-
Debug(fmt.Sprintf("%s: stopping client protocol for connection %+v", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr))
104+
Debug("stopping client protocol",
105+
"component", "network",
106+
"protocol", ProtocolName,
107+
"connection_id", c.callbackContext.ConnectionId.String(),
108+
)
101109
msg := NewMsgClientDone()
102110
err = c.SendMessage(msg)
103111
})
@@ -107,7 +115,18 @@ func (c *Client) Stop() error {
107115
// GetBlockRange starts an async process to fetch all blocks in the specified range (inclusive)
108116
func (c *Client) GetBlockRange(start common.Point, end common.Point) error {
109117
c.Protocol.Logger().
110-
Debug(fmt.Sprintf("%s: client called GetBlockRange(start: {Slot: %d, Hash: %x}, end: {Slot: %d, Hash: %x})", ProtocolName, start.Slot, start.Hash, end.Slot, end.Hash))
118+
Debug(
119+
fmt.Sprintf("calling GetBlockRange(start: {Slot: %d, Hash: %x}, end: {Slot: %d, Hash: %x})",
120+
start.Slot,
121+
start.Hash,
122+
end.Slot,
123+
end.Hash,
124+
),
125+
"component", "network",
126+
"protocol", ProtocolName,
127+
"role", "client",
128+
"connection_id", c.callbackContext.ConnectionId.String(),
129+
)
111130
c.busyMutex.Lock()
112131
c.blockUseCallback = true
113132
msg := NewMsgRequestRange(start, end)
@@ -129,7 +148,13 @@ func (c *Client) GetBlockRange(start common.Point, end common.Point) error {
129148
// GetBlock requests and returns a single block specified by the provided point
130149
func (c *Client) GetBlock(point common.Point) (ledger.Block, error) {
131150
c.Protocol.Logger().
132-
Debug(fmt.Sprintf("%s: client called GetBlock(point: {Slot: %d, Hash: %x})", ProtocolName, point.Slot, point.Hash))
151+
Debug(
152+
fmt.Sprintf("calling GetBlock(point: {Slot: %d, Hash: %x})", point.Slot, point.Hash),
153+
"component", "network",
154+
"protocol", ProtocolName,
155+
"role", "client",
156+
"connection_id", c.callbackContext.ConnectionId.String(),
157+
)
133158
c.busyMutex.Lock()
134159
c.blockUseCallback = false
135160
msg := NewMsgRequestRange(point, point)
@@ -175,22 +200,37 @@ func (c *Client) messageHandler(msg protocol.Message) error {
175200

176201
func (c *Client) handleStartBatch() error {
177202
c.Protocol.Logger().
178-
Debug(fmt.Sprintf("%s: client start batch for %+v", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr))
203+
Debug("starting batch",
204+
"component", "network",
205+
"protocol", ProtocolName,
206+
"role", "client",
207+
"connection_id", c.callbackContext.ConnectionId.String(),
208+
)
179209
c.startBatchResultChan <- nil
180210
return nil
181211
}
182212

183213
func (c *Client) handleNoBlocks() error {
184214
c.Protocol.Logger().
185-
Debug(fmt.Sprintf("%s: client no blocks found for %+v", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr))
215+
Debug("no blocks returned",
216+
"component", "network",
217+
"protocol", ProtocolName,
218+
"role", "client",
219+
"connection_id", c.callbackContext.ConnectionId.String(),
220+
)
186221
err := fmt.Errorf("block(s) not found")
187222
c.startBatchResultChan <- err
188223
return nil
189224
}
190225

191226
func (c *Client) handleBlock(msgGeneric protocol.Message) error {
192227
c.Protocol.Logger().
193-
Debug(fmt.Sprintf("%s: client block found for %+v", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr))
228+
Debug("block returned",
229+
"component", "network",
230+
"protocol", ProtocolName,
231+
"role", "client",
232+
"connection_id", c.callbackContext.ConnectionId.String(),
233+
)
194234
msg := msgGeneric.(*MsgBlock)
195235
// Decode only enough to get the block type value
196236
var wrappedBlock WrappedBlock
@@ -217,7 +257,12 @@ func (c *Client) handleBlock(msgGeneric protocol.Message) error {
217257

218258
func (c *Client) handleBatchDone() error {
219259
c.Protocol.Logger().
220-
Debug(fmt.Sprintf("%s: client batch done for %+v", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr))
260+
Debug("batch done",
261+
"component", "network",
262+
"protocol", ProtocolName,
263+
"role", "client",
264+
"connection_id", c.callbackContext.ConnectionId.String(),
265+
)
221266
c.busyMutex.Unlock()
222267
return nil
223268
}

protocol/chainsync/client.go

Lines changed: 132 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ func NewClient(
117117
func (c *Client) Start() {
118118
c.onceStart.Do(func() {
119119
c.Protocol.Logger().
120-
Debug(fmt.Sprintf("%s: starting client protocol for connection %+v", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr))
120+
Debug("starting client protocol",
121+
"component", "network",
122+
"protocol", ProtocolName,
123+
"connection_id", c.callbackContext.ConnectionId.String(),
124+
)
121125
c.Protocol.Start()
122126
// Start goroutine to cleanup resources on protocol shutdown
123127
go func() {
@@ -132,7 +136,11 @@ func (c *Client) Stop() error {
132136
var err error
133137
c.onceStop.Do(func() {
134138
c.Protocol.Logger().
135-
Debug(fmt.Sprintf("%s: stopping client protocol for connection %+v", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr))
139+
Debug("stopping client protocol",
140+
"component", "network",
141+
"protocol", ProtocolName,
142+
"connection_id", c.callbackContext.ConnectionId.String(),
143+
)
136144
c.busyMutex.Lock()
137145
defer c.busyMutex.Unlock()
138146
msg := NewMsgDone()
@@ -146,7 +154,12 @@ func (c *Client) Stop() error {
146154
// GetCurrentTip returns the current chain tip
147155
func (c *Client) GetCurrentTip() (*Tip, error) {
148156
c.Protocol.Logger().
149-
Debug(fmt.Sprintf("%s: client %+v called GetCurrentTip()", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr))
157+
Debug("calling GetCurrentTip()",
158+
"component", "network",
159+
"protocol", ProtocolName,
160+
"role", "client",
161+
"connection_id", c.callbackContext.ConnectionId.String(),
162+
)
150163
done := atomic.Bool{}
151164
requestResultChan := make(chan Tip, 1)
152165
requestErrorChan := make(chan error, 1)
@@ -186,13 +199,25 @@ func (c *Client) GetCurrentTip() (*Tip, error) {
186199
waitingForCurrentTipChan = nil
187200
case tip := <-waitingResultChan:
188201
c.Protocol.Logger().
189-
Debug(fmt.Sprintf("%s: returning tip results {Slot: %d, Hash: %x, BlockNumber: %d} to %+v", ProtocolName, tip.Point.Slot, tip.Point.Hash, tip.BlockNumber, c.callbackContext.ConnectionId.RemoteAddr))
202+
Debug(
203+
fmt.Sprintf("received tip results {Slot: %d, Hash: %x, BlockNumber: %d}", tip.Point.Slot, tip.Point.Hash, tip.BlockNumber),
204+
"component", "network",
205+
"protocol", ProtocolName,
206+
"role", "client",
207+
"connection_id", c.callbackContext.ConnectionId.String(),
208+
)
190209
// The result from the other request is ready.
191210
done.Store(true)
192211
return &tip, nil
193212
case tip := <-requestResultChan:
194213
c.Protocol.Logger().
195-
Debug(fmt.Sprintf("%s: returning tip results {Slot: %d, Hash: %x, BlockNumber: %d} to %+v", ProtocolName, tip.Point.Slot, tip.Point.Hash, tip.BlockNumber, c.callbackContext.ConnectionId.RemoteAddr))
214+
Debug(
215+
fmt.Sprintf("received tip results {Slot: %d, Hash: %x, BlockNumber: %d}", tip.Point.Slot, tip.Point.Hash, tip.BlockNumber),
216+
"component", "network",
217+
"protocol", ProtocolName,
218+
"role", "client",
219+
"connection_id", c.callbackContext.ConnectionId.String(),
220+
)
196221
// If waitingForCurrentTipChan is full, the for loop that empties it might finish the
197222
// loop before the select statement that writes to it is triggered. For that reason we
198223
// require requestResultChan here.
@@ -215,16 +240,49 @@ func (c *Client) GetAvailableBlockRange(
215240
if len(intersectPoints) == 0 {
216241
intersectPoints = []common.Point{common.NewPointOrigin()}
217242
}
243+
244+
// Debug logging
218245
switch len(intersectPoints) {
219246
case 1:
220247
c.Protocol.Logger().
221-
Debug(fmt.Sprintf("%s: client %+v called GetAvailableBlockRange(intersectPoints: []{Slot: %d, Hash: %x})", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr, intersectPoints[0].Slot, intersectPoints[0].Hash))
248+
Debug(
249+
fmt.Sprintf(
250+
"calling GetAvailableBlockRange(intersectPoints: []{Slot: %d, Hash: %x})",
251+
intersectPoints[0].Slot,
252+
intersectPoints[0].Hash,
253+
),
254+
"component", "network",
255+
"protocol", ProtocolName,
256+
"role", "client",
257+
"connection_id", c.callbackContext.ConnectionId.String(),
258+
)
222259
case 2:
223260
c.Protocol.Logger().
224-
Debug(fmt.Sprintf("%s: client %+v called GetAvailableBlockRange(intersectPoints: []{Slot: %d, Hash: %x},{Slot: %d, Hash: %x})", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr, intersectPoints[0].Slot, intersectPoints[0].Hash, intersectPoints[1].Slot, intersectPoints[1].Hash))
261+
Debug(
262+
fmt.Sprintf(
263+
"calling GetAvailableBlockRange(intersectPoints: []{Slot: %d, Hash: %x},{Slot: %d, Hash: %x})",
264+
intersectPoints[0].Slot,
265+
intersectPoints[0].Hash,
266+
intersectPoints[1].Slot,
267+
intersectPoints[1].Hash,
268+
),
269+
"component", "network",
270+
"protocol", ProtocolName,
271+
"role", "client",
272+
"connection_id", c.callbackContext.ConnectionId.String(),
273+
)
225274
default:
226275
c.Protocol.Logger().
227-
Debug(fmt.Sprintf("%s: client %+v called GetAvailableBlockRange(intersectPoints: %+v)", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr, intersectPoints))
276+
Debug(
277+
fmt.Sprintf(
278+
"calling GetAvailableBlockRange(intersectPoints: %+v)",
279+
intersectPoints,
280+
),
281+
"component", "network",
282+
"protocol", ProtocolName,
283+
"role", "client",
284+
"connection_id", c.callbackContext.ConnectionId.String(),
285+
)
228286
}
229287

230288
// Find our chain intersection
@@ -300,16 +358,49 @@ func (c *Client) Sync(intersectPoints []common.Point) error {
300358
if len(intersectPoints) == 0 {
301359
intersectPoints = []common.Point{common.NewPointOrigin()}
302360
}
361+
362+
// Debug logging
303363
switch len(intersectPoints) {
304364
case 1:
305365
c.Protocol.Logger().
306-
Debug(fmt.Sprintf("%s: client %+v called Sync(intersectPoints: []{Slot: %d, Hash: %x})", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr, intersectPoints[0].Slot, intersectPoints[0].Hash))
366+
Debug(
367+
fmt.Sprintf(
368+
"calling Sync(intersectPoints: []{Slot: %d, Hash: %x})",
369+
intersectPoints[0].Slot,
370+
intersectPoints[0].Hash,
371+
),
372+
"component", "network",
373+
"protocol", ProtocolName,
374+
"role", "client",
375+
"connection_id", c.callbackContext.ConnectionId.String(),
376+
)
307377
case 2:
308378
c.Protocol.Logger().
309-
Debug(fmt.Sprintf("%s: client %+v called Sync(intersectPoints: []{Slot: %d, Hash: %x},{Slot: %d, Hash: %x})", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr, intersectPoints[0].Slot, intersectPoints[0].Hash, intersectPoints[1].Slot, intersectPoints[1].Hash))
379+
Debug(
380+
fmt.Sprintf(
381+
"calling Sync(intersectPoints: []{Slot: %d, Hash: %x},{Slot: %d, Hash: %x})",
382+
intersectPoints[0].Slot,
383+
intersectPoints[0].Hash,
384+
intersectPoints[1].Slot,
385+
intersectPoints[1].Hash,
386+
),
387+
"component", "network",
388+
"protocol", ProtocolName,
389+
"role", "client",
390+
"connection_id", c.callbackContext.ConnectionId.String(),
391+
)
310392
default:
311393
c.Protocol.Logger().
312-
Debug(fmt.Sprintf("%s: client %+v called Sync(intersectPoints: %+v)", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr, intersectPoints))
394+
Debug(
395+
fmt.Sprintf(
396+
"calling Sync(intersectPoints: %+v)",
397+
intersectPoints,
398+
),
399+
"component", "network",
400+
"protocol", ProtocolName,
401+
"role", "client",
402+
"connection_id", c.callbackContext.ConnectionId.String(),
403+
)
313404
}
314405

315406
intersectResultChan, cancel := c.wantIntersectFound()
@@ -478,13 +569,23 @@ func (c *Client) messageHandler(msg protocol.Message) error {
478569

479570
func (c *Client) handleAwaitReply() error {
480571
c.Protocol.Logger().
481-
Debug(fmt.Sprintf("%s: client await reply for %+v", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr))
572+
Debug("waiting for next reply",
573+
"component", "network",
574+
"protocol", ProtocolName,
575+
"role", "client",
576+
"connection_id", c.callbackContext.ConnectionId.String(),
577+
)
482578
return nil
483579
}
484580

485581
func (c *Client) handleRollForward(msgGeneric protocol.Message) error {
486582
c.Protocol.Logger().
487-
Debug(fmt.Sprintf("%s: client roll forward for %+v", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr))
583+
Debug("roll forward",
584+
"component", "network",
585+
"protocol", ProtocolName,
586+
"role", "client",
587+
"connection_id", c.callbackContext.ConnectionId.String(),
588+
)
488589
firstBlockChan := func() chan<- clientPointResult {
489590
select {
490591
case ch := <-c.wantFirstBlockChan:
@@ -594,7 +695,12 @@ func (c *Client) handleRollForward(msgGeneric protocol.Message) error {
594695

595696
func (c *Client) handleRollBackward(msg protocol.Message) error {
596697
c.Protocol.Logger().
597-
Debug(fmt.Sprintf("%s: client roll backward for %+v", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr))
698+
Debug("roll backward",
699+
"component", "network",
700+
"protocol", ProtocolName,
701+
"role", "client",
702+
"connection_id", c.callbackContext.ConnectionId.String(),
703+
)
598704
msgRollBackward := msg.(*MsgRollBackward)
599705
c.sendCurrentTip(msgRollBackward.Tip)
600706
if len(c.wantFirstBlockChan) == 0 {
@@ -621,7 +727,12 @@ func (c *Client) handleRollBackward(msg protocol.Message) error {
621727

622728
func (c *Client) handleIntersectFound(msg protocol.Message) error {
623729
c.Protocol.Logger().
624-
Debug(fmt.Sprintf("%s: client intersect found for %+v", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr))
730+
Debug("chain intersect found",
731+
"component", "network",
732+
"protocol", ProtocolName,
733+
"role", "client",
734+
"connection_id", c.callbackContext.ConnectionId.String(),
735+
)
625736
msgIntersectFound := msg.(*MsgIntersectFound)
626737
c.sendCurrentTip(msgIntersectFound.Tip)
627738

@@ -635,7 +746,12 @@ func (c *Client) handleIntersectFound(msg protocol.Message) error {
635746

636747
func (c *Client) handleIntersectNotFound(msgGeneric protocol.Message) error {
637748
c.Protocol.Logger().
638-
Debug(fmt.Sprintf("%s: client intersect not found for %+v", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr))
749+
Debug("chain intersect not found",
750+
"component", "network",
751+
"protocol", ProtocolName,
752+
"role", "client",
753+
"connection_id", c.callbackContext.ConnectionId.String(),
754+
)
639755
msgIntersectNotFound := msgGeneric.(*MsgIntersectNotFound)
640756
c.sendCurrentTip(msgIntersectNotFound.Tip)
641757

protocol/handshake/client.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
7070
func (c *Client) Start() {
7171
c.onceStart.Do(func() {
7272
c.Protocol.Logger().
73-
Debug(fmt.Sprintf("%s: starting client protocol for connection %+v", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr))
73+
Debug("starting client protocol",
74+
"component", "network",
75+
"protocol", ProtocolName,
76+
"connection_id", c.callbackContext.ConnectionId.String(),
77+
)
7478
c.Protocol.Start()
7579
// Send our ProposeVersions message
7680
msg := NewMsgProposeVersions(c.config.ProtocolVersionMap)
@@ -97,7 +101,12 @@ func (c *Client) messageHandler(msg protocol.Message) error {
97101

98102
func (c *Client) handleAcceptVersion(msg protocol.Message) error {
99103
c.Protocol.Logger().
100-
Debug(fmt.Sprintf("%s: client accept version for %+v", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr))
104+
Debug("accepted version negotiation",
105+
"component", "network",
106+
"protocol", ProtocolName,
107+
"role", "client",
108+
"connection_id", c.callbackContext.ConnectionId.String(),
109+
)
101110
if c.config.FinishedFunc == nil {
102111
return fmt.Errorf(
103112
"received handshake AcceptVersion message but no callback function is defined",
@@ -120,7 +129,12 @@ func (c *Client) handleAcceptVersion(msg protocol.Message) error {
120129

121130
func (c *Client) handleRefuse(msgGeneric protocol.Message) error {
122131
c.Protocol.Logger().
123-
Debug(fmt.Sprintf("%s: client refuse for %+v", ProtocolName, c.callbackContext.ConnectionId.RemoteAddr))
132+
Debug("refused handshake",
133+
"component", "network",
134+
"protocol", ProtocolName,
135+
"role", "client",
136+
"connection_id", c.callbackContext.ConnectionId.String(),
137+
)
124138
msg := msgGeneric.(*MsgRefuse)
125139
var err error
126140
switch msg.Reason[0].(uint64) {

0 commit comments

Comments
 (0)