Search in sources :

Example 6 with LogStream

use of com.amazonaws.services.logs.model.LogStream in project aws-athena-query-federation by awslabs.

the class CloudwatchMetadataHandlerTest method doGetTable.

@Test
public void doGetTable() {
    logger.info("doGetTable - enter");
    String expectedSchema = "schema-20";
    when(mockAwsLogs.describeLogStreams(any(DescribeLogStreamsRequest.class))).thenAnswer((InvocationOnMock invocationOnMock) -> {
        DescribeLogStreamsRequest request = (DescribeLogStreamsRequest) invocationOnMock.getArguments()[0];
        assertTrue(request.getLogGroupName().equals(expectedSchema));
        DescribeLogStreamsResult result = new DescribeLogStreamsResult();
        Integer nextToken;
        if (request.getNextToken() == null) {
            nextToken = 1;
        } else if (Integer.valueOf(request.getNextToken()) < 3) {
            nextToken = Integer.valueOf(request.getNextToken()) + 1;
        } else {
            nextToken = null;
        }
        List<LogStream> logStreams = new ArrayList<>();
        if (request.getNextToken() == null || Integer.valueOf(request.getNextToken()) < 3) {
            for (int i = 0; i < 10; i++) {
                LogStream nextLogStream = new LogStream();
                nextLogStream.setLogStreamName("table-" + String.valueOf(i));
                logStreams.add(nextLogStream);
            }
        }
        result.withLogStreams(logStreams);
        if (nextToken != null) {
            result.setNextToken(String.valueOf(nextToken));
        }
        return result;
    });
    GetTableRequest req = new GetTableRequest(identity, "queryId", "default", new TableName(expectedSchema, "table-9"));
    GetTableResponse res = handler.doGetTable(allocator, req);
    logger.info("doGetTable - {} {}", res.getTableName(), res.getSchema());
    assertEquals(new TableName(expectedSchema, "table-9"), res.getTableName());
    assertTrue(res.getSchema() != null);
    verify(mockAwsLogs, times(1)).describeLogStreams(any(DescribeLogStreamsRequest.class));
    logger.info("doGetTable - exit");
}
Also used : GetTableRequest(com.amazonaws.athena.connector.lambda.metadata.GetTableRequest) TableName(com.amazonaws.athena.connector.lambda.domain.TableName) GetTableResponse(com.amazonaws.athena.connector.lambda.metadata.GetTableResponse) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArrayList(java.util.ArrayList) DescribeLogStreamsRequest(com.amazonaws.services.logs.model.DescribeLogStreamsRequest) LogStream(com.amazonaws.services.logs.model.LogStream) DescribeLogStreamsResult(com.amazonaws.services.logs.model.DescribeLogStreamsResult) Test(org.junit.Test)

Example 7 with LogStream

use of com.amazonaws.services.logs.model.LogStream in project cloudwatch-logback-appender by j256.

the class CloudWatchAppenderTest method testMoreAwsCalls.

