我正在尝试将buffalo设置为将数据发送到AWS X-Ray。我刚接触buffalo/go,我完全迷失在文档中...
我的actions.go
package actions
import (
"fmt"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/envy"
"github.com/aws/aws-xray-sdk-go/xray"
contenttype "github.com/gobuffalo/mw-contenttype"
"github.com/gobuffalo/x/sessions"
"github.com/rs/cors"
)
var ENV = envy.Get("GO_ENV", "development")
var app *buffalo.App
func App() *buffalo.App {
if app == nil {
app = buffalo.New(buffalo.Options{
Env: ENV,
SessionStore: sessions.Null{},
PreWares: []buffalo.PreWare{
cors.Default().Handler,
},
SessionName: "__session",
})
app.Use(contenttype.Set("application/json"))
app.Use(XRayStart)
app.GET("/", HomeHandler)
}
return app
}
// XRayStart starts xray
func XRayStart(next buffalo.Handler) buffalo.Handler {
return func(c buffalo.Context) error {
fmt.Println("1")
h := xray.Handler(xray.NewFixedSegmentNamer("WordAPI"), buffalo.WrapBuffaloHandler(next))
fmt.Println(h)
err := next(c)
return err
}
}
func init() {
fmt.Println("init")
xray.Configure(xray.Config{
DaemonAddr: "127.0.0.1:2000", // default
ServiceVersion: "1.2.3",
})
}当我执行curl时,我从HomeHandler得到了正确的响应,并且中间件中的日志被打印出来(h不是空的)。init的调用也是正确的。在守护进程方面,我什么也看不到:
官方文档中有以下示例代码
func main() {
http.Handle("/", xray.Handler(xray.NewFixedSegmentNamer("myApp"), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello!"))
})))
http.ListenAndServe(":8000", nil)
}我想我的端口是不正确的..
对如何进行有什么建议吗?
谢谢
发布于 2019-10-10 08:02:02
h := xray.Handler(xray.NewFixedSegmentNamer("WordAPI"), buffalo.WrapBuffaloHandler(next))这行代码创建了一个xray http处理程序,但从未使用过它。您可以使用Buffalo WrapHandler()将其转换回buffalo处理程序并侦听传入的请求。所以这将会起作用:
app.GET("/",
buffalo.WrapHandler(xray.Handler(xray.NewFixedSegmentNamer("WordAPI"),
buffalo.WrapBuffaloHandler(HomeHandler))))https://stackoverflow.com/questions/58003968
复制相似问题