private void fetchCloudWatchLogs(String logStreamName, PrintStream logger) { if(logStreamName == null || "".equals(logStreamName)) return; logger.println("Fetching logs from cloudwatch logs for final attempt..."); logger.println("-------------------------------------------------------"); AWSLogs awslogs = AWSLogsClientBuilder.defaultClient(); GetLogEventsResult logEventsResult = awslogs.getLogEvents( new GetLogEventsRequest() .withLogGroupName("/aws/batch/job") .withLogStreamName(logStreamName) ); for(OutputLogEvent ole : logEventsResult.getEvents()) { logger.printf("[%s] %s%n", df.format(new Date(ole.getTimestamp())), ole.getMessage()); } }
@Test public void testGettersSetters() { @SuppressWarnings("unchecked") final Layout<ILoggingEvent> layout = mock(Layout.class); final AWSLogs awsLogs = mock(AWSLogs.class); final String logGroupName = "myGroup"; final String logStreamName = "myStream"; final CloudWatchAppender appender = new CloudWatchAppender(); appender.setLayout(layout); appender.setAwsLogs(awsLogs); appender.setLogGroupName(logGroupName); appender.setLogStreamName(logStreamName); assertEquals(layout, appender.getLayout()); assertEquals(awsLogs, appender.getAwsLogs()); assertEquals(logGroupName, appender.getLogGroupName()); assertEquals(logStreamName, appender.getLogStreamName()); }
@Test public void testDefaultValues() { final Context mockContext = mock(Context.class); final PutLogEventsResult mockResult = mock(PutLogEventsResult.class); when(mockResult.getNextSequenceToken()).thenReturn("2"); final AWSLogs mockAwsLogs = mock(AWSLogs.class); when(mockAwsLogs.putLogEvents(any())).thenReturn(mockResult); final CloudWatchAppender appender = new CloudWatchAppender(); appender.setContext(mockContext); appender.setAwsLogs(mockAwsLogs); appender.start(); appender.doAppend(new LoggingEvent()); appender.stop(); }
@Test public void testAlreadyExists() { final Context mockContext = mock(Context.class); final PutLogEventsResult mockResult = mock(PutLogEventsResult.class); when(mockResult.getNextSequenceToken()).thenReturn("2"); final AWSLogs mockAwsLogs = mock(AWSLogs.class); when(mockAwsLogs.createLogGroup(any())).thenThrow(ResourceAlreadyExistsException.class); when(mockAwsLogs.createLogStream(any())).thenThrow(ResourceAlreadyExistsException.class); when(mockAwsLogs.putLogEvents(any())).thenReturn(mockResult); final CloudWatchAppender appender = new CloudWatchAppender(); appender.setContext(mockContext); appender.setAwsLogs(mockAwsLogs); appender.start(); appender.doAppend(new LoggingEvent()); appender.stop(); }
/** * For internal use only. This constructor lets us switch the AWSLogs implementation for testing. */ public CloudwatchLogsLogEventPutter(CloudwatchLogsConfig config, BlockingQueue<CloudwatchLogsLogEvent> eventQueue, AWSLogs awsLogs, boolean enabled) { this.config = config; logGroupName = config.getLogGroup(); String image = config.getImage(); app = image.substring(0, image.indexOf(":")); this.eventQueue = eventQueue; this.enabled = enabled; logsClient = awsLogs; }
static AWSLogs createLogsClient(CloudwatchLogsConfig config) { AWSLogsClientBuilder builder = AWSLogsClientBuilder.standard(); if (config.getEndpoint() != null) { // Non-AWS mock endpoint builder.setCredentials(new AWSStaticCredentialsProvider(new AnonymousAWSCredentials())); builder.setEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(config.getEndpoint(), config.getRegion())); } else { builder.setRegion(config.getRegion()); } return builder.build(); }
AWSLogsStub(String logGroupName, String logStreamName, String logRegion) { this.logGroupName = logGroupName; this.logStreamName = logStreamName; AWSLogs awsLogs = new AWSLogsClient(); if (logRegion != null) { awsLogs.setRegion(RegionUtils.getRegion(logRegion)); } this.awsLogs = awsLogs; }
public static void main(String[] args) { AWSLogs client = AWSLogsClientBuilder.standard().withRegion("eu-west-1").build(); // Harvest all the log groups in this region DescribeLogGroupsRequest describeLogGroupsRequest = new DescribeLogGroupsRequest(); DescribeLogGroupsResult describeLogGroupsResult = client .describeLogGroups(describeLogGroupsRequest); List<LogGroup> logGroups = describeLogGroupsResult.getLogGroups(); String token = describeLogGroupsResult.getNextToken(); int index = 0; while (token != null) { index++; describeLogGroupsRequest.setNextToken(token); describeLogGroupsResult = client.describeLogGroups(describeLogGroupsRequest); logGroups.addAll(describeLogGroupsResult.getLogGroups()); token = describeLogGroupsResult.getNextToken(); System.out.println(index); System.out.println(token); } // Delete each log group logGroups.stream().forEach( (logGroup) -> { DeleteLogGroupRequest deleteLogGroupRequest = new DeleteLogGroupRequest(logGroup .getLogGroupName()); try { RetryHelper.DoWithRetries(() -> client.deleteLogGroup(deleteLogGroupRequest), AmazonServiceException.class, Optional.of("429"), (logString) -> { }); } catch (Exception e) { e.printStackTrace(); } }); }
public static void main(String[] args) { final String USAGE = "To run this example, supply:\n" + "* a filter name\n" + "* filter pattern\n" + "* log group name\n" + "* lambda function arn\n\n" + "Ex: PutSubscriptionFilter <filter-name> \\\n" + " <filter pattern> \\\n" + " <log-group-name> \\\n" + " <lambda-function-arn>\n"; if (args.length != 4) { System.out.println(USAGE); System.exit(1); } String filter = args[0]; String pattern = args[1]; String log_group = args[2]; String function_arn = args[3]; final AWSLogs cwl = AWSLogsClientBuilder.defaultClient(); PutSubscriptionFilterRequest request = new PutSubscriptionFilterRequest() .withFilterName(filter) .withFilterPattern(pattern) .withLogGroupName(log_group) .withDestinationArn(function_arn); PutSubscriptionFilterResult response = cwl.putSubscriptionFilter(request); System.out.printf( "Successfully created CloudWatch logs subscription filter %s", filter); }
public static void main(String[] args) { final String USAGE = "To run this example, supply a filter name and log group name\n" + "Ex: DeleteSubscriptionFilter <filter-name> <log-group-name>\n"; if (args.length != 2) { System.out.println(USAGE); System.exit(1); } String filter = args[0]; String log_group = args[1]; final AWSLogs logs = AWSLogsClientBuilder.defaultClient(); DeleteSubscriptionFilterRequest request = new DeleteSubscriptionFilterRequest() .withFilterName(filter) .withLogGroupName(log_group); DeleteSubscriptionFilterResult response = logs.deleteSubscriptionFilter(request); System.out.printf( "Successfully deleted CloudWatch logs subscription filter %s", filter); }
public static AWSLogs createMockClient() { staticFactoryMock = new MockCloudWatchClient(); return staticFactoryMock.createClient(); }
/** * Creates the client proxy. This is used internally, and also by the test * for calling a static factory method. */ public AWSLogs createClient() { return (AWSLogs)Proxy.newProxyInstance( getClass().getClassLoader(), new Class<?>[] { AWSLogs.class }, MockCloudWatchClient.this); }
/** * This function is used as a client factory by the smoketest. */ public static AWSLogs createClient() { return AWSLogsClientBuilder.defaultClient(); }
public AWSLogs getAwsLogs() { return awsLogs; }
public void setAwsLogs(final AWSLogs awsLogs) { this.awsLogs = awsLogs; }
@Test(timeout = 15000) public void concurrencyAndThroughput() throws InterruptedException { final AWSLogs awsLogs = mock(AWSLogs.class); when(awsLogs.putLogEvents(any(PutLogEventsRequest.class))).thenReturn(new PutLogEventsResult()); final CloudwatchLogsLogbackAppender appender = new CloudwatchLogsLogbackAppender() { @Override CloudwatchLogsLogEventPutter createCloudwatchLogsLogEventPutter() { return new CloudwatchLogsLogEventPutter(config, eventQueue, awsLogs, true); } }; appender.setConfig(new CloudwatchLogsConfig()); appender.start(); ExecutorService executorService = Executors.newFixedThreadPool(NUM_THREADS); final LoggerContext loggerContext = new LoggerContext(); for (int i = 0; i < NUM_THREADS; i++) { executorService.execute(new Runnable() { @Override public void run() { String fqcn = "class-" + UUID.randomUUID(); Logger logger = loggerContext.getLogger(fqcn); for (int j = 0; j < EVENTS_PER_THREAD; j++) { appender.append(new LoggingEvent(fqcn, logger, Level.DEBUG, "msg-" + j, null, null)); } } }); } executorService.shutdown(); executorService.awaitTermination(1, TimeUnit.MINUTES); while (NUM_THREADS * EVENTS_PER_THREAD > appender.putter.getProcessedCount()) { Thread.sleep(100); } appender.stop(); assertEquals(0, appender.getDiscardedCount()); assertEquals(NUM_THREADS * EVENTS_PER_THREAD, appender.getProcessedCount()); assertEquals(NUM_THREADS * EVENTS_PER_THREAD, appender.putter.getProcessedCount()); }
/** * Returns a CloudwatchLogs service client. * * <p>This method is provided so unit tests can mock out CloudwatchLogs. */ public AWSLogs getAWSLogsClient(String region) { // Use a getter here so unit tests can substitute a mock client AWSLogs client = AWSLogsClientBuilder.standard().withRegion(region).build(); return client; }
public static void main(String[] args) { final String USAGE = "To run this example, supply a log group name\n" + "Ex: DescribeSubscriptionFilters <log-group-name>\n"; if (args.length != 1) { System.out.println(USAGE); System.exit(1); } String log_group = args[0]; final AWSLogs logs = AWSLogsClientBuilder.defaultClient(); boolean done = false; DescribeSubscriptionFiltersRequest request = new DescribeSubscriptionFiltersRequest() .withLogGroupName(log_group) .withLimit(1); while(!done) { DescribeSubscriptionFiltersResult response = logs.describeSubscriptionFilters(request); for(SubscriptionFilter filter : response.getSubscriptionFilters()) { System.out.printf( "Retrieved filter with name %s, " + "pattern %s " + "and destination arn %s", filter.getFilterName(), filter.getFilterPattern(), filter.getDestinationArn()); } request.setNextToken(response.getNextToken()); if(response.getNextToken() == null) { done = true; } } }
@Override protected void createAWSClient() { client = tryClientFactory(clientFactoryMethod, AWSLogs.class, true); if ((client == null) && (clientEndpoint == null)) { client = tryClientFactory("com.amazonaws.services.logs.AWSLogsClientBuilder.defaultClient", AWSLogs.class, false); } if (client == null) { LogLog.debug(getClass().getSimpleName() + ": creating service client via constructor"); client = tryConfigureEndpointOrRegion(new AWSLogsClient(), clientEndpoint); } }
/** * Creates a new EventPutter for the current AWS region. * * @param config The config to use. * @param eventQueue The event queue to consume from. * @return The new EventPutter. */ public static CloudwatchLogsLogEventPutter create(CloudwatchLogsConfig config, BlockingQueue<CloudwatchLogsLogEvent> eventQueue) { boolean enabled = config.getRegion() != null || config.getEndpoint() != null; AWSLogs logsClient = enabled ? createLogsClient(config) : null; return new CloudwatchLogsLogEventPutter(config, eventQueue, logsClient, enabled); }