@Override public void run(Logging logging, Void arg) { for (Metric metric : logging.listMetrics().iterateAll()) { System.out.println(metric); } } }
/** Example of listing metrics, specifying the page size. */ // [TARGET listMetrics(ListOption...)] public Page<Metric> listMetrics() { // [START listMetrics] Page<Metric> metrics = logging.listMetrics(ListOption.pageSize(100)); for (Metric metric : metrics.iterateAll()) { // do something with the metric } // [END listMetrics] return metrics; }
public static void main(String... args) throws Exception { // Create a service object // Credentials are inferred from the environment try (Logging logging = LoggingOptions.getDefaultInstance().getService()) { // Create a metric MetricInfo metricInfo = MetricInfo.newBuilder("test-metric", "severity >= ERROR") .setDescription("Log entries with severity higher or equal to ERROR") .build(); logging.create(metricInfo); // List metrics Page<Metric> metrics = logging.listMetrics(); for (Metric metric : metrics.iterateAll()) { System.out.println(metric); } } } }
@Test public void testListMetrics() throws InterruptedException { String firstName = formatForTest("test-list-metrics-1"); String secondName = formatForTest("test-list-metrics-2"); Metric firstMetric = logging().create(MetricInfo.of(firstName, "severity>=ERROR")); Metric secondMetric = logging().create(MetricInfo.of(secondName, "severity>=ERROR")); Logging.ListOption[] options = {Logging.ListOption.pageSize(1)}; Page<Metric> metricPage = logging().listMetrics(options); Set<Metric> metrics = Sets.newHashSet(metricPage.iterateAll()); while (!metrics.contains(firstMetric) || !metrics.contains(secondMetric)) { Thread.sleep(500); metrics = Sets.newHashSet(logging().listMetrics(options).iterateAll()); } firstMetric.delete(); secondMetric.delete(); }
@Test public void testListMetricsEmpty() { EasyMock.replay(rpcFactoryMock); logging = options.getService(); ListLogMetricsRequest request = ListLogMetricsRequest.newBuilder().setParent(PROJECT_PB).build(); List<Metric> sinkList = ImmutableList.of(); ListLogMetricsResponse response = ListLogMetricsResponse.newBuilder() .setNextPageToken("") .addAllMetrics(Lists.transform(sinkList, METRIC_TO_PB_FUNCTION)) .build(); ApiFuture<ListLogMetricsResponse> futureResponse = ApiFutures.immediateFuture(response); EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); EasyMock.replay(loggingRpcMock); Page<Metric> page = logging.listMetrics(); assertNull(page.getNextPageToken()); assertNull(page.getNextPage()); assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Metric.class)); }
@Test public void testListMetrics() { String cursor = "cursor"; EasyMock.replay(rpcFactoryMock); logging = options.getService(); ListLogMetricsRequest request = ListLogMetricsRequest.newBuilder().setParent(PROJECT_PB).build(); List<Metric> sinkList = ImmutableList.of( new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO))); ListLogMetricsResponse response = ListLogMetricsResponse.newBuilder() .setNextPageToken(cursor) .addAllMetrics(Lists.transform(sinkList, METRIC_TO_PB_FUNCTION)) .build(); ApiFuture<ListLogMetricsResponse> futureResponse = ApiFutures.immediateFuture(response); EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); EasyMock.replay(loggingRpcMock); Page<Metric> page = logging.listMetrics(); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Metric.class)); }
@Test public void testListMetricsWithOptions() { String cursor = "cursor"; EasyMock.replay(rpcFactoryMock); logging = options.getService(); ListLogMetricsRequest request = ListLogMetricsRequest.newBuilder() .setPageToken(cursor) .setPageSize(42) .setParent(PROJECT_PB) .build(); List<Metric> sinkList = ImmutableList.of( new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO))); ListLogMetricsResponse response = ListLogMetricsResponse.newBuilder() .setNextPageToken(cursor) .addAllMetrics(Lists.transform(sinkList, METRIC_TO_PB_FUNCTION)) .build(); ApiFuture<ListLogMetricsResponse> futureResponse = ApiFutures.immediateFuture(response); EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); EasyMock.replay(loggingRpcMock); Page<Metric> page = logging.listMetrics(ListOption.pageSize(42), ListOption.pageToken(cursor)); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Metric.class)); }
EasyMock.expect(loggingRpcMock.list(request2)).andReturn(futureResponse2); EasyMock.replay(loggingRpcMock); Page<Metric> page = logging.listMetrics(); assertEquals(cursor1, page.getNextPageToken()); assertArrayEquals(sinkList1.toArray(), Iterables.toArray(page.getValues(), Metric.class));