Skip to content
Open
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
65 changes: 64 additions & 1 deletion base/cvd/cuttlefish/host/libs/metrics/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,38 @@ cf_cc_library(
],
)

cf_cc_library(
name = "gce_environment",
srcs = [
"gce_environment.cc",
],
hdrs = [
"gce_environment.h",
],
deps = [
"//cuttlefish/host/libs/web/http_client",
"//cuttlefish/host/libs/web/http_client:curl_global_init",
"//cuttlefish/host/libs/web/http_client:curl_http_client",
"//cuttlefish/host/libs/web/http_client:http_string",
"//cuttlefish/result",
"@abseil-cpp//absl/strings",
"@fmt",
],
)

cf_cc_library(
name = "github_environment",
srcs = [
"github_environment.cc",
],
hdrs = [
"github_environment.h",
],
deps = [
"//cuttlefish/common/libs/utils:environment",
],
)

cf_cc_library(
name = "guest_metrics",
srcs = [
Expand All @@ -86,6 +118,36 @@ cf_cc_library(
],
)

cf_cc_library(
name = "host_metrics",
srcs = [
"host_metrics.cc",
],
hdrs = [
"host_metrics.h",
],
deps = [
"//cuttlefish/common/libs/utils:host_info",
"//cuttlefish/host/libs/metrics:gce_environment",
"//cuttlefish/host/libs/metrics:github_environment",
"//cuttlefish/host/libs/metrics:invoker",
"//cuttlefish/result",
],
)

cf_cc_library(
name = "invoker",
srcs = [
"invoker.cc",
],
hdrs = [
"invoker.h",
],
deps = [
"//cuttlefish/common/libs/utils:environment",
],
)

cf_cc_library(
name = "metrics",
srcs = [
Expand Down Expand Up @@ -123,6 +185,7 @@ cf_cc_library(
"//cuttlefish/host/libs/metrics:device_event_type",
"//cuttlefish/host/libs/metrics:flag_metrics",
"//cuttlefish/host/libs/metrics:guest_metrics",
"//cuttlefish/host/libs/metrics:host_metrics",
"//external_proto:cf_flags_cc_proto",
"//external_proto:cf_guest_cc_proto",
"//external_proto:cf_host_cc_proto",
Expand Down Expand Up @@ -161,13 +224,13 @@ cf_cc_library(
],
deps = [
"//cuttlefish/common/libs/utils:files",
"//cuttlefish/common/libs/utils:host_info",
"//cuttlefish/common/libs/utils:tee_logging",
"//cuttlefish/host/commands/cvd/version",
"//cuttlefish/host/libs/metrics:device_event_type",
"//cuttlefish/host/libs/metrics:enabled",
"//cuttlefish/host/libs/metrics:flag_metrics",
"//cuttlefish/host/libs/metrics:guest_metrics",
"//cuttlefish/host/libs/metrics:host_metrics",
"//cuttlefish/host/libs/metrics:metrics_conversion",
"//cuttlefish/host/libs/metrics:metrics_transmitter",
"//cuttlefish/host/libs/metrics:metrics_writer",
Expand Down
94 changes: 94 additions & 0 deletions base/cvd/cuttlefish/host/libs/metrics/gce_environment.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (C) 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <vector>

#include <fmt/format.h>
#include "absl/strings/str_split.h"

#include "cuttlefish/host/libs/web/http_client/curl_global_init.h"
#include "cuttlefish/host/libs/web/http_client/curl_http_client.h"
#include "cuttlefish/host/libs/web/http_client/http_client.h"
#include "cuttlefish/host/libs/web/http_client/http_string.h"
#include "cuttlefish/result/result.h"

namespace cuttlefish {
namespace {

constexpr std::string_view kDetectUrl = "metadata.google.internal";
constexpr std::string_view kDetectHeader = "Metadata-Flavor";
constexpr std::string_view kExpectedDetectHeaderValue = "Google";

// https://docs.cloud.google.com/compute/docs/metadata/predefined-metadata-keys
constexpr std::string_view kGceMetadataUrlBase =
"http://metadata.google.internal/computeMetadata/v1";
constexpr std::string_view kNumericProjectId = "project/numeric-project-id";
constexpr std::string_view kZone = "instance/zone";

// https://docs.cloud.google.com/compute/docs/instances/detect-compute-engine
Result<bool> IsGceEnvironment(HttpClient& http_client) {
const HttpResponse<std::string> response =
CF_EXPECT(HttpGetToString(http_client, std::string(kDetectUrl)));
return HeaderValue(response.headers, kDetectHeader) ==
kExpectedDetectHeaderValue;
}

// https://docs.cloud.google.com/compute/docs/metadata/querying-metadata
Result<std::string> QueryGceMetadata(HttpClient& http_client,
std::string_view metadata_key) {
const std::vector<std::string> required_metadata_query_header = {
fmt::format("{}: {}", kDetectHeader, kExpectedDetectHeaderValue)};
const std::string url =
fmt::format("{}/{}", kGceMetadataUrlBase, metadata_key);
HttpResponse<std::string> response = CF_EXPECT(
HttpGetToString(http_client, url, required_metadata_query_header));
return response.data;
}

Result<std::string> GetZoneValue(std::string_view response_string) {
std::vector<std::string> components = absl::StrSplit(response_string, "/");
CF_EXPECTF(
components.size() == 4 && components[2] == "zones",
"Unexpected format in response string for zone metadata, expected "
"the form \"projects/<project_num>/zones/<zone>\" and received: {}",
response_string);
return components[3];
}

} // namespace

Result<std::optional<GceEnvironment>> DetectGceEnvironment() {
CurlGlobalInit curl_init;
std::unique_ptr<HttpClient> http_client = CurlHttpClient();
if (!CF_EXPECT(IsGceEnvironment(*http_client))) {
return std::nullopt;
}
const std::string zone_response =
CF_EXPECT(QueryGceMetadata(*http_client, kZone));
return GceEnvironment{
.numeric_project_id =
CF_EXPECT(QueryGceMetadata(*http_client, kNumericProjectId)),
.zone = CF_EXPECT(GetZoneValue(zone_response)),
};
}

} // namespace cuttlefish
33 changes: 33 additions & 0 deletions base/cvd/cuttlefish/host/libs/metrics/gce_environment.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <optional>
#include <string>

#include "cuttlefish/result/result.h"

namespace cuttlefish {

struct GceEnvironment {
std::string numeric_project_id;
std::string zone;
};

Result<std::optional<GceEnvironment>> DetectGceEnvironment();

} // namespace cuttlefish
58 changes: 58 additions & 0 deletions base/cvd/cuttlefish/host/libs/metrics/github_environment.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

#include <optional>
#include <string>
#include <string_view>

#include "cuttlefish/common/libs/utils/environment.h"

namespace cuttlefish {
namespace {

// https://docs.github.com/en/actions/reference/workflows-and-actions/variables
// set to "true" when GitHub Actions is running the workflow
constexpr std::string_view kGitHubActions = "GITHUB_ACTIONS";
// holds the owner and repository name
constexpr std::string_view kGitHubRepository = "GITHUB_REPOSITORY";

constexpr std::string_view kAndroidCuttlefish = "google/android-cuttlefish";
constexpr std::string_view kCloudAndroidOrchestration =
"google/cloud-android-orchestration";

GitHubRepository ToGitHubRepository(std::string_view gh_env_str) {
if (gh_env_str == kAndroidCuttlefish) {
return GitHubRepository::AndroidCuttlefish;
} else if (gh_env_str == kCloudAndroidOrchestration) {
return GitHubRepository::CloudAndroidOrchestration;
} else {
return GitHubRepository::Unknown;
}
}

} // namespace

std::optional<GitHubRepository> DetectGitHubRepository() {
if (StringFromEnv(std::string(kGitHubActions)) != "true") {
return std::nullopt;
}
return ToGitHubRepository(
StringFromEnv(std::string(kGitHubRepository), "unknown"));
}

} // namespace cuttlefish
31 changes: 31 additions & 0 deletions base/cvd/cuttlefish/host/libs/metrics/github_environment.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <optional>

namespace cuttlefish {

enum class GitHubRepository {
AndroidCuttlefish,
CloudAndroidOrchestration,
Unknown,
};

std::optional<GitHubRepository> DetectGitHubRepository();

} // namespace cuttlefish
55 changes: 55 additions & 0 deletions base/cvd/cuttlefish/host/libs/metrics/host_metrics.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

#include <optional>

#include "cuttlefish/common/libs/utils/host_info.h"
#include "cuttlefish/host/libs/metrics/gce_environment.h"
#include "cuttlefish/host/libs/metrics/github_environment.h"
#include "cuttlefish/host/libs/metrics/invoker.h"
#include "cuttlefish/result/result.h"

namespace cuttlefish {
namespace {

Result<std::optional<Environment>> GetEnvironment() {
const std::optional<GitHubRepository> github_environment =
DetectGitHubRepository();
if (github_environment) {
return github_environment;
}

const std::optional gce_environment = CF_EXPECT(DetectGceEnvironment());
if (gce_environment) {
return gce_environment;
}

return std::nullopt;
}

} // namespace

Result<HostMetrics> GetHostMetrics() {
return HostMetrics{
.os = GetHostInfo(),
.invoker = GetInvoker(),
.environment = CF_EXPECT(GetEnvironment()),
};
}

} // namespace cuttlefish
Loading
Loading