Search in sources :

Example 6 with DescribeLogStreamsRequest

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

the class CloudwatchRecordWorker method describeLogStreams.

@VisibleForTesting
DescribeLogStreamsResult describeLogStreams() {
    DescribeLogStreamsRequest describeLogStreams = new DescribeLogStreamsRequest();
    describeLogStreams.setLogGroupName(logGroup);
    describeLogStreams.setLogStreamNamePrefix(logStream);
    try {
        DescribeLogStreamsResult describeLogStreamsResult = getAwsLogsClient().describeLogStreams(describeLogStreams);
        if (!describeLogStreamsResult.getLogStreams().isEmpty()) {
            return describeLogStreamsResult;
        } else {
            CreateLogStreamRequest createLogStreamRequest = new CreateLogStreamRequest();
            createLogStreamRequest.setLogGroupName(logGroup);
            createLogStreamRequest.setLogStreamName(logStream);
            getAwsLogsClient().createLogStream(createLogStreamRequest);
            return getAwsLogsClient().describeLogStreams(describeLogStreams);
        }
    } catch (ResourceNotFoundException re) {
        LOGGER.error("Error during describing AWS CloudWatch log streams: log group '{}' not found", logGroup, re);
        throw re;
    }
}
Also used : CreateLogStreamRequest(com.amazonaws.services.logs.model.CreateLogStreamRequest) DescribeLogStreamsRequest(com.amazonaws.services.logs.model.DescribeLogStreamsRequest) DescribeLogStreamsResult(com.amazonaws.services.logs.model.DescribeLogStreamsResult) ResourceNotFoundException(com.amazonaws.services.logs.model.ResourceNotFoundException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 7 with DescribeLogStreamsRequest

use of com.amazonaws.services.logs.model.DescribeLogStreamsRequest 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 8 with DescribeLogStreamsRequest

use of com.amazonaws.services.logs.model.DescribeLogStreamsRequest 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 9 with DescribeLogStreamsRequest

use of com.amazonaws.services.logs.model.DescribeLogStreamsRequest 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)

Example 10 with DescribeLogStreamsRequest

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

the class CloudwatchMetadataHandler method doListTables.

/**
 * List LogStreams within the requested schema (aka LogGroup) in your Cloudwatch account treating each as a 'table'.
 *
 * @see MetadataHandler
 */
@Override
public ListTablesResponse doListTables(BlockAllocator blockAllocator, ListTablesRequest listTablesRequest) throws TimeoutException {
    String nextToken = null;
    String logGroupName = tableResolver.validateSchema(listTablesRequest.getSchemaName());
    DescribeLogStreamsRequest request = new DescribeLogStreamsRequest(logGroupName);
    DescribeLogStreamsResult result;
    List<TableName> tables = new ArrayList<>();
    if (listTablesRequest.getPageSize() == UNLIMITED_PAGE_SIZE_VALUE) {
        do {
            if (tables.size() > MAX_RESULTS) {
                throw new RuntimeException("Too many log streams, exceeded max metadata results for table count.");
            }
            result = invoker.invoke(() -> awsLogs.describeLogStreams(request));
            result.getLogStreams().forEach(next -> tables.add(toTableName(listTablesRequest, next)));
            request.setNextToken(result.getNextToken());
            logger.info("doListTables: Listing log streams  with token {} and size {}", result.getNextToken(), tables.size());
        } while (result.getNextToken() != null);
    } else {
        request.setNextToken(listTablesRequest.getNextToken());
        request.setLimit(listTablesRequest.getPageSize());
        result = invoker.invoke(() -> awsLogs.describeLogStreams(request));
        result.getLogStreams().forEach(next -> tables.add(toTableName(listTablesRequest, next)));
        nextToken = result.getNextToken();
        logger.info("doListTables: Listing log streams with token {} and size {}", result.getNextToken(), tables.size());
    }
    // We add a special table that represents all log streams. This is helpful depending on how
    // you have your logs organized.
    tables.add(new TableName(listTablesRequest.getSchemaName(), ALL_LOG_STREAMS_TABLE));
    return new ListTablesResponse(listTablesRequest.getCatalogName(), tables, nextToken);
}
Also used : TableName(com.amazonaws.athena.connector.lambda.domain.TableName) ArrayList(java.util.ArrayList) DescribeLogStreamsRequest(com.amazonaws.services.logs.model.DescribeLogStreamsRequest) ListTablesResponse(com.amazonaws.athena.connector.lambda.metadata.ListTablesResponse) DescribeLogStreamsResult(com.amazonaws.services.logs.model.DescribeLogStreamsResult)

Aggregations

DescribeLogStreamsRequest (com.amazonaws.services.logs.model.DescribeLogStreamsRequest)11 DescribeLogStreamsResult (com.amazonaws.services.logs.model.DescribeLogStreamsResult)10 LogStream (com.amazonaws.services.logs.model.LogStream)9 TableName (com.amazonaws.athena.connector.lambda.domain.TableName)4 ArrayList (java.util.ArrayList)4 CreateLogStreamRequest (com.amazonaws.services.logs.model.CreateLogStreamRequest)3 Test (org.junit.Test)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 ListTablesResponse (com.amazonaws.athena.connector.lambda.metadata.ListTablesResponse)2 HashSet (java.util.HashSet)2 Test (org.junit.jupiter.api.Test)2 Block (com.amazonaws.athena.connector.lambda.data.Block)1 Constraints (com.amazonaws.athena.connector.lambda.domain.predicate.Constraints)1 EquatableValueSet (com.amazonaws.athena.connector.lambda.domain.predicate.EquatableValueSet)1 ValueSet (com.amazonaws.athena.connector.lambda.domain.predicate.ValueSet)1 GetTableLayoutRequest (com.amazonaws.athena.connector.lambda.metadata.GetTableLayoutRequest)1 GetTableLayoutResponse (com.amazonaws.athena.connector.lambda.metadata.GetTableLayoutResponse)1 GetTableRequest (com.amazonaws.athena.connector.lambda.metadata.GetTableRequest)1 GetTableResponse (com.amazonaws.athena.connector.lambda.metadata.GetTableResponse)1 ListTablesRequest (com.amazonaws.athena.connector.lambda.metadata.ListTablesRequest)1