演示如何获取指标描述符的详细信息。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
C#
public static object GetMetricDetails(string projectId, string metricType)
{
MetricServiceClient client = MetricServiceClient.Create();
MetricDescriptorName name = new MetricDescriptorName(projectId, metricType);
try
{
var response = client.GetMetricDescriptor(name);
string metric = JObject.Parse($"{response}").ToString();
Console.WriteLine($"{ metric }");
}
catch (Grpc.Core.RpcException ex)
when (ex.Status.StatusCode == Grpc.Core.StatusCode.NotFound)
{ }
return 0;
}
Go
import (
"context"
"fmt"
"io"
monitoring "cloud.google.com/go/monitoring/apiv3"
"cloud.google.com/go/monitoring/apiv3/v2/monitoringpb"
)
// getMetricDescriptor gets the descriptor for the given metricType and prints
// information about it. metricType is the type of the metric, for example
// compute.googleapis.com/firewall/dropped_packets_count.
func getMetricDescriptor(w io.Writer, projectID, metricType string) error {
ctx := context.Background()
c, err := monitoring.NewMetricClient(ctx)
if err != nil {
return fmt.Errorf("NewMetricClient: %v", err)
}
defer c.Close()
req := &monitoringpb.GetMetricDescriptorRequest{
Name: fmt.Sprintf("projects/%s/metricDescriptors/%s", projectID, metricType),
}
resp, err := c.GetMetricDescriptor(ctx, req)
if err != nil {
return fmt.Errorf("could not get custom metric: %v", err)
}
fmt.Fprintf(w, "Name: %v\n", resp.GetName())
fmt.Fprintf(w, "Description: %v\n", resp.GetDescription())
fmt.Fprintf(w, "Type: %v\n", resp.GetType())
fmt.Fprintf(w, "Metric Kind: %v\n", resp.GetMetricKind())
fmt.Fprintf(w, "Value Type: %v\n", resp.GetValueType())
fmt.Fprintf(w, "Unit: %v\n", resp.GetUnit())
fmt.Fprintf(w, "Labels:\n")
for _, l := range resp.GetLabels() {
fmt.Fprintf(w, "\t%s (%s) - %s", l.GetKey(), l.GetValueType(), l.GetDescription())
}
return nil
}
Java
// Your Google Cloud Platform project ID
String projectId = System.getProperty("projectId");
final MetricServiceClient client = MetricServiceClient.create();
MonitoredResourceDescriptorName name = MonitoredResourceDescriptorName.of(projectId, type);
MonitoredResourceDescriptor response = client.getMonitoredResourceDescriptor(name);
System.out.println("Printing monitored resource descriptor: ");
System.out.println(response);
Node.js
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.MetricServiceClient();
async function getMetricDescriptor() {
/**
* TODO(developer): Uncomment and edit the following lines of code.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const metricId = 'custom.googleapis.com/your/id';
const request = {
name: client.projectMetricDescriptorPath(projectId, metricId),
};
// Retrieves a metric descriptor
const [descriptor] = await client.getMetricDescriptor(request);
console.log(`Name: ${descriptor.displayName}`);
console.log(`Description: ${descriptor.description}`);
console.log(`Type: ${descriptor.type}`);
console.log(`Kind: ${descriptor.metricKind}`);
console.log(`Value Type: ${descriptor.valueType}`);
console.log(`Unit: ${descriptor.unit}`);
console.log('Labels:');
descriptor.labels.forEach(label => {
console.log(` ${label.key} (${label.valueType}) - ${label.description}`);
});
}
getMetricDescriptor();
PHP
use Google\Cloud\Monitoring\V3\MetricServiceClient;
/**
* Example:
* ```
* get_descriptor($projectId);
* ```
*
* @param string $projectId Your project ID
* @param string $metricId The ID of the Metric Descriptor to get
*/
function get_descriptor($projectId, $metricId)
{
$metrics = new MetricServiceClient([
'projectId' => $projectId,
]);
$metricName = $metrics->metricDescriptorName($projectId, $metricId);
$descriptor = $metrics->getMetricDescriptor($metricName);
printf('Name: ' . $descriptor->getDisplayName() . PHP_EOL);
printf('Description: ' . $descriptor->getDescription() . PHP_EOL);
printf('Type: ' . $descriptor->getType() . PHP_EOL);
printf('Metric Kind: ' . $descriptor->getMetricKind() . PHP_EOL);
printf('Value Type: ' . $descriptor->getValueType() . PHP_EOL);
printf('Unit: ' . $descriptor->getUnit() . PHP_EOL);
printf('Labels:' . PHP_EOL);
foreach ($descriptor->getLabels() as $labels) {
printf(' %s (%s) - %s' . PHP_EOL,
$labels->getKey(),
$labels->getValueType(),
$labels->getDescription());
}
}
Python
from google.cloud import monitoring_v3
client = monitoring_v3.MetricServiceClient()
descriptor = client.get_metric_descriptor(name=metric_name)
pprint.pprint(descriptor)
Ruby
# Your Google Cloud Platform project ID
# project_id = "YOUR_PROJECT_ID"
# Example metric type
# metric_type = "custom.googleapis.com/my_metric"
client = Google::Cloud::Monitoring.metric_service
metric_name = client.metric_descriptor_path project: project_id,
metric_descriptor: metric_type
descriptor = client.get_metric_descriptor name: metric_name
p descriptor
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。