duanpin9531 2018-05-07 17:17
浏览 93
已采纳

Golang AWS Xray.sql错误

I am getting error on mysql query using aws xras.sql. I get doc fro implementation from link below https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-go-sqlclients.html

Here is my code for route and database query

http.Handle("/", xray.Handler(xray.NewDynamicSegmentNamer("MyApp", "*.example.com"), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    db , _ := xray.SQL("mysql", "golang:root@localhost:3306/bonusapi")
    defer db.Close()

    var ctx context.Context
    db.QueryRow(ctx, "SELECT * FROM user")
    w.Write([]byte("Allah Hoo"))
})))

Here is error log

    2018/05/07 21:30:22 http: panic serving 127.0.0.1:46921: runtime error: invalid memory address or nil pointer dereference
goroutine 33 [running]:
net/http.(*conn).serve.func1(0xc0421c4000)
    C:/Go/src/net/http/server.go:1726 +0xd7
panic(0x7896a0, 0xa0e460)
    C:/Go/src/runtime/panic.go:502 +0x237
github.com/aws/aws-xray-sdk-go/xray.getTraceHeaderFromContext(0x0, 0x0, 0x78fd60)
    C:/Go/src/github.com/aws/aws-xray-sdk-go/xray/lambda.go:35 +0x2d
github.com/aws/aws-xray-sdk-go/xray.BeginSubsegment(0x0, 0x0, 0x7f8f47, 0x7, 0x0, 0x0, 0x0)
    C:/Go/src/github.com/aws/aws-xray-sdk-go/xray/segment.go:160 +0x98
github.com/aws/aws-xray-sdk-go/xray.Capture(0x0, 0x0, 0x7f8f47, 0x7, 0xc042035a88, 0x0, 0x0)
    C:/Go/src/github.com/aws/aws-xray-sdk-go/xray/capture.go:19 +0x62
github.com/aws/aws-xray-sdk-go/xray.(*DB).QueryRow(0xc0421d6200, 0x0, 0x0, 0x7fd9e7, 0x12, 0x0, 0x0, 0x0, 0xc042199b50)
    C:/Go/src/github.com/aws/aws-xray-sdk-go/xray/sql_go18.go:75 +0xf4
main.main.func1(0x87d400, 0xc042002140, 0xc0421d4100)
    D:/PROJECTS/cProject/tom-berger/eon-bone-api/src/main/application.go:45 +0xbe
net/http.HandlerFunc.ServeHTTP(0x8135b8, 0x87d400, 0xc042002140, 0xc0421d4100)
    C:/Go/src/net/http/server.go:1947 +0x4b
github.com/aws/aws-xray-sdk-go/xray.httpTrace(0xc042026a80, 0x87aa80, 0x8135b8, 0x87d7c0, 0xc0421de000, 0xc0421d4100, 0xc04203c280)
    C:/Go/src/github.com/aws/aws-xray-sdk-go/xray/handler.go:150 +0x5a4
github.com/aws/aws-xray-sdk-go/xray.Handler.func1(0x87d7c0, 0xc0421de000, 0xc0421d4000)
    C:/Go/src/github.com/aws/aws-xray-sdk-go/xray/handler.go:115 +0x191
net/http.HandlerFunc.ServeHTTP(0xc0421806f0, 0x87d7c0, 0xc0421de000, 0xc0421d4000)
    C:/Go/src/net/http/server.go:1947 +0x4b
net/http.(*ServeMux).ServeHTTP(0xa1ad40, 0x87d7c0, 0xc0421de000, 0xc0421d4000)
    C:/Go/src/net/http/server.go:2337 +0x137
net/http.serverHandler.ServeHTTP(0xc04217ea90, 0x87d7c0, 0xc0421de000, 0xc0421d4000)
    C:/Go/src/net/http/server.go:2694 +0xc3
net/http.(*conn).serve(0xc0421c4000, 0x87db40, 0xc04203c080)
    C:/Go/src/net/http/server.go:1830 +0x658
created by net/http.(*Server).Serve
    C:/Go/src/net/http/server.go:2795 +0x282
  • 写回答

1条回答 默认 最新

  • dongshuohuan5291 2018-05-07 17:42
    关注

    The problem is that you're using a nil context. This can be inferred from the stack trace, since the panic happens in a function called getTraceHeaderFromContext, and confirmed by looking at the location of the panic in the aws library line 35 where the panic occurred:

        if traceHeaderValue := ctx.Value(LambdaTraceHeaderKey); traceHeaderValue != nil {
    

    Never do this. Always instantiate your context properly:

    ctx := context.Background()
    

    Although in your case, the best option is to use the context that already exists in the HTTP request:

    ctx := r.Context()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?