Skip to content

Commit 0125d9c

Browse files
committed
Populate metrics fields with gathered data
Also, convert the `event_type` parameters to an enum, to simplify conversion. Bug:449821767
1 parent 6016dd0 commit 0125d9c

File tree

6 files changed

+170
-16
lines changed

6 files changed

+170
-16
lines changed

base/cvd/cuttlefish/host/libs/metrics/BUILD.bazel

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ package(
44
default_visibility = ["//:android_cuttlefish"],
55
)
66

7+
cf_cc_library(
8+
name = "event_type",
9+
srcs = [
10+
"event_type.cc",
11+
],
12+
hdrs = [
13+
"event_type.h",
14+
],
15+
)
16+
717
cf_cc_library(
818
name = "metrics",
919
srcs = [
@@ -51,10 +61,14 @@ cf_cc_library(
5161
"//cuttlefish/host/commands/metrics:events",
5262
"//cuttlefish/host/commands/metrics:send",
5363
"//cuttlefish/host/commands/metrics:utils",
64+
"//cuttlefish/host/libs/metrics:event_type",
5465
"//cuttlefish/host/libs/metrics:metrics_headers",
5566
"//cuttlefish/host/libs/metrics:metrics_writer",
5667
"//cuttlefish/host/libs/metrics:session_id",
68+
"//external_proto:cf_guest_cc_proto",
69+
"//external_proto:cf_host_cc_proto",
5770
"//external_proto:cf_log_cc_proto",
71+
"//external_proto:cf_metrics_event_v2_cc_proto",
5872
"//external_proto:clientanalytics_cc_proto",
5973
"//libbase",
6074
"@fmt",
@@ -75,6 +89,7 @@ cf_cc_library(
7589
"//cuttlefish/common/libs/utils:host_info",
7690
"//cuttlefish/common/libs/utils:random",
7791
"//cuttlefish/common/libs/utils:result",
92+
"//cuttlefish/host/libs/metrics:event_type",
7893
"@fmt",
7994
],
8095
)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (C) 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "cuttlefish/host/libs/metrics/event_type.h"
18+
19+
#include <string>
20+
21+
namespace cuttlefish {
22+
23+
std::string EventTypeString(EventType event_type) {
24+
switch (event_type) {
25+
case EventType::DeviceInstantiation:
26+
return "device_instantiation";
27+
case EventType::DeviceBootStart:
28+
return "device_boot_start";
29+
case EventType::DeviceBootComplete:
30+
return "device_boot_complete";
31+
case EventType::DeviceStop:
32+
return "device_stop";
33+
}
34+
}
35+
36+
} // namespace cuttlefish
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (C) 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include <string>
20+
21+
namespace cuttlefish {
22+
23+
enum class EventType {
24+
DeviceInstantiation,
25+
DeviceBootStart,
26+
DeviceBootComplete,
27+
DeviceStop,
28+
};
29+
30+
std::string EventTypeString(EventType event_type);
31+
32+
} // namespace cuttlefish

base/cvd/cuttlefish/host/libs/metrics/metrics_orchestration.cc

Lines changed: 82 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,25 @@
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

4044
namespace cuttlefish {
4145
namespace {
4246

4347
using 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;
4453
using wireless_android_play_playlog::LogEvent;
4554
using 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

129199
void 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

135205
void 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

141211
void 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

base/cvd/cuttlefish/host/libs/metrics/metrics_writer.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "cuttlefish/common/libs/utils/host_info.h"
2727
#include "cuttlefish/common/libs/utils/random.h"
2828
#include "cuttlefish/common/libs/utils/result.h"
29+
#include "cuttlefish/host/libs/metrics/event_type.h"
2930

3031
namespace cuttlefish {
3132
namespace {
@@ -37,12 +38,12 @@ std::string GenerateFilenameSuffix() {
3738

3839
} // namespace
3940

40-
Result<void> WriteMetricsEvent(std::string_view event_type,
41+
Result<void> WriteMetricsEvent(EventType event_type,
4142
const std::string& metrics_directory,
4243
std::string_view session_id,
4344
const HostInfo& host_metrics) {
4445
const std::string event_filepath =
45-
fmt::format("{}/{}_{}_{}", metrics_directory, event_type,
46+
fmt::format("{}/{}_{}_{}", metrics_directory, EventTypeString(event_type),
4647
std::chrono::system_clock::now(), GenerateFilenameSuffix());
4748
// TODO: chadreynolds - convert (what will be a proto) to text
4849
CF_EXPECT(WriteNewFile(

base/cvd/cuttlefish/host/libs/metrics/metrics_writer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121

2222
#include "cuttlefish/common/libs/utils/host_info.h"
2323
#include "cuttlefish/common/libs/utils/result.h"
24+
#include "cuttlefish/host/libs/metrics/event_type.h"
2425

2526
namespace cuttlefish {
2627

27-
// TODO: chadreynolds - add support for more fields than HostInfo
28-
Result<void> WriteMetricsEvent(std::string_view event_type,
28+
Result<void> WriteMetricsEvent(EventType event_type,
2929
const std::string& metrics_directory,
3030
std::string_view session_id,
3131
const HostInfo& host_metrics);

0 commit comments

Comments
 (0)