演示如何获取受监控的资源的详细信息。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
C#
public static object GetMonitoredResource(string projectId, string resourceId)
{
MetricServiceClient client = MetricServiceClient.Create();
MonitoredResourceDescriptorName name = new MonitoredResourceDescriptorName(projectId, resourceId);
var response = client.GetMonitoredResourceDescriptor(name);
string resource = JObject.Parse($"{response}").ToString();
Console.WriteLine($"{ resource }");
return 0;
}
Go
import (
"context"
"fmt"
"io"
monitoring "cloud.google.com/go/monitoring/apiv3"
"cloud.google.com/go/monitoring/apiv3/v2/monitoringpb"
)
// getMonitoredResource gets the descriptor for the given resourceType and
// prints information about it. resource should be of the form
// "projects/[PROJECT_ID]/monitoredResourceDescriptors/[RESOURCE_TYPE]".
func getMonitoredResource(w io.Writer, resource string) error {
ctx := context.Background()
c, err := monitoring.NewMetricClient(ctx)
if err != nil {
return fmt.Errorf("NewMetricClient: %v", err)
}
defer c.Close()
req := &monitoringpb.GetMonitoredResourceDescriptorRequest{
Name: fmt.Sprintf(resource),
}
resp, err := c.GetMonitoredResourceDescriptor(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, "Labels:\n")
for _, l := range resp.GetLabels() {
fmt.Fprintf(w, "\t%s (%s) - %s", l.GetKey(), l.GetValueType(), l.GetDescription())
}
return nil
}
Java
void getMonitoredResource(String resourceId) throws IOException {
String projectId = System.getProperty("projectId");
MetricServiceClient client = MetricServiceClient.create();
MonitoredResourceDescriptorName name =
MonitoredResourceDescriptorName.of(projectId, resourceId);
MonitoredResourceDescriptor response = client.getMonitoredResourceDescriptor(name);
System.out.println("Retrieved Monitored Resource: " + gson.toJson(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 getMonitoredResourceDescriptor() {
/**
* TODO(developer): Uncomment and edit the following lines of code.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const resourceType = 'some_resource_type, e.g. cloudsql_database';
const request = {
name: client.projectMonitoredResourceDescriptorPath(
projectId,
resourceType
),
};
// Lists monitored resource descriptors
const [descriptor] = await client.getMonitoredResourceDescriptor(request);
console.log(`Name: ${descriptor.displayName}`);
console.log(`Description: ${descriptor.description}`);
console.log(`Type: ${descriptor.type}`);
console.log('Labels:');
descriptor.labels.forEach(label => {
console.log(` ${label.key} (${label.valueType}) - ${label.description}`);
});
}
getMonitoredResourceDescriptor();
PHP
use Google\Cloud\Monitoring\V3\MetricServiceClient;
/**
* Example:
* ```
* get_resource('your-project-id', 'gcs_bucket');
* ```
*
* @param string $projectId Your project ID
* @param string $resourceType The resource type of the monitored resource.
*/
function get_resource($projectId, $resourceType)
{
$metrics = new MetricServiceClient([
'projectId' => $projectId,
]);
$metricName = $metrics->monitoredResourceDescriptorName($projectId, $resourceType);
$resource = $metrics->getMonitoredResourceDescriptor($metricName);
printf('Name: %s' . PHP_EOL, $resource->getName());
printf('Type: %s' . PHP_EOL, $resource->getType());
printf('Display Name: %s' . PHP_EOL, $resource->getDisplayName());
printf('Description: %s' . PHP_EOL, $resource->getDescription());
printf('Labels:' . PHP_EOL);
foreach ($resource->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()
resource_path = (
f"projects/{project_id}/monitoredResourceDescriptors/{resource_type_name}"
)
pprint.pprint(client.get_monitored_resource_descriptor(name=resource_path))
Ruby
# Your Google Cloud Platform project ID
# project_id = "YOUR_PROJECT_ID"
# The resource type
# resource_type = "gce_instance"
client = Google::Cloud::Monitoring.metric_service
resource_path = client.monitored_resource_descriptor_path(
project: project_id,
monitored_resource_descriptor: resource_type
)
result = client.get_monitored_resource_descriptor name: resource_path
p result
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。