/** * Example of asynchronously creating a metric for logs with severity higher or equal to ERROR. */ // [TARGET createAsync(MetricInfo)] // [VARIABLE "my_metric_name"] public Metric createMetricAsync(String metricName) throws ExecutionException, InterruptedException { // [START createMetricAsync] MetricInfo metricInfo = MetricInfo.of(metricName, "severity>=ERROR"); Future<Metric> future = logging.createAsync(metricInfo); // ... Metric metric = future.get(); // [END createMetricAsync] return metric; }
/** * Example of asynchronously creating a sink to export logs to a BigQuery dataset (in the {@link * LoggingOptions#getProjectId()} project). */ // [TARGET createAsync(SinkInfo)] // [VARIABLE "my_sink_name"] // [VARIABLE "my_dataset"] public Sink createSinkAsync(String sinkName, String datasetName) throws ExecutionException, InterruptedException { // [START createSinkAsync] SinkInfo sinkInfo = SinkInfo.of(sinkName, DatasetDestination.of(datasetName)); Future<Sink> future = logging.createAsync(sinkInfo); // ... Sink sink = future.get(); // [END createSinkAsync] return sink; }
@Test public void testCreateSinkAsync() throws ExecutionException, InterruptedException { LogSink sinkPb = SINK_INFO.toPb(PROJECT); ApiFuture<LogSink> response = ApiFutures.immediateFuture(sinkPb); CreateSinkRequest request = CreateSinkRequest.newBuilder().setParent(PROJECT_PB).setSink(sinkPb).build(); EasyMock.expect(loggingRpcMock.create(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); Sink sink = logging.createAsync(SINK_INFO).get(); assertEquals(new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)), sink); }
@Test public void testCreateMetricAsync() throws ExecutionException, InterruptedException { LogMetric metricPb = METRIC_INFO.toPb(); ApiFuture<LogMetric> response = ApiFutures.immediateFuture(metricPb); CreateLogMetricRequest request = CreateLogMetricRequest.newBuilder().setParent(PROJECT_PB).setMetric(metricPb).build(); EasyMock.expect(loggingRpcMock.create(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); Metric metric = logging.createAsync(METRIC_INFO).get(); assertEquals(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), metric); }