3131#include " cuttlefish/host/commands/metrics/events.h"
3232#include " cuttlefish/host/commands/metrics/send.h"
3333#include " cuttlefish/host/commands/metrics/utils.h"
34+ #include " cuttlefish/host/libs/metrics/event_type.h"
3435#include " cuttlefish/host/libs/metrics/metrics_defs.h"
3536#include " cuttlefish/host/libs/metrics/metrics_writer.h"
3637#include " cuttlefish/host/libs/metrics/session_id.h"
38+ #include " external_proto/cf_guest.pb.h"
39+ #include " external_proto/cf_host.pb.h"
3740#include " external_proto/cf_log.pb.h"
41+ #include " external_proto/cf_metrics_event_v2.pb.h"
3842#include " external_proto/clientanalytics.pb.h"
3943
4044namespace cuttlefish {
4145namespace {
4246
4347using logs::proto::wireless::android::cuttlefish::CuttlefishLogEvent;
48+ using logs::proto::wireless::android::cuttlefish::events::CuttlefishGuest;
49+ using logs::proto::wireless::android::cuttlefish::events::CuttlefishGuest_EventType;
50+ using logs::proto::wireless::android::cuttlefish::events::CuttlefishHost;
51+ using logs::proto::wireless::android::cuttlefish::events::CuttlefishHost_OsType;
52+ using logs::proto::wireless::android::cuttlefish::events::MetricsEventV2;
4453using wireless_android_play_playlog::LogEvent;
4554using wireless_android_play_playlog::LogRequest;
4655
@@ -64,10 +73,72 @@ Result<void> SetUpMetrics(const std::string& metrics_directory) {
6473 return {};
6574}
6675
67- Result<void > TransmitMetrics () {
76+ CuttlefishGuest_EventType ConvertEventType (EventType event_type) {
77+ switch (event_type) {
78+ case EventType::DeviceInstantiation:
79+ return CuttlefishGuest_EventType::
80+ CuttlefishGuest_EventType_CUTTLEFISH_GUEST_EVENT_TYPE_VM_INSTANTIATION;
81+ case EventType::DeviceBootStart:
82+ return CuttlefishGuest_EventType::
83+ CuttlefishGuest_EventType_CUTTLEFISH_GUEST_EVENT_TYPE_UNSPECIFIED;
84+ case EventType::DeviceBootComplete:
85+ return CuttlefishGuest_EventType::
86+ CuttlefishGuest_EventType_CUTTLEFISH_GUEST_EVENT_TYPE_DEVICE_BOOT_COMPLETED;
87+ case EventType::DeviceStop:
88+ return CuttlefishGuest_EventType::
89+ CuttlefishGuest_EventType_CUTTLEFISH_GUEST_EVENT_TYPE_VM_STOP;
90+ }
91+ }
92+
93+ CuttlefishHost_OsType ConvertHostOs (const HostInfo& host_info) {
94+ switch (host_info.os ) {
95+ case Os::Unknown:
96+ return CuttlefishHost_OsType::
97+ CuttlefishHost_OsType_CUTTLEFISH_HOST_OS_TYPE_UNSPECIFIED;
98+ case Os::Linux:
99+ switch (host_info.arch ) {
100+ case Arch::Arm:
101+ return CuttlefishHost_OsType::
102+ CuttlefishHost_OsType_CUTTLEFISH_HOST_OS_TYPE_LINUX_AARCH32;
103+ case Arch::Arm64:
104+ return CuttlefishHost_OsType::
105+ CuttlefishHost_OsType_CUTTLEFISH_HOST_OS_TYPE_LINUX_AARCH64;
106+ case Arch::RiscV64:
107+ return CuttlefishHost_OsType::
108+ CuttlefishHost_OsType_CUTTLEFISH_HOST_OS_TYPE_LINUX_RISCV64;
109+ case Arch::X86:
110+ return CuttlefishHost_OsType::
111+ CuttlefishHost_OsType_CUTTLEFISH_HOST_OS_TYPE_LINUX_X86;
112+ case Arch::X86_64:
113+ return CuttlefishHost_OsType::
114+ CuttlefishHost_OsType_CUTTLEFISH_HOST_OS_TYPE_LINUX_X86_64;
115+ }
116+ }
117+ }
118+
119+ // TODO: chadreynolds - move this logic into a separate file dedicated to
120+ // conversion logic
121+ void PopulateMetricsEvent (EventType event_type,
122+ CuttlefishLogEvent& cf_log_event,
123+ const HostInfo& host_metrics,
124+ std::string_view session_id) {
125+ cf_log_event.set_session_id (session_id);
126+ MetricsEventV2* metrics_event = cf_log_event.mutable_metrics_event_v2 ();
127+
128+ CuttlefishGuest* guest = metrics_event->add_guest ();
129+ guest->set_event_type (ConvertEventType (event_type));
130+ guest->set_guest_id (std::string (session_id) + " 1" );
131+
132+ CuttlefishHost* host = metrics_event->mutable_host ();
133+ host->set_host_os (ConvertHostOs (host_metrics));
134+ host->set_host_os_version (host_metrics.release );
135+ }
136+
137+ Result<void > TransmitMetrics (EventType event_type, const HostInfo& host_metrics,
138+ std::string_view session_id) {
68139 uint64_t now_ms = metrics::GetEpochTimeMs ();
69140 CuttlefishLogEvent cf_log_event = metrics::BuildCfLogEvent (now_ms);
70- cf_log_event. mutable_metrics_event_v2 ( );
141+ PopulateMetricsEvent (event_type, cf_log_event, host_metrics, session_id );
71142 LogEvent log_event = metrics::BuildLogEvent (now_ms, cf_log_event);
72143 LogRequest log_request =
73144 metrics::BuildLogRequest (now_ms, std::move (log_event));
@@ -78,11 +149,11 @@ Result<void> TransmitMetrics() {
78149 return {};
79150}
80151
81- Result<void > GatherAndWriteMetrics (std::string_view event_type,
152+ Result<void > GatherAndWriteMetrics (EventType event_type,
82153 const std::string& metrics_directory) {
83154 const std::string session_id =
84155 CF_EXPECT (ReadSessionIdFile (metrics_directory));
85- HostInfo host_metrics = GetHostInfo ();
156+ const HostInfo host_metrics = GetHostInfo ();
86157 // TODO: chadreynolds - gather the rest of the data (guest/flag information)
87158 // TODO: chadreynolds - convert data to the proto representation
88159 CF_EXPECT (WriteMetricsEvent (event_type, metrics_directory, session_id,
@@ -91,13 +162,12 @@ Result<void> GatherAndWriteMetrics(std::string_view event_type,
91162 LOG (INFO) << " This will automatically send diagnostic information to "
92163 " Google, such as crash reports and usage data from the host "
93164 " machine managing the Android Virtual Device." ;
94- CF_EXPECT (TransmitMetrics ());
165+ CF_EXPECT (TransmitMetrics (event_type, host_metrics, session_id ));
95166 }
96167 return {};
97168}
98169
99- void RunMetrics (const std::string& metrics_directory,
100- std::string_view event_type) {
170+ void RunMetrics (const std::string& metrics_directory, EventType event_type) {
101171 if (!FileExists (metrics_directory)) {
102172 LOG (INFO) << " Metrics directory does not exist, perhaps metrics were not "
103173 " initialized." ;
@@ -107,7 +177,7 @@ void RunMetrics(const std::string& metrics_directory,
107177 GatherAndWriteMetrics (event_type, metrics_directory);
108178 if (!event_result.ok ()) {
109179 LOG (INFO) << fmt::format (" Failed to gather metrics for {}. Error: {}" ,
110- event_type, event_result.error ());
180+ EventTypeString ( event_type) , event_result.error ());
111181 }
112182}
113183
@@ -123,25 +193,25 @@ void GatherVmInstantiationMetrics(const LocalInstanceGroup& instance_group) {
123193 metrics_setup_result.error ());
124194 return ;
125195 }
126- RunMetrics (metrics_directory, " device instantiation " );
196+ RunMetrics (metrics_directory, EventType::DeviceInstantiation );
127197}
128198
129199void GatherVmStartMetrics (const LocalInstanceGroup& instance_group) {
130200 const std::string metrics_directory =
131201 GetMetricsDirectoryFilepath (instance_group);
132- RunMetrics (metrics_directory, " device start " );
202+ RunMetrics (metrics_directory, EventType::DeviceBootStart );
133203}
134204
135205void GatherVmBootCompleteMetrics (const LocalInstanceGroup& instance_group) {
136206 const std::string metrics_directory =
137207 GetMetricsDirectoryFilepath (instance_group);
138- RunMetrics (metrics_directory, " device boot complete " );
208+ RunMetrics (metrics_directory, EventType::DeviceBootComplete );
139209}
140210
141211void GatherVmStopMetrics (const LocalInstanceGroup& instance_group) {
142212 const std::string metrics_directory =
143213 GetMetricsDirectoryFilepath (instance_group);
144- RunMetrics (metrics_directory, " device stop " );
214+ RunMetrics (metrics_directory, EventType::DeviceStop );
145215}
146216
147217} // namespace cuttlefish
0 commit comments