Skip to content

Commit bd9e39e

Browse files
authored
fix: Fix incorrect requests logging (#2005)
Fixes #2004.
1 parent 46514c8 commit bd9e39e

File tree

5 files changed

+88
-12
lines changed

5 files changed

+88
-12
lines changed

src/rpc/RPCHelpers.hpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "rpc/Errors.hpp"
3131
#include "rpc/common/Types.hpp"
3232
#include "util/JsonUtils.hpp"
33+
#include "util/Taggable.hpp"
3334
#include "util/log/Logger.hpp"
3435
#include "web/Context.hpp"
3536

@@ -744,12 +745,13 @@ decodeCTID(T const ctid) noexcept
744745
* @brief Log the duration of the request processing
745746
*
746747
* @tparam T The type of the duration
747-
* @param ctx The context of the request
748+
* @param request The request to log
749+
* @param tag The tag of the context of the request
748750
* @param dur The duration to log
749751
*/
750-
template <typename T>
752+
template <typename DurationType>
751753
void
752-
logDuration(web::Context const& ctx, T const& dur)
754+
logDuration(boost::json::object const& request, util::BaseTagDecorator const& tag, DurationType const& dur)
753755
{
754756
using boost::json::serialize;
755757

@@ -759,15 +761,15 @@ logDuration(web::Context const& ctx, T const& dur)
759761
auto const millis = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();
760762
auto const seconds = std::chrono::duration_cast<std::chrono::seconds>(dur).count();
761763
auto const msg = fmt::format(
762-
"Request processing duration = {} milliseconds. request = {}", millis, serialize(util::removeSecret(ctx.params))
764+
"Request processing duration = {} milliseconds. request = {}", millis, serialize(util::removeSecret(request))
763765
);
764766

765767
if (seconds > kDURATION_ERROR_THRESHOLD_SECONDS) {
766-
LOG(log.error()) << ctx.tag() << msg;
768+
LOG(log.error()) << tag << msg;
767769
} else if (seconds > 1) {
768-
LOG(log.warn()) << ctx.tag() << msg;
770+
LOG(log.warn()) << tag << msg;
769771
} else
770-
LOG(log.info()) << ctx.tag() << msg;
772+
LOG(log.info()) << tag << msg;
771773
}
772774

773775
/**

src/web/RPCServerHandler.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ class RPCServerHandler {
196196

197197
auto [result, timeDiff] = util::timed([&]() { return rpcEngine_->buildResponse(*context); });
198198

199-
auto us = std::chrono::duration<int, std::milli>(timeDiff);
200-
rpc::logDuration(*context, us);
199+
auto const us = std::chrono::duration<int, std::milli>(timeDiff);
200+
rpc::logDuration(request, context->tag(), us);
201201

202202
boost::json::object response;
203203

src/web/ng/RPCServerHandler.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ class RPCServerHandler {
241241
auto [result, timeDiff] = util::timed([&]() { return rpcEngine_->buildResponse(*context); });
242242

243243
auto us = std::chrono::duration<int, std::milli>(timeDiff);
244-
rpc::logDuration(*context, us);
244+
rpc::logDuration(request, context->tag(), us);
245245

246246
boost::json::object response;
247247

tests/common/util/NameGenerator.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
#pragma once
2121

22-
#include <string>
23-
2422
namespace tests::util {
2523

2624
static auto const kNAME_GENERATOR = [](auto const& info) { return info.param.testName; };

tests/unit/rpc/RPCHelpersTests.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,21 @@
2424
#include "rpc/RPCHelpers.hpp"
2525
#include "rpc/common/Types.hpp"
2626
#include "util/AsioContextTestFixture.hpp"
27+
#include "util/LoggerFixtures.hpp"
2728
#include "util/MockAmendmentCenter.hpp"
2829
#include "util/MockBackendTestFixture.hpp"
2930
#include "util/MockPrometheus.hpp"
3031
#include "util/NameGenerator.hpp"
32+
#include "util/Taggable.hpp"
3133
#include "util/TestObject.hpp"
34+
#include "util/newconfig/ConfigDefinition.hpp"
35+
#include "util/newconfig/ConfigValue.hpp"
36+
#include "util/newconfig/Types.hpp"
3237

3338
#include <boost/asio/impl/spawn.hpp>
3439
#include <boost/asio/spawn.hpp>
3540
#include <boost/json/array.hpp>
41+
#include <boost/json/object.hpp>
3642
#include <boost/json/parse.hpp>
3743
#include <fmt/core.h>
3844
#include <gmock/gmock.h>
@@ -48,9 +54,11 @@
4854
#include <xrpl/protocol/jss.h>
4955

5056
#include <array>
57+
#include <chrono>
5158
#include <cstddef>
5259
#include <cstdint>
5360
#include <optional>
61+
#include <sstream>
5462
#include <stdexcept>
5563
#include <string>
5664
#include <string_view>
@@ -1166,3 +1174,71 @@ TEST_P(IsAdminCmdParameterTest, Test)
11661174
auto const testBundle = GetParam();
11671175
EXPECT_EQ(isAdminCmd(testBundle.method, boost::json::parse(testBundle.testJson).as_object()), testBundle.expected);
11681176
}
1177+
1178+
struct RPCHelpersLogDurationTestBundle {
1179+
std::string testName;
1180+
std::chrono::milliseconds duration;
1181+
std::string expectedLogLevel;
1182+
bool expectDuration;
1183+
};
1184+
1185+
struct RPCHelpersLogDurationTest : LoggerFixture, testing::WithParamInterface<RPCHelpersLogDurationTestBundle> {
1186+
boost::json::object const request = {
1187+
{"method", "account_info"},
1188+
{"params",
1189+
boost::json::array{
1190+
boost::json::object{{"account", "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh"}, {"secret", "should be deleted"}}
1191+
}}
1192+
};
1193+
util::TagDecoratorFactory tagFactory{util::config::ClioConfigDefinition{
1194+
{"log_tag_style", util::config::ConfigValue{util::config::ConfigType::String}.defaultValue("none")}
1195+
}};
1196+
struct DummyTaggable : util::Taggable {
1197+
DummyTaggable(util::TagDecoratorFactory& f) : util::Taggable(f)
1198+
{
1199+
}
1200+
};
1201+
DummyTaggable taggable{tagFactory};
1202+
};
1203+
1204+
TEST_P(RPCHelpersLogDurationTest, LogDuration)
1205+
{
1206+
auto const& tag = taggable.tag();
1207+
// TOOD: Update in https://github.com/XRPLF/clio/issues/2008
1208+
auto const tagStr = [&tag]() {
1209+
std::stringstream ss;
1210+
ss << tag;
1211+
return ss.str();
1212+
}();
1213+
1214+
logDuration(request, tag, GetParam().duration);
1215+
1216+
std::string const output = getLoggerString();
1217+
1218+
EXPECT_NE(output.find(GetParam().expectedLogLevel), std::string::npos) << output;
1219+
EXPECT_NE(output.find(tagStr), std::string::npos);
1220+
1221+
if (GetParam().expectDuration) {
1222+
std::string durationStr = std::to_string(GetParam().duration.count()) + " milliseconds";
1223+
EXPECT_NE(output.find(durationStr), std::string::npos);
1224+
}
1225+
1226+
EXPECT_NE(output.find("account_info"), std::string::npos);
1227+
EXPECT_EQ(output.find("should be deleted"), std::string::npos);
1228+
}
1229+
1230+
INSTANTIATE_TEST_SUITE_P(
1231+
RPCHelpersLogDurationTests,
1232+
RPCHelpersLogDurationTest,
1233+
testing::Values(
1234+
RPCHelpersLogDurationTestBundle{"ShortDurationLogsAsInfo", std::chrono::milliseconds(500), "RPC:NFO", true},
1235+
RPCHelpersLogDurationTestBundle{
1236+
"MediumDurationLogsAsWarning",
1237+
std::chrono::milliseconds(5000),
1238+
"RPC:WRN",
1239+
true
1240+
},
1241+
RPCHelpersLogDurationTestBundle{"LongDurationLogsAsError", std::chrono::milliseconds(15000), "RPC:ERR", true}
1242+
),
1243+
tests::util::kNAME_GENERATOR
1244+
);

0 commit comments

Comments
 (0)