use of com.amazonaws.services.logs.model.DescribeLogGroupsRequest in project aws-athena-query-federation by awslabs.
the class CloudwatchTableResolver method loadLogGroups.
/**
* Loads the requested LogGroup as identified by the schemaName.
*
* @param schemaName The schemaName to load.
* @return The actual LogGroup name in cloudwatch.
* @note This method also primes the cache with other LogGroups found along the way while scanning Cloudwatch.
*/
private String loadLogGroups(String schemaName) throws TimeoutException {
// As an optimization, see if the table name is an exact match (meaning likely no casing issues)
String result = loadLogGroup(schemaName);
if (result != null) {
return result;
}
logger.info("loadLogGroups: Did not find a match for the schema, falling back to LogGroup scan for {}", schemaName);
DescribeLogGroupsRequest validateSchemaRequest = new DescribeLogGroupsRequest();
DescribeLogGroupsResult validateSchemaResult;
do {
validateSchemaResult = invoker.invoke(() -> awsLogs.describeLogGroups(validateSchemaRequest));
for (LogGroup next : validateSchemaResult.getLogGroups()) {
String nextLogGroupName = next.getLogGroupName();
schemaCache.put(schemaName.toLowerCase(), nextLogGroupName);
if (nextLogGroupName.equalsIgnoreCase(schemaName)) {
logger.info("loadLogGroups: Matched {} for {}", nextLogGroupName, schemaName);
return nextLogGroupName;
}
}
validateSchemaRequest.setNextToken(validateSchemaResult.getNextToken());
} while (validateSchemaResult.getNextToken() != null);
// We could not find a match
throw new IllegalArgumentException("No such schema " + schemaName);
}
use of com.amazonaws.services.logs.model.DescribeLogGroupsRequest in project aws-athena-query-federation by awslabs.
the class CloudwatchMetadataHandlerTest method doListSchemaNames.
@Test
public void doListSchemaNames() throws TimeoutException {
logger.info("doListSchemas - enter");
when(mockAwsLogs.describeLogGroups(any(DescribeLogGroupsRequest.class))).thenAnswer((InvocationOnMock invocationOnMock) -> {
DescribeLogGroupsRequest request = (DescribeLogGroupsRequest) invocationOnMock.getArguments()[0];
DescribeLogGroupsResult result = new DescribeLogGroupsResult();
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<LogGroup> logGroups = new ArrayList<>();
if (request.getNextToken() == null || Integer.valueOf(request.getNextToken()) < 3) {
for (int i = 0; i < 10; i++) {
LogGroup nextLogGroup = new LogGroup();
nextLogGroup.setLogGroupName("schema-" + String.valueOf(i));
logGroups.add(nextLogGroup);
}
}
result.withLogGroups(logGroups);
if (nextToken != null) {
result.setNextToken(String.valueOf(nextToken));
}
return result;
});
ListSchemasRequest req = new ListSchemasRequest(identity, "queryId", "default");
ListSchemasResponse res = handler.doListSchemaNames(allocator, req);
logger.info("doListSchemas - {}", res.getSchemas());
assertTrue(res.getSchemas().size() == 30);
verify(mockAwsLogs, times(4)).describeLogGroups(any(DescribeLogGroupsRequest.class));
verifyNoMoreInteractions(mockAwsLogs);
logger.info("doListSchemas - exit");
}
use of com.amazonaws.services.logs.model.DescribeLogGroupsRequest in project aws-athena-query-federation by awslabs.
the class CloudwatchMetadataHandler method doListSchemaNames.
/**
* List LogGroups in your Cloudwatch account treating each as a 'schema' (aka database)
*
* @see MetadataHandler
*/
@Override
public ListSchemasResponse doListSchemaNames(BlockAllocator blockAllocator, ListSchemasRequest listSchemasRequest) throws TimeoutException {
DescribeLogGroupsRequest request = new DescribeLogGroupsRequest();
DescribeLogGroupsResult result;
List<String> schemas = new ArrayList<>();
do {
if (schemas.size() > MAX_RESULTS) {
throw new RuntimeException("Too many log groups, exceeded max metadata results for schema count.");
}
result = invoker.invoke(() -> awsLogs.describeLogGroups(request));
result.getLogGroups().forEach(next -> schemas.add(next.getLogGroupName().toLowerCase()));
request.setNextToken(result.getNextToken());
logger.info("doListSchemaNames: Listing log groups {} {}", result.getNextToken(), schemas.size());
} while (result.getNextToken() != null);
return new ListSchemasResponse(listSchemasRequest.getCatalogName(), schemas);
}
use of com.amazonaws.services.logs.model.DescribeLogGroupsRequest in project aws-athena-query-federation by awslabs.
the class CloudwatchTableResolver method loadLogGroup.
/**
* Optomizaiton that attempts to load a specific LogStream as identified by the TableName.
*
* @param schemaName The schemaName to load.
* @return The CloudwatchTableName or null if not found.
*/
private String loadLogGroup(String schemaName) throws TimeoutException {
DescribeLogGroupsRequest request = new DescribeLogGroupsRequest().withLogGroupNamePrefix(schemaName);
DescribeLogGroupsResult result = invoker.invoke(() -> awsLogs.describeLogGroups(request));
for (LogGroup next : result.getLogGroups()) {
String nextLogGroupName = next.getLogGroupName();
if (nextLogGroupName.equalsIgnoreCase(schemaName)) {
logger.info("loadLogGroup: Matched {} for {}", nextLogGroupName, schemaName);
return nextLogGroupName;
}
}
return null;
}
Aggregations