Compute Engine 活动日志与审核日志并不相同。审核日志不但包含旧版活动日志所含的信息,而且还会包含其他信息。我们建议您使用审核日志来代替活动日志。如果您已在使用活动日志,请参阅从活动日志迁移到审核日志。
Compute Engine 提供的活动日志可让您跟踪会对您的项目产生影响的特定事件,例如 API 调用和系统事件。 具体而言,活动日志会提供有关以下各项的信息:
- Compute Engine API 调用:
GCE_API_CALL
事件是会更改资源状态的 API 调用。例如,用于创建磁盘、更新实例元数据、创建实例组及更改机器类型的 API 调用会记录在活动日志中。不会更新资源的 API 调用(例如get
和list
请求)则不会记录在活动日志中。 - 操作日志:当 API 调用完成更改资源状态时,系统会记录
GCE_OPERATION_DONE
事件,而 Compute Engine 会返回活动日志中所记录的已完成操作事件。 - 系统日志:当 Compute Engine 执行系统事件时,系统会将
GCE_SYSTEM_EVENT
事件记录在活动日志中。例如,透明维护事件会记录为系统事件。
例如,对于 API 事件,活动日志会提供 API 请求的开始和结束时间、请求正文的细节、发出 API 请求的授权用户以及请求端点等详细信息。 您可以下载活动日志来搜索特定的 API 请求或查看由 Compute Engine 发起的系统事件。
活动日志不提供有关项目账单或使用情况的信息,例如虚拟机实例的运行时间或费用。如需了解账单日志,请参阅导出账单功能。如需了解用量日志,请参阅查看用量报告。
活动日志作为 Cloud Logging 服务的一部分提供。如需详细了解 Logging 的一般信息,请参阅 Cloud Logging 文档。
准备工作
- 熟悉 Cloud Logging。
-
如果您尚未设置身份验证,请进行设置。身份验证是通过其进行身份验证以访问 Google Cloud 服务和 API 的过程。如需从本地开发环境运行代码或示例,您可以按如下方式向 Compute Engine 进行身份验证。
Select the tab for how you plan to use the samples on this page:
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
-
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
- Set a default region and zone.
REST
如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 gcloud CLI 的凭据。
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证。
-
执行此任务所需的权限
您必须拥有以下权限才能执行此任务:
- 针对项目的
logging.logServiceIndexes.list
权限 - 针对项目的
logging.logServices.list
权限
查看日志
默认情况下,所有 Compute Engine 项目都会启用活动日志记录。
您可以通过 Google Cloud 控制台中的日志浏览器来查看项目的活动日志:
- 在 Google Cloud 控制台中,转到 Logging 页面。
- 在日志浏览器中,从第一个下拉列表中选择您的资源类型并进行过滤。
- 在所有日志下拉列表中选择 compute.googleapis.com/activity_log,以查看 Compute Engine 活动日志。
路由日志
如需了解如何导出活动日志,请参阅 Cloud Logging 文档中的配置和管理接收器。
识别日志文件
当您将日志导出到 Cloud Storage 时,系统会按日志条目对象文档所述的结构存储日志文件。
Compute Engine 日志文件会按以下目录结构进行存储:
<bucket>/compute.googleapis.com/activity_log/<year>/<month>/<day>
以下是一个存储在名为 my-bucket
的 Cloud Storage 存储桶中的日志文件名示例:
my-bucket/compute.googleapis.com/activity_log/2014/10/31/20:00:00_20:59:59_S0.json
在 BigQuery 中,活动日志会存储在一组表中,其中每个表对应一种日志类型和一个日期,并使用以下格式来命名:
<dataset>.compute_googleapis_com_activity_log_YYYYMMDD
如需了解如何在 BigQuery 中查询活动日志,请参阅 BigQuery 中的日志条目。
读取活动日志
活动日志结构如 LogEntry 类型文档所述。
Compute Engine 活动日志包含以下内容:
compute.googleapis.com/activity_log
:作为log
字段的值metadata
:描述时间戳等常规信息structPayload
:包含日志条目的具体内容
如需详细了解每条日志条目提供的通用字段,您可以查看 LogEntry 说明;但如需了解活动日志条目的载荷内容(这些内容特定于 Compute Engine),请参阅下文中的说明。
载荷内容
日志条目的内容会以 JSON 对象格式提供,并存储在 structPayload
字段中。structPayload
字段包含以下信息:
字段 | 类型 | 说明 |
---|---|---|
actor |
字符串 | 执行操作的用户或服务账号的电子邮件。此内容与 user_id 相同。 |
error |
字符串 | 如果在此事件期间发生任何错误,则提供错误详细信息。如果没有错误,则省略此字段。错误通常将造成请求无法成功完成。您可以使用此字段来调试失败的请求。 |
event_subtype |
字符串 | 将特定子类型描述为 API 方法。
例如,插入新实例的请求会显示为 请参阅 API 参考,获取 API 方法的完整列表。 |
event_timestamp_us |
时间戳 | 所记录事件的时间戳(从标准 Epoch 起算,以微秒为单位)。此内容与 metadata.timestamp 相同。 |
event_type |
字符串 | 描述一般事件类型。
可以是下列值之一:
|
info |
字符串 | 包含额外信息的可选字段(如果适用)。如果没有可显示的额外信息,则省略此字段。 |
operation |
字符串 | 当发出更新或更改任何资源的 API 请求时,系统会创建相应的操作对象以跟踪请求完成情况。这一属性会描述此事件的操作对象,并提供操作名称、操作地区或区域以及操作 ID 等信息。 |
request |
JSON | 包含原始 API 请求正文。
"request": { "url": "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/instances\", "body": "{ \"canIpForward\":"false", \"description\":\"\", \"disks\":[{ \"autoDelete\":true, \"boot\":true, \"deviceName\":\"example-instance\", \"initializeParams\":{ \"diskSizeGb\":\"10\", \"diskType\":\"https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/diskTypes/pd-standard\", \"sourceImage\":\"https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20150818\" }, \"mode\":\"READ_WRITE\", \"type\":\"PERSISTENT\" }], \"machineType\":\"https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/machineTypes/n1-standard-1\", \"metadata\":{ \"items\":[] }, \"name\":\"example-instance\", \"networkInterfaces\":[{ \"accessConfigs\":[{ \"name\":\"External NAT\", \"type\":\"ONE_TO_ONE_NAT\" }], \"network\":\"https://www.googleapis.com/compute/v1/projects/myproject/global/networks/default\" }], \"scheduling\":{ \"automaticRestart\":true, \"onHostMaintenance\":\"MIGRATE\", \"preemptible\":false }, \"serviceAccounts\":[{ \"email\":\"default\", \"scopes\":[ \"https://www.googleapis.com/auth/devstorage.read_only\", \"https://www.googleapis.com/auth/logging.write\" ] }], \"tags\":{ \"items\":[] }, \"zone\":\"https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f\" }" } |
resource |
JSON | 描述此事件正在修改的特定资源。
例如,虚拟机 (VM) 实例会被视为资源;虚拟机的资源属性示例如下所示:
"resource": { "type": "instance", "name": "example-instance", "id": "0", "zone": "us-central1-f" } API 参考文档中详述了资源类型的列表。 注意:如果某个操作会影响多个资源,则可能会有多个日志条目具有相同的 |
trace_id |
字符串 | 系统提供的跟踪 ID,用于对一个操作所触发的相关日志进行分组。例如:trace_id: "operation-1442436581415-51fe3700bd85a-7fd317e3-f1a3555e" |
user_agent |
字符串 | 描述执行了此请求的客户端。例如,如果您使用 Cloud Java 客户端库发出请求,则用户代理为 Google-API-Java-Client 。 |
version |
字符串 | 当前的日志格式版本会指示 Compute Engine 日志架构。当前版本为 1.2。
注意:Compute Engine API 版本控制不同于日志格式版本控制。 |
warning |
字符串 | 如果在此事件期间出现任何警告,则提供这些警告的详细信息。警告是参考信息,不影响请求,这和错误不同。 |
日志条目示例
例如,以下日志条目描述了创建虚拟机的 API 请求:
{ "log": "compute.googleapis.com/activity_log", "insertId": "2015-09-16|13:49:42.532185-07|10.106.9.208|335899593", "metadata": { "severity": "INFO", "projectId": "835469197146", "serviceName": "compute.googleapis.com", "zone": "us-central1-f", "labels":{ "compute.googleapis.com/resource_id":"0", "compute.googleapis.com/resource_name":"example-instance", "compute.googleapis.com/resource_type":"instance", "compute.googleapis.com/resource_zone":"us-central1-f" }, "timestamp": "2015-09-16T20:49:42.423637Z" }, "structPayload": { "version": "1.2", "trace_id": "operation-1442436581415-51fe3700bd85a-7fd317e3-f1a3555e", "event_timestamp_us": "1442436582423637", "event_type": "GCE_API_CALL", "event_subtype": "compute.instances.insert", "resource": { "type": "instance", "name": "example-instance", "id": "0", "zone": "us-central1-f" }, "actor": { "user": "user@example.com" }, "ip_address": "", "user_agent": "apitools-client/1.0", "request": { "url": "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/instances", "body": "{ \"canIpForward\":false, \"description\":\"\", \"disks\":[{ \"autoDelete\":true, \"boot\":true, \"deviceName\":\"example-instance\", \"initializeParams\":{ \"diskSizeGb\":\"10\", \"diskType\":\"https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/diskTypes/pd-standard\", \"sourceImage\":\"https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20150818\" }, \"mode\":\"READ_WRITE\", \"type\":\"PERSISTENT\" }], \"machineType\":\"https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/machineTypes/n1-standard-1\", \"metadata\":{ \"items\":[] }, \"name\":\"example-instance\", \"networkInterfaces\":[{ \"accessConfigs\":[{ \"name\":\"External NAT\", \"type\":\"ONE_TO_ONE_NAT\" }], \"network\":\"https://www.googleapis.com/compute/v1/projects/myproject/global/networks/default\" }], \"scheduling\":{ \"automaticRestart\":true, \"onHostMaintenance\":\"MIGRATE\", \"preemptible\":false }, \"serviceAccounts\":[{ \"email\":\"default\", \"scopes\":[ \"https://www.googleapis.com/auth/devstorage.read_only\", \"https://www.googleapis.com/auth/logging.write\" ] }], \"tags\":{ \"items\":[] }, \"zone\":\"https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f\" }" }, "operation": { "type": "operation", "name": "operation-1442436581415-51fe3700bd85a-7fd317e3-f1a3555e", "id": "291347737657178184", "zone": "us-central1-f" } } }
已弃用的活动日志条目
以下活动日志条目将于 2020 年 6 月 1 日停用且无替代项:
受监控的资源类型 | 事件子类型 |
---|---|
gce_backend_service |
BackendServiceConfigProgramming |
gce_instance |
RE_ENCRYPT_SSL_CERTIFICATE_FLOW |
gce_instance |
addFirewallRuleToSecurityPolicy |
gce_instance |
attachCloudLink |
gce_instance |
attachFirewallSecurityPolicy |
gce_instance |
compute.instanceGroupManagers.updateHealth |
gce_instance |
compute.instanceGroups.detachHealthCheck |
gce_instance |
compute.instanceNetworkConfig.updateName |
gce_instance |
compute.regionInstanceGroups.attachHealthCheck |
gce_instance |
compute.regionInstanceGroups.detachHealthCheck |
gce_instance |
createFirewallSecurityPolicy |
gce_instance |
deleteFirewallSecurityPolicy |
gce_instance |
detachFirewallSecurityPolicy |
gce_instance |
patchFirewallRuleInSecurityPolicy |
gce_instance |
removeCloudLink |
gce_instance |
removeFirewallRuleFromSecurityPolicy |
gce_instance |
updateFirewallSecurityPolicy |
gce_instance |
updateVpnTunnel |
gce_instance_group |
compute.instanceGroups.attachHealthCheck |
gce_instance_group |
compute.instanceGroups.attachNetworkInterfaces |
gce_instance_group |
compute.instanceGroups.detachHealthCheck |
gce_instance_group |
compute.regionInstanceGroups.attachHealthCheck |
gce_instance_group |
compute.regionInstanceGroups.detachHealthCheck |
gce_instance_template |
compute.zoneInstanceTemplates.insert |
gce_network |
compute.networks.switchLegacyToCustomMode |
gce_project |
compute.projects.moveProjectNetworking |
gce_reserved_address |
compute.addresses.insertDnsForwarding |
gce_reserved_address |
compute.addresses.insertNatAddress |
gce_ssl_certificate |
RE_ENCRYPT_SSL_CERTIFICATE_FLOW |
gce_ssl_certificate |
SslCertificateAddManagedCertificateChallenge |
gce_ssl_certificate |
SslCertificateProvisionManagedCertificate |
gce_ssl_certificate |
SslCertificateRemoveManagedCertificateChallenge |
gce_subnetwork |
compute.subnetworks.createOrUpdateVirtualSubnetwork |
vpn_tunnel |
updateVpnTunnel |