Skip to content

Commit

Permalink
feat: add prod_debug option to export event message
Browse files Browse the repository at this point in the history
Bug: 369159855
Change-Id: I0f82482f4cbfbc5a5060e1718ee1e6c01bfb36d6
GitOrigin-RevId: d1965d3a4db56452ad4367a0f23ec93e6d82789c
  • Loading branch information
Privacy Sandbox Team authored and copybara-github committed Sep 24, 2024
1 parent 81dcce7 commit 166d8b8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/logger/request_context_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ class ContextImpl final : public PSLogContext {
ContextImpl(
const absl::btree_map<std::string, std::string>& context_map,
const ConsentedDebugConfiguration& debug_config,
absl::AnyInvocable<DebugInfo*()> debug_info = []() { return nullptr; })
absl::AnyInvocable<DebugInfo*()> debug_info = []() { return nullptr; },
bool prod_debug = false)
: consented_sink(ServerToken()),
debug_response_sink_(std::move(debug_info)) {
Update(context_map, debug_config);
Update(context_map, debug_config, prod_debug);
}

// note: base class PSLogContext has no virtual destructor!
Expand All @@ -115,17 +116,19 @@ class ContextImpl final : public PSLogContext {
absl::LogSink* DebugResponseSink() override { return &debug_response_sink_; };

void Update(const absl::btree_map<std::string, std::string>& new_context,
const ConsentedDebugConfiguration& debug_config) {
const ConsentedDebugConfiguration& debug_config,
bool prod_debug = false) {
context_ = FormatContext(new_context);
consented_sink.client_debug_token_ =
debug_config.is_consented() ? debug_config.token() : "";
debug_response_sink_.should_log_ = debug_config.is_debug_info_in_response();
prod_debug_ = prod_debug;
}

template <typename T>
void SetEventMessageField(T&& field) {
if constexpr (!std::is_same_v<std::nullptr_t, EventMessageProvider>) {
if ((is_debug_response() && !IsProd()) || is_consented()) {
if ((is_debug_response() && !IsProd()) || is_consented() || prod_debug_) {
provider_.Set(std::forward<T>(field));
}
}
Expand All @@ -136,7 +139,7 @@ class ContextImpl final : public PSLogContext {
if (is_debug_response()) {
AddEventMessage(provider_.Get(), debug_response_sink_.debug_info_);
}
if (if_export_consented && is_consented()) {
if ((if_export_consented && is_consented()) || prod_debug_) {
absl::StatusOr<std::string> json_str = ProtoToJson(provider_.Get());
if (json_str.ok()) {
logger_private->EmitLogRecord(
Expand Down Expand Up @@ -207,6 +210,7 @@ class ContextImpl final : public PSLogContext {
ConsentedSinkImpl consented_sink;
DebugResponseSinkImpl debug_response_sink_;
EventMessageProvider provider_;
bool prod_debug_ = false;

static constexpr std::string_view kLogAttribute = "ps_tee_log_type";
};
Expand Down
13 changes: 13 additions & 0 deletions src/logger/request_context_impl_prod_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,19 @@ TEST_F(EventMessageTest, NotConsented) {
EXPECT_THAT(ReadSs(), IsEmpty());
}

TEST_F(EventMessageTest, ProdDebug) {
em_instance_ = std::make_unique<ContextImpl<MockEventMessageProvider>>(
absl::btree_map<std::string, std::string>{}, mismatched_token_,
[]() { return nullptr; }, /*prod_debug =*/true);
SetServerTokenForTestOnly(kServerToken);
CHECK(!em_instance_->is_consented());
em_instance_->SetEventMessageField(std::string("test gen id"));
em_instance_->ExportEventMessage(/*if_export_consented=*/true);
std::string otlp_output = ReadSs();
EXPECT_THAT(otlp_output, ContainsRegex("test gen id"));
EXPECT_THAT(otlp_output, ContainsRegex("ps_tee_log_type: event_message"));
}

TEST(FormatContext, NoContextGeneratesEmptyString) {
EXPECT_THAT(FormatContext({}), IsEmpty());
}
Expand Down

0 comments on commit 166d8b8

Please sign in to comment.