Skip to content

Commit be2c27d

Browse files
authored
feat: redact nodes url when logging (#67)
1 parent ef2d994 commit be2c27d

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

pkg/app/run.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func createNodePool(ctx context.Context, nodes []string) (*rpc.Pool, error) {
224224

225225
status, err := rpcNodes[i].Status(ctx)
226226
if err != nil {
227-
log.Error().Err(err).Msgf("failed to connect to %s", endpoint)
227+
log.Error().Err(err).Msgf("failed to connect to %s", rpcNodes[i].Redacted())
228228
continue
229229
}
230230

@@ -234,9 +234,9 @@ func createNodePool(ctx context.Context, nodes []string) (*rpc.Pool, error) {
234234
logger := log.With().Int64("height", blockHeight).Str("chainID", chainID).Logger()
235235

236236
if rpcNodes[i].IsSynced() {
237-
logger.Info().Msgf("connected to %s", endpoint)
237+
logger.Info().Msgf("connected to %s", rpcNodes[i].Redacted())
238238
} else {
239-
logger.Warn().Msgf("connected to %s (but node is catching up)", endpoint)
239+
logger.Warn().Msgf("connected to %s (but node is catching up)", rpcNodes[i].Redacted())
240240
}
241241
}
242242

pkg/rpc/node.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package rpc
33
import (
44
"context"
55
"fmt"
6+
"net/url"
67
"sync"
78
"sync/atomic"
89
"time"
@@ -32,6 +33,9 @@ type NodeOption func(*Node)
3233
type Node struct {
3334
Client *http.HTTP
3435

36+
// Save endpoint url for redacted logging
37+
endpoint *url.URL
38+
3539
onStart []OnNodeStart
3640
onStatus []OnNodeStatus
3741
onEvent map[string][]OnNodeEvent
@@ -45,8 +49,11 @@ type Node struct {
4549
}
4650

4751
func NewNode(client *http.HTTP, options ...NodeOption) *Node {
52+
endpoint, _ := url.Parse(client.Remote())
53+
4854
node := &Node{
4955
Client: client,
56+
endpoint: endpoint,
5057
started: make(chan struct{}),
5158
startedOnce: sync.Once{},
5259
subscriptions: make(map[string]<-chan ctypes.ResultEvent),
@@ -60,6 +67,24 @@ func NewNode(client *http.HTTP, options ...NodeOption) *Node {
6067
return node
6168
}
6269

70+
func (n *Node) Endpoint() string {
71+
if n.endpoint == nil {
72+
return n.Client.Remote()
73+
}
74+
ep := *n.endpoint
75+
if _, has := ep.User.Password(); has {
76+
ep.User = nil
77+
}
78+
return ep.String()
79+
}
80+
81+
func (n *Node) Redacted() string {
82+
if n.endpoint == nil {
83+
return n.Client.Remote()
84+
}
85+
return n.endpoint.Redacted()
86+
}
87+
6388
func (n *Node) OnStart(callback OnNodeStart) {
6489
n.onStart = append(n.onStart, callback)
6590
}
@@ -142,25 +167,25 @@ func (n *Node) Start(ctx context.Context) error {
142167
for {
143168
select {
144169
case <-ctx.Done():
145-
log.Debug().Err(ctx.Err()).Str("node", n.Client.Remote()).Msgf("stopping node status loop")
170+
log.Debug().Err(ctx.Err()).Str("node", n.Redacted()).Msgf("stopping node status loop")
146171
return nil
147172

148173
case evt := <-blocksEvents:
149-
log.Debug().Str("node", n.Client.Remote()).Msg("got new block event")
174+
log.Debug().Str("node", n.Redacted()).Msg("got new block event")
150175
n.saveLatestBlock(evt.Data.(types.EventDataNewBlock).Block)
151176
n.handleEvent(ctx, EventNewBlock, &evt)
152177
blocksTicker.Reset(10 * time.Second)
153178

154179
case evt := <-validatorEvents:
155-
log.Debug().Str("node", n.Client.Remote()).Msg("got validator set update event")
180+
log.Debug().Str("node", n.Redacted()).Msg("got validator set update event")
156181
n.handleEvent(ctx, EventValidatorSetUpdates, &evt)
157182

158183
case <-blocksTicker.C:
159-
log.Debug().Str("node", n.Client.Remote()).Msg("syncing latest blocks")
184+
log.Debug().Str("node", n.Redacted()).Msg("syncing latest blocks")
160185
n.syncBlocks(ctx)
161186

162187
case <-statusTicker.C:
163-
log.Debug().Str("node", n.Client.Remote()).Msg("syncing status")
188+
log.Debug().Str("node", n.Redacted()).Msg("syncing status")
164189
n.syncStatus(ctx)
165190
}
166191
}
@@ -190,7 +215,7 @@ func (n *Node) syncStatus(ctx context.Context) (*ctypes.ResultStatus, error) {
190215
retry.Delay(1 * time.Second),
191216
retry.Attempts(2),
192217
// retry.OnRetry(func(_ uint, err error) {
193-
// log.Warn().Err(err).Msgf("retrying status on %s", n.Client.Remote())
218+
// log.Warn().Err(err).Msgf("retrying status on %s", n.Redacted())
194219
// }),
195220
}
196221

@@ -201,12 +226,12 @@ func (n *Node) syncStatus(ctx context.Context) (*ctypes.ResultStatus, error) {
201226
n.status.Store(status)
202227

203228
if err != nil {
204-
return status, fmt.Errorf("failed to get status of %s: %w", n.Client.Remote(), err)
229+
return status, fmt.Errorf("failed to get status of %s: %w", n.Redacted(), err)
205230
}
206231

207232
if status.SyncInfo.CatchingUp {
208233
// We're catching up, not synced
209-
log.Warn().Msgf("node %s is catching up at block %d", n.Client.Remote(), status.SyncInfo.LatestBlockHeight)
234+
log.Warn().Msgf("node %s is catching up at block %d", n.Redacted(), status.SyncInfo.LatestBlockHeight)
210235
return status, nil
211236
}
212237

pkg/watcher/block.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (w *BlockWatcher) OnNodeStart(ctx context.Context, node *rpc.Node) error {
6666
for {
6767
select {
6868
case <-ctx.Done():
69-
log.Debug().Err(ctx.Err()).Str("node", node.Client.Remote()).Msgf("stopping block watcher loop")
69+
log.Debug().Err(ctx.Err()).Str("node", node.Redacted()).Msgf("stopping block watcher loop")
7070
return
7171
case <-ticker.C:
7272
if err := w.syncValidatorSet(ctx, node); err != nil {
@@ -112,7 +112,7 @@ func (w *BlockWatcher) handleNodeBlock(ctx context.Context, node *rpc.Node, bloc
112112
}
113113

114114
// Set node block height
115-
w.metrics.NodeBlockHeight.WithLabelValues(node.ChainID(), node.Client.Remote()).Set(float64(block.Height))
115+
w.metrics.NodeBlockHeight.WithLabelValues(node.ChainID(), node.Endpoint()).Set(float64(block.Height))
116116

117117
// Extract block info
118118
w.blockChan <- NewBlockInfo(block, w.computeValidatorStatus(block))
@@ -148,7 +148,7 @@ func (w *BlockWatcher) syncValidatorSet(ctx context.Context, n *rpc.Node) error
148148
}
149149

150150
log.Debug().
151-
Str("node", n.Client.Remote()).
151+
Str("node", n.Redacted()).
152152
Int("validators", len(validators)).
153153
Msgf("validator set")
154154

pkg/watcher/status.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ func (w *StatusWatcher) OnNodeStatus(ctx context.Context, n *rpc.Node, status *c
5353
}
5454

5555
log.Debug().
56-
Str("node", n.Client.Remote()).
56+
Str("node", n.Redacted()).
5757
Int64("height", blockHeight).
5858
Bool("synced", synced).
5959
Msgf("node status")
6060

61-
w.metrics.NodeSynced.WithLabelValues(chainID, n.Client.Remote()).Set(
61+
w.metrics.NodeSynced.WithLabelValues(chainID, n.Endpoint()).Set(
6262
metrics.BoolToFloat64(synced),
6363
)
6464

0 commit comments

Comments
 (0)