Search in sources :

Example 1 with ProtoPayload

use of com.google.cloud.logging.Payload.ProtoPayload in project google-cloud-java by GoogleCloudPlatform.

the class BaseSystemTest method testWriteAndListLogEntriesAsync.

@Test
public void testWriteAndListLogEntriesAsync() throws InterruptedException, ExecutionException {
    String logName = formatForTest("test-write-log-entries-async-log");
    String filter = "logName = projects/" + logging().getOptions().getProjectId() + "/logs/" + logName;
    StringPayload firstPayload = StringPayload.of("stringPayload");
    LogEntry firstEntry = LogEntry.newBuilder(firstPayload).setSeverity(Severity.ALERT).build();
    ProtoPayload secondPayload = ProtoPayload.of(Any.pack(StringValue.newBuilder().setValue("protoPayload").build()));
    LogEntry secondEntry = LogEntry.newBuilder(secondPayload).setSeverity(Severity.DEBUG).build();
    logging().write(ImmutableList.of(firstEntry, secondEntry), WriteOption.labels(ImmutableMap.of("key1", "value1")), WriteOption.resource(MonitoredResource.newBuilder("global").build()), WriteOption.logName(logName));
    logging().flush();
    EntryListOption[] options = { EntryListOption.filter(filter), EntryListOption.pageSize(1) };
    AsyncPage<LogEntry> page = logging().listLogEntriesAsync(options).get();
    while (Iterators.size(page.iterateAll().iterator()) < 2) {
        Thread.sleep(500);
        page = logging().listLogEntriesAsync(options).get();
    }
    Iterator<LogEntry> iterator = page.iterateAll().iterator();
    assertTrue(iterator.hasNext());
    LogEntry entry = iterator.next();
    assertEquals(firstPayload, entry.getPayload());
    assertEquals(logName, entry.getLogName());
    assertEquals(ImmutableMap.of("key1", "value1"), entry.getLabels());
    assertEquals("global", entry.getResource().getType());
    assertNull(entry.getHttpRequest());
    assertEquals(Severity.ALERT, entry.getSeverity());
    assertNull(entry.getOperation());
    assertNotNull(entry.getInsertId());
    assertNotNull(entry.getTimestamp());
    assertTrue(iterator.hasNext());
    entry = iterator.next();
    assertEquals(secondPayload, entry.getPayload());
    assertEquals(logName, entry.getLogName());
    assertEquals(ImmutableMap.of("key1", "value1"), entry.getLabels());
    assertEquals("global", entry.getResource().getType());
    assertNull(entry.getOperation());
    assertEquals(Severity.DEBUG, entry.getSeverity());
    assertNull(entry.getHttpRequest());
    assertNotNull(entry.getInsertId());
    assertNotNull(entry.getTimestamp());
    assertTrue(logging().deleteLogAsync(logName).get());
}
Also used : ProtoPayload(com.google.cloud.logging.Payload.ProtoPayload) StringPayload(com.google.cloud.logging.Payload.StringPayload) EntryListOption(com.google.cloud.logging.Logging.EntryListOption) Test(org.junit.Test)

Aggregations

EntryListOption (com.google.cloud.logging.Logging.EntryListOption)1 ProtoPayload (com.google.cloud.logging.Payload.ProtoPayload)1 StringPayload (com.google.cloud.logging.Payload.StringPayload)1 Test (org.junit.Test)1