Skip to content

Commit 70514a5

Browse files
committed
feat(metrics): add block transaction count histogram for empty block monitoring
Replace the dedicated tron:block_empty_total counter with a more comprehensive tron:block_transaction_count histogram that tracks the distribution of transaction counts per block. Changes: - Add overloaded init() method in MetricsHistogram to support custom buckets - Add BLOCK_TRANSACTION_COUNT histogram with buckets [0, 10, 50, 100, 200, 500, 1000, 2000, 5000, 10000] - Record transaction count for all blocks (including empty blocks with txCount=0) - Empty blocks can be queried via bucket le=0.0 - Remove unused BLOCK_EMPTY counter - Keep SR_SET_CHANGE counter for SR set change monitoring This provides richer insights for network analysis while still supporting empty block monitoring via histogram bucket queries. Closes #6590
1 parent 69fe11b commit 70514a5

5 files changed

Lines changed: 29 additions & 17 deletions

File tree

common/src/main/java/org/tron/common/prometheus/MetricKeys.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public static class Counter {
1717
public static final String P2P_ERROR = "tron:p2p_error";
1818
public static final String P2P_DISCONNECT = "tron:p2p_disconnect";
1919
public static final String INTERNAL_SERVICE_FAIL = "tron:internal_service_fail";
20-
public static final String BLOCK_EMPTY = "tron:block_empty_total";
2120
public static final String SR_SET_CHANGE = "tron:sr_set_change_total";
2221

2322
private Counter() {
@@ -64,6 +63,7 @@ public static class Histogram {
6463
public static final String MESSAGE_PROCESS_LATENCY = "tron:message_process_latency_seconds";
6564
public static final String BLOCK_FETCH_LATENCY = "tron:block_fetch_latency_seconds";
6665
public static final String BLOCK_RECEIVE_DELAY = "tron:block_receive_delay_seconds";
66+
public static final String BLOCK_TRANSACTION_COUNT = "tron:block_transaction_count";
6767

6868
private Histogram() {
6969
throw new IllegalStateException("Histogram");

common/src/main/java/org/tron/common/prometheus/MetricsCounter.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ class MetricsCounter {
1818
init(MetricKeys.Counter.P2P_DISCONNECT, "tron p2p disconnect .", "type");
1919
init(MetricKeys.Counter.INTERNAL_SERVICE_FAIL, "internal Service fail.",
2020
"class", "method");
21-
init(MetricKeys.Counter.BLOCK_EMPTY,
22-
"Total number of empty blocks (no transactions).",
23-
"type");
2421
init(MetricKeys.Counter.SR_SET_CHANGE,
2522
"Total SR set changes during maintenance periods.",
2623
"witness", "change_type");

common/src/main/java/org/tron/common/prometheus/MetricsHistogram.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public class MetricsHistogram {
4848
init(MetricKeys.Histogram.BLOCK_FETCH_LATENCY, "fetch block latency.");
4949
init(MetricKeys.Histogram.BLOCK_RECEIVE_DELAY,
5050
"receive block delay time, receiveTime - blockTime.");
51+
init(MetricKeys.Histogram.BLOCK_TRANSACTION_COUNT,
52+
"Distribution of transaction counts per block.",
53+
new double[]{0, 10, 50, 100, 200, 500, 1000, 2000, 5000, 10000},
54+
"miner");
5155
}
5256

5357
private MetricsHistogram() {
@@ -62,6 +66,17 @@ private static void init(String name, String help, String... labels) {
6266
.register());
6367
}
6468

69+
private static void init(String name, String help, double[] buckets, String... labels) {
70+
Histogram.Builder builder = Histogram.build()
71+
.name(name)
72+
.help(help)
73+
.labelNames(labels);
74+
if (buckets != null && buckets.length > 0) {
75+
builder.buckets(buckets);
76+
}
77+
container.put(name, builder.register());
78+
}
79+
6580
static Histogram.Timer startTimer(String key, String... labels) {
6681
if (Metrics.enabled()) {
6782
Histogram histogram = container.get(key);

framework/src/main/java/org/tron/core/metrics/blockchain/BlockChainMetricManager.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,10 @@ public void applyBlock(BlockCapsule block) {
175175
MetricLabels.Counter.TXS_SUCCESS, MetricLabels.Counter.TXS_SUCCESS);
176176
}
177177

178-
// Empty block detection
179-
if (block.getTransactions().isEmpty()) {
180-
Metrics.counterInc(MetricKeys.Counter.BLOCK_EMPTY, 1,
181-
MetricLabels.Counter.BLOCK_EMPTY);
182-
}
178+
// Record transaction count distribution for all blocks (including empty blocks)
179+
int txCount = block.getTransactions().size();
180+
Metrics.histogramObserve(MetricKeys.Histogram.BLOCK_TRANSACTION_COUNT, txCount,
181+
StringUtil.encode58Check(address));
183182

184183
// SR set change detection
185184
List<ByteString> currentSrList =

framework/src/test/java/org/tron/core/metrics/prometheus/PrometheusApiServiceTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ private BlockCapsule createTestBlockCapsule(long time,
177177
@Test
178178
public void testEmptyBlockMetric() throws Exception {
179179
ECKey ecKey = ECKey.fromPrivate(privateKey);
180+
String minerBase58 = org.tron.common.utils.StringUtil.encode58Check(ecKey.getAddress());
180181
ByteString witnessAddress = ByteString.copyFrom(ecKey.getAddress());
181182

182183
chainBaseManager.getWitnessScheduleStore().saveActiveWitnesses(new ArrayList<>());
@@ -192,19 +193,19 @@ public void testEmptyBlockMetric() throws Exception {
192193
block.sign(privateKey);
193194

194195
Double beforeValue = CollectorRegistry.defaultRegistry.getSampleValue(
195-
"tron:block_empty_total",
196-
new String[]{"type"},
197-
new String[]{"empty"});
196+
"tron:block_transaction_count_bucket",
197+
new String[]{"miner", "le"},
198+
new String[]{minerBase58, "0.0"});
198199
double before = beforeValue == null ? 0.0 : beforeValue;
199200

200201
blockChainMetricManager.applyBlock(block);
201202

202203
Double afterValue = CollectorRegistry.defaultRegistry.getSampleValue(
203-
"tron:block_empty_total",
204-
new String[]{"type"},
205-
new String[]{"empty"});
206-
Assert.assertNotNull("tron:block_empty_total counter should exist", afterValue);
207-
Assert.assertEquals("Counter should have incremented by 1",
204+
"tron:block_transaction_count_bucket",
205+
new String[]{"miner", "le"},
206+
new String[]{minerBase58, "0.0"});
207+
Assert.assertNotNull("Empty block bucket should exist for miner: " + minerBase58, afterValue);
208+
Assert.assertEquals("Histogram bucket le=0.0 should have incremented by 1",
208209
before + 1.0, afterValue, 0.001);
209210
}
210211

0 commit comments

Comments
 (0)