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()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集