Skip to content

Commit 6315ffc

Browse files
committed
refactor(metrics): extract MINER_LABEL constant for histogram labels
Extract repeated 'miner' string literal into MINER_LABEL constant in MetricsHistogram to follow DRY principle and fix SonarQube warning. Also update PrometheusApiServiceTest to use MINER_LABEL constant for test assertions. Relates tronprotocol#6624
1 parent 39dd708 commit 6315ffc

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
public class MetricsHistogram {
1010

1111
private static final Map<String, Histogram> container = new ConcurrentHashMap<>();
12+
private static final String MINER_LABEL = "miner";
1213

1314
static {
1415
init(MetricKeys.Histogram.INTERNAL_SERVICE_LATENCY, "Internal Service latency.",
@@ -20,7 +21,7 @@ public class MetricsHistogram {
2021
init(MetricKeys.Histogram.JSONRPC_SERVICE_LATENCY, "JsonRpc Service latency.",
2122
"method");
2223
init(MetricKeys.Histogram.MINER_LATENCY, "miner latency.",
23-
"miner");
24+
MINER_LABEL);
2425
init(MetricKeys.Histogram.PING_PONG_LATENCY, "node ping pong latency.");
2526
init(MetricKeys.Histogram.VERIFY_SIGN_LATENCY, "verify sign latency for trx , block.",
2627
"type");
@@ -36,7 +37,7 @@ public class MetricsHistogram {
3637
init(MetricKeys.Histogram.PROCESS_TRANSACTION_LATENCY, "process transaction latency.",
3738
"type", "contract");
3839
init(MetricKeys.Histogram.MINER_DELAY, "miner delay time, actualTime - planTime.",
39-
"miner");
40+
MINER_LABEL);
4041
init(MetricKeys.Histogram.UDP_BYTES, "udp_bytes traffic.",
4142
"type");
4243
init(MetricKeys.Histogram.TCP_BYTES, "tcp_bytes traffic.",
@@ -52,7 +53,7 @@ public class MetricsHistogram {
5253
init(MetricKeys.Histogram.BLOCK_TRANSACTION_COUNT,
5354
"Distribution of transaction counts per block.",
5455
new double[]{0, 10, 50, 100, 200, 500, 1000, 2000, 5000, 10000},
55-
"miner");
56+
MINER_LABEL);
5657
}
5758

5859
private MetricsHistogram() {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040

4141
@Slf4j(topic = "metric")
4242
public class PrometheusApiServiceTest extends BaseTest {
43+
44+
private static final String MINER_LABEL = "miner";
45+
4346
static LocalDateTime localDateTime = LocalDateTime.now();
4447
@Resource
4548
private DposSlot dposSlot;
@@ -87,7 +90,7 @@ protected void check(byte[] address, Map<ByteString, String> witnessAndAccount)
8790
// Query histogram bucket le="0.0" for empty blocks
8891
Double emptyBlock = CollectorRegistry.defaultRegistry.getSampleValue(
8992
"tron:block_transaction_count_bucket",
90-
new String[] {"miner", "le"}, new String[] {minerBase58, "0.0"});
93+
new String[] {MINER_LABEL, "le"}, new String[] {minerBase58, "0.0"});
9194

9295
Assert.assertNotNull("Empty block bucket should exist for miner: " + minerBase58, emptyBlock);
9396
Assert.assertEquals("Should have 1 empty block", 1, emptyBlock.intValue());
@@ -125,7 +128,7 @@ protected void check(byte[] address, Map<ByteString, String> witnessAndAccount)
125128
// Collect empty blocks count from histogram bucket
126129
Double witnessEmptyBlock = CollectorRegistry.defaultRegistry.getSampleValue(
127130
"tron:block_transaction_count_bucket",
128-
new String[] {"miner", "le"}, new String[] {witnessBase58, "0.0"});
131+
new String[] {MINER_LABEL, "le"}, new String[] {witnessBase58, "0.0"});
129132
Assert.assertNotNull("Empty block bucket should exist for witness: " + witnessBase58,
130133
witnessEmptyBlock);
131134
totalNewWitnessEmptyBlocks += witnessEmptyBlock;

0 commit comments

Comments
 (0)