Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(common): truncation support for plain strings in the RPC log #9351

Merged
merged 1 commit into from
Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions google/cloud/internal/log_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ std::string DebugString(Status const& status, TracingOptions const& options) {
return std::move(os).str();
}

std::string RequestIdForLogging() {
static std::atomic<std::uint64_t> generator{0};
return std::to_string(++generator);
std::string DebugString(std::string s, TracingOptions const& options) {
std::size_t const pos = options.truncate_string_field_longer_than();
if (s.size() > pos) s.replace(pos, std::string::npos, "...<truncated>...");
return s;
}

char const* DebugFutureStatus(std::future_status status) {
Expand All @@ -123,6 +124,11 @@ char const* DebugFutureStatus(std::future_status status) {
return msg;
}

std::string RequestIdForLogging() {
static std::atomic<std::uint64_t> generator{0};
return std::to_string(++generator);
}

} // namespace internal
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace cloud
Expand Down
2 changes: 2 additions & 0 deletions google/cloud/internal/log_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ std::string DebugString(google::protobuf::Message const& m,

std::string DebugString(Status const& status, TracingOptions const& options);

std::string DebugString(std::string s, TracingOptions const& options);

char const* DebugFutureStatus(std::future_status s);

// Create a unique ID that can be used to match asynchronous requests/response
Expand Down
17 changes: 17 additions & 0 deletions google/cloud/internal/log_wrapper_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,23 @@ TEST(LogWrapper, Truncate) {
EXPECT_EQ(text, DebugString(MakeMutation(), tracing_options));
}

TEST(LogWrapper, TruncateString) {
TracingOptions tracing_options;
tracing_options.SetOptions("truncate_string_field_longer_than=8");
struct Case {
std::string actual;
std::string expected;
} cases[] = {
{"1234567", "1234567"},
{"12345678", "12345678"},
{"123456789", "12345678...<truncated>..."},
{"1234567890", "12345678...<truncated>..."},
};
for (auto const& c : cases) {
EXPECT_EQ(c.expected, DebugString(c.actual, tracing_options));
}
}

TEST(LogWrapper, FutureStatus) {
struct Case {
std::future_status actual;
Expand Down