analytics: Add operation result code for bandwidth upgrade analytics, I

PiperOrigin-RevId: 704204474
This commit is contained in:
Edwin Wu
2024-12-09 02:41:43 -08:00
committed by Copybara-Service
parent d7a1fb8c48
commit 4c42919211
4 changed files with 160 additions and 27 deletions
@@ -738,12 +738,19 @@ void AnalyticsRecorder::OnBandwidthUpgradeStarted(
void AnalyticsRecorder::OnBandwidthUpgradeError(
const std::string &endpoint_id, BandwidthUpgradeResult result,
BandwidthUpgradeErrorStage error_stage) {
BandwidthUpgradeErrorStage error_stage,
OperationResultCode operation_result_code) {
MutexLock lock(&mutex_);
if (!CanRecordAnalyticsLocked("OnBandwidthUpgradeError")) {
return;
}
FinishUpgradeAttemptLocked(endpoint_id, result, error_stage);
// If the same records existed, drop this one.
if (EraseIfBandwidthUpgradeRecordExistedLocked(
endpoint_id, result, error_stage, operation_result_code)) {
return;
}
FinishUpgradeAttemptLocked(endpoint_id, result, error_stage,
operation_result_code);
}
void AnalyticsRecorder::OnBandwidthUpgradeSuccess(
@@ -753,7 +760,8 @@ void AnalyticsRecorder::OnBandwidthUpgradeSuccess(
return;
}
FinishUpgradeAttemptLocked(endpoint_id, UPGRADE_RESULT_SUCCESS,
UPGRADE_SUCCESS);
UPGRADE_SUCCESS,
OperationResultCode::DETAIL_SUCCESS);
}
void AnalyticsRecorder::OnErrorCode(const ErrorCodeParams &params) {
@@ -1156,9 +1164,39 @@ bool AnalyticsRecorder::ConnectionAttemptResultCodeExistedLocked(
return false;
}
// If bandwidth upgrade always failed on the same fromMedium, toMedium, result,
// stage and result code, we'll drop the duplicate logs for preventing the waste
// of log storage space
bool AnalyticsRecorder::EraseIfBandwidthUpgradeRecordExistedLocked(
const std::string &endpoint_id, BandwidthUpgradeResult result,
BandwidthUpgradeErrorStage error_stage,
OperationResultCode operation_result_code) {
if (current_strategy_session_ == nullptr) {
return false;
}
auto it = bandwidth_upgrade_attempts_.find(endpoint_id);
if (it != bandwidth_upgrade_attempts_.end()) {
ConnectionsLog::BandwidthUpgradeAttempt *attempt = it->second.get();
for (auto &existing_attempt :
current_strategy_session_->upgrade_attempt()) {
if (attempt->from_medium() == existing_attempt.from_medium() &&
attempt->to_medium() == existing_attempt.to_medium() &&
result == existing_attempt.upgrade_result() &&
error_stage == existing_attempt.error_stage() &&
operation_result_code ==
existing_attempt.operation_result().result_code()) {
bandwidth_upgrade_attempts_.erase(it);
return true;
}
}
}
return false;
}
void AnalyticsRecorder::FinishUpgradeAttemptLocked(
const std::string &endpoint_id, BandwidthUpgradeResult result,
BandwidthUpgradeErrorStage error_stage, bool erase_item) {
BandwidthUpgradeErrorStage error_stage,
OperationResultCode operation_result_code, bool erase_item) {
if (current_strategy_session_ == nullptr) {
NEARBY_LOGS(INFO) << "Unable to record upgrade attempt due to null "
"current_strategy_session_";
@@ -1173,6 +1211,13 @@ void AnalyticsRecorder::FinishUpgradeAttemptLocked(
attempt->duration_millis());
attempt->set_error_stage(error_stage);
attempt->set_upgrade_result(result);
auto operation_result_proto =
std::make_unique<ConnectionsLog::OperationResult>();
operation_result_proto->set_result_code(operation_result_code);
operation_result_proto->set_result_category(
GetOperationResultCateory(operation_result_code));
attempt->set_allocated_operation_result(operation_result_proto.release());
*current_strategy_session_->add_upgrade_attempt() = *attempt;
if (erase_item) {
bandwidth_upgrade_attempts_.erase(it);
@@ -1199,8 +1244,10 @@ void AnalyticsRecorder::FinishStrategySessionLocked() {
// Finish any pending upgrade attempts.
for (const auto &item : bandwidth_upgrade_attempts_) {
FinishUpgradeAttemptLocked(item.first, UNFINISHED_ERROR,
UPGRADE_UNFINISHED, /*erase_item=*/false);
FinishUpgradeAttemptLocked(
item.first, UNFINISHED_ERROR, UPGRADE_UNFINISHED,
OperationResultCode::DEVICE_STATE_ERROR_UNFINISHED_UPGRADE_ATTEMPTS,
/*erase_item=*/false);
}
bandwidth_upgrade_attempts_.clear();
@@ -182,7 +182,9 @@ class AnalyticsRecorder {
const std::string &endpoint_id,
location::nearby::proto::connections::BandwidthUpgradeResult result,
location::nearby::proto::connections::BandwidthUpgradeErrorStage
error_stage) ABSL_LOCKS_EXCLUDED(mutex_);
error_stage,
location::nearby::proto::connections::OperationResultCode
operation_result_code) ABSL_LOCKS_EXCLUDED(mutex_);
void OnBandwidthUpgradeSuccess(const std::string &endpoint_id)
ABSL_LOCKS_EXCLUDED(mutex_);
@@ -371,11 +373,20 @@ class AnalyticsRecorder {
location::nearby::proto::connections::ConnectionAttemptType type,
location::nearby::proto::connections::OperationResultCode
operation_result_code) ABSL_SHARED_LOCKS_REQUIRED(mutex_);
bool EraseIfBandwidthUpgradeRecordExistedLocked(
const std::string &endpoint_id,
location::nearby::proto::connections::BandwidthUpgradeResult result,
location::nearby::proto::connections::BandwidthUpgradeErrorStage
error_stage,
location::nearby::proto::connections::OperationResultCode
operation_result_code) ABSL_SHARED_LOCKS_REQUIRED(mutex_);
void FinishUpgradeAttemptLocked(
const std::string &endpoint_id,
location::nearby::proto::connections::BandwidthUpgradeResult result,
location::nearby::proto::connections::BandwidthUpgradeErrorStage
error_stage,
location::nearby::proto::connections::OperationResultCode
operation_result_code,
bool erase_item = true) ABSL_SHARED_LOCKS_REQUIRED(mutex_);
void FinishStrategySessionLocked() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
@@ -888,8 +888,9 @@ TEST(AnalyticsRecorderTest, UpgradeAttemptWorks) {
analytics_recorder.OnBandwidthUpgradeStarted(
endpoint_id_1, BLUETOOTH, WIFI_LAN, INCOMING, connection_token);
// Error to upgrade.
analytics_recorder.OnBandwidthUpgradeError(endpoint_id, WIFI_LAN_MEDIUM_ERROR,
WIFI_LAN_SOCKET_CREATION);
analytics_recorder.OnBandwidthUpgradeError(
endpoint_id, WIFI_LAN_MEDIUM_ERROR, WIFI_LAN_SOCKET_CREATION,
OperationResultCode::CONNECTIVITY_WIFI_LAN_INVALID_CREDENTIAL);
// Success to upgrade.
analytics_recorder.OnBandwidthUpgradeSuccess(endpoint_id_1);
// Upgrade is unfinished.
@@ -920,6 +921,10 @@ TEST(AnalyticsRecorderTest, UpgradeAttemptWorks) {
upgrade_result: WIFI_LAN_MEDIUM_ERROR
error_stage: WIFI_LAN_SOCKET_CREATION
connection_token: "connection_token"
operation_result <
result_category: CATEGORY_CONNECTIVITY_ERROR
result_code: CONNECTIVITY_WIFI_LAN_INVALID_CREDENTIAL
>
>
upgrade_attempt <
direction: INCOMING
@@ -928,6 +933,10 @@ TEST(AnalyticsRecorderTest, UpgradeAttemptWorks) {
upgrade_result: UPGRADE_RESULT_SUCCESS
error_stage: UPGRADE_SUCCESS
connection_token: "connection_token"
operation_result <
result_category: CATEGORY_SUCCESS
result_code: DETAIL_SUCCESS
>
>
upgrade_attempt {
direction: INCOMING
@@ -936,6 +945,10 @@ TEST(AnalyticsRecorderTest, UpgradeAttemptWorks) {
upgrade_result: UNFINISHED_ERROR
error_stage: UPGRADE_UNFINISHED
connection_token: "connection_token"
operation_result <
result_category: CATEGORY_DEVICE_STATE_ERROR
result_code: DEVICE_STATE_ERROR_UNFINISHED_UPGRADE_ATTEMPTS
>
}
>)pb");
@@ -962,8 +975,9 @@ TEST(AnalyticsRecorderTest, StartListeningForIncomingConnectionsWorks) {
analytics_recorder.OnBandwidthUpgradeStarted(
endpoint_id_1, BLUETOOTH, WIFI_LAN, INCOMING, connection_token);
// Error to upgrade.
analytics_recorder.OnBandwidthUpgradeError(endpoint_id, WIFI_LAN_MEDIUM_ERROR,
WIFI_LAN_SOCKET_CREATION);
analytics_recorder.OnBandwidthUpgradeError(
endpoint_id, WIFI_LAN_MEDIUM_ERROR, WIFI_LAN_SOCKET_CREATION,
OperationResultCode::CONNECTIVITY_WIFI_LAN_INVALID_CREDENTIAL);
// Success to upgrade.
analytics_recorder.OnBandwidthUpgradeSuccess(endpoint_id_1);
@@ -982,6 +996,10 @@ TEST(AnalyticsRecorderTest, StartListeningForIncomingConnectionsWorks) {
upgrade_result: WIFI_LAN_MEDIUM_ERROR
error_stage: WIFI_LAN_SOCKET_CREATION
connection_token: "connection_token"
operation_result <
result_category: CATEGORY_CONNECTIVITY_ERROR
result_code: CONNECTIVITY_WIFI_LAN_INVALID_CREDENTIAL
>
>
upgrade_attempt <
direction: INCOMING
@@ -990,6 +1008,10 @@ TEST(AnalyticsRecorderTest, StartListeningForIncomingConnectionsWorks) {
upgrade_result: UPGRADE_RESULT_SUCCESS
error_stage: UPGRADE_SUCCESS
connection_token: "connection_token"
operation_result <
result_category: CATEGORY_SUCCESS
result_code: DETAIL_SUCCESS
>
>
>)pb");
@@ -1498,8 +1520,9 @@ TEST(AnalyticsRecorderTest,
analytics_recorder.OnBandwidthUpgradeStarted(
endpoint_id_1, BLUETOOTH, WIFI_LAN, INCOMING, connection_token);
// - Error to upgrade.
analytics_recorder.OnBandwidthUpgradeError(endpoint_id, WIFI_LAN_MEDIUM_ERROR,
WIFI_LAN_SOCKET_CREATION);
analytics_recorder.OnBandwidthUpgradeError(
endpoint_id, WIFI_LAN_MEDIUM_ERROR, WIFI_LAN_SOCKET_CREATION,
OperationResultCode::CONNECTIVITY_WIFI_LAN_INVALID_CREDENTIAL);
// - Success to upgrade.
analytics_recorder.OnBandwidthUpgradeSuccess(endpoint_id_1);
@@ -1535,6 +1558,10 @@ TEST(AnalyticsRecorderTest,
upgrade_result: WIFI_LAN_MEDIUM_ERROR
error_stage: WIFI_LAN_SOCKET_CREATION
connection_token: "connection_token"
operation_result <
result_category: CATEGORY_CONNECTIVITY_ERROR
result_code: CONNECTIVITY_WIFI_LAN_INVALID_CREDENTIAL
>
>
upgrade_attempt <
direction: INCOMING
@@ -1543,6 +1570,10 @@ TEST(AnalyticsRecorderTest,
upgrade_result: UPGRADE_RESULT_SUCCESS
error_stage: UPGRADE_SUCCESS
connection_token: "connection_token"
operation_result <
result_category: CATEGORY_SUCCESS
result_code: DETAIL_SUCCESS
>
>
upgrade_attempt {
direction: INCOMING
@@ -1551,6 +1582,10 @@ TEST(AnalyticsRecorderTest,
upgrade_result: UNFINISHED_ERROR
error_stage: UPGRADE_UNFINISHED
connection_token: "connection_token"
operation_result <
result_category: CATEGORY_DEVICE_STATE_ERROR
result_code: DEVICE_STATE_ERROR_UNFINISHED_UPGRADE_ATTEMPTS
>
}
>)pb");
EXPECT_THAT(event_logger.GetLoggedClientSession(),
@@ -1600,6 +1635,10 @@ TEST(AnalyticsRecorderTest,
upgrade_result: WIFI_LAN_MEDIUM_ERROR
error_stage: WIFI_LAN_SOCKET_CREATION
connection_token: "connection_token"
operation_result <
result_category: CATEGORY_CONNECTIVITY_ERROR
result_code: CONNECTIVITY_WIFI_LAN_INVALID_CREDENTIAL
>
>
upgrade_attempt <
direction: INCOMING
@@ -1608,6 +1647,10 @@ TEST(AnalyticsRecorderTest,
upgrade_result: UPGRADE_RESULT_SUCCESS
error_stage: UPGRADE_SUCCESS
connection_token: "connection_token"
operation_result <
result_category: CATEGORY_SUCCESS
result_code: DETAIL_SUCCESS
>
>
upgrade_attempt {
direction: INCOMING
@@ -1616,6 +1659,10 @@ TEST(AnalyticsRecorderTest,
upgrade_result: UNFINISHED_ERROR
error_stage: UPGRADE_UNFINISHED
connection_token: "connection_token"
operation_result <
result_category: CATEGORY_DEVICE_STATE_ERROR
result_code: DEVICE_STATE_ERROR_UNFINISHED_UPGRADE_ATTEMPTS
>
}
>)pb");
EXPECT_THAT(event_logger.GetLoggedClientSession(),
+42 -14
View File
@@ -263,9 +263,11 @@ void BwuManager::InitiateBwuForEndpoint(ClientProxy* client,
<< "BwuManager couldn't complete the upgrade for endpoint "
<< endpoint_id
<< " because it couldn't find an existing EndpointChannel for it.";
// TODO(edwinwu): Add a new error code for this case.
client->GetAnalyticsRecorder().OnBandwidthUpgradeError(
endpoint_id, location::nearby::proto::connections::CHANNEL_ERROR,
location::nearby::proto::connections::NETWORK_AVAILABLE);
location::nearby::proto::connections::NETWORK_AVAILABLE,
OperationResultCode::DETAIL_UNKNOWN);
return;
}
@@ -302,10 +304,12 @@ void BwuManager::InitiateBwuForEndpoint(ClientProxy* client,
UpgradePathInfo info;
info.set_medium(parser::MediumToUpgradePathInfoMedium(proposed_medium));
// TODO(edwinwu): Add a new error code for this case.
ProcessUpgradeFailureEvent(client, endpoint_id, info);
client->GetAnalyticsRecorder().OnBandwidthUpgradeError(
endpoint_id, location::nearby::proto::connections::RESULT_IO_ERROR,
location::nearby::proto::connections::NETWORK_AVAILABLE);
location::nearby::proto::connections::NETWORK_AVAILABLE,
OperationResultCode::DETAIL_UNKNOWN);
return;
}
if (!channel->Write(bytes).Ok()) {
@@ -660,9 +664,11 @@ void BwuManager::RunUpgradeProtocol(
<< endpoint_id
<< " when registering the new EndpointChannel, short-circuiting the "
"upgrade protocol.";
// TODO(edwinwu): Add a new error code for this case.
client->GetAnalyticsRecorder().OnBandwidthUpgradeError(
endpoint_id, location::nearby::proto::connections::CHANNEL_ERROR,
location::nearby::proto::connections::PRIOR_ENDPOINT_CHANNEL);
location::nearby::proto::connections::PRIOR_ENDPOINT_CHANNEL,
OperationResultCode::DETAIL_UNKNOWN);
return;
}
channel_manager_->ReplaceChannelForEndpoint(
@@ -677,9 +683,11 @@ void BwuManager::RunUpgradeProtocol(
"BWU_NEGOTIATION.LAST_WRITE_TO_PRIOR_CHANNEL OfflineFrame to "
"endpoint "
<< endpoint_id << ", short-circuiting the upgrade protocol.";
// TODO(edwinwu): Add a new error code for this case.
client->GetAnalyticsRecorder().OnBandwidthUpgradeError(
endpoint_id, location::nearby::proto::connections::RESULT_IO_ERROR,
location::nearby::proto::connections::LAST_WRITE_TO_PRIOR_CHANNEL);
location::nearby::proto::connections::LAST_WRITE_TO_PRIOR_CHANNEL,
OperationResultCode::DETAIL_UNKNOWN);
return;
}
NEARBY_VLOG(1) << "BwuManager successfully wrote "
@@ -787,9 +795,12 @@ void BwuManager::ProcessBwuPathAvailableEvent(
} else if (client->GetCancellationFlag(endpoint_id)->Cancelled()) {
connection_attempt_result =
location::nearby::proto::connections::RESULT_CANCELLED;
// TODO(edwinwu): Replace DETAIL_UNKNOWN with the one returned by
// ProcessBwuPathAvailableEventInternal.
client->GetAnalyticsRecorder().OnBandwidthUpgradeError(
endpoint_id, location::nearby::proto::connections::RESULT_REMOTE_ERROR,
location::nearby::proto::connections::UPGRADE_CANCEL);
location::nearby::proto::connections::UPGRADE_CANCEL,
OperationResultCode::DETAIL_UNKNOWN);
} else {
connection_attempt_result =
location::nearby::proto::connections::RESULT_ERROR;
@@ -837,7 +848,7 @@ void BwuManager::ProcessBwuPathAvailableEvent(
std::unique_ptr<EndpointChannel>
BwuManager::ProcessBwuPathAvailableEventInternal(
ClientProxy* client, const string& endpoint_id,
ClientProxy* client, const std::string& endpoint_id,
const UpgradePathInfo& upgrade_path_info) {
Medium medium =
parser::UpgradePathInfoMediumToMedium(upgrade_path_info.medium());
@@ -892,7 +903,8 @@ BwuManager::ProcessBwuPathAvailableEventInternal(
<< endpoint_id << ", aborting upgrade.";
client->GetAnalyticsRecorder().OnBandwidthUpgradeError(
endpoint_id, location::nearby::proto::connections::RESULT_IO_ERROR,
location::nearby::proto::connections::SOCKET_CREATION);
location::nearby::proto::connections::SOCKET_CREATION,
result.error().operation_result_code().value());
return nullptr;
}
std::unique_ptr<EndpointChannel> new_channel = std::move(result.value());
@@ -912,9 +924,12 @@ BwuManager::ProcessBwuPathAvailableEventInternal(
<< "BwuManager failed to write BWU_NEGOTIATION.CLIENT_INTRODUCTION "
"OfflineFrame to newly-created EndpointChannel "
<< new_channel->GetName() << ", aborting upgrade.";
// TODO(edwinwu): Replace DETAIL_UNKNOWN with the one returned by
// CreateUpgradedEndpointChannel.
client->GetAnalyticsRecorder().OnBandwidthUpgradeError(
endpoint_id, location::nearby::proto::connections::RESULT_IO_ERROR,
location::nearby::proto::connections::CLIENT_INTRODUCTION);
location::nearby::proto::connections::CLIENT_INTRODUCTION,
OperationResultCode::DETAIL_UNKNOWN);
return {};
}
@@ -963,9 +978,12 @@ void BwuManager::RunUpgradeFailedProtocol(
<< endpoint_id
<< " when sending an upgrade failure frame, short-circuiting the "
"upgrade protocol.";
// TODO(edwinwu): Replace DETAIL_UNKNOWN with the one returned by
// ProcessBwuPathAvailableEventInternal.
client->GetAnalyticsRecorder().OnBandwidthUpgradeError(
endpoint_id, location::nearby::proto::connections::CHANNEL_ERROR,
location::nearby::proto::connections::NETWORK_AVAILABLE);
location::nearby::proto::connections::NETWORK_AVAILABLE,
OperationResultCode::DETAIL_UNKNOWN);
return;
}
@@ -977,9 +995,12 @@ void BwuManager::RunUpgradeFailedProtocol(
<< "BwuManager failed to write BWU_NEGOTIATION.UPGRADE_FAILURE "
"OfflineFrame to endpoint "
<< endpoint_id << ", short-circuiting the upgrade protocol.";
// TODO(edwinwu): Replace DETAIL_UNKNOWN with the one returned by
// ProcessBwuPathAvailableEventInternal.
client->GetAnalyticsRecorder().OnBandwidthUpgradeError(
endpoint_id, location::nearby::proto::connections::RESULT_IO_ERROR,
location::nearby::proto::connections::NETWORK_AVAILABLE);
location::nearby::proto::connections::NETWORK_AVAILABLE,
OperationResultCode::DETAIL_UNKNOWN);
return;
}
@@ -1123,9 +1144,12 @@ void BwuManager::ProcessLastWriteToPriorChannelEvent(
"OfflineFrame to endpoint "
<< endpoint_id
<< ", short-circuiting the upgrade protocol.";
// TODO(edwinwu): Replace DETAIL_UNKNOWN with the one returned by
// ProcessBwuPathAvailableEventInternal.
client->GetAnalyticsRecorder().OnBandwidthUpgradeError(
endpoint_id, location::nearby::proto::connections::RESULT_IO_ERROR,
location::nearby::proto::connections::SAFE_TO_CLOSE_PRIOR_CHANNEL);
location::nearby::proto::connections::SAFE_TO_CLOSE_PRIOR_CHANNEL,
OperationResultCode::DETAIL_UNKNOWN);
return;
}
NEARBY_VLOG(1) << "BwuManager successfully wrote "
@@ -1256,9 +1280,12 @@ void BwuManager::ProcessUpgradeFailureEvent(
<< endpoint_id
<< " because we have other connected endpoints and can't try a new "
"upgrade medium.";
// TODO(edwinwu): Replace DETAIL_UNKNOWN with the one returned by
// ProcessBwuPathAvailableEventInternal.
client->GetAnalyticsRecorder().OnBandwidthUpgradeError(
endpoint_id, location::nearby::proto::connections::CHANNEL_ERROR,
location::nearby::proto::connections::NETWORK_AVAILABLE);
location::nearby::proto::connections::NETWORK_AVAILABLE,
OperationResultCode::DETAIL_UNKNOWN);
return;
}
@@ -1463,8 +1490,9 @@ void BwuManager::AttemptToRecordBandwidthUpgradeErrorForUnknownEndpoint(
// them if they want to repeatedly attempt to connect or if they want to
// give up and have us try a different medium. This isn't a decision we can
// make for them.
client->GetAnalyticsRecorder().OnBandwidthUpgradeError(endpoint_id, result,
error_stage);
// TODO(edwinwu): Replace DETAIL_UNKNOWN.
client->GetAnalyticsRecorder().OnBandwidthUpgradeError(
endpoint_id, result, error_stage, OperationResultCode::DETAIL_UNKNOWN);
NEARBY_LOGS(INFO)
<< "BwuManager got error "
<< location::nearby::proto::connections::BandwidthUpgradeResult_Name(