列出指标描述符
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
演示了如何列出可用的指标描述符。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
Node.js
如需向 Monitoring 进行身份验证,请设置应用默认凭证。
如需了解详情,请参阅为本地开发环境设置身份验证。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[],[],null,["Demonstrates how to list available metric descriptors.\n\nExplore further\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [List metric and resource types](/monitoring/custom-metrics/browsing-metrics)\n\nCode sample \n\nC#\n\n\nTo authenticate to Monitoring, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n public static object ListMetrics(string projectId)\n {\n MetricServiceClient client = MetricServiceClient.Create();\n ProjectName projectName = new ProjectName(projectId);\n PagedEnumerable\u003cListMetricDescriptorsResponse, MetricDescriptor\u003e metrics = client.ListMetricDescriptors(projectName);\n foreach (MetricDescriptor metric in metrics)\n {\n Console.WriteLine($\"{metric.Name}: {metric.DisplayName}\");\n }\n return 0;\n }\n\nGo\n\n\nTo authenticate to Monitoring, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \tmonitoring \"cloud.google.com/go/monitoring/apiv3\"\n \t\"cloud.google.com/go/monitoring/apiv3/v2/monitoringpb\"\n \t\"google.golang.org/api/iterator\"\n )\n\n // listMetrics lists all the metrics available to be monitored in the API.\n func listMetrics(w io.Writer, projectID string) error {\n \tctx := context.Background()\n \tc, err := monitoring.NewMetricClient(ctx)\n \tif err != nil {\n \t\treturn err\n \t}\n \tdefer c.Close()\n\n \treq := &monitoringpb.ListMetricDescriptorsRequest{\n \t\tName: \"projects/\" + projectID,\n \t}\n \titer := c.ListMetricDescriptors(ctx, req)\n\n \tfor {\n \t\tresp, err := iter.Next()\n \t\tif err == iterator.Done {\n \t\t\tbreak\n \t\t}\n \t\tif err != nil {\n \t\t\treturn fmt.Errorf(\"Could not list metrics: %w\", err)\n \t\t}\n \t\tfmt.Fprintf(w, \"%v\\n\", resp.GetType())\n \t}\n \tfmt.Fprintln(w, \"Done\")\n \treturn nil\n }\n\nJava\n\n\nTo authenticate to Monitoring, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n // Your Google Cloud Platform project ID\n String projectId = System.getProperty(\"projectId\");\n ProjectName name = ProjectName.of(projectId);\n\n ListMetricDescriptorsRequest request =\n ListMetricDescriptorsRequest.newBuilder().setName(name.toString()).build();\n\n // Instantiates a client\n try (final MetricServiceClient client = MetricServiceClient.create();) {\n ListMetricDescriptorsPagedResponse response = client.listMetricDescriptors(request);\n\n System.out.println(\"Listing descriptors: \");\n\n for (MetricDescriptor d : response.iterateAll()) {\n System.out.println(d.getName() + \" \" + d.getDisplayName());\n }\n }\n\nNode.js\n\n\nTo authenticate to Monitoring, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n // Imports the Google Cloud client library\n const monitoring = require('https://cloud.google.com/nodejs/docs/reference/monitoring/latest/overview.html');\n\n // Creates a client\n const client = new monitoring.https://cloud.google.com/nodejs/docs/reference/monitoring/latest/overview.html();\n\n async function listMetricDescriptors() {\n /**\n * TODO(developer): Uncomment and edit the following lines of code.\n */\n // const projectId = 'YOUR_PROJECT_ID';\n\n const request = {\n name: client.projectPath(projectId),\n };\n\n // Lists metric descriptors\n const [descriptors] = await client.listMetricDescriptors(request);\n console.log('Metric Descriptors:');\n descriptors.forEach(descriptor =\u003e console.log(descriptor.name));\n }\n listMetricDescriptors();\n\nPHP\n\n\nTo authenticate to Monitoring, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n use Google\\Cloud\\Monitoring\\V3\\Client\\MetricServiceClient;\n use Google\\Cloud\\Monitoring\\V3\\ListMetricDescriptorsRequest;\n\n /**\n * Example:\n * ```\n * list_descriptors($projectId);\n * ```\n *\n * @param string $projectId Your project ID\n */\n function list_descriptors($projectId)\n {\n $metrics = new MetricServiceClient([\n 'projectId' =\u003e $projectId,\n ]);\n\n $projectName = 'projects/' . $projectId;\n $listMetricDescriptorsRequest = (new ListMetricDescriptorsRequest())\n -\u003esetName($projectName);\n $descriptors = $metrics-\u003elistMetricDescriptors($listMetricDescriptorsRequest);\n\n printf('Metric Descriptors:' . PHP_EOL);\n foreach ($descriptors-\u003eiterateAllElements() as $descriptor) {\n printf($descriptor-\u003egetName() . PHP_EOL);\n }\n }\n\nPython\n\n\nTo authenticate to Monitoring, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import https://cloud.google.com/python/docs/reference/monitoring/latest/\n\n client = https://cloud.google.com/python/docs/reference/monitoring/latest/.https://cloud.google.com/python/docs/reference/monitoring/latest/google.cloud.monitoring_v3.services.metric_service.MetricServiceClient.html()\n project_name = f\"projects/{project_id}\"\n descriptors = client.https://cloud.google.com/python/docs/reference/monitoring/latest/google.cloud.monitoring_v3.services.metric_service.MetricServiceClient.html#google_cloud_monitoring_v3_services_metric_service_MetricServiceClient_list_metric_descriptors(name=project_name)\n for descriptor in descriptors:\n print(descriptor.type)\n\nRuby\n\n\nTo authenticate to Monitoring, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n gem \"google-cloud-monitoring\"\n require \"google/cloud/monitoring\"\n\n # Your Google Cloud Platform project ID\n # project_id = \"YOUR_PROJECT_ID\"\n\n client = Google::Cloud::https://cloud.google.com/ruby/docs/reference/google-cloud-monitoring-dashboard-v1/latest/Google-Cloud-Monitoring.html.https://cloud.google.com/ruby/docs/reference/google-cloud-monitoring/latest/Google-Cloud-Monitoring.html\n project_name = client.project_path project: project_id\n\n results = client.list_metric_descriptors name: project_name\n results.each do |descriptor|\n p descriptor.type\n end\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=monitoring)."]]