尝试使用Golang在CloudWatch上放置PutLogEvents时获取SerializationException [英] Getting SerializationException while trying to PutLogEvents on cloudwatch using golang

查看:124
本文介绍了尝试使用Golang在CloudWatch上放置PutLogEvents时获取SerializationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我的程序实现以下目标:
在aws cloudwatch上创建日志组
在上述日志组下创建日志流
在上述日志流下放置日志事件

I am trying to achieve following using my program: Create log-group on aws cloudwatch Create log-stream under above log-group Put log-events under above log-stream

所有这些都用go lang

All this using go lang

package main

import (
    "time"
    "fmt"
    "github.com/jcxplorer/cwlogger"
    "github.com/aws/aws-sdk-go/service/cloudwatchlogs"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/aws"
)

func main() {
    sess := session.Must(session.NewSessionWithOptions(session.Options{
        SharedConfigState: session.SharedConfigEnable,
    }))

    svc := cloudwatchlogs.New(sess)
    logGroupName := "my-log-group";
    logStreamName := "my-log-stream";
    logGroupInput := cloudwatchlogs.CreateLogGroupInput{LogGroupName: &logGroupName}
    svc.CreateLogGroup(&logGroupInput);
    logStreamInput := cloudwatchlogs.CreateLogStreamInput{LogGroupName: &logGroupName, LogStreamName: &logStreamName}
    svc.CreateLogStream(&logStreamInput)

    logevents := make([]*cloudwatchlogs.InputLogEvent, 1)

    logevents = append(logevents, &cloudwatchlogs.InputLogEvent{
        Message:   aws.String("Simple log message"),
        Timestamp: aws.Int64(111),
    })

    p := cloudwatchlogs.PutLogEventsInput{LogEvents: logevents, LogGroupName: &logGroupName, LogStreamName: &logStreamName}
    resp, err := svc.PutLogEvents(&p)
    if err != nil {
        panic(err)
    }
    fmt.Print("Next Token: {}", resp.NextSequenceToken)
}

现在,当我在上面的代码中运行时,它成功创建了日志组和日志流,我可以在AWS CloudWatch中进行验证。但是由于某些原因,PutLogEvents失败并出现以下错误:

Now when I run above code, it successfully creates log-group and log-stream and I can verify that in aws cloudwatch. But for some reason PutLogEvents fails with following error:

panic: SerializationException: 
    status code: 400, request id: 0685efcc-47e3-11e9-b528-81f33ec2f468

我不确定这里可能出什么问题了。任何建议或指示都将非常有帮助。

I am not sure what may be wrong here. Any suggestion or direction will be really helpful.

预先感谢。

推荐答案

SerializationException的原因是: logevents:= make([] * cloudwatchlogs.InputLogEvent,1)
,然后是append,此操作在slice中创建了第一个空条目。我用
logevents替换代码:= make([] * cloudwatchlogs.InputLogEvent,0)并解决了。

Reason for SerializationException was:logevents := make([]*cloudwatchlogs.InputLogEvent, 1) followed by append which created first empty entry in slice. I replaced code with logevents := make([]*cloudwatchlogs.InputLogEvent, 0) and it got resolved.

另外,在调试过程中查找未填充日志的原因时,我发现所使用的时间戳值无效。根据AWS文档,每个事件的时间戳不能超过14天,并且将来不能超过2小时。
这是链接: https://docs.aws。 amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html

Additionally while debugging to find why logs were not getting populated I figured out that timestamp value used is not valid one. According to aws documentation timestamp for each event can't be older than 14 days and can't be more than 2hr in future. Here is link: https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html

希望对将来遇到类似问题的人会有所帮助。

Hope it will be helpful to someone facing similar issue in future.

这篇关于尝试使用Golang在CloudWatch上放置PutLogEvents时获取SerializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