software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsRequest Java Examples

The following examples show how to use software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CloudWatchLogPublisherTest.java    From cloudformation-cli-java-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void testPublishLogEventsHappyCase() {
    final CloudWatchLogPublisher logPublisher = new CloudWatchLogPublisher(cloudWatchLogsProvider, LOG_GROUP_NAME,
                                                                           LOG_STREAM_NAME, platformLambdaLogger,
                                                                           metricsPublisherProxy);
    final ArgumentCaptor<
        PutLogEventsRequest> putLogEventsRequestArgumentCaptor = ArgumentCaptor.forClass(PutLogEventsRequest.class);

    when(cloudWatchLogsProvider.get()).thenReturn(cloudWatchLogsClient);
    when(cloudWatchLogsClient.putLogEvents(putLogEventsRequestArgumentCaptor.capture())).thenReturn(null);
    final String msgToLog = "How is it going?";
    logPublisher.refreshClient();
    logPublisher.publishLogEvent(msgToLog);

    assertThat(putLogEventsRequestArgumentCaptor.getValue().logGroupName()).isEqualTo(LOG_GROUP_NAME);
    assertThat(putLogEventsRequestArgumentCaptor.getValue().logEvents().get(0).message()).isEqualTo(msgToLog);

    verify(cloudWatchLogsClient).putLogEvents(putLogEventsRequestArgumentCaptor.getValue());
    verifyNoMoreInteractions(cloudWatchLogsProvider);
}
 
Example #2
Source File: CloudWatchLogPublisherTest.java    From cloudformation-cli-java-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void testPublishLogEventsWithErrorAndNullMetricsPublisher() {
    final CloudWatchLogPublisher logPublisher = new CloudWatchLogPublisher(cloudWatchLogsProvider, LOG_GROUP_NAME,
                                                                           LOG_STREAM_NAME, platformLambdaLogger, null);
    final ArgumentCaptor<
        PutLogEventsRequest> putLogEventsRequestArgumentCaptor = ArgumentCaptor.forClass(PutLogEventsRequest.class);
    final ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class);

    when(cloudWatchLogsProvider.get()).thenReturn(cloudWatchLogsClient);
    when(cloudWatchLogsClient.putLogEvents(putLogEventsRequestArgumentCaptor.capture()))
        .thenThrow(new RuntimeException("AccessDenied"));
    doNothing().when(platformLambdaLogger).log(stringArgumentCaptor.capture());

    final String msgToLog = "How is it going?";
    logPublisher.refreshClient();
    logPublisher.publishLogEvent(msgToLog);

    assertThat(putLogEventsRequestArgumentCaptor.getValue().logGroupName()).isEqualTo(LOG_GROUP_NAME);
    assertThat(putLogEventsRequestArgumentCaptor.getValue().logEvents().get(0).message()).isEqualTo(msgToLog);

    verify(cloudWatchLogsClient).putLogEvents(putLogEventsRequestArgumentCaptor.getValue());
    verify(metricsPublisherProxy, times(0)).publishProviderLogDeliveryExceptionMetric(any(), any());
    assertThat(stringArgumentCaptor.getValue().contains("AccessDenied"));
    verifyNoMoreInteractions(cloudWatchLogsProvider, metricsPublisherProxy);
}
 
Example #3
Source File: ServiceIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that we have deserialized the exception response correctly. See TT0064111680
 */
@Test
public void putLogEvents_InvalidSequenceNumber_HasExpectedSequenceNumberInException() {
    // First call to PutLogEvents does not need a sequence number, subsequent calls do
    awsLogs.putLogEvents(PutLogEventsRequest.builder()
                                            .logGroupName(logGroupName)
                                            .logStreamName(logStreamName)
                                            .logEvents(InputLogEvent.builder().message(LOG_MESSAGE).timestamp(LOG_MESSAGE_TIMESTAMP).build())
                                            .build());
    try {
        // This call requires a sequence number, if we provide an invalid one the service should
        // throw an exception with the expected sequence number
        awsLogs.putLogEvents(
                PutLogEventsRequest.builder()
                                   .logGroupName(logGroupName)
                                   .logStreamName(logStreamName)
                                   .logEvents(InputLogEvent.builder().message(LOG_MESSAGE).timestamp(LOG_MESSAGE_TIMESTAMP).build())
                                   .sequenceToken("invalid")
                                   .build());
    } catch (InvalidSequenceTokenException e) {
        assertNotNull(e.expectedSequenceToken());
    }
}
 
