您可以使用 Cloud Monitoring API 中的 timeSeries.list 方法来读取指标数据(也称为时序数据)。目前有多种方式可调用该方法,包括使用特定语言的客户端库,或使用 Metrics Explorer 创建图表。有关指标和时序的介绍,请参阅指标、时序和资源。如需了解如何读取指标数据,请参阅检索时序数据。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Observability in Cloud Tasks\n\n[Google Cloud Observability](/stackdriver/docs) provides monitoring, logging, and diagnostic\ntools. These tools can help you monitor and analyze Cloud Tasks\nactivity and growth, and understand the behavior, health, and performance of\nyour applications.\n\nBy default, Cloud Tasks is configured to do the following:\n\n- Send data and system audit logs to Cloud Logging. You can use the collected logs to debug, troubleshoot, and gain insights about your applications.\n- Send system and resource metrics to Cloud Monitoring. You can use the collected metrics to monitor health and performance, identify trends and issues, and notify for changes in behavior.\n\nSend audit logs to Cloud Logging\n--------------------------------\n\nCloud Tasks sends the following types of audit log data to\nCloud Logging:\n\n- [Admin Activity audit logs](/logging/docs/audit#admin-activity)\n- [Data Access audit logs](/logging/docs/audit#data-access)\n- [System Event audit logs](/logging/docs/audit#system-event)\n- [Policy Denied audit logs](/logging/docs/audit#policy_denied)\n\nData Access audit logs are\n[disabled by default](/logging/docs/audit/configure-data-access) because these\naudit logs can be quite large.\n\nFor more information, see\n[Cloud Tasks audit logging](/tasks/docs/audit-logging).\n\nUse Cloud Logging with queues\n-----------------------------\n\nYou can use Cloud Logging with Cloud Tasks queues. The following\noperations are logged:\n\n- Task operations:\n - `CreateTask`\n - `DeleteTask`\n- Attempt operations:\n - `AttemptDispatch`\n - `AttemptResponse`\n\n### Enable logging\n\nYou can enabling logging using the Google Cloud console or the Google Cloud CLI. \n\n### Console\n\n1. In the Google Cloud console, go to the **Cloud Tasks**\n \\\u003e **Queues** page.\n\n\n [Go to Queues](https://console.cloud.google.com/cloudtasks)\n\n \u003cbr /\u003e\n\n2. Click the name of the queue that you want to enable logging for.\n\n3. On the **Queue details** page, click the **Logs** tab.\n\n4. Click **Enable logs** and, to confirm, click **Enable**.\n\n### gcloud\n\nTo enable logging, you can use either the\n[`gcloud tasks queues create`](/sdk/gcloud/reference/tasks/queues/create)\nor the [`gcloud tasks queues update`](/sdk/gcloud/reference/tasks/queues/update)\ncommand. \n\n```bash\ngcloud tasks queues create QUEUE_ID \\\n --log-sampling-ratio=LOG_SAMPLING_RATION\n```\n\nor \n\n```bash\ngcloud tasks queues update QUEUE_ID \\\n --log-sampling-ratio=LOG_SAMPLING_RATIO\n```\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eQUEUE_ID\u003c/var\u003e: the ID of your queue\n- \u003cvar translate=\"no\"\u003eLOG_SAMPLING_RATIO\u003c/var\u003e: the fraction of the operations logged. Any value between `1.0` and `0.0` logs that fraction of operations. To log all operations, set it to `1.0`. To log no operations, set it to `0.0` (this is the default).\n\n### Disable logging\n\nYou can disable logging using the Google Cloud console or the Google Cloud CLI. \n\n### Console\n\n1. In the Google Cloud console, go to the **Cloud Tasks**\n \\\u003e **Queues** page.\n\n\n [Go to Queues](https://console.cloud.google.com/cloudtasks)\n\n \u003cbr /\u003e\n\n2. Click the name of the queue that you want to disable logging for.\n\n3. On the **Queue details** page, click the **Logs** tab.\n\n4. Click the ellipsis icon\n more_vert\\\u003e\n **Disable logs** . To confirm, click **Disable**.\n\n### gcloud\n\nTo disable logging, use the\n[`gcloud tasks queues update`](/sdk/gcloud/reference/tasks/queues/update)\ncommand and set the `log-sampling-ratio` flag to `0.0`. \n\n```bash\ngcloud tasks queues update QUEUE_ID \\\n --log-sampling-ratio=0.0\n```\n\nReplace \u003cvar translate=\"no\"\u003eQUEUE_ID\u003c/var\u003e with the ID of your queue.\n\nQuery and view logs\n-------------------\n\nYou can use following tools to view and analyze your logs:\n\n- **Logs Explorer** is designed to to help you troubleshoot and analyze the performance of your services and applications. To learn how to view logs in the Logs Explorer, follow the instructions in [View logs by using the Logs Explorer](/logging/docs/view/logs-explorer-interface).\n- **Log Analytics** is designed to run queries to analyze your logs, and then view or chart the results. To learn how to use Log Analytics, follow the instructions in [Query and analyze logs with Log Analytics](/logging/docs/analyze/query-and-view).\n\nYou can filter for Cloud Tasks logs in the Logs Explorer by\nsearching for `cloudtasks` in the search field.\n\nFor more information about querying and analyzing logs, see\n[Query and view logs overview](/logging/docs/log-analytics).\n\nSend metrics to Cloud Monitoring\n--------------------------------\n\nCloud Tasks sends metric data from monitored resources to\nGoogle Cloud Observability. A monitored resource in Monitoring represents a\nlogical or physical entity, such as a virtual machine, a database, or an\napplication. Monitored resources contain a unique set of metrics that can be\nexplored, reported through a dashboard, or used to create alerts.\n\nCloud Tasks sends metric data from the following monitored resource\nto Google Cloud Observability:\n\n- [`cloud_tasks_queues`](/monitoring/api/resources#tag_cloud_tasks_queue)\n\nEach resource also has a set of resource labels, which are key-value pairs that\nhold additional information about the resource. Resource labels are available\nfor all metrics associated with the resource and are detailed in the following\ntable.\n\n### Resource metrics and labels\n\n\nThe \"metric type\" strings in this table must be prefixed\nwith `cloudtasks.googleapis.com/`. That prefix has been\nomitted from the entries in the table.\nWhen querying a label, use the `metric.labels.` prefix; for\nexample, `metric.labels.`\u003cvar translate=\"no\"\u003eLABEL\u003c/var\u003e`=\"`\u003cvar translate=\"no\"\u003eVALUE\u003c/var\u003e`\"`.\n\nTable generated at 2025-08-20 20:46:56 UTC.\n\nTo view all resource types, see [Monitored resource types](/monitoring/api/resources).\nTo view all metric types, see [Google Cloud metrics](/monitoring/api/metrics_gcp).\n\n### Read metric data\n\nYou can read metric data, also called time-series data, by using the\n`timeSeries.list` method in the Cloud Monitoring API. There are several ways to\ncall the method, including using a language-specific client library, or by\ncreating a chart with\n[Metrics Explorer](/monitoring/charts/metrics-explorer). For an introduction to metrics\nand time series, see\n[Metrics, time series, and resources](/monitoring/api/v3/metrics). To learn how\nto read your metric data, see\n[Retrieve time-series data](/monitoring/custom-metrics/reading-metrics).\n\n### Use Monitoring dashboards and alerts\n\nYou can use Monitoring dashboards and their associated charts to\nvisualize the data for Cloud Tasks metrics.\n\nTo monitor these metrics in Monitoring, you can\n[create custom dashboards](/monitoring/charts/dashboards#custom_dashboards).\nYou can also [add alerts](/monitoring/alerts/using-alerting-api) based on\nthese metrics."]]