@Test(timeout = 10000)
public void testMoreAwsCalls() throws InterruptedException {
    CloudWatchAppender appender = new CloudWatchAppender();
    AWSLogs logsClient = createMock(AWSLogs.class);
    AmazonEC2 ec2Client = createMock(AmazonEC2.class);
    appender.setTestAwsLogsClient(logsClient);
    appender.setTestAmazonEc2Client(ec2Client);
    appender.setMaxBatchSize(1);
    appender.setRegion("region");
    final String logGroup = "pfqoejpfqe";
    appender.setLogGroup(logGroup);
    final String logStream = "pffqjfqjpoqoejpfqe";
    appender.setLogStream(logStream);
    appender.setContext(LOGGER_CONTEXT);
    PatternLayout layout = new PatternLayout();
    layout.setContext(LOGGER_CONTEXT);
    layout.setPattern("[%thread] %level %logger{20} - %msg%n%xThrowable");
    layout.start();
    appender.setLayout(layout);
    final String loggerName = "name";
    final Level level = Level.DEBUG;
    String message = "kuykregddwqwef4wve";
    LoggingEvent event = createEvent(loggerName, level, message, System.currentTimeMillis());
    String threadName = Thread.currentThread().getName();
    final String fullMessage = "[" + threadName + "] " + level + " " + loggerName + " - " + message + "\n";
    DescribeLogGroupsResult logGroupsResult = new DescribeLogGroupsResult().withLogGroups(Arrays.asList(new LogGroup().withLogGroupName(logGroup)));
    expect(logsClient.describeLogGroups(isA(DescribeLogGroupsRequest.class))).andReturn(logGroupsResult);
    DescribeLogStreamsResult logStreamsResult = new DescribeLogStreamsResult().withLogStreams(Arrays.asList(new LogStream().withLogStreamName(logStream)));
    expect(logsClient.describeLogStreams(isA(DescribeLogStreamsRequest.class))).andReturn(logStreamsResult);
    final PutLogEventsResult putLogEventsResult = new PutLogEventsResult();
    String sequence = "ewopjfewfj";
    putLogEventsResult.setNextSequenceToken(sequence);
    expect(logsClient.putLogEvents(isA(PutLogEventsRequest.class))).andAnswer(new IAnswer<PutLogEventsResult>() {

        @Override
        public PutLogEventsResult answer() {
            PutLogEventsRequest request = (PutLogEventsRequest) getCurrentArguments()[0];
            assertEquals(logGroup, request.getLogGroupName());
            assertEquals(logStream, request.getLogStreamName());
            List<InputLogEvent> events = request.getLogEvents();
            assertEquals(1, events.size());
            assertEquals(fullMessage, events.get(0).getMessage());
            return putLogEventsResult;
        }
    }).times(2);
    logsClient.shutdown();
    // =====================================
    replay(logsClient, ec2Client);
    appender.start();
    // for coverage
    appender.start();
    appender.append(event);
    Thread.sleep(100);
    appender.append(event);
    while (appender.getEventsWrittenCount() < 2) {
        Thread.sleep(100);
    }
    appender.stop();
    verify(logsClient, ec2Client);
}
Also used : DescribeLogGroupsResult(com.amazonaws.services.logs.model.DescribeLogGroupsResult) InputLogEvent(com.amazonaws.services.logs.model.InputLogEvent) PatternLayout(ch.qos.logback.classic.PatternLayout) PutLogEventsResult(com.amazonaws.services.logs.model.PutLogEventsResult) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) LogStream(com.amazonaws.services.logs.model.LogStream) AWSLogs(com.amazonaws.services.logs.AWSLogs) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggingEvent(ch.qos.logback.classic.spi.LoggingEvent) IAnswer(org.easymock.IAnswer) LogGroup(com.amazonaws.services.logs.model.LogGroup) Level(ch.qos.logback.classic.Level) DescribeLogStreamsRequest(com.amazonaws.services.logs.model.DescribeLogStreamsRequest) DescribeLogGroupsRequest(com.amazonaws.services.logs.model.DescribeLogGroupsRequest) DescribeLogStreamsResult(com.amazonaws.services.logs.model.DescribeLogStreamsResult) PutLogEventsRequest(com.amazonaws.services.logs.model.PutLogEventsRequest) Test(org.junit.Test)

Example 8 with LogStream

use of com.amazonaws.services.logs.model.LogStream in project cloudbreak by hortonworks.

the class CloudwatchRecordWorkerTest method testDescribeLogStreamsWhenLogStreamDoesNotExist.

@Test
public void testDescribeLogStreamsWhenLogStreamDoesNotExist() {
    DescribeLogStreamsRequest describeLogStreamsRequest = new DescribeLogStreamsRequest();
    describeLogStreamsRequest.setLogGroupName("TestGroup");
    describeLogStreamsRequest.setLogStreamNamePrefix(underTest.initLogStream("TestStream"));
    DescribeLogStreamsResult describeLogStreamsResult = new DescribeLogStreamsResult();
    Set<LogStream> logStreams = new HashSet<>();
    describeLogStreamsResult.setLogStreams(logStreams);
    when(awsLogsClient.describeLogStreams(describeLogStreamsRequest)).thenReturn(describeLogStreamsResult);
    when(awsLogsClient.describeLogStreams(describeLogStreamsRequest)).thenReturn(describeLogStreamsResult);
    CreateLogStreamRequest createLogStreamRequest = new CreateLogStreamRequest();
    createLogStreamRequest.setLogGroupName("TestGroup");
    createLogStreamRequest.setLogStreamName(underTest.initLogStream("TestStream"));
    when(awsLogsClient.createLogStream(createLogStreamRequest)).thenReturn(new CreateLogStreamResult());
    underTest.describeLogStreams();
    verify(awsLogsClient, times(2)).describeLogStreams(describeLogStreamsRequest);
    verify(awsLogsClient, times(1)).createLogStream(createLogStreamRequest);
}
Also used : CreateLogStreamRequest(com.amazonaws.services.logs.model.CreateLogStreamRequest) CreateLogStreamResult(com.amazonaws.services.logs.model.CreateLogStreamResult) DescribeLogStreamsRequest(com.amazonaws.services.logs.model.DescribeLogStreamsRequest) LogStream(com.amazonaws.services.logs.model.LogStream) DescribeLogStreamsResult(com.amazonaws.services.logs.model.DescribeLogStreamsResult) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 9 with LogStream

