Skip to content

Commit ac957ca

Browse files
committed
Add logic for saving and retrieving session IDs
And add session_id to the metrics event file. `GenerateUuid` copied from the recently deleted `cvd_metrics_api.cpp`. See 4c3a2b1 for the deletion commit. Bug: 448398451
1 parent aa1ea15 commit ac957ca

File tree

7 files changed

+112
-2
lines changed

7 files changed

+112
-2
lines changed

base/cvd/MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ bazel_dep(name = "libarchive", version = "3.7.9")
2929
bazel_dep(name = "libevent", version = "2.1.12-stable.bcr.0")
3030
bazel_dep(name = "libjpeg_turbo", version = "2.1.91")
3131
bazel_dep(name = "libpng", version = "1.6.47.bcr.1")
32+
bazel_dep(name = "libuuid", version = "2.39.3.bcr.1")
3233
bazel_dep(name = "libxml2", version = "2.13.5")
3334
bazel_dep(name = "libzip", version = "1.10.1")
3435
bazel_dep(name = "lz4", version = "1.9.4.bcr.2")

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ cf_cc_library(
4747
"//cuttlefish/common/libs/utils:result",
4848
"//cuttlefish/host/commands/cvd/instances",
4949
"//cuttlefish/host/libs/metrics:metrics_writer",
50+
"//cuttlefish/host/libs/metrics:session_id",
5051
"//libbase",
5152
"@fmt",
5253
],
@@ -69,3 +70,18 @@ cf_cc_library(
6970
"@fmt",
7071
],
7172
)
73+
74+
cf_cc_library(
75+
name = "session_id",
76+
srcs = [
77+
"session_id.cc",
78+
],
79+
hdrs = [
80+
"session_id.h",
81+
],
82+
deps = [
83+
"//cuttlefish/common/libs/utils:files",
84+
"//cuttlefish/common/libs/utils:result",
85+
"@libuuid",
86+
],
87+
)

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "cuttlefish/common/libs/utils/result.h"
2727
#include "cuttlefish/host/commands/cvd/instances/instance_group_record.h"
2828
#include "cuttlefish/host/libs/metrics/metrics_writer.h"
29+
#include "cuttlefish/host/libs/metrics/session_id.h"
2930

3031
namespace cuttlefish {
3132
namespace {
@@ -46,14 +47,17 @@ std::string GetMetricsDirectoryFilepath(
4647
Result<void> SetUpMetrics(const std::string& metrics_directory) {
4748
CF_EXPECT(EnsureDirectoryExists(metrics_directory));
4849
CF_EXPECT(WriteNewFile(metrics_directory + "/README", kReadmeText));
50+
CF_EXPECT(GenerateSessionIdFile(metrics_directory));
4951
return {};
5052
}
5153

5254
Result<void> GatherMetrics(const std::string& metrics_directory) {
55+
const std::string session_id =
56+
CF_EXPECT(ReadSessionIdFile(metrics_directory));
5357
HostInfo host_metrics = GetHostInfo();
5458
// TODO: chadreynolds - gather the rest of the data (guest/flag information)
5559
// TODO: chadreynolds - convert data to the proto representation
56-
CF_EXPECT(WriteMetricsEvent(metrics_directory, host_metrics));
60+
CF_EXPECT(WriteMetricsEvent(metrics_directory, session_id, host_metrics));
5761
// TODO: chadreynolds - if <TBD> condition, transmit metrics event as well
5862
return {};
5963
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <chrono>
2020
#include <string>
21+
#include <string_view>
2122

2223
#include <fmt/format.h>
2324

@@ -37,12 +38,15 @@ std::string GenerateFilenameSuffix() {
3738
} // namespace
3839

3940
Result<void> WriteMetricsEvent(const std::string& metrics_directory,
41+
std::string_view session_id,
4042
const HostInfo& host_metrics) {
4143
const std::string event_filepath =
4244
fmt::format("{}/vm-instantiation_{}_{}", metrics_directory,
4345
std::chrono::system_clock::now(), GenerateFilenameSuffix());
4446
// TODO: chadreynolds - convert (what will be a proto) to text
45-
CF_EXPECT(WriteNewFile(event_filepath, host_metrics.to_string()));
47+
CF_EXPECT(WriteNewFile(
48+
event_filepath,
49+
fmt::format("{}\n{}\n", host_metrics.to_string(), session_id)));
4650
return {};
4751
}
4852

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#pragma once
1818

1919
#include <string>
20+
#include <string_view>
2021

2122
#include "cuttlefish/common/libs/utils/host_info.h"
2223
#include "cuttlefish/common/libs/utils/result.h"
@@ -25,6 +26,7 @@ namespace cuttlefish {
2526

2627
// TODO: chadreynolds - add support for more fields than HostInfo
2728
Result<void> WriteMetricsEvent(const std::string& metrics_directory,
29+
std::string_view session_id,
2830
const HostInfo& host_metrics);
2931

3032
} // namespace cuttlefish
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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/session_id.h"
18+
19+
#include <string>
20+
#include <string_view>
21+
22+
#include <uuid/uuid.h>
23+
24+
#include "cuttlefish/common/libs/utils/files.h"
25+
#include "cuttlefish/common/libs/utils/result.h"
26+
27+
namespace cuttlefish {
28+
namespace {
29+
30+
constexpr char kSessionIdFileName[] = "metrics_session_id.txt";
31+
constexpr int kUuidStringLength = 36; // per uuid_unparse(3)
32+
33+
std::string GenerateUuid() {
34+
uuid_t uuid;
35+
uuid_generate_random(uuid);
36+
std::string uuid_str = std::string(kUuidStringLength, 'x');
37+
uuid_unparse(uuid, uuid_str.data());
38+
return uuid_str;
39+
}
40+
41+
} // namespace
42+
43+
Result<std::string> ReadSessionIdFile(const std::string& metrics_directory) {
44+
return CF_EXPECT(ReadFileContents(metrics_directory + kSessionIdFileName));
45+
}
46+
47+
Result<void> GenerateSessionIdFile(const std::string& metrics_directory) {
48+
const std::string session_id = GenerateUuid();
49+
CF_EXPECT(WriteNewFile(metrics_directory + kSessionIdFileName, session_id));
50+
return {};
51+
}
52+
53+
} // namespace cuttlefish
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
#include <string_view>
21+
22+
#include "cuttlefish/common/libs/utils/result.h"
23+
24+
namespace cuttlefish {
25+
26+
Result<std::string> ReadSessionIdFile(const std::string& metrics_directory);
27+
28+
Result<void> GenerateSessionIdFile(const std::string& metrics_directory);
29+
30+
} // namespace cuttlefish

0 commit comments

Comments
 (0)