Search in sources :

Example 1 with WriteLogEntriesRequest

use of com.google.logging.v2.WriteLogEntriesRequest 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 2 with WriteLogEntriesRequest

use of com.google.logging.v2.WriteLogEntriesRequest 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)

Example 3 with WriteLogEntriesRequest

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

the class LoggingImplTest method testFlush.

@Test
public void testFlush() throws InterruptedException {
    SettableApiFuture<WriteLogEntriesResponse> mockRpcResponse = SettableApiFuture.create();
    replay(rpcFactoryMock);
    logging = options.getService();
    WriteLogEntriesRequest request = WriteLogEntriesRequest.newBuilder().addAllEntries(Iterables.transform(ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2), LogEntry.toPbFunction(PROJECT))).build();
    EasyMock.expect(loggingRpcMock.write(request)).andReturn(mockRpcResponse);
    EasyMock.replay(loggingRpcMock);
    // no messages, nothing to flush.
    logging.flush();
    // send a message
    logging.write(ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2));
    Thread flushWaiter = new Thread(new Runnable() {

        @Override
        public void run() {
            logging.flush();
        }
    });
    flushWaiter.start();
    // flushWaiter should be waiting for mockRpc to complete.
    flushWaiter.join(1000);
    assertTrue(flushWaiter.isAlive());
    // With the RPC completed, flush should return, and the thread should terminate.
    mockRpcResponse.set(null);
    flushWaiter.join(1000);
    assertFalse(flushWaiter.isAlive());
}
Also used : WriteLogEntriesRequest(com.google.logging.v2.WriteLogEntriesRequest) WriteLogEntriesResponse(com.google.logging.v2.WriteLogEntriesResponse) Test(org.junit.Test)

Example 4 with WriteLogEntriesRequest

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

the class LoggingImplTest method testWriteLogEntriesAsyncWithOptions.

@Test
public void testWriteLogEntriesAsyncWithOptions() {
    Map<String, String> labels = ImmutableMap.of("key", "value");
    WriteLogEntriesRequest request = WriteLogEntriesRequest.newBuilder().putAllLabels(labels).setLogName(LOG_NAME_PB).setResource(MONITORED_RESOURCE.toPb()).addAllEntries(Iterables.transform(ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2), LogEntry.toPbFunction(PROJECT))).build();
    WriteLogEntriesResponse response = WriteLogEntriesResponse.newBuilder().build();
    EasyMock.expect(loggingRpcMock.write(request)).andReturn(ApiFutures.immediateFuture(response));
    EasyMock.replay(rpcFactoryMock, loggingRpcMock);
    logging = options.getService();
    logging.write(ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2), WriteOption.logName(LOG_NAME), WriteOption.resource(MONITORED_RESOURCE), WriteOption.labels(labels));
    logging.flush();
}
Also used : WriteLogEntriesRequest(com.google.logging.v2.WriteLogEntriesRequest) WriteLogEntriesResponse(com.google.logging.v2.WriteLogEntriesResponse) Test(org.junit.Test)

Example 5 with WriteLogEntriesRequest

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

the class LoggingImplTest method testWriteLogEntriesAsync.

@Test
public void testWriteLogEntriesAsync() throws ExecutionException, InterruptedException {
    WriteLogEntriesRequest request = WriteLogEntriesRequest.newBuilder().addAllEntries(Iterables.transform(ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2), LogEntry.toPbFunction(PROJECT))).build();
    WriteLogEntriesResponse response = WriteLogEntriesResponse.newBuilder().build();
    EasyMock.expect(loggingRpcMock.write(request)).andReturn(ApiFutures.immediateFuture(response));
    EasyMock.replay(rpcFactoryMock, loggingRpcMock);
    logging = options.getService();
    logging.write(ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2));
    logging.flush();
}
Also used : WriteLogEntriesRequest(com.google.logging.v2.WriteLogEntriesRequest) WriteLogEntriesResponse(com.google.logging.v2.WriteLogEntriesResponse) Test(org.junit.Test)

Aggregations

WriteLogEntriesRequest (com.google.logging.v2.WriteLogEntriesRequest)7 WriteLogEntriesResponse (com.google.logging.v2.WriteLogEntriesResponse)6 Test (org.junit.Test)6 MonitoredResource (com.google.api.MonitoredResource)1 MonitoredResource (com.google.cloud.MonitoredResource)1 LogEntry (com.google.logging.v2.LogEntry)1 LogNameOneof (com.google.logging.v2.LogNameOneof)1 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1