[[["易于理解","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-28。"],[],[],null,["# Use the code editor for PromQL\n\nThis page describes how to use the code editor for PromQL. The editor is\navailable in the Google Cloud console and provides suggestions and other tools\nfor creating valid PromQL queries.\n\nThis page doesn't cover the PromQL language; for information about\nPromQL, we recommend consulting open source documentation. The following\nresources can help you get started:\n\n- [Querying Prometheus](https://prometheus.io/docs/prometheus/latest/querying/basics/)\n- [Query examples](https://prometheus.io/docs/prometheus/latest/querying/examples/)\n- [PromQL cheat sheet](https://promlabs.com/promql-cheat-sheet/)\n\nAccess the code editor\n----------------------\n\nYou access PromQL from the following pages in the Google Cloud console:\n\n- **Metrics Explorer**\n- **Add widget** when creating dashboards\n\nTo open the code editor when using Metrics Explorer, do the following:\n\n1. In the Google Cloud console, go to the\n *leaderboard* **Metrics explorer** page:\n\n [Go to **Metrics explorer**](https://console.cloud.google.com/monitoring/metrics-explorer)\n\n \u003cbr /\u003e\n\n If you use the search bar to find this page, then select the result whose subheading is\n **Monitoring**.\n2. In the toolbar of the query-builder pane, select the button whose name is either *code* **MQL** or *code* **PromQL**.\n3. Verify that **PromQL** is selected in the **Language** toggle. The language toggle is in the same toolbar that lets you format your query.\n4. After you complete your query, you can configure the legend or change how\n the data is displayed. For more information, see the following pages:\n\n - [Set chart display options](/monitoring/charts/chart-view-options)\n - [Configure legends](/monitoring/charts/working-with-legends)\n\nUse the code editor\n-------------------\n\nTo use the code editor, type your query into the text field and\nclick **Run Query**.\n\n### Autocompletion\n\nAs you type your query, the editor displays a list of syntactically valid\ncompletions. You can manually bring up the autocompletion menu by pressing\n`Control+Space`, and you can dismiss it by pressing `Escape`.\n\nTo navigate the list of options, use the arrow keys.\nPressing `Enter` inserts the selected choice.\n\nTo insert the prefix common to all available choices, press `Tab`.\n\n### Find and replace\n\nThe editor supports a find-and-replace feature. To find, and optionally\nreplace, terms in your query, press `Control+F` (or `Command+F` on Mac). The\nfollowing screenshot shows the feature:\n\n### Editor controls\n\nThe editor has two controls that determine how it interacts with your query.\n\nBy default, your query is executed only when you click **Run Query**.\nYou can set up the editor to automatically execute your query as\nyou type, but this option is disabled by default.\n\nTo enable the auto-run option, use the **Auto-run** toggle.\nWhen this option is enabled, the editor works as follows:\n\n- The **Run Query** button isn't displayed.\n- Errors messages appear continuously as you type.\n- If your cursor is at the end of a query that is incomplete but has no\n errors, then the editor shows a yellow triangle in the control bar. If there\n is room, the text **Incomplete query** also appears.\n\n The editor does not attempt to evaluate incomplete queries.\n\nThere is also a grab-bar between the code editor and the chart region.\nUse this bar to change the relative sizes of the two regions.\n\nSave charts\n-----------\n\nHow you save a chart depends on the context in which you created the\nchart:\n\n- From Metrics Explorer.\n- As part of creating or editing a custom dashboard.\n\n### Save charts from Metrics Explorer\n\nFrom the code editor in Metrics Explorer, you have the option to save\nyour chart to a custom dashboard. To save your chart, do the following:\n\n1. In the Google Cloud console, go to the\n *leaderboard* **Metrics explorer** page:\n\n [Go to **Metrics explorer**](https://console.cloud.google.com/monitoring/metrics-explorer)\n\n \u003cbr /\u003e\n\n If you use the search bar to find this page, then select the result whose subheading is\n **Monitoring**.\n2. In the toolbar of the query-builder pane, select the button whose name is either *code* **MQL** or *code* **PromQL**.\n3. Verify that **PromQL** is selected in the **Language** toggle. The language toggle is in the same toolbar that lets you format your query.\n4. Enter your query in the editor.\n5.\n If you do not have the auto-run option for the code editor enabled, then run\n your query by clicking **Run Query** before saving the chart.\n By default, custom dashboards periodically save your changes. When you don't run\n your query first, the code editor saves the last query you ran.\n\n6. Click **Save Chart**.\n\n7. To continue saving the chart:\n\n 1. Give the chart a descriptive title.\n 2. Select the dashboard for the new chart.\n 3. Click **Save** on the dialog.\n\n### Save charts on a custom dashboard\n\nTo save a new chart to your dashboard, do the following:\n\n1. Give the chart a descriptive title.\n\n2.\n If you do not have the auto-run option for the code editor enabled, then run\n your query by clicking **Run Query** before saving the chart.\n By default, custom dashboards periodically save your changes. When you don't run\n your query first, the code editor saves the last query you ran.\n\n3. If you don't have autosave enabled for your dashboard, then\n click **Save**.\n\nThe Google Cloud console supports [page-level filters for custom\ndashboards](/monitoring/charts/filter-dashboard). These filters are supported for PromQL\ncharts on your custom dashboards.\n\nExit the code editor\n--------------------\n\nTo exit the code editor without saving your query,\nclick one of the other selector tabs. Any query text in the editor\nis discarded, but you are given the option to save it to the clipboard."]]