diff --git a/base/cvd/cuttlefish/host/libs/metrics/BUILD.bazel b/base/cvd/cuttlefish/host/libs/metrics/BUILD.bazel index 014e42a52a..54179e3ccc 100644 --- a/base/cvd/cuttlefish/host/libs/metrics/BUILD.bazel +++ b/base/cvd/cuttlefish/host/libs/metrics/BUILD.bazel @@ -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", ], ) @@ -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", diff --git a/base/cvd/cuttlefish/host/libs/metrics/metrics_conversion.cc b/base/cvd/cuttlefish/host/libs/metrics/metrics_conversion.cc index ebfcae134c..f86ed65782 100644 --- a/base/cvd/cuttlefish/host/libs/metrics/metrics_conversion.cc +++ b/base/cvd/cuttlefish/host/libs/metrics/metrics_conversion.cc @@ -16,22 +16,23 @@ #include "cuttlefish/host/libs/metrics/metrics_conversion.h" -#include +#include +#include +#include #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:: @@ -39,8 +40,21 @@ using logs::proto::wireless::android::cuttlefish::events:: 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) { @@ -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(); @@ -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 diff --git a/base/cvd/cuttlefish/host/libs/metrics/metrics_conversion.h b/base/cvd/cuttlefish/host/libs/metrics/metrics_conversion.h index 78510bf4f4..531a034340 100644 --- a/base/cvd/cuttlefish/host/libs/metrics/metrics_conversion.h +++ b/base/cvd/cuttlefish/host/libs/metrics/metrics_conversion.h @@ -16,6 +16,7 @@ #pragma once +#include #include #include "cuttlefish/common/libs/utils/host_info.h" @@ -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 diff --git a/base/cvd/cuttlefish/host/libs/metrics/metrics_orchestration.cc b/base/cvd/cuttlefish/host/libs/metrics/metrics_orchestration.cc index e3cbc7ff82..ef6eb9ceb4 100644 --- a/base/cvd/cuttlefish/host/libs/metrics/metrics_orchestration.cc +++ b/base/cvd/cuttlefish/host/libs/metrics/metrics_orchestration.cc @@ -16,6 +16,7 @@ #include "cuttlefish/host/libs/metrics/metrics_orchestration.h" +#include #include #include @@ -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" @@ -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(now); +} + std::string GetMetricsDirectoryFilepath( const LocalInstanceGroup& instance_group) { return instance_group.HomeDir() + "/metrics"; @@ -63,9 +70,11 @@ Result 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) {