@@ -13,11 +13,12 @@ import (
1313
1414 "github.com/textileio/powergate/util"
1515 "go.opencensus.io/stats"
16+ "go.opencensus.io/stats/view"
1617)
1718
1819var (
19- lotusSyncStatusInterval = time .Second * 10
20- log = logging .Logger ("lotus-client" )
20+ heightProbingInterval = time .Second * 10
21+ log = logging .Logger ("lotus-client" )
2122)
2223
2324// ClientBuilder creates a new Lotus client.
@@ -57,23 +58,29 @@ func NewBuilder(maddr ma.Multiaddr, authToken string, connRetries int) (ClientBu
5758 }, nil
5859}
5960
60- func monitorLotusSync (ctx context.Context , c * apistruct.FullNodeStruct ) {
61- refreshHeightMetric (ctx , c )
62- for {
63- select {
64- case <- ctx .Done ():
65- log .Debug ("closing lotus sync monitor" )
66- return
67- case <- time .After (lotusSyncStatusInterval ):
68- refreshHeightMetric (ctx , c )
69- }
61+ // MonitorLotusSync fires a goroutine that will generate
62+ // metrics with Lotus node height.
63+ func MonitorLotusSync (clientBuilder ClientBuilder ) {
64+ if err := view .Register (vHeight ); err != nil {
65+ log .Fatalf ("register metrics views: %v" , err )
7066 }
67+ go func () {
68+ for {
69+ refreshHeightMetric (clientBuilder )
70+ time .Sleep (heightProbingInterval )
71+ }
72+ }()
7173}
7274
73- func refreshHeightMetric (ctx context.Context , c * apistruct.FullNodeStruct ) {
74- heaviest , err := c .ChainHead (ctx )
75+ func refreshHeightMetric (clientBuilder ClientBuilder ) {
76+ c , cls , err := clientBuilder ()
77+ if err != nil {
78+ log .Error ("creating lotus client for monitoring: %s" , err )
79+ }
80+ defer cls ()
81+ heaviest , err := c .ChainHead (context .Background ())
7582 if err != nil {
76- log .Errorf ("error when getting lotus sync status: %s" , err )
83+ log .Errorf ("get lotus sync status: %s" , err )
7784 return
7885 }
7986 stats .Record (context .Background (), mLotusHeight .M (int64 (heaviest .Height ())))
0 commit comments