Get a monitored resource
Stay organized with collections
Save and categorize content based on your preferences.
Demonstrates how to get the details of a monitored resource.
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 get the details of a monitored resource.\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 GetMonitoredResource(string projectId, string resourceId)\n {\n MetricServiceClient client = MetricServiceClient.Create();\n MonitoredResourceDescriptorName name = new MonitoredResourceDescriptorName(projectId, resourceId);\n var response = client.GetMonitoredResourceDescriptor(name);\n string resource = JObject.Parse($\"{response}\").ToString();\n Console.WriteLine($\"{ resource }\");\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 )\n\n // getMonitoredResource gets the descriptor for the given resourceType and\n // prints information about it. resource should be of the form\n // \"projects/[PROJECT_ID]/monitoredResourceDescriptors/[RESOURCE_TYPE]\".\n func getMonitoredResource(w io.Writer, resource string) error {\n \tctx := context.Background()\n \tc, err := monitoring.NewMetricClient(ctx)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"NewMetricClient: %w\", err)\n \t}\n \tdefer c.Close()\n \treq := &monitoringpb.GetMonitoredResourceDescriptorRequest{\n \t\tName: fmt.Sprintf(resource),\n \t}\n \tresp, err := c.GetMonitoredResourceDescriptor(ctx, req)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"could not get custom metric: %w\", err)\n \t}\n\n \tfmt.Fprintf(w, \"Name: %v\\n\", resp.GetName())\n \tfmt.Fprintf(w, \"Description: %v\\n\", resp.GetDescription())\n \tfmt.Fprintf(w, \"Type: %v\\n\", resp.GetType())\n \tfmt.Fprintf(w, \"Labels:\\n\")\n \tfor _, l := range resp.GetLabels() {\n \t\tfmt.Fprintf(w, \"\\t%s (%s) - %s\", l.GetKey(), l.GetValueType(), l.GetDescription())\n \t}\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 void getMonitoredResource(String resourceId) throws IOException {\n String projectId = System.getProperty(\"projectId\");\n\n try (final MetricServiceClient client = MetricServiceClient.create();) {\n MonitoredResourceDescriptorName name =\n MonitoredResourceDescriptorName.of(projectId, resourceId);\n MonitoredResourceDescriptor response = client.getMonitoredResourceDescriptor(name);\n System.out.println(\"Retrieved Monitored Resource: \" + gson.toJson(response));\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 getMonitoredResourceDescriptor() {\n /**\n * TODO(developer): Uncomment and edit the following lines of code.\n */\n // const projectId = 'YOUR_PROJECT_ID';\n // const resourceType = 'some_resource_type, e.g. cloudsql_database';\n\n const request = {\n name: client.https://cloud.google.com/nodejs/docs/reference/monitoring/latest/monitoring/v3.metricserviceclient.html(\n projectId,\n resourceType\n ),\n };\n\n // Lists monitored resource descriptors\n const [descriptor] = await client.getMonitoredResourceDescriptor(request);\n\n console.log(`Name: ${descriptor.displayName}`);\n console.log(`Description: ${descriptor.description}`);\n console.log(`Type: ${descriptor.type}`);\n console.log('Labels:');\n descriptor.labels.forEach(label =\u003e {\n console.log(` ${label.key} (${label.valueType}) - ${label.description}`);\n });\n }\n getMonitoredResourceDescriptor();\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\\GetMonitoredResourceDescriptorRequest;\n\n /**\n * Example:\n * ```\n * get_resource('your-project-id', 'gcs_bucket');\n * ```\n *\n * @param string $projectId Your project ID\n * @param string $resourceType The resource type of the monitored resource.\n */\n function get_resource($projectId, $resourceType)\n {\n $metrics = new MetricServiceClient([\n 'projectId' =\u003e $projectId,\n ]);\n\n $metricName = $metrics-\u003emonitoredResourceDescriptorName($projectId, $resourceType);\n $getMonitoredResourceDescriptorRequest = (new GetMonitoredResourceDescriptorRequest())\n -\u003esetName($metricName);\n $resource = $metrics-\u003egetMonitoredResourceDescriptor($getMonitoredResourceDescriptorRequest);\n\n printf('Name: %s' . PHP_EOL, $resource-\u003egetName());\n printf('Type: %s' . PHP_EOL, $resource-\u003egetType());\n printf('Display Name: %s' . PHP_EOL, $resource-\u003egetDisplayName());\n printf('Description: %s' . PHP_EOL, $resource-\u003egetDescription());\n printf('Labels:' . PHP_EOL);\n foreach ($resource-\u003egetLabels() as $labels) {\n printf(' %s (%s) - %s' . PHP_EOL,\n $labels-\u003egetKey(),\n $labels-\u003egetValueType(),\n $labels-\u003egetDescription());\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 resource_path = (\n f\"projects/{project_id}/monitoredResourceDescriptors/{resource_type_name}\"\n )\n descriptor = 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_get_monitored_resource_descriptor(name=resource_path)\n pprint.pprint(descriptor)\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 # Your Google Cloud Platform project ID\n # project_id = \"YOUR_PROJECT_ID\"\n\n # The resource type\n # resource_type = \"gce_instance\"\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 resource_path = client.https://cloud.google.com/ruby/docs/reference/google-cloud-monitoring-v3/latest/Google-Cloud-Monitoring-V3-MetricService-Paths.html(\n project: project_id,\n monitored_resource_descriptor: resource_type\n )\n\n result = client.get_monitored_resource_descriptor name: resource_path\n p result\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)."]]