Search in sources :

Example 1 with EntryListOption

use of com.google.cloud.logging.Logging.EntryListOption 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)

Example 2 with EntryListOption

use of com.google.cloud.logging.Logging.EntryListOption in project google-cloud-java by GoogleCloudPlatform.

the class LoggingTest method testEntryListOption.

@Test
public void testEntryListOption() {
    EntryListOption listOption = EntryListOption.pageToken(PAGE_TOKEN);
    assertEquals(PAGE_TOKEN, listOption.getValue());
    assertEquals(EntryListOption.OptionType.PAGE_TOKEN, listOption.getOptionType());
    // page size
    listOption = EntryListOption.pageSize(PAGE_SIZE);
    assertEquals(PAGE_SIZE, listOption.getValue());
    assertEquals(EntryListOption.OptionType.PAGE_SIZE, listOption.getOptionType());
    // filter
    listOption = EntryListOption.filter(FILTER);
    assertEquals(FILTER, listOption.getValue());
    assertEquals(EntryListOption.OptionType.FILTER, listOption.getOptionType());
    // sort order
    listOption = EntryListOption.sortOrder(SortingField.TIMESTAMP, SortingOrder.ASCENDING);
    assertEquals("timestamp asc", listOption.getValue());
    assertEquals(EntryListOption.OptionType.ORDER_BY, listOption.getOptionType());
    listOption = EntryListOption.sortOrder(SortingField.TIMESTAMP, SortingOrder.DESCENDING);
    assertEquals("timestamp desc", listOption.getValue());
    assertEquals(EntryListOption.OptionType.ORDER_BY, listOption.getOptionType());
}
Also used : EntryListOption(com.google.cloud.logging.Logging.EntryListOption) Test(org.junit.Test)

Example 3 with EntryListOption

use of com.google.cloud.logging.Logging.EntryListOption in project google-cloud-java by GoogleCloudPlatform.

the class BaseSystemTest method testWriteAndListLogEntries.

@Test
public void testWriteAndListLogEntries() throws InterruptedException {
    String logName = formatForTest("test-write-log-entries-log");
    String filter = "logName = projects/" + logging().getOptions().getProjectId() + "/logs/" + logName;
    StringPayload firstPayload = StringPayload.of("stringPayload");
    LogEntry firstEntry = LogEntry.newBuilder(firstPayload).addLabel("key1", "value1").setLogName(logName).setHttpRequest(HttpRequest.newBuilder().setStatus(500).build()).setResource(MonitoredResource.newBuilder("global").build()).build();
    JsonPayload secondPayload = JsonPayload.of(ImmutableMap.<String, Object>of("jsonKey", "jsonValue"));
    LogEntry secondEntry = LogEntry.newBuilder(secondPayload).addLabel("key2", "value2").setLogName(logName).setOperation(Operation.of("operationId", "operationProducer")).setResource(MonitoredResource.newBuilder("cloudsql_database").build()).build();
    logging().write(ImmutableList.of(firstEntry));
    logging().write(ImmutableList.of(secondEntry));
    EntryListOption[] options = { EntryListOption.filter(filter), EntryListOption.pageSize(1) };
    Page<LogEntry> page = logging().listLogEntries(options);
    while (Iterators.size(page.iterateAll().iterator()) < 2) {
        Thread.sleep(500);
        page = logging().listLogEntries(options);
    }
    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());
    assertEquals(HttpRequest.newBuilder().setStatus(500).build(), entry.getHttpRequest());
    assertEquals(Severity.DEFAULT, 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("key2", "value2"), entry.getLabels());
    assertEquals("cloudsql_database", entry.getResource().getType());
    assertEquals(Operation.of("operationId", "operationProducer"), entry.getOperation());
    assertEquals(Severity.DEFAULT, entry.getSeverity());
    assertNull(entry.getHttpRequest());
    assertNotNull(entry.getInsertId());
    assertNotNull(entry.getTimestamp());
    options = new EntryListOption[] { EntryListOption.filter(filter), EntryListOption.sortOrder(SortingField.TIMESTAMP, SortingOrder.DESCENDING) };
    page = logging().listLogEntries(options);
    while (Iterators.size(page.iterateAll().iterator()) < 2) {
        Thread.sleep(500);
        page = logging().listLogEntries(options);
    }
    iterator = page.iterateAll().iterator();
    Long lastTimestamp = iterator.next().getTimestamp();
    while (iterator.hasNext()) {
        assertTrue(iterator.next().getTimestamp() <= lastTimestamp);
    }
    assertTrue(logging().deleteLog(logName));
}
Also used : StringPayload(com.google.cloud.logging.Payload.StringPayload) JsonPayload(com.google.cloud.logging.Payload.JsonPayload) EntryListOption(com.google.cloud.logging.Logging.EntryListOption) Test(org.junit.Test)

Aggregations

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