在 Artifact Registry 中配置平台日志,以便您可以查看有关向 Artifact Registry 代码库发出的成功和失败请求的信息。平台日志由 Cloud Logging 生成,可以在Logs Explorer中查看。您可以查看 Artifact Registry 平台日志,以便排查失败的请求、维护成功请求的记录,以及查看制品的下载次数。
平台日志的格式为 LogEntry。
概览
Artifact Registry 会为数据平面 API 调用和控制平面 API 调用生成平台日志。控制平面操作包括代码库管理操作(例如创建代码库、修改代码库说明和删除代码库)以及制品管理操作(例如列出制品、标记制品和删除制品)。数据平面操作包括从 Artifact Registry 代码库推送和拉取制品等操作。
这两种类型的来电的日志都包含以下信息:
- 请求详情
- 回答详情
- 状态
- 标签
- 方法名称
- 资源名称
- 资源位置
- 操作 ID(适用于长时间运行的操作)
- 代码库格式(适用于代码库资源)
- 代码库模式(适用于代码库资源)
此外,控制平面 API 调用的日志还包含以下信息:
- HTTP 信息
- 请求方法
- 请求网址
- 请求大小(以字节为单位)
- 状态
- 响应大小(以字节为单位)
- 用户代理
- 远程 IP(发出请求的客户端 IP)
- 协议
- Referer
- 延迟时间
您可以在 Logs Explorer 中查看 Artifact Registry 的平台日志。
准备工作
所需的角色
如需获得管理平台日志所需的权限,请让管理员向您授予项目的以下 IAM 角色:
- Artifact Registry Admin (
roles/artifactregistry.admin) - Logging Viewer (
roles/logging.viewer)
如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
启用平台日志记录
您可以为整个 Cloud de Confiance 项目或特定代码库启用平台日志记录。
为项目启用平台日志记录
如需为您的 Cloud de Confiance 项目启用平台日志记录,请运行以下命令:
gcloud CLI
gcloud artifacts projects update --enable-platform-logs --severity=SEVERITY --location LOCATION --project PROJECT
curl
curl -v -H "Authorization: Bearer $(gcloud auth print-access-token)" -X PATCH -H "X-GFE-SSL: yes" -H "Content-Type: application/json" -d '{"name": "projects/'PROJECT'/locations/'LOCATION'/projectConfig", "platformLogsConfig": {"logging_state": "ENABLED", "severity_level": "SEVERITY"}}' "https://artifactregistry.googleapis.com/v1/projects/PROJECT/locations/LOCATION/projectConfig"
其中:
- SEVERITY 决定了触发 Artifact Registry 平台日志创建的操作类型。您可以输入
INFO、ERROR或排除--severity字段,以针对所有支持的操作创建平台日志。 - LOCATION 是代码库的位置。
- PROJECT 是项目 ID。如果省略此标志,则系统会使用当前项目或默认项目。
为代码库启用平台日志记录
gcloud CLI
gcloud artifacts repositories update --enable-platform-logs --severity=SEVERITY REPOSITORY --location LOCATION --project PROJECT
curl
curl -v -H "Authorization: Bearer $(gcloud auth print-access-token)" -X PATCH \
-H "X-GFE-SSL: yes" -H "Content-Type: application/json" \
-d '{"name": "projects/'PROJECT'/locations/'LOCATION'/repositories/'REPOSITORY'", "platformLogsConfig": {"loggingState": "ENABLED", "severityLevel": "SEVERITY"}' \
"https://artifactregistry.googleapis.com/v1/projects/PROJECT/locations/LOCATION/repositories/REPOSITORY?updateMask=platformLogsConfig"
其中:
- SEVERITY 决定了触发 Artifact Registry 平台日志创建的操作类型。您可以输入
INFO、ERROR或排除--severity字段,以针对所有支持的操作创建平台日志。 - REPOSITORY 是您在 Artifact Registry 中的代码库的名称。
- LOCATION 是代码库的位置。
- PROJECT 是项目 ID。如果省略此标志,则系统会使用当前项目或默认项目。
验证平台日志记录配置
您可以验证是否已在项目或特定代码库中启用 Artifact Registry 平台日志记录。
验证项目的平台日志记录
如需验证您的项目中是否已启用 Artifact Registry 平台日志记录,请运行以下命令:
gcloud CLI
gcloud artifacts projects describe --location LOCATION --project PROJECT
curl
curl -v -H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://artifactregistry.googleapis.com/v1/projects/PROJECT/locations/LOCATION/projectConfig"
其中:
- LOCATION 是代码库的位置。
- PROJECT 是项目 ID。如果省略此标志,则系统会使用当前项目或默认项目。
输出类似于以下内容:
{
"name": "projects/PROJECT/locations/{"<var>LOCATION</var>"}}/projectConfig"
"platformLogsConfig": {
"loggingState": "ENABLED"
"severityLevel": "INFO"
}
}
验证代码库的平台日志记录
如需验证是否已在代码库中启用 Artifact Registry 平台日志记录,请运行以下命令:
gcloud CLI
gcloud artifacts repositories describe REPOSITORY --location LOCATION --project PROJECT
curl
curl -v -H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://artifactregistry.googleapis.com/v1/projects/PROJECT/locations/LOCATION/repositories/REPOSITORY"
其中:
- REPOSITORY 是代码库的名称。
- LOCATION 是代码库的位置。
- PROJECT 是项目 ID。如果省略此标志,则系统会使用当前项目或默认项目。
输出类似于以下内容:
{
"name": "projects/PROJECT/locations/LOCATION/repositories/REPOSITORY"
"platformLogsConfig": {
"loggingState": "ENABLED",
"severityLevel": "INFO"
}
}
停用平台日志记录
您可以为整个 Cloud de Confiance 项目或特定代码库停用平台日志记录。
为项目停用平台日志记录
如需为您的 Cloud de Confiance 项目停用平台日志记录,请运行以下命令:
gcloud CLI
gcloud artifacts projects update --disable-platform-logs --location LOCATION --project PROJECT
curl
curl -v -H "Authorization: Bearer $(gcloud auth print-access-token)" -X PATCH \
-H "X-GFE-SSL: yes" -H "Content-Type: application/json" \
-d '{"name": "projects/'PROJECT'/locations/'LOCATION'/projectConfig", "platformLogsConfig": {"logging_state": "DISABLED"}}' \
"https://artifactregistry.googleapis.com/v1/projects/PROJECT/locations/LOCATION/projectConfig"
其中:
- LOCATION 是代码库的位置。
- PROJECT 是项目 ID。如果省略此标志,则系统会使用当前项目或默认项目。
为代码库停用平台日志记录
如需为代码库停用平台日志记录,请运行以下命令:
gcloud CLI
gcloud artifacts repositories update --disable-platform-logs REPOSITORY --location LOCATION --project PROJECT
curl
curl -v -H "Authorization: Bearer $(gcloud auth print-access-token)" -X PATCH \
-H "X-GFE-SSL: yes" -H "Content-Type: application/json" \
-d '{"name": "projects/'PROJECT'/locations/'LOCATION'/repositories/'REPOSITORY'", "platformLogsConfig": {"loggingState": "DISABLED", "severityLevel": "INFO"}' \
"https://artifactregistry.googleapis.com/v1/projects/PROJECT/locations/LOCATION/repositories/REPOSITORY?updateMask=platformLogsConfig"
其中:
- REPOSITORY 是您在 Artifact Registry 中的代码库的名称。
- LOCATION 是代码库的位置。
- PROJECT 是项目 ID。如果省略此标志,则系统会使用当前项目或默认项目。
清除平台日志记录配置
您可以清除项目或代码库的平台日志记录配置。运行清除命令后,项目或代码库的 loggingState 和 severityLevel 会设置为 null。
如果您清除项目的配置,则只有已启用平台日志记录的代码库才会生成平台日志。如果您清除代码库的配置,该代码库会从项目中继承其平台日志记录配置。
清除项目的平台日志记录配置
如需清除 Cloud de Confiance 项目的平台日志记录配置,请运行以下命令:
gcloud CLI
gcloud artifacts projects update --clear-platform-logs --location LOCATION --project PROJECT
curl
curl -v -H "Authorization: Bearer $(gcloud auth print-access-token)" -X PATCH \
-H "X-GFE-SSL: yes" -H "Content-Type: application/json" \
-d '{"name": "projects/'PROJECT'/locations/'LOCATION'/projectConfig", "platformLogsConfig": {}}' \
"https://artifactregistry.googleapis.com/v1/projects/PROJECT/locations/LOCATION/projectConfig"
其中:
- LOCATION 是代码库的位置。
- PROJECT 是项目 ID。如果省略此标志,则系统会使用当前项目或默认项目。
清除代码库的平台日志记录配置
如需清除代码库的平台日志记录配置,请运行以下命令:
gcloud CLI
gcloud artifacts repositories update --clear-platform-logs REPOSITORY --location LOCATION --project PROJECT
curl
curl -v -H "Authorization: Bearer $(gcloud auth print-access-token)" -X PATCH \
-H "X-GFE-SSL: yes" -H "Content-Type: application/json" \
-d '{"name": "projects/'PROJECT'/locations/'LOCATION'/repositories/'REPOSITORY'", "platformLogsConfig": {}' \
"https://artifactregistry.googleapis.com/v1/projects/PROJECT/locations/LOCATION/repositories/REPOSITORY?updateMask=platformLogsConfig"
其中:
- REPOSITORY 是您在 Artifact Registry 中的代码库的名称。
- LOCATION 是代码库的位置。
- PROJECT 是项目 ID。如果省略此标志,则系统会使用当前项目或默认项目。
查看平台日志
如需查看平台日志,请执行以下操作:
在 Cloud de Confiance 控制台中打开“日志浏览器”页面。
在查询窗格中,点击日志名称下拉菜单。
输入 requests,然后选中 Artifact Registry 标题下的 requests 复选框。
点击应用。
Logging 会在查询结果窗格中显示来自数据平面和控制平面 API 调用的日志。
如需查看日志的内容,请点击查询结果中列出的任何日志旁边的箭头。
如需详细了解如何在 Logs Explorer 中解读查询结果,请参阅查看查询结果。
使用 Observability Analytics 分析平台日志
您可以使用 Observability Analytics 对 Artifact Registry 日志运行 SQL 查询,从而更详细地分析日志。本部分包含针对常见用例的多个示例查询。您可以通过过滤 LogEntry 结构中的不同字段来调整这些查询,以分析 Artifact Registry 使用情况的其他方面。
在开始使用 Observability Analytics 之前,请执行以下操作:
配置日志存储分区以使用 Observability Analytics。
配置 Identity and Access Management 角色和权限,以使用可观测性分析。
接下来,按照相应步骤输入并运行自定义查询。 进入 SQL 查询窗格后,您可以输入以下示例查询之一:
示例:按下载量排名的前 10 个 IP 地址
此查询会评估您的 Artifact Registry 平台日志,并返回过去 7 天内从您的代码库下载数据最多的前 10 个 IP 地址:
SELECT
http_request.remote_ip AS client_ip,
SUM(http_request.response_size) AS total_download_bytes
FROM
`YOUR_PROJECT.YOUR_LOCATION.YOUR_BUCKET._AllLogs`
WHERE
log_id = "artifactregistry.googleapis.com/requests"
AND JSON_VALUE(resource.labels.repository_id) IS NOT NULL
AND http_request.request_method = "GET"
AND http_request.status >= 200 AND http_request.status < 300
AND http_request.response_size > 0
AND CAST(http_request.response_size AS INT64) > 0
AND timestamp >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 DAY)
GROUP BY
client_ip
ORDER BY
total_download_bytes DESC
LIMIT 10;
示例:每个代码库的请求数
此查询会评估您的 Artifact Registry 平台日志,并返回过去 24 小时内向每个代码库发出的请求数:
SELECT
JSON_VALUE(resource.labels.repository_id) AS repository,
JSON_VALUE(resource.labels.location) AS location,
COUNT(*) AS request_count
FROM
`YOUR_PROJECT.YOUR_LOCATION.YOUR_BUCKET._AllLogs`
WHERE
log_id = "artifactregistry.googleapis.com/requests"
AND JSON_VALUE(resource.labels.repository_id) IS NOT NULL
AND timestamp >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 DAY)
GROUP BY
repository,
location
ORDER BY
request_count DESC;
示例:失败的请求
此查询会评估您的 Artifact Registry 平台日志,并列出过去 1 小时内最近 10 个失败请求的详细信息:
SELECT
timestamp,
http_request.status,
http_request.request_method,
http_request.request_url,
http_request.remote_ip,
JSON_VALUE(resource.labels.repository_id) AS repository,
JSON_VALUE(labels.resource_name) AS artifact_name,
trace
FROM
`YOUR_PROJECT.YOUR_LOCATION.YOUR_BUCKET._AllLogs`
WHERE
log_id = "artifactregistry.googleapis.com/requests"
AND JSON_VALUE(resource.labels.repository_id) IS NOT NULL
AND http_request.status >= 400
AND timestamp >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 HOUR)
ORDER BY
timestamp DESC
LIMIT 10;