use of com.google.monitoring.v3.ListMetricDescriptorsRequest in project google-cloud-java by GoogleCloudPlatform.
the class MetricServiceClientTest method listMetricDescriptorsTest.
@Test
@SuppressWarnings("all")
public void listMetricDescriptorsTest() {
String nextPageToken = "";
MetricDescriptor metricDescriptorsElement = MetricDescriptor.newBuilder().build();
List<MetricDescriptor> metricDescriptors = Arrays.asList(metricDescriptorsElement);
ListMetricDescriptorsResponse expectedResponse = ListMetricDescriptorsResponse.newBuilder().setNextPageToken(nextPageToken).addAllMetricDescriptors(metricDescriptors).build();
mockMetricService.addResponse(expectedResponse);
ProjectName name = ProjectName.create("[PROJECT]");
ListMetricDescriptorsPagedResponse pagedListResponse = client.listMetricDescriptors(name);
List<MetricDescriptor> resources = Lists.newArrayList(pagedListResponse.iterateAll());
Assert.assertEquals(1, resources.size());
Assert.assertEquals(expectedResponse.getMetricDescriptorsList().get(0), resources.get(0));
List<GeneratedMessageV3> actualRequests = mockMetricService.getRequests();
Assert.assertEquals(1, actualRequests.size());
ListMetricDescriptorsRequest actualRequest = (ListMetricDescriptorsRequest) actualRequests.get(0);
Assert.assertEquals(name, actualRequest.getNameAsProjectName());
}
use of com.google.monitoring.v3.ListMetricDescriptorsRequest in project java-docs-samples by GoogleCloudPlatform.
the class Snippets method listMetricDescriptors.
/**
* Returns the first page of all metric descriptors.
*/
void listMetricDescriptors() throws IOException {
// [START monitoring_list_descriptors]
// Your Google Cloud Platform project ID
String projectId = System.getProperty("projectId");
final MetricServiceClient client = MetricServiceClient.create();
ProjectName name = ProjectName.of(projectId);
ListMetricDescriptorsRequest request = ListMetricDescriptorsRequest.newBuilder().setName(name.toString()).build();
ListMetricDescriptorsPagedResponse response = client.listMetricDescriptors(request);
System.out.println("Listing descriptors: ");
for (MetricDescriptor d : response.iterateAll()) {
System.out.println(d.getName() + " " + d.getDisplayName());
}
// [END monitoring_list_descriptors]
}
use of com.google.monitoring.v3.ListMetricDescriptorsRequest in project java-docs-samples by GoogleCloudPlatform.
the class BigQueryRunner method createMetricsIfNeeded.
// [END bigquery_logging_list_time_series]
// [START bigquery_logging_list_and_create_metrics]
private void createMetricsIfNeeded() {
// If all required metrics already exist, no need to make service calls.
if (REQUIRED_METRICS.stream().map(MetricDescriptor::getDisplayName).allMatch(existingMetrics::contains)) {
return;
}
ListMetricDescriptorsRequest listMetricsRequest = ListMetricDescriptorsRequest.newBuilder().setName(projectName).setFilter(CUSTOM_METRIC_FILTER).build();
ListMetricDescriptorsPagedResponse listMetricsResponse = client.listMetricDescriptors(listMetricsRequest);
for (MetricDescriptor existingMetric : listMetricsResponse.iterateAll()) {
existingMetrics.add(existingMetric.getDisplayName());
}
REQUIRED_METRICS.stream().filter(metric -> !existingMetrics.contains(metric.getDisplayName())).forEach(this::createMetric);
}
Aggregations