Skip to content

Commit 37207f7

Browse files
esuwunickeskov
andauthored
Metrics report fsm channel length (#1723)
* Added a channel monitoring metric * Fix issue. * Add 'lenProvider' interface. --------- Co-authored-by: Nikolay Eskov <mr.eskov1@yandex.ru>
1 parent 5f0fed1 commit 37207f7

2 files changed

Lines changed: 51 additions & 19 deletions

File tree

pkg/metrics/metrics.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ const (
2222
reportInterval = time.Second
2323
bufferSize = 2000
2424

25-
eventInv = "Inv"
26-
eventReceived = "Received"
27-
eventApplied = "Applied"
28-
eventAppended = "Appended"
29-
eventDeclined = "Declined"
30-
eventMined = "Mined"
31-
eventScore = "Score"
32-
eventUtx = "Utx"
25+
eventInv = "Inv"
26+
eventReceived = "Received"
27+
eventApplied = "Applied"
28+
eventAppended = "Appended"
29+
eventDeclined = "Declined"
30+
eventMined = "Mined"
31+
eventScore = "Score"
32+
eventUtx = "Utx"
33+
eventFSMChannelLen = "FSMChannelLength"
3334
)
3435

3536
/*
@@ -220,6 +221,15 @@ func Utx(utxCount int) {
220221
reportUtx(t, f)
221222
}
222223

224+
func FSMChannelLength(length int) {
225+
if rep == nil {
226+
return
227+
}
228+
t := emptyTags().node().withEvent(eventFSMChannelLen)
229+
f := emptyFields().withChannelLength(length)
230+
reportFSMChannelLength(t, f)
231+
}
232+
223233
type tags map[string]string
224234

225235
func emptyTags() tags {
@@ -345,6 +355,11 @@ func (f fields) withHeight(height proto.Height) fields {
345355
return f
346356
}
347357

358+
func (f fields) withChannelLength(chLength int) fields {
359+
f["channel-size"] = chLength
360+
return f
361+
}
362+
348363
type reporter struct {
349364
c influx.Client
350365
id int
@@ -470,6 +485,15 @@ func reportUtx(t tags, f fields) {
470485
rep.in <- p
471486
}
472487

488+
func reportFSMChannelLength(t tags, f fields) {
489+
p, err := influx.NewPoint("fsm-channel", t, f, time.Now())
490+
if err != nil {
491+
zap.S().Warnf("Failed to create metrics point 'fsm-channel': %v", err)
492+
return
493+
}
494+
rep.in <- p
495+
}
496+
473497
func shortID(id proto.BlockID) string {
474498
return id.String()[:6]
475499
}

pkg/node/node.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/pkg/errors"
1414
"go.uber.org/zap"
1515

16+
"github.com/wavesplatform/gowaves/pkg/metrics"
1617
"github.com/wavesplatform/gowaves/pkg/node/fsm"
1718
"github.com/wavesplatform/gowaves/pkg/node/fsm/tasks"
1819
"github.com/wavesplatform/gowaves/pkg/node/messages"
@@ -155,8 +156,10 @@ func (a *Node) Run(
155156
ctx context.Context, p peer.Parent, internalMessageCh <-chan messages.InternalMessage,
156157
networkMsgCh <-chan network.InfoMessage, syncPeer *network.SyncPeer,
157158
) {
159+
protoMessagesChan := chanLenProvider[peer.ProtoMessage](p.MessageCh)
160+
158161
go a.runOutgoingConnections(ctx)
159-
go a.runInternalMetrics(ctx, p.MessageCh)
162+
go a.runInternalMetrics(ctx, protoMessagesChan)
160163
go a.runIncomingConnections(ctx)
161164

162165
tasksCh := make(chan tasks.AsyncTask, 10)
@@ -228,20 +231,25 @@ func (a *Node) runIncomingConnections(ctx context.Context) {
228231
}
229232
}
230233

231-
func (a *Node) runInternalMetrics(ctx context.Context, ch chan peer.ProtoMessage) {
234+
type lenProvider interface {
235+
Len() int
236+
}
237+
238+
type chanLenProvider[T any] <-chan T
239+
240+
func (c chanLenProvider[T]) Len() int { return len(c) }
241+
242+
func (a *Node) runInternalMetrics(ctx context.Context, protoMessagesChan lenProvider) {
243+
ticker := time.NewTicker(metricInternalChannelSizeUpdateInterval)
244+
defer ticker.Stop()
232245
for {
233-
timer := time.NewTimer(metricInternalChannelSizeUpdateInterval)
234246
select {
235247
case <-ctx.Done():
236-
if !timer.Stop() {
237-
select {
238-
case <-timer.C:
239-
default:
240-
}
241-
}
242248
return
243-
case <-timer.C:
244-
metricInternalChannelSize.Set(float64(len(ch)))
249+
case <-ticker.C:
250+
l := protoMessagesChan.Len()
251+
metrics.FSMChannelLength(l)
252+
metricInternalChannelSize.Set(float64(l))
245253
}
246254
}
247255
}

0 commit comments

Comments
 (0)