Skip to content
This repository was archived by the owner on Jul 13, 2022. It is now read-only.

Commit b1cc4f9

Browse files
authored
Update metrics (#525)
- Adds additional metrics from utils package - Migrate docs to separate markdown file
1 parent 4b2d5e6 commit b1cc4f9

File tree

5 files changed

+54
-14
lines changed

5 files changed

+54
-14
lines changed

README.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,7 @@ For testing purposes, chainbridge provides 5 test keys. The can be used with `--
9797

9898
## Metrics
9999

100-
Basic metrics and a health status check can be enabled with the `--metrics` flag (default port `8001`, use `--metricsPort` to specify).
101-
102-
The endpoint `/health` will return the current block height and a timestamp of when it was processed. If the timestamp is at least 120 seconds old an error will be returned.
103-
104-
Prometheus metrics are served on `/metrics`.
100+
See [metrics.md](/docs/metrics.md).
105101

106102
# Chain Implementations
107103

chains/ethereum/listener.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ func (l *listener) pollBlocks() error {
114114
continue
115115
}
116116

117+
if l.metrics != nil {
118+
l.metrics.LatestKnownBlock.Set(float64(latestBlock.Int64()))
119+
}
120+
117121
// Sleep if the difference is less than BlockDelay; (latest - current) < BlockDelay
118122
if big.NewInt(0).Sub(latestBlock, currentBlock).Cmp(BlockDelay) == -1 {
119123
l.log.Debug("Block not ready, will retry", "target", currentBlock, "latest", latestBlock)
@@ -135,14 +139,17 @@ func (l *listener) pollBlocks() error {
135139
l.log.Error("Failed to write latest block to blockstore", "block", currentBlock, "err", err)
136140
}
137141

138-
// Goto next block and reset retry counter
139-
currentBlock.Add(currentBlock, big.NewInt(1))
140-
l.latestBlock.Height = big.NewInt(0).Set(latestBlock)
141-
l.latestBlock.LastUpdated = time.Now()
142-
retry = BlockRetryLimit
143142
if l.metrics != nil {
144143
l.metrics.BlocksProcessed.Inc()
144+
l.metrics.LatestProcessedBlock.Set(float64(latestBlock.Int64()))
145145
}
146+
147+
l.latestBlock.Height = big.NewInt(0).Set(latestBlock)
148+
l.latestBlock.LastUpdated = time.Now()
149+
150+
// Goto next block and reset retry counter
151+
currentBlock.Add(currentBlock, big.NewInt(1))
152+
retry = BlockRetryLimit
146153
}
147154
}
148155
}

chains/substrate/listener.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ func (l *listener) pollBlocks() error {
131131
continue
132132
}
133133

134+
if l.metrics != nil {
135+
l.metrics.LatestKnownBlock.Set(float64(finalizedHeader.Number))
136+
}
137+
134138
// Sleep if the block we want comes after the most recently finalized block
135139
if currentBlock > uint64(finalizedHeader.Number) {
136140
l.log.Trace("Block not yet finalized", "target", currentBlock, "latest", finalizedHeader.Number)
@@ -163,13 +167,15 @@ func (l *listener) pollBlocks() error {
163167
l.log.Error("Failed to write to blockstore", "err", err)
164168
}
165169

170+
if l.metrics != nil {
171+
l.metrics.BlocksProcessed.Inc()
172+
l.metrics.LatestProcessedBlock.Set(float64(currentBlock))
173+
}
174+
166175
currentBlock++
167176
l.latestBlock.Height = big.NewInt(0).SetUint64(currentBlock)
168177
l.latestBlock.LastUpdated = time.Now()
169178
retry = BlockRetryLimit
170-
if l.metrics != nil {
171-
l.metrics.BlocksProcessed.Inc()
172-
}
173179
}
174180
}
175181
}

docs/metrics.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Metrics
2+
3+
Basic metrics and a health status check can be enabled with the `--metrics` flag (default port `8001`, use `--metricsPort` to specify).
4+
5+
## Prometheus
6+
Prometheus metrics are served on `/metrics`. For each chain that exists, this provides:
7+
- `<chain>_blocks_processed`: the number of blocks processed by the chains listener.
8+
- `<chain>_latest_processed_block`: most recent block that has been processed by the listener.
9+
- `<chain>_latest_known_block`: most recent block that exists on the chain.
10+
- `<chain>_votes_submitted`: number of votes submitted by the relayer.
11+
12+
## Health Check
13+
The endpoint `/health` will return the current known block height, and a timestamp of when it was first seen for every chain:
14+
```json
15+
{
16+
"chains": [
17+
{
18+
"chainId": "Number",
19+
"height": "Number",
20+
"lastUpdated": "Date"
21+
}
22+
]
23+
}
24+
```
25+
26+
If the timestamp is at least 120 seconds old an error will be returned instead:
27+
```json
28+
{
29+
"error": "String"
30+
}
31+
```

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.13
44

55
require (
66
github.com/ChainSafe/chainbridge-substrate-events v0.0.0-20200715141113-87198532025e
7-
github.com/ChainSafe/chainbridge-utils v1.0.2
7+
github.com/ChainSafe/chainbridge-utils v1.0.3
88
github.com/ChainSafe/log15 v1.0.0
99
github.com/aristanetworks/goarista v0.0.0-20200609010056-95bcf8053598 // indirect
1010
github.com/btcsuite/btcd v0.20.1-beta // indirect

0 commit comments

Comments
 (0)