Skip to content

Commit

Permalink
Merge pull request #58 from NodeFactoryIo/mmuftic/add-target-block-to…
Browse files Browse the repository at this point in the history
…-metrics

Add target block to metrics
  • Loading branch information
mace authored Jan 20, 2021
2 parents 20c7e84 + e16bc27 commit 1000367
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=0.3.1
version=0.3.2
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
# Changelog

## [v0.3.2](https://github.com/NodeFactoryIo/vedran-daemon/tree/v0.3.2)

[Full Changelog](https://github.com/NodeFactoryIo/vedran-daemon/compare/v0.3.1...v0.3.2)

### Fixed
- Add target block to metrics [\#58](https://github.com/NodeFactoryIo/vedran-daemon/pull/58) ([MakMuftic](https://github.com/MakMuftic))

### Added

### Changed

## [v0.3.1](https://github.com/NodeFactoryIo/vedran-daemon/tree/v0.3.1)

[Full Changelog](https://github.com/NodeFactoryIo/vedran-daemon/compare/v0.3.0...HEAD)
[Full Changelog](https://github.com/NodeFactoryIo/vedran-daemon/compare/v0.3.0...v0.3.1)

### Fixed
- Fix displaying configuration [\#53](https://github.com/NodeFactoryIo/vedran-daemon/pull/53) ([MakMuftic](https://github.com/MakMuftic))
Expand Down
15 changes: 13 additions & 2 deletions internal/node/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Metrics struct {
PeerCount *float64 `json:"peer_count"`
BestBlockHeight *float64 `json:"best_block_height"`
FinalizedBlockHeight *float64 `json:"finalized_block_height"`
TargetBlockHeight *float64 `json:"target_block_height"`
ReadyTransactionCount *float64 `json:"ready_transaction_count"`
}

Expand All @@ -36,10 +37,20 @@ func (client *client) GetMetrics() (*Metrics, error) {
return nil, err
}

blockMetrics := metricFamilies["polkadot_block_height"].GetMetric()
bestBlockHeight := blockMetrics[0].Gauge.Value
finalizedBlockHeight := blockMetrics[1].Gauge.Value
targetBlockHeight := bestBlockHeight
// if node not synced there is additional metric - target block
if len(blockMetrics) == 3 {
targetBlockHeight = blockMetrics[2].Gauge.Value
}

metrics := &Metrics{
metricFamilies["polkadot_sync_peers"].GetMetric()[0].Gauge.Value,
metricFamilies["polkadot_block_height"].GetMetric()[0].Gauge.Value,
metricFamilies["polkadot_block_height"].GetMetric()[1].Gauge.Value,
bestBlockHeight,
finalizedBlockHeight,
targetBlockHeight,
metricFamilies["polkadot_ready_transactions_number"].GetMetric()[0].Gauge.Value,
}
return metrics, nil
Expand Down
36 changes: 34 additions & 2 deletions internal/node/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestGetMetrics(t *testing.T) {
peerCount := float64(19)
bestBlockHeight := float64(432933)
finalizedBlockHeight := float64(432640)
targetBlockHeight := float64(1547694)
readyTransactionCount := float64(0)
tests := []Test{
{
Expand Down Expand Up @@ -54,12 +55,13 @@ func TestGetMetrics(t *testing.T) {
_, _ = io.WriteString(w, `invalid`)
}},
{
name: "Returns metrics if prometheus response valid",
name: "Returns metrics if prometheus response valid, not synced",
args: args{"valid"},
want: &Metrics{
PeerCount: &peerCount,
BestBlockHeight: &bestBlockHeight,
FinalizedBlockHeight: &finalizedBlockHeight,
TargetBlockHeight: &targetBlockHeight,
ReadyTransactionCount: &readyTransactionCount,
},
wantErr: false,
Expand All @@ -80,7 +82,37 @@ func TestGetMetrics(t *testing.T) {
# TYPE polkadot_ready_transactions_number gauge
polkadot_ready_transactions_number 0
`)
}},
},
},
{
name: "Returns metrics if prometheus response valid, synced",
args: args{"valid"},
want: &Metrics{
PeerCount: &peerCount,
BestBlockHeight: &bestBlockHeight,
FinalizedBlockHeight: &finalizedBlockHeight,
TargetBlockHeight: &bestBlockHeight,
ReadyTransactionCount: &readyTransactionCount,
},
wantErr: false,
handleFunc: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
_, _ = io.WriteString(
w,
`
# HELP polkadot_sync_peers Number of peers we sync with
# TYPE polkadot_sync_peers gauge
polkadot_sync_peers 19
# HELP polkadot_block_height Block height info of the chain
# TYPE polkadot_block_height gauge
polkadot_block_height{status="best"} 432933
polkadot_block_height{status="finalized"} 432640
# HELP polkadot_ready_transactions_number Number of transactions in the ready queue
# TYPE polkadot_ready_transactions_number gauge
polkadot_ready_transactions_number 0
`)
},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 1000367

Please sign in to comment.