duanbozhong9689 2019-03-16 12:49
浏览 111
已采纳

尝试使用Golang在CloudWatch上放置PutLogEvents时获取SerializationException

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

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)
}

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.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • doubomudichen0832 2019-03-18 01:07
    关注

    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.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误