我已根据AWS GO SDK example为InvalidSequenceTokenException
方法密切编制了一些代码。 SDK似乎有一些我已经遇到的错误,我得到了成功调用API的预期响应,它返回下一个序列令牌以用于后续调用。这是我的代码
import (
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go/aws"
"github.com/opensoft/dvs-api/config"
"strings"
)
func nextSequenceToken() (string, error) {
params := &cloudwatchlogs.DescribeLogStreamsInput{
LogGroupName: aws.String(config.AwsLogGroup), // Required
LogStreamNamePrefix: aws.String(config.AwsLogStream),
Descending: aws.Bool(true),
Limit: aws.Int64(1),
}
resp, err := config.AwsCloudWatchLogsSvc.DescribeLogStreams(params)
if err != nil {
return "", err
}
// Parsing from string because resp.NextToken is *always* empty ... bug?
parsed := ""
parts := strings.Split(resp.GoString(), "\n")
for _, part := range parts {
if strings.Contains(part, "UploadSequenceToken") {
parsed = strings.TrimLeft(part, "UploadSequenceToken: \"")
parsed = strings.TrimRight(parsed, "\"")
}
}
return parsed, nil
}
func LogEvent(message string, unixTime int64) error {
nextToken, te := nextSequenceToken()
if te != nil {
return te
}
params := &cloudwatchlogs.PutLogEventsInput{
LogEvents: []*cloudwatchlogs.InputLogEvent{// Required
{// Required
Message: aws.String(message), // Required
Timestamp: aws.Int64(unixTime), // Required
},
// More values...
},
LogGroupName: aws.String(config.AwsLogGroup), // Required
LogStreamName: aws.String(config.AwsLogStream), // Required
}
if nextToken != "" {
params.SequenceToken = &nextToken
}
resp, err := config.AwsCloudWatchLogsSvc.PutLogEvents(params)
if err != nil {
return err
}
// Parsing from string because resp.NextSequenceToken is *always* empty ... bug?
parsed := ""
parts := strings.Split(resp.GoString(), "\n")
for _, part := range parts {
if strings.Contains(part, "NextSequenceToken") {
parsed = strings.TrimLeft(part, "NextSequenceToken: \"")
parsed = strings.TrimRight(parsed, "\",")
}
}
return nil
}
我还继续并从组中删除了日志流,只是为了确保有连接,我确实从AWS收到错误,告诉我日志流无效,同样,重新创建日志流修复了所有内容。
所以我的代码似乎是正确的,但是我登录的任何内容都不会显示在CloudWatch Logs上,而我的日志仍然是空的。如果我的代码是正确的并且没有从AWS返回任何错误那么我还应该在哪里寻找问题?
答案 0 :(得分:0)
您的nextSequenceToken()返回一个尾随字符。这是更正后的功能。
func nextSequenceToken() (string, error) {
params := &cloudwatchlogs.DescribeLogStreamsInput{
LogGroupName: aws.String(config.AwsLogGroup), // Required
LogStreamNamePrefix: aws.String(config.AwsLogStream),
Descending: aws.Bool(true),
Limit: aws.Int64(1),
}
resp, err := config.AwsCloudWatchLogsSvc.DescribeLogStreams(params)
if err != nil {
return "", err
}
return *(resp.LogStreams[0].UploadSequenceToken), nil
}