cloudwatch-logback-appender icon indicating copy to clipboard operation
cloudwatch-logback-appender copied to clipboard

InvalidSequenceTokenException

Open plutext opened this issue 5 years ago • 1 comments

From AWS Lamba, you might get:

com.amazonaws.AmazonServiceException: Unable to unmarshall exception response with the unmarshallers provided (Service: AWSLogs; Status Code: 400; Error Code: InvalidSequenceTokenException; Request ID: 5645e9c0-5552-11e9-b9f8-71f29888b2c7)

    at com.amazonaws.services.logs.AWSLogsClient.doInvoke(AWSLogsClient.java:2447)
    at com.amazonaws.services.logs.AWSLogsClient.invoke(AWSLogsClient.java:2423)
    at com.amazonaws.services.logs.AWSLogsClient.executePutLogEvents(AWSLogsClient.java:1951)
    at com.amazonaws.services.logs.AWSLogsClient.putLogEvents(AWSLogsClient.java:1926)

This seems to fix it:

        try {
        	nextToken = awsLogs.putLogEvents(
                    logEventReq
                            .withSequenceToken(nextToken)
                            .withLogEvents(events)
            ).getNextSequenceToken();
        	
		} catch (final InvalidSequenceTokenException iste) {
			
			// Copy https://github.com/graingert/cloudwatch-logback-appender/blob/master/src/main/java/com/brickcommerce/aws/logging/CloudWatchWriter.java#L67
			System.err.println("InvalidSequenceTokenException, will reset the token to the expected one");
			nextToken = iste.getExpectedSequenceToken();

		} catch (com.amazonaws.AmazonServiceException ase) {
			
			// But as per stack trace copied above this is what is actually caught on Lambda
			
			// Unable to unmarshall exception response with the unmarshallers provided 
			// (Service: AWSLogs; Status Code: 400; Error Code: InvalidSequenceTokenException; Request ID: 5ad1e6cc-5fdc-11e9-bdef-8759912c2f97
			System.err.println("Response wrapped InvalidSequenceTokenException, will reset the token to the expected one");
			
		    LogStream stream = findLogStream(groupName, currentStreamName);
            nextToken = stream.getUploadSequenceToken();
			
		} catch (Exception e) {
			logContext.addError("Exception while adding log events.", e);
		}

See also https://stackoverflow.com/questions/36876563/amazon-cloud-watch-log-putlogeventsrequest-the-given-sequencetoken-is-invali for reports of this error using other logging libs

plutext avatar Apr 16 '19 00:04 plutext

Hi @plutext,

thanks for your tip. Can it be that the problem occurs as you are writing into the same log stream from different processes? In this case it might be a good idea to make the log stream name unique. Please have a look here: https://github.com/dibog/cloudwatch-logback-appender/blob/master/README.md#-unique-log-stream-names

Cheers, Dieter

dibog avatar Apr 16 '19 04:04 dibog