Search in sources :

Example 1 with LogEntry

use of com.google.logging.v2.LogEntry in project google-cloud-java by GoogleCloudPlatform.

the class LoggingImpl method listLogEntriesAsync.

private static ApiFuture<AsyncPage<LogEntry>> listLogEntriesAsync(final LoggingOptions serviceOptions, final Map<Option.OptionType, ?> options) {
    final ListLogEntriesRequest request = listLogEntriesRequest(serviceOptions, options);
    ApiFuture<ListLogEntriesResponse> list = serviceOptions.getLoggingRpcV2().list(request);
    return transform(list, new Function<ListLogEntriesResponse, AsyncPage<LogEntry>>() {

        @Override
        public AsyncPage<LogEntry> apply(ListLogEntriesResponse listLogEntrysResponse) {
            List<LogEntry> entries = listLogEntrysResponse.getEntriesList() == null ? ImmutableList.<LogEntry>of() : Lists.transform(listLogEntrysResponse.getEntriesList(), LogEntry.FROM_PB_FUNCTION);
            String cursor = listLogEntrysResponse.getNextPageToken().equals("") ? null : listLogEntrysResponse.getNextPageToken();
            return new AsyncPageImpl<>(new LogEntryPageFetcher(serviceOptions, cursor, options), cursor, entries);
        }
    });
}
Also used : ListLogEntriesResponse(com.google.logging.v2.ListLogEntriesResponse) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) AsyncPage(com.google.api.gax.paging.AsyncPage) ListLogEntriesRequest(com.google.logging.v2.ListLogEntriesRequest)

Example 2 with LogEntry

use of com.google.logging.v2.LogEntry in project google-cloud-java by GoogleCloudPlatform.

the class LoggingImpl method writeLogEntriesRequest.

private static WriteLogEntriesRequest writeLogEntriesRequest(LoggingOptions serviceOptions, Iterable<LogEntry> logEntries, Map<Option.OptionType, ?> options) {
    String projectId = serviceOptions.getProjectId();
    WriteLogEntriesRequest.Builder builder = WriteLogEntriesRequest.newBuilder();
    String logName = LOG_NAME.get(options);
    if (logName != null) {
        builder.setLogName(LogName.create(projectId, logName).toString());
    }
    MonitoredResource resource = RESOURCE.get(options);
    if (resource != null) {
        builder.setResource(resource.toPb());
    }
    Map<String, String> labels = LABELS.get(options);
    if (labels != null) {
        builder.putAllLabels(labels);
    }
    builder.addAllEntries(Iterables.transform(logEntries, LogEntry.toPbFunction(projectId)));
    return builder.build();
}
Also used : WriteLogEntriesRequest(com.google.logging.v2.WriteLogEntriesRequest) MonitoredResource(com.google.cloud.MonitoredResource)

Example 3 with LogEntry

use of com.google.logging.v2.LogEntry in project google-cloud-java by GoogleCloudPlatform.

the class LoggingImplTest method testListLogEntriesAsync.

@Test
public void testListLogEntriesAsync() throws ExecutionException, InterruptedException {
    String cursor = "cursor";
    EasyMock.replay(rpcFactoryMock);
    logging = options.getService();
    ListLogEntriesRequest request = ListLogEntriesRequest.newBuilder().addProjectIds(PROJECT).build();
    List<LogEntry> entriesList = ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2);
    ListLogEntriesResponse response = ListLogEntriesResponse.newBuilder().setNextPageToken(cursor).addAllEntries(Lists.transform(entriesList, LogEntry.toPbFunction(PROJECT))).build();
    ApiFuture<ListLogEntriesResponse> futureResponse = ApiFutures.immediateFuture(response);
    EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse);
    EasyMock.replay(loggingRpcMock);
    AsyncPage<LogEntry> page = logging.listLogEntriesAsync().get();
    assertEquals(cursor, page.getNextPageToken());
    assertArrayEquals(entriesList.toArray(), Iterables.toArray(page.getValues(), LogEntry.class));
}
Also used : ListLogEntriesResponse(com.google.logging.v2.ListLogEntriesResponse) ListLogEntriesRequest(com.google.logging.v2.ListLogEntriesRequest) Test(org.junit.Test)

