Skip to content

Commit

Permalink
feat: Put server logs in the response DebugInfo for consented requests
Browse files Browse the repository at this point in the history
Bug: b/365964575
Change-Id: I0620363268a898fe6eedf3e9499444f2d7c289a5
GitOrigin-RevId: b38d265eaa9072fbe8e9d2c13d859bfad5da5351
  • Loading branch information
emmafu2022 authored and copybara-github committed Sep 30, 2024
1 parent c2275d6 commit 6dd724d
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,9 @@ grpc::Status GetValuesV2Handler::GetValues(
const V2EncoderDecoder& v2_codec) const {
PS_VLOG(9) << "Update log context " << request.log_context() << ";"
<< request.consented_debug_config();
request_context_factory.UpdateLogContext(request.log_context(),
request.consented_debug_config());
request_context_factory.UpdateLogContext(
request.log_context(), request.consented_debug_config(),
[response]() { return response->mutable_debug_info(); });
if (request.partitions().empty()) {
return grpc::Status(StatusCode::INTERNAL,
"At least 1 partition is required");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,13 @@ INSTANTIATE_TEST_SUITE_P(
.core_request_body = kv_server::
kConsentedV2RequestMultiplePartitionsWithLogContextInJson,
.is_consented = true,
},
TestingParameters{
.protocol_type = ProtocolType::kObliviousHttp,
.content_type = kContentEncodingCborHeaderValue,
.core_request_body = kv_server::
kConsentedV2RequestMultiPartWithDebugInfoResponseInJson,
.is_consented = true,
}));

