Skip to content
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
5 changes: 2 additions & 3 deletions base/cvd/cuttlefish/host/libs/metrics/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,13 @@ cf_cc_library(
],
deps = [
"//cuttlefish/common/libs/utils:host_info",
"//cuttlefish/host/commands/metrics:clearcut_protos",
"//cuttlefish/host/commands/metrics:events",
"//cuttlefish/host/commands/metrics:utils",
"//cuttlefish/host/libs/metrics:event_type",
"//external_proto:cf_guest_cc_proto",
"//external_proto:cf_host_cc_proto",
"//external_proto:cf_log_cc_proto",
"//external_proto:cf_metrics_event_v2_cc_proto",
"//external_proto:clientanalytics_cc_proto",
"//external_proto:log_source_enum_cc_proto",
],
)

Expand All @@ -79,6 +77,7 @@ cf_cc_library(
"//cuttlefish/common/libs/utils:result",
"//cuttlefish/host/commands/cvd/instances",
"//cuttlefish/host/commands/cvd/metrics:is_enabled",
"//cuttlefish/host/commands/cvd/version",
"//cuttlefish/host/libs/metrics:event_type",
"//cuttlefish/host/libs/metrics:metrics_conversion",
"//cuttlefish/host/libs/metrics:metrics_transmitter",
Expand Down
66 changes: 52 additions & 14 deletions base/cvd/cuttlefish/host/libs/metrics/metrics_conversion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,45 @@

#include "cuttlefish/host/libs/metrics/metrics_conversion.h"

#include <cstdint>
#include <chrono>
#include <string>
#include <string_view>

#include "cuttlefish/common/libs/utils/host_info.h"
#include "cuttlefish/host/commands/metrics/clearcut_protos.h"
#include "cuttlefish/host/commands/metrics/events.h"
#include "cuttlefish/host/commands/metrics/utils.h"
#include "cuttlefish/host/libs/metrics/event_type.h"
#include "external_proto/cf_guest.pb.h"
#include "external_proto/cf_host.pb.h"
#include "external_proto/cf_log.pb.h"
#include "external_proto/cf_metrics_event_v2.pb.h"
#include "external_proto/clientanalytics.pb.h"
#include "external_proto/log_source_enum.pb.h"

namespace cuttlefish {
namespace {

using google::protobuf::Timestamp;
using logs::proto::wireless::android::cuttlefish::CuttlefishLogEvent;
using logs::proto::wireless::android::cuttlefish::events::CuttlefishGuest;
using logs::proto::wireless::android::cuttlefish::events::
CuttlefishGuest_EventType;
using logs::proto::wireless::android::cuttlefish::events::CuttlefishHost;
using logs::proto::wireless::android::cuttlefish::events::CuttlefishHost_OsType;
using logs::proto::wireless::android::cuttlefish::events::MetricsEventV2;
using wireless_android_play_playlog::ClientInfo;
using wireless_android_play_playlog::LogEvent;
using wireless_android_play_playlog::LogRequest;
using wireless_android_play_playlog::LogSourceEnum::LogSource;

static constexpr LogSource kLogSourceId = LogSource::CUTTLEFISH_METRICS;
static constexpr char kLogSourceStr[] = "CUTTLEFISH_METRICS";
static constexpr ClientInfo::ClientType kCppClientType = ClientInfo::CPLUSPLUS;

Timestamp ToTimestamp(std::chrono::milliseconds ms) {
Timestamp timestamp;
timestamp.set_nanos((ms.count() % 1000) * 1000000);
timestamp.set_seconds(ms.count() / 1000);
return timestamp;
}

CuttlefishGuest_EventType ConvertEventType(EventType event_type) {
switch (event_type) {
Expand Down Expand Up @@ -85,11 +99,17 @@ CuttlefishHost_OsType ConvertHostOs(const HostInfo& host_info) {
}
}

void PopulateMetricsEvent(EventType event_type,
CuttlefishLogEvent& cf_log_event,
const HostInfo& host_metrics,
std::string_view session_id) {
CuttlefishLogEvent BuildCuttlefishLogEvent(const EventType event_type,
const HostInfo& host_metrics,
std::string_view session_id,
std::string_view cf_common_version,
std::chrono::milliseconds now) {
CuttlefishLogEvent cf_log_event;
cf_log_event.set_device_type(CuttlefishLogEvent::CUTTLEFISH_DEVICE_TYPE_HOST);
cf_log_event.set_session_id(session_id);
cf_log_event.set_cuttlefish_version(cf_common_version);
*cf_log_event.mutable_timestamp_ms() = ToTimestamp(now);

MetricsEventV2* metrics_event = cf_log_event.mutable_metrics_event_v2();

CuttlefishGuest* guest = metrics_event->add_guest();
Expand All @@ -99,18 +119,36 @@ void PopulateMetricsEvent(EventType event_type,
CuttlefishHost* host = metrics_event->mutable_host();
host->set_host_os(ConvertHostOs(host_metrics));
host->set_host_os_version(host_metrics.release);

return cf_log_event;
}

LogRequest BuildLogRequest(std::chrono::milliseconds now,
const CuttlefishLogEvent& cf_log_event) {
LogRequest log_request;
log_request.set_request_time_ms(now.count());
log_request.set_log_source(kLogSourceId);
log_request.set_log_source_name(kLogSourceStr);

ClientInfo* client_info = log_request.mutable_client_info();
client_info->set_client_type(kCppClientType);

LogEvent* log_event = log_request.add_log_event();
log_event->set_event_time_ms(now.count());
log_event->set_source_extension(cf_log_event.SerializeAsString());
return log_request;
}

} // namespace

LogRequest ConstructLogRequest(EventType event_type,
const HostInfo& host_metrics,
std::string_view session_id) {
uint64_t now_ms = metrics::GetEpochTimeMs();
CuttlefishLogEvent cf_log_event = metrics::BuildCfLogEvent(now_ms);
PopulateMetricsEvent(event_type, cf_log_event, host_metrics, session_id);
LogEvent log_event = metrics::BuildLogEvent(now_ms, cf_log_event);
return metrics::BuildLogRequest(now_ms, std::move(log_event));
std::string_view session_id,
std::string_view cf_common_version,
std::chrono::milliseconds now) {
CuttlefishLogEvent cf_log_event = BuildCuttlefishLogEvent(
event_type, host_metrics, session_id, cf_common_version, now);
return BuildLogRequest(now, cf_log_event);
}

} // namespace cuttlefish
4 changes: 3 additions & 1 deletion base/cvd/cuttlefish/host/libs/metrics/metrics_conversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#pragma once

#include <chrono>
#include <string_view>

#include "cuttlefish/common/libs/utils/host_info.h"
Expand All @@ -26,6 +27,7 @@ namespace cuttlefish {

wireless_android_play_playlog::LogRequest ConstructLogRequest(
EventType event_type, const HostInfo& host_metrics,
std::string_view session_id);
std::string_view session_id, std::string_view cf_common_version,
std::chrono::milliseconds now);

} // namespace cuttlefish
13 changes: 11 additions & 2 deletions base/cvd/cuttlefish/host/libs/metrics/metrics_orchestration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "cuttlefish/host/libs/metrics/metrics_orchestration.h"

#include <chrono>
#include <string>

#include <android-base/logging.h>
Expand All @@ -26,6 +27,7 @@
#include "cuttlefish/common/libs/utils/result.h"
#include "cuttlefish/host/commands/cvd/instances/instance_group_record.h"
#include "cuttlefish/host/commands/cvd/metrics/is_enabled.h"
#include "cuttlefish/host/commands/cvd/version/version.h"
#include "cuttlefish/host/libs/metrics/event_type.h"
#include "cuttlefish/host/libs/metrics/metrics_conversion.h"
#include "cuttlefish/host/libs/metrics/metrics_transmitter.h"
Expand All @@ -46,6 +48,11 @@ constexpr char kReadmeText[] =
"step"
" when it does>";

std::chrono::milliseconds GetEpochTime() {
auto now = std::chrono::system_clock::now().time_since_epoch();
return std::chrono::duration_cast<std::chrono::milliseconds>(now);
}

std::string GetMetricsDirectoryFilepath(
const LocalInstanceGroup& instance_group) {
return instance_group.HomeDir() + "/metrics";
Expand All @@ -63,9 +70,11 @@ Result<void> GatherAndWriteMetrics(EventType event_type,
const std::string session_id =
CF_EXPECT(ReadSessionIdFile(metrics_directory));
const HostInfo host_metrics = GetHostInfo();
const std::string cf_common_version = GetVersionIds().ToString();
std::chrono::milliseconds now = GetEpochTime();
// TODO: chadreynolds - gather the rest of the data (guest/flag information)
const LogRequest log_request =
ConstructLogRequest(event_type, host_metrics, session_id);
const LogRequest log_request = ConstructLogRequest(
event_type, host_metrics, session_id, cf_common_version, now);

CF_EXPECT(WriteMetricsEvent(event_type, metrics_directory, log_request));
if (kEnableCvdMetrics) {
Expand Down
Loading