我正在尝试设置 buffalo 将数据发送到 AWS X-Ray。我是水牛/去的新手,我完全迷失在文档中......
我的操作.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",
})
}
当我卷曲时,我从 HomeHandler 得到正确的响应,并且中间件内的日志打印(h 不为空(。初始化也被正确调用。在恶魔方面,我什么也没看到:(
官方文档有此示例代码
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)
}
我想我的端口不正确。.
关于如何进行的任何建议?
谢谢
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))))