gpt4 book ai didi

mysql - Golang aws xray.sql错误

转载 作者:数据小太阳 更新时间:2023-10-29 03:11:38 27 4
gpt4 key购买 nike

我在使用 aws xras.sql 进行 mysql 查询时遇到错误。我从下面的链接中获取实现文档 https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-go-sqlclients.html

这是我的路由和数据库查询代码

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

这是错误日志

    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

最佳答案

问题是您使用的是 nil 上下文。这可以从堆栈跟踪中推断出来,因为 panic 发生在一个名为 getTraceHeaderFromContext 的函数中,并通过查看 panic 在 aws library line 35 中的位置来确认。 panic 发生的地方:

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

永远不要这样做。始终正确实例化您的上下文:

ctx := context.Background()

尽管在您的情况下,最好的选择是使用 HTTP 请求中已存在的上下文:

ctx := r.Context()

关于mysql - Golang aws xray.sql错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50219395/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com