TEST_P(GetValuesHandlerTest, Success) {
Expand Down
31 changes: 24 additions & 7 deletions components/util/request_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ RequestLogContext& RequestContext::GetRequestLogContext() const {
void RequestContext::UpdateLogContext(
const privacy_sandbox::server_common::LogContext& log_context,
const privacy_sandbox::server_common::ConsentedDebugConfiguration&
consented_debug_config) {
request_log_context_ =
std::make_unique<RequestLogContext>(log_context, consented_debug_config);
consented_debug_config,
std::optional<
absl::AnyInvocable<privacy_sandbox::server_common::DebugInfo*()>>
debug_info_opt) {
request_log_context_ = std::make_unique<RequestLogContext>(
log_context, consented_debug_config, std::move(debug_info_opt));
if (request_log_context_->GetRequestLoggingContext().is_consented()) {
const std::string generation_id =
request_log_context_->GetLogContext().generation_id().empty()
Expand Down Expand Up @@ -71,11 +74,21 @@ RequestContext::GetPSLogContext() const {
RequestLogContext::RequestLogContext(
const privacy_sandbox::server_common::LogContext& log_context,
const privacy_sandbox::server_common::ConsentedDebugConfiguration&
consented_debug_config)
consented_debug_config,
std::optional<
absl::AnyInvocable<privacy_sandbox::server_common::DebugInfo*()>>
debug_info_opt)
: log_context_(log_context),
consented_debug_config_(consented_debug_config),
request_logging_context_(GetContextMap(log_context),
consented_debug_config) {}
consented_debug_config) {
if (debug_info_opt.has_value()) {
request_logging_context_ =
privacy_sandbox::server_common::log::ContextImpl<>(
GetContextMap(log_context), consented_debug_config,
std::move(debug_info_opt.value()));
}
}

privacy_sandbox::server_common::log::ContextImpl<>&
RequestLogContext::GetRequestLoggingContext() {
Expand Down Expand Up @@ -111,8 +124,12 @@ const RequestContext& RequestContextFactory::Get() const {
void RequestContextFactory::UpdateLogContext(
const privacy_sandbox::server_common::LogContext& log_context,
const privacy_sandbox::server_common::ConsentedDebugConfiguration&
consented_debug_config) {
request_context_->UpdateLogContext(log_context, consented_debug_config);
consented_debug_config,
std::optional<
absl::AnyInvocable<privacy_sandbox::server_common::DebugInfo*()>>
debug_info_opt) {
request_context_->UpdateLogContext(log_context, consented_debug_config,
std::move(debug_info_opt));
}

} // namespace kv_server
16 changes: 13 additions & 3 deletions components/util/request_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define COMPONENTS_UTIL_REQUEST_CONTEXT_H_

#include <memory>
#include <optional>
#include <string>
#include <utility>

Expand All @@ -33,7 +34,10 @@ class RequestLogContext {
explicit RequestLogContext(
const privacy_sandbox::server_common::LogContext& log_context,
const privacy_sandbox::server_common::ConsentedDebugConfiguration&
consented_debug_config);
consented_debug_config,
std::optional<
absl::AnyInvocable<privacy_sandbox::server_common::DebugInfo*()>>
debug_info_opt = std::nullopt);

privacy_sandbox::server_common::log::ContextImpl<>&
GetRequestLoggingContext();
Expand Down Expand Up @@ -79,7 +83,10 @@ class RequestContext {
void UpdateLogContext(
const privacy_sandbox::server_common::LogContext& log_context,
const privacy_sandbox::server_common::ConsentedDebugConfiguration&
consented_debug_config);
consented_debug_config,
std::optional<
absl::AnyInvocable<privacy_sandbox::server_common::DebugInfo*()>>
debug_info_opt = std::nullopt);
UdfRequestMetricsContext& GetUdfRequestMetricsContext() const;
InternalLookupMetricsContext& GetInternalLookupMetricsContext() const;
RequestLogContext& GetRequestLogContext() const;
Expand Down Expand Up @@ -128,7 +135,10 @@ class RequestContextFactory {
void UpdateLogContext(
const privacy_sandbox::server_common::LogContext& log_context,
const privacy_sandbox::server_common::ConsentedDebugConfiguration&
consented_debug_config);
consented_debug_config,
std::optional<
absl::AnyInvocable<privacy_sandbox::server_common::DebugInfo*()>>
debug_info_opt = std::nullopt);
// Not movable and copyable to prevent making unnecessary
// copies of underlying shared_ptr of request context, and moving of
// shared ownership of request context
Expand Down
4 changes: 4 additions & 0 deletions public/query/v2/get_values_v2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,8 @@ message GetValuesResponse {
// Note that single partition responses in cbor are not currently supported.
ResponsePartition single_partition = 1;
repeated CompressionGroup compression_groups = 2;
// Debug logs to send back to upstream servers (only in non_prod)
// The server name in the debug info will be set by the upstream servers after
// they get response from KV server
privacy_sandbox.server_common.DebugInfo debug_info = 3;
}
67 changes: 67 additions & 0 deletions public/test_util/request_example.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,73 @@ constexpr std::string_view
}
})";

// Consented V2 request example with multiple partitions with log context and
// debug info response flag
constexpr std::string_view
kConsentedV2RequestMultiPartWithDebugInfoResponseInJson =
R"(
{
"metadata": {
"hostname": "example.com"
},
"partitions": [
{
"id": 0,
"compressionGroupId": 0,
"arguments": [
{
"tags": [
"structured",
"groupNames"
],
"data": [
"hello"
]
}
]
},
{
"id": 1,
"compressionGroupId": 1,
"arguments": [
{
"tags": [
"custom",
"keys"
],
"data": [
"key1"
]
}
]
},
{
"id": 2,
"compressionGroupId": 0,
"arguments": [
{
"tags": [
"custom",
"keys"
],
"data": [
"key2"
]
}
]
}
],
"consented_debug_config": {
"is_consented": true,
"token": "debug_token",
"is_debug_info_in_response": true
},
"log_context": {
"generation_id": "client_UUID",
"adtech_debug_id": "adtech_debug_test"
}
})";

// Example consented debug token used in the unit tests
constexpr std::string_view kExampleConsentedDebugToken = "debug_token";

Expand Down

0 comments on commit 6dd724d

Please sign in to comment.