List monitored resources
Stay organized with collections
Save and categorize content based on your preferences.
Demonstrates how to list monitored resources.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["Demonstrates how to list monitored resources.\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 ListMonitoredResources(string projectId)\n {\n Console.WriteLine(\"Starting to List Monitored Resources...\");\n MetricServiceClient client = MetricServiceClient.Create();\n ProjectName projectName = new ProjectName(projectId);\n\n PagedEnumerable\u003cListMonitoredResourceDescriptorsResponse, MonitoredResourceDescriptor\u003e\n resources = client.ListMonitoredResourceDescriptors(projectName);\n if (resources.Any())\n {\n foreach (MonitoredResourceDescriptor resource in resources.Take(10))\n {\n Console.WriteLine($\"{resource.Name}: {resource.DisplayName}\");\n }\n }\n else\n { \n Console.WriteLine(\"No resources found.\");\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 // listMonitoredResources lists all the resources available to be monitored.\n func listMonitoredResources(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.ListMonitoredResourceDescriptorsRequest{\n \t\tName: \"projects/\" + projectID,\n \t}\n \titer := c.ListMonitoredResourceDescriptors(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 time series: %w\", err)\n \t\t}\n \t\tfmt.Fprintf(w, \"%v\\n\", resp)\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 ListMonitoredResourceDescriptorsRequest request =\n ListMonitoredResourceDescriptorsRequest.newBuilder().setName(name.toString()).build();\n\n System.out.println(\"Listing monitored resource descriptors: \");\n\n // Instantiates a client\n try (final MetricServiceClient client = MetricServiceClient.create();) {\n ListMonitoredResourceDescriptorsPagedResponse response =\n client.listMonitoredResourceDescriptors(request);\n\n for (MonitoredResourceDescriptor d : response.iterateAll()) {\n System.out.println(d.getType());\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 listMonitoredResourceDescriptors() {\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 monitored resource descriptors\n const [descriptors] =\n await client.listMonitoredResourceDescriptors(request);\n console.log('Monitored Resource Descriptors:');\n descriptors.forEach(descriptor =\u003e {\n console.log(descriptor.name);\n console.log(` Type: ${descriptor.type}`);\n if (descriptor.labels) {\n console.log(' Labels:');\n descriptor.labels.forEach(label =\u003e {\n console.log(\n ` ${label.key} (${label.valueType}): ${label.description}`\n );\n });\n }\n console.log();\n });\n }\n listMonitoredResourceDescriptors();\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\\ListMonitoredResourceDescriptorsRequest;\n\n /**\n * Example:\n * ```\n * list_resources('your-project-id');\n * ```\n *\n * @param string $projectId Your project ID\n */\n function list_resources($projectId)\n {\n $metrics = new MetricServiceClient([\n 'projectId' =\u003e $projectId,\n ]);\n $projectName = 'projects/' . $projectId;\n $listMonitoredResourceDescriptorsRequest = (new ListMonitoredResourceDescriptorsRequest())\n -\u003esetName($projectName);\n $descriptors = $metrics-\u003elistMonitoredResourceDescriptors($listMonitoredResourceDescriptorsRequest);\n foreach ($descriptors-\u003eiterateAllElements() as $descriptor) {\n print($descriptor-\u003egetType() . 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 resource_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_monitored_resource_descriptors(name=project_name)\n for descriptor in resource_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/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_monitored_resource_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)."]]