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 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器