gpt4 book ai didi

go - 尽管 GO AWS SDK 没有错误,但无法使 CloudWatchLogs 正常工作

转载 作者:数据小太阳 更新时间:2023-10-29 03:28:01 26 4
gpt4 key购买 nike

我根据 AWS GO SDK 整理了一些代码 example对于 InvalidSequenceTokenException 方法。 SDK 似乎有一些我已经解决的错误,我得到了成功调用 API 的预期响应,该 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 返回任何错误,那么我还应该在哪里寻找问题?

最佳答案

您的 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
}

关于go - 尽管 GO AWS SDK 没有错误,但无法使 CloudWatchLogs 正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33837062/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com