Search in sources :

Example 11 with AWSLogsClient

use of com.amazonaws.services.logs.AWSLogsClient in project cvs-auto-svc by dvsa.

the class AwsUtil method checkLogsFor.

public static boolean checkLogsFor(String log, String keyValuePair) {
    Regions clientRegion = Regions.EU_WEST_1;
    AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard().withRegion(clientRegion).build();
    System.out.println(System.getProperty("AWS_ROLE"));
    AssumeRoleRequest assumeRequest = new AssumeRoleRequest().withRoleArn(System.getProperty("AWS_ROLE")).withDurationSeconds(3600).withRoleSessionName(UUID.randomUUID().toString());
    AssumeRoleResult assumeResult = stsClient.assumeRole(assumeRequest);
    BasicSessionCredentials temporaryCredentials = new BasicSessionCredentials(assumeResult.getCredentials().getAccessKeyId(), assumeResult.getCredentials().getSecretAccessKey(), assumeResult.getCredentials().getSessionToken());
    AWSLogs logsClient = new AWSLogsClient(temporaryCredentials).withRegion(clientRegion);
    String logGroup = log + "-" + loader.getBranchName();
    for (int times = 0; times < 15; times++) {
        System.out.println("... " + times + " ...");
        DescribeLogStreamsRequest describeLogStreamsRequest = new DescribeLogStreamsRequest().withLogGroupName(logGroup).withOrderBy("LastEventTime").withDescending(true).withLimit(1);
        DescribeLogStreamsResult describeLogStreamsResult = logsClient.describeLogStreams(describeLogStreamsRequest);
        LogStream logStream = describeLogStreamsResult.getLogStreams().get(0);
        GetLogEventsRequest getLogEventsRequest = new GetLogEventsRequest().withLogGroupName(logGroup).withLogStreamName(logStream.getLogStreamName());
        GetLogEventsResult result = logsClient.getLogEvents(getLogEventsRequest);
        for (OutputLogEvent event : result.getEvents()) {
            System.out.println("*****************************");
            System.out.println("# event: " + event.getMessage());
            System.out.println("Looking for: " + keyValuePair);
            if (event.getMessage().contains(keyValuePair)) {
                System.out.println("!!!!!!!!!!!!!!!###### FOUND !!! ######!!!!!!!!!!!!!!!");
                System.out.println("$$$$$$$$$$$   " + logStream.getLogStreamName() + "   $$$$$$$$$$$");
                return true;
            }
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    return false;
}
Also used : AssumeRoleRequest(com.amazonaws.services.securitytoken.model.AssumeRoleRequest) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) Regions(com.amazonaws.regions.Regions) AssumeRoleResult(com.amazonaws.services.securitytoken.model.AssumeRoleResult) AWSLogs(com.amazonaws.services.logs.AWSLogs) AWSLogsClient(com.amazonaws.services.logs.AWSLogsClient) AWSSecurityTokenService(com.amazonaws.services.securitytoken.AWSSecurityTokenService)

Example 12 with AWSLogsClient

use of com.amazonaws.services.logs.AWSLogsClient in project cvs-auto-svc by dvsa.

the class AwsUtil method checkDispatcherLogsForData.

public static boolean checkDispatcherLogsForData(String... keyValuePairs) {
    Regions clientRegion = Regions.EU_WEST_1;
    AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard().withRegion(clientRegion).build();
    System.out.println(System.getProperty("AWS_ROLE"));
    AssumeRoleRequest assumeRequest = new AssumeRoleRequest().withRoleArn(System.getProperty("AWS_ROLE")).withDurationSeconds(3600).withRoleSessionName(UUID.randomUUID().toString());
    AssumeRoleResult assumeResult = stsClient.assumeRole(assumeRequest);
    BasicSessionCredentials temporaryCredentials = new BasicSessionCredentials(assumeResult.getCredentials().getAccessKeyId(), assumeResult.getCredentials().getSecretAccessKey(), assumeResult.getCredentials().getSessionToken());
    AWSLogs logsClient = new AWSLogsClient(temporaryCredentials).withRegion(clientRegion);
    String logGroup = "/aws/lambda/edh-dispatcher-" + loader.getBranchName();
    logStreamLoop: for (int times = 0; times < 50; times++) {
        System.out.println("... " + times + " ...");
        DescribeLogStreamsRequest describeLogStreamsRequest = new DescribeLogStreamsRequest().withLogGroupName(logGroup).withOrderBy("LastEventTime").withDescending(true).withLimit(20);
        DescribeLogStreamsResult describeLogStreamsResult = logsClient.describeLogStreams(describeLogStreamsRequest);
        LogStream logStream = describeLogStreamsResult.getLogStreams().get(0);
        GetLogEventsRequest getLogEventsRequest = new GetLogEventsRequest().withLogGroupName(logGroup).withLogStreamName(logStream.getLogStreamName());
        GetLogEventsResult result = logsClient.getLogEvents(getLogEventsRequest);
        eventLoop: for (OutputLogEvent event : result.getEvents()) {
            System.out.println("\n----------------------------------------------------------------------");
            for (String keyValuePair : keyValuePairs) {
                System.out.println("searching inside event for: " + keyValuePair);
                if (!event.getMessage().contains(keyValuePair)) {
                    System.out.println("########################## " + keyValuePair + " NOT FOUND in event: \n" + event.getMessage() + "\n ##########################");
                    continue eventLoop;
                }
            }
            System.out.println("!!!!!!!!!!!!!!!###### FOUND !!! ######!!!!!!!!!!!!!!!");
            System.out.println("$$$$$$$$$$$   " + logStream.getLogStreamName() + "   $$$$$$$$$$$");
            return true;
        }
        try {
            System.out.println("########################## " + keyValuePairs + " NOT FOUND in log stream: \n" + logStream.getLogStreamName() + "\n ##########################");
            System.out.println("waiting 2 seconds");
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    return false;
}
Also used : AssumeRoleRequest(com.amazonaws.services.securitytoken.model.AssumeRoleRequest) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) Regions(com.amazonaws.regions.Regions) AssumeRoleResult(com.amazonaws.services.securitytoken.model.AssumeRoleResult) AWSLogs(com.amazonaws.services.logs.AWSLogs) AWSLogsClient(com.amazonaws.services.logs.AWSLogsClient) AWSSecurityTokenService(com.amazonaws.services.securitytoken.AWSSecurityTokenService)

Example 13 with AWSLogsClient

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

the class Ec2InstanceNameConverterTest method testInstanceName.

@Test(timeout = 10000)
public void testInstanceName() throws InterruptedException {
    String instanceName = "jefwjpefwjewfp";
    Ec2InstanceNameConverter.setInstanceName(instanceName);
    AWSLogsClient awsLogClient = createMock(AWSLogsClient.class);
    appender.setAwsLogsClient(awsLogClient);
    String prefix = "logstream-";
    appender.setLogStream(prefix + "%instanceName");
    final String expectedLogStream = prefix + instanceName;
    PatternLayout layout = new PatternLayout();
    layout.setPattern("%msg");
    layout.setContext(LOGGER_CONTEXT);
    layout.start();
    appender.setLayout(layout);
    LoggingEvent event = new LoggingEvent();
    event.setTimeStamp(System.currentTimeMillis());
    event.setLoggerName("name");
    event.setLevel(Level.DEBUG);
    event.setMessage("message");
    final PutLogEventsResult result = new PutLogEventsResult();
    result.setNextSequenceToken("ewopjfewfj");
    expect(awsLogClient.putLogEvents(isA(PutLogEventsRequest.class))).andAnswer(new IAnswer<PutLogEventsResult>() {

        @Override
        public PutLogEventsResult answer() {
            PutLogEventsRequest request = (PutLogEventsRequest) getCurrentArguments()[0];
            assertEquals(LOG_GROUP, request.getLogGroupName());
            assertEquals(expectedLogStream, request.getLogStreamName());
            return result;
        }
    });
    awsLogClient.shutdown();
    // =====================================
    replay(awsLogClient);
    appender.start();
    appender.append(event);
    while (appender.getEventsWrittenCount() < 1) {
        Thread.sleep(10);
    }
    appender.stop();
    verify(awsLogClient);
}
Also used : LoggingEvent(ch.qos.logback.classic.spi.LoggingEvent) AWSLogsClient(com.amazonaws.services.logs.AWSLogsClient) PatternLayout(ch.qos.logback.classic.PatternLayout) PutLogEventsResult(com.amazonaws.services.logs.model.PutLogEventsResult) PutLogEventsRequest(com.amazonaws.services.logs.model.PutLogEventsRequest) Test(org.junit.Test)

Example 14 with AWSLogsClient

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

the class Ec2InstanceIdConverterTest method testInstanceNameUnknown.

@Test(timeout = 10000)
public void testInstanceNameUnknown() throws InterruptedException {
    Ec2InstanceIdConverter.setInstanceId(null);
    AWSLogsClient awsLogClient = createMock(AWSLogsClient.class);
    appender.setAwsLogsClient(awsLogClient);
    String prefix = "logstream-";
    appender.setLogStream(prefix + "%instanceId");
    final String expectedLogStream = prefix + "unknown";
    PatternLayout layout = new PatternLayout();
    layout.setPattern("%msg");
    layout.setContext(LOGGER_CONTEXT);
    layout.start();
    appender.setLayout(layout);
    LoggingEvent event = new LoggingEvent();
    event.setTimeStamp(System.currentTimeMillis());
    event.setLoggerName("name");
    event.setLevel(Level.DEBUG);
    event.setMessage("message");
    final PutLogEventsResult result = new PutLogEventsResult();
    result.setNextSequenceToken("ewopjfewfj");
    expect(awsLogClient.putLogEvents(isA(PutLogEventsRequest.class))).andAnswer(new IAnswer<PutLogEventsResult>() {

        @Override
        public PutLogEventsResult answer() {
            PutLogEventsRequest request = (PutLogEventsRequest) getCurrentArguments()[0];
            assertEquals(LOG_GROUP, request.getLogGroupName());
            assertEquals(expectedLogStream, request.getLogStreamName());
            return result;
        }
    });
    awsLogClient.shutdown();
    // =====================================
    replay(awsLogClient);
    appender.start();
    appender.append(event);
    while (appender.getEventsWrittenCount() < 1) {
        Thread.sleep(10);
    }
    appender.stop();
    verify(awsLogClient);
}
Also used : LoggingEvent(ch.qos.logback.classic.spi.LoggingEvent) AWSLogsClient(com.amazonaws.services.logs.AWSLogsClient) PatternLayout(ch.qos.logback.classic.PatternLayout) PutLogEventsResult(com.amazonaws.services.logs.model.PutLogEventsResult) PutLogEventsRequest(com.amazonaws.services.logs.model.PutLogEventsRequest) Test(org.junit.Test)

Example 15 with AWSLogsClient

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

the class SystemPropertyConverterTest method testNoEnvNameSpecified.

@Test(timeout = 5000)
public void testNoEnvNameSpecified() throws InterruptedException {
    AWSLogsClient awsLogClient = createMock(AWSLogsClient.class);
    appender.setAwsLogsClient(awsLogClient);
    String prefix = "logstream-";
    appender.setLogStream(prefix + "%systemProperty");
    final String expectedLogStream = prefix + null;
    PatternLayout layout = new PatternLayout();
    layout.setPattern("%msg");
    layout.setContext(LOGGER_CONTEXT);
    layout.start();
    appender.setLayout(layout);
    LoggingEvent event = new LoggingEvent();
    event.setTimeStamp(System.currentTimeMillis());
    event.setLoggerName("name");
    event.setLevel(Level.DEBUG);
    event.setMessage("message");
    final PutLogEventsResult result = new PutLogEventsResult();
    result.setNextSequenceToken("ewopjfewfj");
    expect(awsLogClient.putLogEvents(isA(PutLogEventsRequest.class))).andAnswer(new IAnswer<PutLogEventsResult>() {

        @Override
        public PutLogEventsResult answer() {
            PutLogEventsRequest request = (PutLogEventsRequest) getCurrentArguments()[0];
            assertEquals(LOG_GROUP, request.getLogGroupName());
            assertEquals(expectedLogStream, request.getLogStreamName());
            return result;
        }
    });
    awsLogClient.shutdown();
    // =====================================
    replay(awsLogClient);
    appender.start();
    appender.append(event);
    while (appender.getEventsWrittenCount() < 1) {
        Thread.sleep(10);
    }
    appender.stop();
    verify(awsLogClient);
}
Also used : LoggingEvent(ch.qos.logback.classic.spi.LoggingEvent) AWSLogsClient(com.amazonaws.services.logs.AWSLogsClient) PatternLayout(ch.qos.logback.classic.PatternLayout) PutLogEventsResult(com.amazonaws.services.logs.model.PutLogEventsResult) PutLogEventsRequest(com.amazonaws.services.logs.model.PutLogEventsRequest) Test(org.junit.Test)

Aggregations

AWSLogsClient (com.amazonaws.services.logs.AWSLogsClient)16 PatternLayout (ch.qos.logback.classic.PatternLayout)13 LoggingEvent (ch.qos.logback.classic.spi.LoggingEvent)13 PutLogEventsRequest (com.amazonaws.services.logs.model.PutLogEventsRequest)13 PutLogEventsResult (com.amazonaws.services.logs.model.PutLogEventsResult)13 Test (org.junit.Test)13 BasicSessionCredentials (com.amazonaws.auth.BasicSessionCredentials)2 Regions (com.amazonaws.regions.Regions)2 AWSLogs (com.amazonaws.services.logs.AWSLogs)2 AWSSecurityTokenService (com.amazonaws.services.securitytoken.AWSSecurityTokenService)2 AssumeRoleRequest (com.amazonaws.services.securitytoken.model.AssumeRoleRequest)2 AssumeRoleResult (com.amazonaws.services.securitytoken.model.AssumeRoleResult)2