Skip to content

Commit cd5b36f

Browse files
committed
Add a counter for requests containing ledger_hash
1 parent 63a7ff7 commit cd5b36f

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010
.sanitizer-report
1111
CMakeUserPresets.json
1212
config.json
13+
CLAUDE.md
14+
.claude/**

src/rpc/Counters.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ Counters::Counters(Reportable const& wq)
152152
"Age of requested ledgers in ledger count"
153153
)
154154
)
155+
, ledgerHashRequestsCounter_(
156+
PrometheusService::counterInt(
157+
"rpc_ledger_hash_requests_total_number",
158+
Labels{},
159+
"Total number of successful requests containing ledger_hash field"
160+
)
161+
)
155162
, workQueue_(std::cref(wq))
156163
, startupTime_{std::chrono::system_clock::now()}
157164
{
@@ -234,7 +241,12 @@ Counters::onInternalError()
234241
void
235242
Counters::recordLedgerRequest(boost::json::object const& params, std::uint32_t currentLedgerSequence)
236243
{
237-
if (not params.contains("ledger_index")) {
244+
if (params.contains(JS(ledger_hash))) {
245+
++ledgerHashRequestsCounter_.get();
246+
return;
247+
}
248+
249+
if (not params.contains(JS(ledger_index))) {
238250
ledgerAgeLedgersHistogram_.get().observe(0);
239251
return;
240252
}

src/rpc/Counters.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class Counters {
6969
CounterType internalErrorCounter_;
7070

7171
std::reference_wrapper<util::prometheus::HistogramInt> ledgerAgeLedgersHistogram_;
72+
CounterType ledgerHashRequestsCounter_;
7273

7374
std::reference_wrapper<Reportable const> workQueue_;
7475
std::chrono::time_point<std::chrono::system_clock> startupTime_;

tests/unit/rpc/CountersTests.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,28 @@ TEST_F(RPCCountersMockPrometheusRecotdLedgerRequestTest, zeroAgeLedger)
259259
params["ledger_index"] = 1000; // Same as current
260260
counters.recordLedgerRequest(params, 1000);
261261
}
262+
263+
TEST_F(RPCCountersMockPrometheusRecotdLedgerRequestTest, ledgerHashRequest)
264+
{
265+
auto& ledgerHashCounterMock = makeMock<CounterInt>("rpc_ledger_hash_requests_total_number", "");
266+
267+
EXPECT_CALL(ledgerHashCounterMock, add(1));
268+
269+
boost::json::object params;
270+
params["ledger_hash"] = "ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789";
271+
counters.recordLedgerRequest(params, 1000);
272+
}
273+
274+
TEST_F(RPCCountersMockPrometheusRecotdLedgerRequestTest, ledgerHashWithIndexIgnoresIndex)
275+
{
276+
auto& ledgerHashCounterMock = makeMock<CounterInt>("rpc_ledger_hash_requests_total_number", "");
277+
278+
// When both ledger_hash and ledger_index are present, only ledger_hash counter should be incremented
279+
EXPECT_CALL(ledgerHashCounterMock, add(1));
280+
// No histogram call should be made
281+
282+
boost::json::object params;
283+
params["ledger_hash"] = "ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789";
284+
params["ledger_index"] = 900; // This should be ignored
285+
counters.recordLedgerRequest(params, 1000);
286+
}

0 commit comments

Comments
 (0)