Skip to content

Commit fcb4159

Browse files
authored
feat: dot/telemetry: Implement basic telemetry connection (#1497)
* implement telemetry connection server * add telemetry for import block * setup telemetry to use node name from Global config * init telemetry connections based on genesis.json values * fix error message formatting * add tests for telemetry * implement no-telemetry cli flag * fix typos * cleanup error check * create TelemetryEndpoint struct * created connection data struct to hold connection data * make TelemetryEndpoints a pointer reference * add tests for interfaceToTelemetryEndpoint * update test to fix random name
1 parent b7df8ed commit fcb4159

File tree

18 files changed

+409
-49
lines changed

18 files changed

+409
-49
lines changed

cmd/gossamer/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ func setDotGlobalConfig(ctx *cli.Context, tomlCfg *ctoml.Config, cfg *dot.Global
407407
cfg.MetricsPort = tomlCfg.Global.MetricsPort
408408
}
409409

410+
// TODO: generate random name if one is not assigned (see issue #1496)
410411
// check --name flag and update node configuration
411412
if name := ctx.GlobalString(NameFlag.Name); name != "" {
412413
cfg.Name = name
@@ -442,6 +443,8 @@ func setDotGlobalConfig(ctx *cli.Context, tomlCfg *ctoml.Config, cfg *dot.Global
442443
cfg.MetricsPort = uint32(metricsPort)
443444
}
444445

446+
cfg.NoTelemetry = ctx.Bool("no-telemetry")
447+
445448
logger.Debug(
446449
"global configuration",
447450
"name", cfg.Name,

cmd/gossamer/config_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,20 @@ func TestGlobalConfigFromFlags(t *testing.T) {
222222
MetricsPort: uint32(9871),
223223
},
224224
},
225+
{
226+
"Test gossamer --no-telemetry",
227+
[]string{"config", "no-telemetry", "name"},
228+
[]interface{}{testCfgFile.Name(), true, testCfg.Global.Name},
229+
dot.GlobalConfig{
230+
Name: testCfg.Global.Name,
231+
ID: testCfg.Global.ID,
232+
BasePath: testCfg.Global.BasePath,
233+
LogLvl: log.LvlInfo,
234+
PublishMetrics: testCfg.Global.PublishMetrics,
235+
MetricsPort: testCfg.Global.MetricsPort,
236+
NoTelemetry: true,
237+
},
238+
},
225239
}
226240

227241
for _, c := range testcases {

cmd/gossamer/flags.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ var (
9898
Name: "metrics-port",
9999
Usage: "Set metric listening port ",
100100
}
101+
102+
// NoTelemetryFlag stops publishing telemetry to default defined in genesis.json
103+
NoTelemetryFlag = cli.BoolFlag{
104+
Name: "no-telemetry",
105+
Usage: "Disable connecting to the Substrate telemetry server",
106+
}
101107
)
102108

103109
// Initialization-only flags
@@ -295,6 +301,9 @@ var (
295301
// metrics flag
296302
PublishMetricsFlag,
297303
MetricsPortFlag,
304+
305+
// telemetry flags
306+
NoTelemetryFlag,
298307
}
299308
)
300309

dot/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type GlobalConfig struct {
5050
LogLvl log.Lvl
5151
PublishMetrics bool
5252
MetricsPort uint32
53+
NoTelemetry bool
5354
}
5455

5556
// LogConfig represents the log levels for individual packages

dot/node.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,23 @@ import (
2323
"os/signal"
2424
"path"
2525
"runtime/debug"
26+
"strconv"
2627
"sync"
2728
"syscall"
29+
"time"
2830

31+
"github.com/ChainSafe/chaindb"
2932
gssmrmetrics "github.com/ChainSafe/gossamer/dot/metrics"
3033
"github.com/ChainSafe/gossamer/dot/network"
3134
"github.com/ChainSafe/gossamer/dot/state"
35+
"github.com/ChainSafe/gossamer/dot/telemetry"
3236
"github.com/ChainSafe/gossamer/dot/types"
3337
"github.com/ChainSafe/gossamer/lib/genesis"
3438
"github.com/ChainSafe/gossamer/lib/keystore"
3539
"github.com/ChainSafe/gossamer/lib/services"
40+
log "github.com/ChainSafe/log15"
3641
"github.com/ethereum/go-ethereum/metrics"
3742
"github.com/ethereum/go-ethereum/metrics/prometheus"
38-
39-
"github.com/ChainSafe/chaindb"
40-
log "github.com/ChainSafe/log15"
4143
)
4244