Example 4 with LogEntry

use of com.google.logging.v2.LogEntry in project google-cloud-java by GoogleCloudPlatform.

the class LoggingClientTest method writeLogEntriesExceptionTest.

@Test
@SuppressWarnings("all")
public void writeLogEntriesExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    mockLoggingServiceV2.addException(exception);
    try {
        LogNameOneof logName = LogNameOneof.from(LogName.create("[PROJECT]", "[LOG]"));
        MonitoredResource resource = MonitoredResource.newBuilder().build();
        Map<String, String> labels = new HashMap<>();
        List<LogEntry> entries = new ArrayList<>();
        client.writeLogEntries(logName, resource, labels, entries);
        Assert.fail("No exception raised");
    } catch (ApiException e) {
        Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode());
    }
}
Also used : LogNameOneof(com.google.logging.v2.LogNameOneof) HashMap(java.util.HashMap) StatusRuntimeException(io.grpc.StatusRuntimeException) ArrayList(java.util.ArrayList) MonitoredResource(com.google.api.MonitoredResource) LogEntry(com.google.logging.v2.LogEntry) ApiException(com.google.api.gax.grpc.ApiException) Test(org.junit.Test)

Example 5 with LogEntry

use of com.google.logging.v2.LogEntry in project google-cloud-java by GoogleCloudPlatform.

the class LoggingClientTest method writeLogEntriesTest.

@Test
@SuppressWarnings("all")
public void writeLogEntriesTest() {
    WriteLogEntriesResponse expectedResponse = WriteLogEntriesResponse.newBuilder().build();
    mockLoggingServiceV2.addResponse(expectedResponse);
    LogNameOneof logName = LogNameOneof.from(LogName.create("[PROJECT]", "[LOG]"));
    MonitoredResource resource = MonitoredResource.newBuilder().build();
    Map<String, String> labels = new HashMap<>();
    List<LogEntry> entries = new ArrayList<>();
    WriteLogEntriesResponse actualResponse = client.writeLogEntries(logName, resource, labels, entries);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockLoggingServiceV2.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    WriteLogEntriesRequest actualRequest = (WriteLogEntriesRequest) actualRequests.get(0);
    Assert.assertEquals(logName, actualRequest.getLogNameAsLogNameOneof());
    Assert.assertEquals(resource, actualRequest.getResource());
    Assert.assertEquals(labels, actualRequest.getLabelsMap());
    Assert.assertEquals(entries, actualRequest.getEntriesList());
}
Also used : LogNameOneof(com.google.logging.v2.LogNameOneof) WriteLogEntriesRequest(com.google.logging.v2.WriteLogEntriesRequest) HashMap(java.util.HashMap) WriteLogEntriesResponse(com.google.logging.v2.WriteLogEntriesResponse) ArrayList(java.util.ArrayList) MonitoredResource(com.google.api.MonitoredResource) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) LogEntry(com.google.logging.v2.LogEntry) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)11 ListLogEntriesRequest (com.google.logging.v2.ListLogEntriesRequest)10 ListLogEntriesResponse (com.google.logging.v2.ListLogEntriesResponse)10 ArrayList (java.util.ArrayList)5 LogEntry (com.google.logging.v2.LogEntry)4 HashMap (java.util.HashMap)3 MonitoredResource (com.google.api.MonitoredResource)2 LogNameOneof (com.google.logging.v2.LogNameOneof)2 WriteLogEntriesRequest (com.google.logging.v2.WriteLogEntriesRequest)2 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)2 List (java.util.List)2 ApiException (com.google.api.gax.grpc.ApiException)1 AsyncPage (com.google.api.gax.paging.AsyncPage)1 MonitoredResource (com.google.cloud.MonitoredResource)1 ListLogEntriesPagedResponse (com.google.cloud.logging.spi.v2.PagedResponseWrappers.ListLogEntriesPagedResponse)1 ImmutableList (com.google.common.collect.ImmutableList)1 DeleteLogRequest (com.google.logging.v2.DeleteLogRequest)1 WriteLogEntriesResponse (com.google.logging.v2.WriteLogEntriesResponse)1 ByteString (com.google.protobuf.ByteString)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1