use of com.amazonaws.services.logs.model.LogStream in project cloudbreak by hortonworks.

the class CloudwatchRecordWorkerTest method testDescribeLogStreamsWhenLogStreamExists.

@Test
public void testDescribeLogStreamsWhenLogStreamExists() {
    DescribeLogStreamsRequest describeLogStreamsRequest = new DescribeLogStreamsRequest();
    describeLogStreamsRequest.setLogGroupName("TestGroup");
    describeLogStreamsRequest.setLogStreamNamePrefix(underTest.initLogStream("TestStream"));
    DescribeLogStreamsResult describeLogStreamsResult = new DescribeLogStreamsResult();
    Set<LogStream> logStreams = new HashSet<>();
    LogStream logStream = new LogStream();
    logStreams.add(logStream);
    describeLogStreamsResult.setLogStreams(logStreams);
    when(awsLogsClient.describeLogStreams(describeLogStreamsRequest)).thenReturn(describeLogStreamsResult);
    underTest.describeLogStreams();
    verify(awsLogsClient, times(1)).describeLogStreams(describeLogStreamsRequest);
}
Also used : DescribeLogStreamsRequest(com.amazonaws.services.logs.model.DescribeLogStreamsRequest) LogStream(com.amazonaws.services.logs.model.LogStream) DescribeLogStreamsResult(com.amazonaws.services.logs.model.DescribeLogStreamsResult) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 10 with LogStream

use of com.amazonaws.services.logs.model.LogStream in project quarkus-logging-cloudwatch by quarkiverse.

the class LoggingCloudWatchHandlerValueFactory method createLogStreamIfNeeded.

private String createLogStreamIfNeeded(AWSLogs awsLogs, LoggingCloudWatchConfig config) {
    String token = null;
    DescribeLogStreamsRequest describeLogStreamsRequest = new DescribeLogStreamsRequest(config.logGroup.get());
    // We need to filter down, as CW returns by default only 50 streams and ours may not be in it.
    describeLogStreamsRequest.withLogStreamNamePrefix(config.logStreamName.get());
    List<LogStream> logStreams = awsLogs.describeLogStreams(describeLogStreamsRequest).getLogStreams();
    boolean found = false;
    for (LogStream ls : logStreams) {
        if (ls.getLogStreamName().equals(config.logStreamName.get())) {
            found = true;
            token = ls.getUploadSequenceToken();
        }
    }
    if (!found) {
        awsLogs.createLogStream(new CreateLogStreamRequest(config.logGroup.get(), config.logStreamName.get()));
    }
    return token;
}
Also used : CreateLogStreamRequest(com.amazonaws.services.logs.model.CreateLogStreamRequest) DescribeLogStreamsRequest(com.amazonaws.services.logs.model.DescribeLogStreamsRequest) LogStream(com.amazonaws.services.logs.model.LogStream)

Aggregations

DescribeLogStreamsRequest (com.amazonaws.services.logs.model.DescribeLogStreamsRequest)11 LogStream (com.amazonaws.services.logs.model.LogStream)11 DescribeLogStreamsResult (com.amazonaws.services.logs.model.DescribeLogStreamsResult)10 Test (org.junit.Test)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 TableName (com.amazonaws.athena.connector.lambda.domain.TableName)3 DescribeLogGroupsRequest (com.amazonaws.services.logs.model.DescribeLogGroupsRequest)3 ArrayList (java.util.ArrayList)3 CreateLogStreamRequest (com.amazonaws.services.logs.model.CreateLogStreamRequest)2 DescribeLogGroupsResult (com.amazonaws.services.logs.model.DescribeLogGroupsResult)2 LogGroup (com.amazonaws.services.logs.model.LogGroup)2 HashSet (java.util.HashSet)2 Test (org.junit.jupiter.api.Test)2 Level (ch.qos.logback.classic.Level)1 PatternLayout (ch.qos.logback.classic.PatternLayout)1 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 LoggingEvent (ch.qos.logback.classic.spi.LoggingEvent)1 Block (com.amazonaws.athena.connector.lambda.data.Block)1 BlockAllocatorImpl (com.amazonaws.athena.connector.lambda.data.BlockAllocatorImpl)1 Constraints (com.amazonaws.athena.connector.lambda.domain.predicate.Constraints)1