use of com.google.api.MetricDescriptor in project google-cloud-java by GoogleCloudPlatform.
the class MetricServiceClientTest method getMetricDescriptorTest.
@Test
@SuppressWarnings("all")
public void getMetricDescriptorTest() {
String name2 = "name2-1052831874";
String type = "type3575610";
String unit = "unit3594628";
String description = "description-1724546052";
String displayName = "displayName1615086568";
MetricDescriptor expectedResponse = MetricDescriptor.newBuilder().setName(name2).setType(type).setUnit(unit).setDescription(description).setDisplayName(displayName).build();
mockMetricService.addResponse(expectedResponse);
MetricDescriptorName name = MetricDescriptorName.create("[PROJECT]", "[METRIC_DESCRIPTOR]");
MetricDescriptor actualResponse = client.getMetricDescriptor(name);
Assert.assertEquals(expectedResponse, actualResponse);
List<GeneratedMessageV3> actualRequests = mockMetricService.getRequests();
Assert.assertEquals(1, actualRequests.size());
GetMetricDescriptorRequest actualRequest = (GetMetricDescriptorRequest) actualRequests.get(0);
Assert.assertEquals(name, actualRequest.getNameAsMetricDescriptorName());
}
use of com.google.api.MetricDescriptor 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.api.MetricDescriptor 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);
}
use of com.google.api.MetricDescriptor in project java-docs-samples by GoogleCloudPlatform.
the class BigQueryRunner method getTimeSeriesValues.
// [START bigquery_logging_list_time_series]
public List<TimeSeriesSummary> getTimeSeriesValues() {
List<TimeSeriesSummary> summaries = Lists.newArrayList();
createMetricsIfNeeded();
for (MetricDescriptor metric : REQUIRED_METRICS) {
ListTimeSeriesRequest listTimeSeriesRequest = ListTimeSeriesRequest.newBuilder().setName(projectName).setFilter(String.format("metric.type = \"%s\"", metric.getType())).setInterval(TimeInterval.newBuilder().setStartTime(Timestamps.subtract(Timestamps.fromMillis(System.currentTimeMillis()), com.google.protobuf.Duration.newBuilder().setSeconds(// 30 days ago
60L * 60L * 24L * 30L).build())).setEndTime(Timestamps.fromMillis(System.currentTimeMillis())).build()).build();
try {
ListTimeSeriesPagedResponse listTimeSeriesResponse = client.listTimeSeries(listTimeSeriesRequest);
ArrayList<TimeSeries> timeSeries = Lists.newArrayList(listTimeSeriesResponse.iterateAll());
summaries.addAll(timeSeries.stream().map(TimeSeriesSummary::fromTimeSeries).collect(Collectors.toList()));
} catch (RuntimeException ex) {
os.println("MetricDescriptors not yet synced. Please try again in a moment.");
}
}
return summaries;
}
use of com.google.api.MetricDescriptor in project instrumentation-java by census-instrumentation.
the class StackdriverExportUtilsTest method createMetricDescriptor.
@Test
public void createMetricDescriptor() {
MetricDescriptor metricDescriptor = StackdriverExportUtils.createMetricDescriptor(METRIC_DESCRIPTOR, PROJECT_ID, "custom.googleapis.com/myorg/", "myorg/", DEFAULT_CONSTANT_LABELS);
assertThat(metricDescriptor.getName()).isEqualTo("projects/" + PROJECT_ID + "/metricDescriptors/custom.googleapis.com/myorg/" + METRIC_NAME);
assertThat(metricDescriptor.getDescription()).isEqualTo(METRIC_DESCRIPTION);
assertThat(metricDescriptor.getDisplayName()).isEqualTo("myorg/" + METRIC_NAME);
assertThat(metricDescriptor.getType()).isEqualTo("custom.googleapis.com/myorg/" + METRIC_NAME);
assertThat(metricDescriptor.getUnit()).isEqualTo(METRIC_UNIT);
assertThat(metricDescriptor.getMetricKind()).isEqualTo(MetricKind.CUMULATIVE);
assertThat(metricDescriptor.getValueType()).isEqualTo(MetricDescriptor.ValueType.DOUBLE);
assertThat(metricDescriptor.getLabelsList()).containsExactly(LabelDescriptor.newBuilder().setKey(LABEL_KEY.get(0).getKey()).setDescription(LABEL_KEY.get(0).getDescription()).setValueType(ValueType.STRING).build(), LabelDescriptor.newBuilder().setKey(StackdriverExportUtils.OPENCENSUS_TASK_KEY.getKey()).setDescription(StackdriverExportUtils.OPENCENSUS_TASK_KEY.getDescription()).setValueType(ValueType.STRING).build());
}
Aggregations