Skip to content

Commit dc4432a

Browse files
authored
refactor(logs): use slog attributes for protocol servers (#765)
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent c3c5184 commit dc4432a

File tree

9 files changed

+256
-58
lines changed

9 files changed

+256
-58
lines changed

protocol/blockfetch/server.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,37 @@ func (s *Server) initProtocol() {
6161

6262
func (s *Server) NoBlocks() error {
6363
s.Protocol.Logger().
64-
Debug(fmt.Sprintf("%s: server %+v called NoBlocks()", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
64+
Debug("calling NoBlocks()",
65+
"component", "network",
66+
"protocol", ProtocolName,
67+
"role", "server",
68+
"connection_id", s.callbackContext.ConnectionId.String(),
69+
)
6570
msg := NewMsgNoBlocks()
6671
return s.SendMessage(msg)
6772
}
6873

6974
func (s *Server) StartBatch() error {
7075
s.Protocol.Logger().
71-
Debug(fmt.Sprintf("%s: server %+v called StartBatch()", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
76+
Debug("calling StartBatch()",
77+
"component", "network",
78+
"protocol", ProtocolName,
79+
"role", "server",
80+
"connection_id", s.callbackContext.ConnectionId.String(),
81+
)
7282
msg := NewMsgStartBatch()
7383
return s.SendMessage(msg)
7484
}
7585

7686
func (s *Server) Block(blockType uint, blockData []byte) error {
7787
s.Protocol.Logger().
78-
Debug(fmt.Sprintf("%s: server %+v called Block(blockType: %+v, blockData: %x)", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr, blockType, blockData))
88+
Debug(
89+
fmt.Sprintf("calling Block(blockType: %+x, blockData: %x)", blockType, blockData),
90+
"component", "network",
91+
"protocol", ProtocolName,
92+
"role", "server",
93+
"connection_id", s.callbackContext.ConnectionId.String(),
94+
)
7995
wrappedBlock := WrappedBlock{
8096
Type: blockType,
8197
RawBlock: blockData,
@@ -90,7 +106,12 @@ func (s *Server) Block(blockType uint, blockData []byte) error {
90106

91107
func (s *Server) BatchDone() error {
92108
s.Protocol.Logger().
93-
Debug(fmt.Sprintf("%s: server %+v called BatchDone()", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
109+
Debug("calling BatchDone()",
110+
"component", "network",
111+
"protocol", ProtocolName,
112+
"role", "server",
113+
"connection_id", s.callbackContext.ConnectionId.String(),
114+
)
94115
msg := NewMsgBatchDone()
95116
return s.SendMessage(msg)
96117
}
@@ -114,7 +135,12 @@ func (s *Server) messageHandler(msg protocol.Message) error {
114135

115136
func (s *Server) handleRequestRange(msg protocol.Message) error {
116137
s.Protocol.Logger().
117-
Debug(fmt.Sprintf("%s: server request range for %+v", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
138+
Debug("request range",
139+
"component", "network",
140+
"protocol", ProtocolName,
141+
"role", "server",
142+
"connection_id", s.callbackContext.ConnectionId.String(),
143+
)
118144
if s.config == nil || s.config.RequestRangeFunc == nil {
119145
return fmt.Errorf(
120146
"received block-fetch RequestRange message but no callback function is defined",
@@ -130,7 +156,12 @@ func (s *Server) handleRequestRange(msg protocol.Message) error {
130156

131157
func (s *Server) handleClientDone() error {
132158
s.Protocol.Logger().
133-
Debug(fmt.Sprintf("%s: server client done for %+v", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
159+
Debug("client done",
160+
"component", "network",
161+
"protocol", ProtocolName,
162+
"role", "server",
163+
"connection_id", s.callbackContext.ConnectionId.String(),
164+
)
134165
// Restart protocol
135166
s.Protocol.Stop()
136167
s.initProtocol()

protocol/chainsync/server.go

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,47 @@ func (s *Server) initProtocol() {
7979

8080
func (s *Server) RollBackward(point common.Point, tip Tip) error {
8181
s.Protocol.Logger().
82-
Debug(fmt.Sprintf("%s: server %+v called RollBackward(point: {Slot: %d, Hash: %x}, tip: {Point: %+v, BlockNumber: %d})", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr, point.Slot, point.Hash, tip.Point, tip.BlockNumber))
82+
Debug(
83+
fmt.Sprintf("calling RollBackward(point: {Slot: %d, Hash: %x}, tip: {Point: {Slot: %d, Hash: %x}, BlockNumber: %d})",
84+
point.Slot, point.Hash,
85+
tip.Point.Slot, tip.Point.Hash,
86+
tip.BlockNumber,
87+
),
88+
"component", "network",
89+
"protocol", ProtocolName,
90+
"role", "server",
91+
"connection_id", s.callbackContext.ConnectionId.String(),
92+
)
8393
msg := NewMsgRollBackward(point, tip)
8494
return s.SendMessage(msg)
8595
}
8696

8797
func (s *Server) AwaitReply() error {
8898
s.Protocol.Logger().
89-
Debug(fmt.Sprintf("%s: server %+v called AwaitReply()", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
99+
Debug("calling AwaitReply()",
100+
"component", "network",
101+
"protocol", ProtocolName,
102+
"role", "server",
103+
"connection_id", s.callbackContext.ConnectionId.String(),
104+
)
90105
msg := NewMsgAwaitReply()
91106
return s.SendMessage(msg)
92107
}
93108

94109
func (s *Server) RollForward(blockType uint, blockData []byte, tip Tip) error {
95110
s.Protocol.Logger().
96-
Debug(fmt.Sprintf("%s: server %+v called RollForward(blockType: %+v, blockData: %x, tip: {Point: {Slot: %d, Hash: %x}, BlockNumber: %d})", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr, blockType, blockData, tip.Point.Slot, tip.Point.Hash, tip.BlockNumber))
111+
Debug(
112+
fmt.Sprintf("calling RollForward(blockType: %+x, blockData: %x, tip: {Point: {Slot: %d, Hash: %x}, BlockNumber: %d})",
113+
blockType,
114+
blockData,
115+
tip.Point.Slot, tip.Point.Hash,
116+
tip.BlockNumber,
117+
),
118+
"component", "network",
119+
"protocol", ProtocolName,
120+
"role", "server",
121+
"connection_id", s.callbackContext.ConnectionId.String(),
122+
)
97123
if s.Mode() == protocol.ProtocolModeNodeToNode {
98124
eraId := ledger.BlockToBlockHeaderTypeMap[blockType]
99125
msg := NewMsgRollForwardNtN(
@@ -136,7 +162,12 @@ func (s *Server) handleRequestNext() error {
136162
// TODO: figure out why this one log message causes a panic (and only this one)
137163
// during tests
138164
//s.Protocol.Logger().
139-
// Debug(fmt.Sprintf("%s: server request next for %+v", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
165+
// Debug("request next",
166+
// "component", "network",
167+
// "protocol", ProtocolName,
168+
// "role", "server",
169+
// "connection_id", s.callbackContext.ConnectionId.String(),
170+
// )
140171
if s.config == nil || s.config.RequestNextFunc == nil {
141172
return fmt.Errorf(
142173
"received chain-sync RequestNext message but no callback function is defined",
@@ -147,7 +178,12 @@ func (s *Server) handleRequestNext() error {
147178

148179
func (s *Server) handleFindIntersect(msg protocol.Message) error {
149180
s.Protocol.Logger().
150-
Debug(fmt.Sprintf("%s: server find intersect for %+v", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
181+
Debug("find intersect",
182+
"component", "network",
183+
"protocol", ProtocolName,
184+
"role", "server",
185+
"connection_id", s.callbackContext.ConnectionId.String(),
186+
)
151187
if s.config == nil || s.config.FindIntersectFunc == nil {
152188
return fmt.Errorf(
153189
"received chain-sync FindIntersect message but no callback function is defined",
@@ -177,7 +213,12 @@ func (s *Server) handleFindIntersect(msg protocol.Message) error {
177213

178214
func (s *Server) handleDone() error {
179215
s.Protocol.Logger().
180-
Debug(fmt.Sprintf("%s: server done for %+v", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
216+
Debug("done",
217+
"component", "network",
218+
"protocol", ProtocolName,
219+
"role", "server",
220+
"connection_id", s.callbackContext.ConnectionId.String(),
221+
)
181222
// Restart protocol
182223
s.Protocol.Stop()
183224
s.initProtocol()

protocol/handshake/server.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ func (s *Server) handleMessage(msg protocol.Message) error {
7777

7878
func (s *Server) handleProposeVersions(msg protocol.Message) error {
7979
s.Protocol.Logger().
80-
Debug(fmt.Sprintf("%s: server propose versions for %+v", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
80+
Debug("propose versions",
81+
"component", "network",
82+
"protocol", ProtocolName,
83+
"role", "server",
84+
"connection_id", s.callbackContext.ConnectionId.String(),
85+
)
8186
if s.config.FinishedFunc == nil {
8287
return fmt.Errorf(
8388
"received handshake ProposeVersions message but no callback function is defined",

protocol/keepalive/server.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ func (s *Server) messageHandler(msg protocol.Message) error {
7070

7171
func (s *Server) handleKeepAlive(msgGeneric protocol.Message) error {
7272
s.Protocol.Logger().
73-
Debug(fmt.Sprintf("%s: server keep alive for %+v", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
73+
Debug("keep alive",
74+
"component", "network",
75+
"protocol", ProtocolName,
76+
"role", "server",
77+
"connection_id", s.callbackContext.ConnectionId.String(),
78+
)
7479
msg := msgGeneric.(*MsgKeepAlive)
7580
if s.config != nil && s.config.KeepAliveFunc != nil {
7681
// Call the user callback function
@@ -84,7 +89,12 @@ func (s *Server) handleKeepAlive(msgGeneric protocol.Message) error {
8489

8590
func (s *Server) handleDone() error {
8691
s.Protocol.Logger().
87-
Debug(fmt.Sprintf("%s: server done for %+v", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
92+
Debug("done",
93+
"component", "network",
94+
"protocol", ProtocolName,
95+
"role", "server",
96+
"connection_id", s.callbackContext.ConnectionId.String(),
97+
)
8898
if s.config != nil && s.config.DoneFunc != nil {
8999
// Call the user callback function
90100
return s.config.DoneFunc(s.callbackContext)

protocol/localstatequery/server.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ func (s *Server) messageHandler(msg protocol.Message) error {
9393

9494
func (s *Server) handleAcquire(msg protocol.Message) error {
9595
s.Protocol.Logger().
96-
Debug(fmt.Sprintf("%s: server acquire for %+v", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
96+
Debug("acquire",
97+
"component", "network",
98+
"protocol", ProtocolName,
99+
"role", "server",
100+
"connection_id", s.callbackContext.ConnectionId.String(),
101+
)
97102
if s.config.AcquireFunc == nil {
98103
return fmt.Errorf(
99104
"received local-state-query Acquire message but no callback function is defined",
@@ -112,7 +117,12 @@ func (s *Server) handleAcquire(msg protocol.Message) error {
112117

113118
func (s *Server) handleQuery(msg protocol.Message) error {
114119
s.Protocol.Logger().
115-
Debug(fmt.Sprintf("%s: server query for %+v", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
120+
Debug("query",
121+
"component", "network",
122+
"protocol", ProtocolName,
123+
"role", "server",
124+
"connection_id", s.callbackContext.ConnectionId.String(),
125+
)
116126
if s.config.QueryFunc == nil {
117127
return fmt.Errorf(
118128
"received local-state-query Query message but no callback function is defined",
@@ -125,7 +135,12 @@ func (s *Server) handleQuery(msg protocol.Message) error {
125135

126136
func (s *Server) handleRelease() error {
127137
s.Protocol.Logger().
128-
Debug(fmt.Sprintf("%s: server release for %+v", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
138+
Debug("release",
139+
"component", "network",
140+
"protocol", ProtocolName,
141+
"role", "server",
142+
"connection_id", s.callbackContext.ConnectionId.String(),
143+
)
129144
if s.config.ReleaseFunc == nil {
130145
return fmt.Errorf(
131146
"received local-state-query Release message but no callback function is defined",
@@ -137,7 +152,12 @@ func (s *Server) handleRelease() error {
137152

138153
func (s *Server) handleReAcquire(msg protocol.Message) error {
139154
s.Protocol.Logger().
140-
Debug(fmt.Sprintf("%s: server reacquire for %+v", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
155+
Debug("reacquire",
156+
"component", "network",
157+
"protocol", ProtocolName,
158+
"role", "server",
159+
"connection_id", s.callbackContext.ConnectionId.String(),
160+
)
141161
if s.config.ReAcquireFunc == nil {
142162
return fmt.Errorf(
143163
"received local-state-query ReAcquire message but no callback function is defined",
@@ -156,7 +176,12 @@ func (s *Server) handleReAcquire(msg protocol.Message) error {
156176

157177
func (s *Server) handleDone() error {
158178
s.Protocol.Logger().
159-
Debug(fmt.Sprintf("%s: server done for %+v", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
179+
Debug("done",
180+
"component", "network",
181+
"protocol", ProtocolName,
182+
"role", "server",
183+
"connection_id", s.callbackContext.ConnectionId.String(),
184+
)
160185
if s.config.DoneFunc == nil {
161186
return fmt.Errorf(
162187
"received local-state-query Done message but no callback function is defined",

protocol/localtxmonitor/server.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ func (s *Server) messageHandler(msg protocol.Message) error {
8585

8686
func (s *Server) handleAcquire() error {
8787
s.Protocol.Logger().
88-
Debug(fmt.Sprintf("%s: server acquire for %+v", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
88+
Debug("acquire",
89+
"component", "network",
90+
"protocol", ProtocolName,
91+
"role", "server",
92+
"connection_id", s.callbackContext.ConnectionId.String(),
93+
)
8994
if s.config.GetMempoolFunc == nil {
9095
return fmt.Errorf(
9196
"received local-tx-monitor Acquire message but no GetMempool callback function is defined",
@@ -126,21 +131,36 @@ func (s *Server) handleAcquire() error {
126131

127132
func (s *Server) handleDone() error {
128133
s.Protocol.Logger().
129-
Debug(fmt.Sprintf("%s: server done for %+v", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
134+
Debug("done",
135+
"component", "network",
136+
"protocol", ProtocolName,
137+
"role", "server",
138+
"connection_id", s.callbackContext.ConnectionId.String(),
139+
)
130140
return nil
131141
}
132142

133143
func (s *Server) handleRelease() error {
134144
s.Protocol.Logger().
135-
Debug(fmt.Sprintf("%s: server release for %+v", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
145+
Debug("release",
146+
"component", "network",
147+
"protocol", ProtocolName,
148+
"role", "server",
149+
"connection_id", s.callbackContext.ConnectionId.String(),
150+
)
136151
s.mempoolCapacity = 0
137152
s.mempoolTxs = nil
138153
return nil
139154
}
140155

141156
func (s *Server) handleHasTx(msg protocol.Message) error {
142157
s.Protocol.Logger().
143-
Debug(fmt.Sprintf("%s: server has tx for %+v", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
158+
Debug("has tx",
159+
"component", "network",
160+
"protocol", ProtocolName,
161+
"role", "server",
162+
"connection_id", s.callbackContext.ConnectionId.String(),
163+
)
144164
msgHasTx := msg.(*MsgHasTx)
145165
txId := hex.EncodeToString(msgHasTx.TxId)
146166
hasTx := false
@@ -159,7 +179,12 @@ func (s *Server) handleHasTx(msg protocol.Message) error {
159179

160180
func (s *Server) handleNextTx() error {
161181
s.Protocol.Logger().
162-
Debug(fmt.Sprintf("%s: server next tx for %+v", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
182+
Debug("next tx",
183+
"component", "network",
184+
"protocol", ProtocolName,
185+
"role", "server",
186+
"connection_id", s.callbackContext.ConnectionId.String(),
187+
)
163188
if s.mempoolNextTxIdx > len(s.mempoolTxs) {
164189
newMsg := NewMsgReplyNextTx(0, nil)
165190
if err := s.SendMessage(newMsg); err != nil {
@@ -178,7 +203,12 @@ func (s *Server) handleNextTx() error {
178203

179204
func (s *Server) handleGetSizes() error {
180205
s.Protocol.Logger().
181-
Debug(fmt.Sprintf("%s: server get sizes for %+v", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
206+
Debug("get sizes",
207+
"component", "network",
208+
"protocol", ProtocolName,
209+
"role", "server",
210+
"connection_id", s.callbackContext.ConnectionId.String(),
211+
)
182212
totalTxSize := 0
183213
for _, tx := range s.mempoolTxs {
184214
totalTxSize += len(tx.Tx)

protocol/localtxsubmission/server.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,12 @@ func (s *Server) messageHandler(msg protocol.Message) error {
7373

7474
func (s *Server) handleSubmitTx(msg protocol.Message) error {
7575
s.Protocol.Logger().
76-
Debug(fmt.Sprintf("%s: server submit tx for %+v", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
76+
Debug("submit tx",
77+
"component", "network",
78+
"protocol", ProtocolName,
79+
"role", "server",
80+
"connection_id", s.callbackContext.ConnectionId.String(),
81+
)
7782
if s.config.SubmitTxFunc == nil {
7883
return fmt.Errorf(
7984
"received local-tx-submission SubmitTx message but no callback function is defined",
@@ -102,6 +107,11 @@ func (s *Server) handleSubmitTx(msg protocol.Message) error {
102107

103108
func (s *Server) handleDone() error {
104109
s.Protocol.Logger().
105-
Debug(fmt.Sprintf("%s: server done for %+v", ProtocolName, s.callbackContext.ConnectionId.RemoteAddr))
110+
Debug("done",
111+
"component", "network",
112+
"protocol", ProtocolName,
113+
"role", "server",
114+
"connection_id", s.callbackContext.ConnectionId.String(),
115+
)
106116
return nil
107117
}

0 commit comments

Comments
 (0)