获取指标描述符
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
演示如何获取指标描述符的详细信息。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
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,["# Get a metric descriptor\n\nDemonstrates how to get the details of a metric descriptor.\n\nExplore further\n---------------\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-----------\n\n### C#\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 GetMetricDetails(string projectId, string metricType)\n {\n MetricServiceClient client = MetricServiceClient.Create();\n MetricDescriptorName name = new MetricDescriptorName(projectId, metricType);\n try\n {\n var response = client.GetMetricDescriptor(name);\n string metric = JObject.Parse($\"{response}\").ToString();\n Console.WriteLine($\"{ metric }\");\n }\n catch (Grpc.Core.RpcException ex)\n when (ex.Status.StatusCode == Grpc.Core.StatusCode.NotFound)\n { }\n return 0;\n }\n\n### Go\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 // getMetricDescriptor gets the descriptor for the given metricType and prints\n // information about it. metricType is the type of the metric, for example\n // compute.googleapis.com/firewall/dropped_packets_count.\n func getMetricDescriptor(w io.Writer, projectID, metricType 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.GetMetricDescriptorRequest{\n \t\tName: fmt.Sprintf(\"projects/%s/metricDescriptors/%s\", projectID, metricType),\n \t}\n \tresp, err := c.GetMetricDescriptor(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, \"Metric Kind: %v\\n\", resp.GetMetricKind())\n \tfmt.Fprintf(w, \"Value Type: %v\\n\", resp.GetValueType())\n \tfmt.Fprintf(w, \"Unit: %v\\n\", resp.GetUnit())\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\n### Java\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 final String projectId = System.getProperty(\"projectId\");\n\n MetricDescriptorName descriptorName = MetricDescriptorName.of(projectId, type);\n\n try (final MetricServiceClient client = MetricServiceClient.create();) {\n MetricDescriptor response = client.getMetricDescriptor(descriptorName);\n\n System.out.println(\"Printing metrics descriptor: \" + response);\n }\n\n### Node.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 getMetricDescriptor() {\n /**\n * TODO(developer): Uncomment and edit the following lines of code.\n */\n // const projectId = 'YOUR_PROJECT_ID';\n // const metricId = 'custom.googleapis.com/your/id';\n\n const request = {\n name: client.https://cloud.google.com/nodejs/docs/reference/monitoring/latest/monitoring/v3.metricserviceclient.html(projectId, metricId),\n };\n\n // Retrieves a metric descriptor\n const [descriptor] = await client.getMetricDescriptor(request);\n console.log(`Name: ${descriptor.displayName}`);\n console.log(`Description: ${descriptor.description}`);\n console.log(`Type: ${descriptor.type}`);\n console.log(`Kind: ${descriptor.metricKind}`);\n console.log(`Value Type: ${descriptor.valueType}`);\n console.log(`Unit: ${descriptor.unit}`);\n console.log('Labels:');\n descriptor.labels.forEach(label =\u003e {\n console.log(` ${label.key} (${label.valueType}) - ${label.description}`);\n });\n }\n getMetricDescriptor();\n\n### PHP\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\\GetMetricDescriptorRequest;\n\n /**\n * Example:\n * ```\n * get_descriptor($projectId);\n * ```\n *\n * @param string $projectId Your project ID\n * @param string $metricId The ID of the Metric Descriptor to get\n */\n function get_descriptor($projectId, $metricId)\n {\n $metrics = new MetricServiceClient([\n 'projectId' =\u003e $projectId,\n ]);\n\n $metricName = $metrics-\u003emetricDescriptorName($projectId, $metricId);\n $getMetricDescriptorRequest = (new GetMetricDescriptorRequest())\n -\u003esetName($metricName);\n $descriptor = $metrics-\u003egetMetricDescriptor($getMetricDescriptorRequest);\n\n printf('Name: ' . $descriptor-\u003egetDisplayName() . PHP_EOL);\n printf('Description: ' . $descriptor-\u003egetDescription() . PHP_EOL);\n printf('Type: ' . $descriptor-\u003egetType() . PHP_EOL);\n printf('Metric Kind: ' . $descriptor-\u003egetMetricKind() . PHP_EOL);\n printf('Value Type: ' . $descriptor-\u003egetValueType() . PHP_EOL);\n printf('Unit: ' . $descriptor-\u003egetUnit() . PHP_EOL);\n printf('Labels:' . PHP_EOL);\n foreach ($descriptor-\u003egetLabels() as $labels) {\n printf(' %s (%s) - %s' . PHP_EOL,\n $labels-\u003egetKey(),\n $labels-\u003egetValueType(),\n $labels-\u003egetDescription());\n }\n }\n\n### Python\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 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_metric_descriptor(name=metric_name)\n pprint.pprint(descriptor)\n\n### Ruby\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 # Example metric type\n # metric_type = \"custom.googleapis.com/my_metric\"\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 metric_name = client.https://cloud.google.com/ruby/docs/reference/google-cloud-monitoring-v3/latest/Google-Cloud-Monitoring-V3-MetricService-Paths.html project: project_id,\n metric_descriptor: metric_type\n\n descriptor = client.get_metric_descriptor name: metric_name\n p descriptor\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=monitoring)."]]