|
| 1 | +# TRON Prometheus Infrastructure Metrics |
| 2 | + |
| 3 | +**PR for [java-tron Issue #6590](https://github.com/tronprotocol/java-tron/issues/6590)** | |
| 4 | +**Penn Blockchain Conference Hackathon 2026 — TRON Bounty 2** |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## What This PR Does |
| 9 | + |
| 10 | +Adds two new Prometheus counters to java-tron's metrics system, enabling node operators to monitor chain health events that are otherwise invisible at the metrics layer. |
| 11 | + |
| 12 | +## New Metrics Reference |
| 13 | + |
| 14 | +| Metric Name | Type | Labels | Description | |
| 15 | +|-------------|------|--------|-------------| |
| 16 | +| `tron:block_empty_total` | Counter | `type="empty"` | Increments each time a block with zero transactions is applied to the chain | |
| 17 | +| `tron:sr_set_change_total` | Counter | `witness`, `change_type` | Increments when the Super Representative set changes during a maintenance period | |
| 18 | + |
| 19 | +### Label Values for `tron:sr_set_change_total` |
| 20 | + |
| 21 | +| Label | Value | Description | |
| 22 | +|-------|-------|-------------| |
| 23 | +| `change_type` | `added` | A new SR entered the active set | |
| 24 | +| `change_type` | `removed` | An existing SR left the active set | |
| 25 | +| `witness` | hex address | The SR address affected | |
| 26 | + |
| 27 | +## Setup Instructions |
| 28 | + |
| 29 | +### Enable Prometheus Metrics |
| 30 | + |
| 31 | +In your node's `config.conf`: |
| 32 | + |
| 33 | +```hocon |
| 34 | +node { |
| 35 | + metricsPrometheusEnable = true |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +Or via CLI flag: |
| 40 | + |
| 41 | +```bash |
| 42 | +java -jar FullNode.jar --metrics-prometheus-enable |
| 43 | +``` |
| 44 | + |
| 45 | +### Prometheus Endpoint |
| 46 | + |
| 47 | +When enabled, metrics are available at: |
| 48 | + |
| 49 | +``` |
| 50 | +http://localhost:9527/metrics |
| 51 | +``` |
| 52 | + |
| 53 | +### Prometheus Configuration |
| 54 | + |
| 55 | +Add to your `prometheus.yml`: |
| 56 | + |
| 57 | +```yaml |
| 58 | +scrape_configs: |
| 59 | + - job_name: 'tron-node' |
| 60 | + static_configs: |
| 61 | + - targets: ['localhost:9527'] |
| 62 | + metrics_path: '/metrics' |
| 63 | +``` |
| 64 | +
|
| 65 | +## PromQL Example Queries |
| 66 | +
|
| 67 | +### Empty Block Rate (per minute) |
| 68 | +
|
| 69 | +```promql |
| 70 | +rate(tron:block_empty_total[1m]) |
| 71 | +``` |
| 72 | + |
| 73 | +### Total Empty Blocks |
| 74 | + |
| 75 | +```promql |
| 76 | +tron:block_empty_total |
| 77 | +``` |
| 78 | + |
| 79 | +### SR Set Changes Over Time |
| 80 | + |
| 81 | +```promql |
| 82 | +rate(tron:sr_set_change_total[5m]) |
| 83 | +``` |
| 84 | + |
| 85 | +### SRs Added vs Removed |
| 86 | + |
| 87 | +```promql |
| 88 | +# Added |
| 89 | +sum by (change_type) (tron:sr_set_change_total{change_type="added"}) |
| 90 | +
|
| 91 | +# Removed |
| 92 | +sum by (change_type) (tron:sr_set_change_total{change_type="removed"}) |
| 93 | +``` |
| 94 | + |
| 95 | +### Alert: High Empty Block Rate |
| 96 | + |
| 97 | +```promql |
| 98 | +rate(tron:block_empty_total[5m]) > 10 |
| 99 | +``` |
| 100 | + |
| 101 | +### Alert: SR Set Changed |
| 102 | + |
| 103 | +```promql |
| 104 | +increase(tron:sr_set_change_total[1h]) > 0 |
| 105 | +``` |
| 106 | + |
| 107 | +## Files Modified |
| 108 | + |
| 109 | +| File | Change | |
| 110 | +|------|--------| |
| 111 | +| `common/src/main/java/org/tron/common/prometheus/MetricKeys.java` | Added `BLOCK_EMPTY` and `SR_SET_CHANGE` constants | |
| 112 | +| `common/src/main/java/org/tron/common/prometheus/MetricLabels.java` | Added `BLOCK_EMPTY`, `SR_ADDED`, `SR_REMOVED` label value constants | |
| 113 | +| `common/src/main/java/org/tron/common/prometheus/MetricsCounter.java` | Registered both counters with Prometheus | |
| 114 | +| `framework/src/main/java/org/tron/core/metrics/blockchain/BlockChainMetricManager.java` | Added empty block and SR set change detection in `applyBlock()` | |
| 115 | +| `framework/src/test/java/org/tron/core/metrics/prometheus/PrometheusApiServiceTest.java` | Added `testEmptyBlockMetric()` and `testSrSetChangeMetric()` tests | |
| 116 | + |
| 117 | +## Build & Test Commands |
| 118 | + |
| 119 | +```bash |
| 120 | +# Build without tests (fast) |
| 121 | +./gradlew clean build -x test |
| 122 | + |
| 123 | +# Compile modified source |
| 124 | +./gradlew :framework:compileJava :common:compileJava |
| 125 | + |
| 126 | +# Run Prometheus metric tests only |
| 127 | +./gradlew :framework:test --tests \ |
| 128 | + "org.tron.core.metrics.prometheus.PrometheusApiServiceTest" |
| 129 | + |
| 130 | +# Run all metrics tests |
| 131 | +./gradlew :framework:test --tests "org.tron.core.metrics.*" |
| 132 | + |
| 133 | +# Full test suite |
| 134 | +./gradlew test |
| 135 | + |
| 136 | +# Coverage report |
| 137 | +./gradlew :framework:jacocoTestReport |
| 138 | +# Report: framework/build/reports/jacoco/test/html/index.html |
| 139 | +``` |
| 140 | + |
| 141 | +## Implementation Details |
| 142 | + |
| 143 | +### Empty Block Detection |
| 144 | + |
| 145 | +In `BlockChainMetricManager.applyBlock()`, after the existing TPS counter logic: |
| 146 | + |
| 147 | +```java |
| 148 | +if (block.getTransactions().isEmpty()) { |
| 149 | + Metrics.counterInc(MetricKeys.Counter.BLOCK_EMPTY, 1, |
| 150 | + MetricLabels.Counter.BLOCK_EMPTY); |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +### SR Set Change Detection |
| 155 | + |
| 156 | +Compares the current active witness list against the previous set, emitting `added` and `removed` labels for each diff: |
| 157 | + |
| 158 | +```java |
| 159 | +List<ByteString> currentSrList = |
| 160 | + chainBaseManager.getWitnessScheduleStore().getActiveWitnesses(); |
| 161 | +Set<String> currentSrSet = currentSrList.stream() |
| 162 | + .map(bs -> Hex.toHexString(bs.toByteArray())) |
| 163 | + .collect(Collectors.toSet()); |
| 164 | + |
| 165 | +if (!previousSrSet.isEmpty() && !currentSrSet.equals(previousSrSet)) { |
| 166 | + for (String sr : Sets.difference(currentSrSet, previousSrSet)) { |
| 167 | + Metrics.counterInc(MetricKeys.Counter.SR_SET_CHANGE, 1, |
| 168 | + sr, MetricLabels.Counter.SR_ADDED); |
| 169 | + } |
| 170 | + for (String sr : Sets.difference(previousSrSet, currentSrSet)) { |
| 171 | + Metrics.counterInc(MetricKeys.Counter.SR_SET_CHANGE, 1, |
| 172 | + sr, MetricLabels.Counter.SR_REMOVED); |
| 173 | + } |
| 174 | +} |
| 175 | +previousSrSet = currentSrSet; |
| 176 | +``` |
| 177 | + |
| 178 | +### Code Style |
| 179 | + |
| 180 | +- Purely additive — zero protocol changes, zero API changes, zero backward compatibility issues |
| 181 | +- Uses existing `Metrics.counterInc()` pattern throughout |
| 182 | +- All constants defined in `MetricKeys.java` and `MetricLabels.java` (no hardcoded strings) |
| 183 | +- Java 8 compatible (no lambdas, no records) |
| 184 | +- No new Gradle dependencies |
| 185 | + |
| 186 | +## Related Issues |
| 187 | + |
| 188 | +- [java-tron #6590](https://github.com/tronprotocol/java-tron/issues/6590) — Prometheus metrics for empty blocks and SR changes |
0 commit comments