4345
var logger = log.New("pkg", "dot")
@@ -306,6 +308,28 @@ func NewNode(cfg *Config, ks *keystore.GlobalKeystore, stopFunc func()) (*Node,
306308
publishMetrics(cfg)
307309
}
308310

311+
gd, err := stateSrvc.Storage.GetGenesisData()
312+
if err != nil {
313+
return nil, err
314+
}
315+
316+
if cfg.Global.NoTelemetry {
317+
return node, nil
318+
}
319+
320+
telemetry.GetInstance().AddConnections(gd.TelemetryEndpoints)
321+
data := &telemetry.ConnectionData{
322+
Authority: cfg.Core.GrandpaAuthority,
323+
Chain: sysSrvc.ChainName(),
324+
GenesisHash: stateSrvc.Block.GenesisHash().String(),
325+
SystemName: sysSrvc.SystemName(),
326+
NodeName: cfg.Global.Name,
327+
SystemVersion: sysSrvc.SystemVersion(),
328+
NetworkID: networkSrvc.NetworkState().PeerID,
329+
StartTime: strconv.FormatInt(time.Now().UnixNano(), 10),
330+
}
331+
telemetry.GetInstance().SendConnection(data)
332+
309333
return node, nil
310334
}
311335

dot/rpc/modules/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ type RuntimeAPI interface {
9090
type SystemAPI interface {
9191
SystemName() string
9292
SystemVersion() string
93-
NodeName() string
9493
Properties() map[string]interface{}
9594
ChainType() string
95+
ChainName() string
9696
}

dot/rpc/modules/system.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func NewSystemModule(net NetworkAPI, sys SystemAPI, core CoreAPI,
8383

8484
// Chain returns the runtime chain
8585
func (sm *SystemModule) Chain(r *http.Request, req *EmptyRequest, res *string) error {
86-
*res = sm.systemAPI.NodeName()
86+
*res = sm.systemAPI.ChainName()
8787
return nil
8888
}
8989

@@ -161,7 +161,7 @@ func (sm *SystemModule) NodeRoles(r *http.Request, req *EmptyRequest, res *[]int
161161
// AccountNextIndex Returns the next valid index (aka. nonce) for given account.
162162
func (sm *SystemModule) AccountNextIndex(r *http.Request, req *StringRequest, res *U64Response) error {
163163
if req == nil || len(req.String) == 0 {
164-
return errors.New("Account address must be valid")
164+
return errors.New("account address must be valid")
165165
}
166166
addressPubKey := crypto.PublicAddressToByteArray(common.Address(req.String))
167167

dot/rpc/modules/system_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,9 @@ func (api *mockSystemAPI) SystemVersion() string {
210210
return api.info.SystemVersion
211211
}
212212

213-
func (api *mockSystemAPI) NodeName() string {
213+
func (api *mockSystemAPI) ChainName() string {
214214
return api.genData.Name
215215
}
216-
217216
func (api *mockSystemAPI) Properties() map[string]interface{} {
218217
return nil
219218
}

dot/sync/syncer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ import (
2424
"os"
2525

2626
"github.com/ChainSafe/gossamer/dot/network"
27+
"github.com/ChainSafe/gossamer/dot/telemetry"
2728
"github.com/ChainSafe/gossamer/dot/types"
2829
"github.com/ChainSafe/gossamer/lib/blocktree"
2930
"github.com/ChainSafe/gossamer/lib/common"
3031
"github.com/ChainSafe/gossamer/lib/runtime"
3132
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
32-
3333
log "github.com/ChainSafe/log15"
3434
)
3535

@@ -339,6 +339,7 @@ func (s *Service) handleBlock(block *types.Block) error {
339339
}
340340
} else {
341341
logger.Debug("🔗 imported block", "number", block.Header.Number, "hash", block.Header.Hash())
342+
telemetry.GetInstance().SendBlockImport(block.Header.Hash().String(), block.Header.Number)
342343
}
343344

344345
// handle consensus digest for authority changes

dot/system/service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ func (s *Service) SystemVersion() string {
5050
return s.systemInfo.SystemVersion
5151
}
5252

53-
// NodeName returns the nodeName (chain name)
54-
func (s *Service) NodeName() string {
53+
// ChainName returns the chain name defined in genesis.json
54+
func (s *Service) ChainName() string {
5555
return s.genesisData.Name
5656
}
5757

0 commit comments

Comments
 (0)