Example #4
Source File: CloudWatchLogPublisherTest.java    From cloudformation-cli-java-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testPublishLogEventsWithError() {
    final CloudWatchLogPublisher logPublisher = new CloudWatchLogPublisher(cloudWatchLogsProvider, LOG_GROUP_NAME,
                                                                           LOG_STREAM_NAME, platformLambdaLogger,
                                                                           metricsPublisherProxy);
    final ArgumentCaptor<
        PutLogEventsRequest> putLogEventsRequestArgumentCaptor = ArgumentCaptor.forClass(PutLogEventsRequest.class);
    final ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class);

    when(cloudWatchLogsProvider.get()).thenReturn(cloudWatchLogsClient);
    when(cloudWatchLogsClient.putLogEvents(putLogEventsRequestArgumentCaptor.capture()))
        .thenThrow(new RuntimeException("AccessDenied"));
    doNothing().when(metricsPublisherProxy).publishProviderLogDeliveryExceptionMetric(any(), any());
    doNothing().when(platformLambdaLogger).log(stringArgumentCaptor.capture());

    final String msgToLog = "How is it going?";
    logPublisher.refreshClient();
    logPublisher.publishLogEvent(msgToLog);

    assertThat(putLogEventsRequestArgumentCaptor.getValue().logGroupName()).isEqualTo(LOG_GROUP_NAME);
    assertThat(putLogEventsRequestArgumentCaptor.getValue().logEvents().get(0).message()).isEqualTo(msgToLog);

    verify(cloudWatchLogsClient).putLogEvents(putLogEventsRequestArgumentCaptor.getValue());
    verify(metricsPublisherProxy).publishProviderLogDeliveryExceptionMetric(any(), any());
    assertThat(stringArgumentCaptor.getValue().contains("AccessDenied"));
    verifyNoMoreInteractions(cloudWatchLogsProvider);
}
 
Example #5
Source File: PutLogEvents.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void putCWLogEvents(CloudWatchLogsClient logsClient, String regionId, String logGroupName, String streamName) {


        // A sequence token is required to put a log event in an existing stream.
        // Look up the stream to find its sequence token.

        // First describe all streams in the log group.
        DescribeLogStreamsRequest logStreamRequest = DescribeLogStreamsRequest.builder()
                .logGroupName(logGroupName)
                .logStreamNamePrefix(streamName)
                .build();
        DescribeLogStreamsResponse describeLogStreamsResponse = logsClient.describeLogStreams(logStreamRequest);

        // Assume that a single stream is returned because a specific stream name was specified in the previous request.
        String sequenceToken = describeLogStreamsResponse.logStreams().get(0).uploadSequenceToken();

        // Build an input log message to put to CloudWatch.
        InputLogEvent inputLogEvent = InputLogEvent.builder()
                .message("{ \"key1\": \"value1\", \"key2\": \"value2\" }")
                .timestamp(System.currentTimeMillis())
                .build();

        // Specify the request parameters.
        PutLogEventsRequest putLogEventsRequest = PutLogEventsRequest.builder()
                .logEvents(Arrays.asList(inputLogEvent))
                .logGroupName(logGroupName)
                .logStreamName(streamName)
                // Sequence token is required so that the log can be written to the
                // latest location in the stream.
                .sequenceToken(sequenceToken)
                .build();
        logsClient.putLogEvents(putLogEventsRequest);
        // snippet-end:[cloudwatch.java2.put_log_events.main]

        System.out.println("Successfully put CloudWatch log event");
    }
 
Example #6
Source File: ServiceIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
/**
 * Test uploading and retrieving log events.
 */
@Test
public void testEventsLogging() {
    // No log event is expected in the newly created log stream
    GetLogEventsResponse getResult = awsLogs.getLogEvents(GetLogEventsRequest.builder().logGroupName(logGroupName).logStreamName(logStreamName).build());
    Assert.assertTrue(getResult.events().isEmpty());

    // Insert a new log event
    PutLogEventsRequest request = PutLogEventsRequest.builder()
                                                     .logGroupName(logGroupName)
                                                     .logStreamName(logStreamName)
                                                     .logEvents(InputLogEvent.builder()
                                                                             .message(LOG_MESSAGE)
                                                                             .timestamp(LOG_MESSAGE_TIMESTAMP)
                                                                             .build())
                                                     .build();
    PutLogEventsResponse putResult = awsLogs.putLogEvents(request);

    Assert.assertNotNull(putResult.nextSequenceToken());

    // The new log event is not instantly available in GetLogEvents operation.
    try {
        Thread.sleep(5000);
    } catch (InterruptedException ignored) {
        // Ignored or expected.
    }

    // Pull the event from the log stream
    getResult = awsLogs.getLogEvents(GetLogEventsRequest.builder().logGroupName(logGroupName).logStreamName(logStreamName).build());
    Assert.assertEquals(1, getResult.events().size());
    Assert.assertNotNull(getResult.nextBackwardToken());
    Assert.assertNotNull(getResult.nextForwardToken());

    OutputLogEvent event = getResult.events().get(0);
    Assert.assertEquals(LOG_MESSAGE, event.message());
    Assert.assertEquals(LOG_MESSAGE_TIMESTAMP, event.timestamp().longValue());

    // Use DescribeLogStreams API to verify that the new log event has
    // updated the following parameters of the log stream.
    final LogStream stream = findLogStreamByName(awsLogs, logGroupName, logStreamName);
    Assert.assertEquals(LOG_MESSAGE_TIMESTAMP, stream.firstEventTimestamp().longValue());
    Assert.assertEquals(LOG_MESSAGE_TIMESTAMP, stream.lastEventTimestamp().longValue());
    Assert.assertNotNull(stream.lastIngestionTime());
}