我正在尝试整合弹性APM和前哨到我的网站使用布法罗。有趣的文件如下:
handlers/sentryHandler.go
package handlers
import (
sentryhttp "github.com/getsentry/sentry-go/http"
"github.com/gobuffalo/buffalo"
)
func SentryHandler(next buffalo.Handler) buffalo.Handler {
handler := buffalo.WrapBuffaloHandler(next)
sentryHandler := sentryhttp.New(sentryhttp.Options{})
return buffalo.WrapHandler(sentryHandler.Handle(handler))
}handlers/elasticAPMHandler.go
package handlers
import (
"fmt"
"github.com/gobuffalo/buffalo"
"go.elastic.co/apm/module/apmhttp"
)
func ElasticAPMHandler(next buffalo.Handler) buffalo.Handler {
fmt.Println("AAA")
handler := apmhttp.Wrap(buffalo.WrapBuffaloHandler(next))
return buffalo.WrapHandler(handler)
}actions/app.go
package actions
import (
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/envy"
forcessl "github.com/gobuffalo/mw-forcessl"
paramlogger "github.com/gobuffalo/mw-paramlogger"
"github.com/unrolled/secure"
"my_website/handlers"
"my_website/models"
"github.com/gobuffalo/buffalo-pop/pop/popmw"
csrf "github.com/gobuffalo/mw-csrf"
i18n "github.com/gobuffalo/mw-i18n"
"github.com/gobuffalo/packr/v2"
)
func App() *buffalo.App {
if app == nil {
app = buffalo.New(buffalo.Options{
Env: ENV,
SessionName: "_my_website_session",
})
// Automatically redirect to SSL
app.Use(forceSSL())
// Catch errors and send them to Sentry.
app.Use(handlers.SentryHandler)
// Get tracing information and send it to Elastic.
app.Use(handlers.ElasticAPMHandler)
// Other Buffalo middleware stuff goes here...
// Routing stuff goes here...
}
return app
}我遇到的问题是,如果我在顶部有Sentry/APM处理程序,那么我会得到像application.html: line 24: "showPagePath": unknown identifier这样的错误。但是,如果我在设置路由之前将其移动到,那么我会得到一个没有找到事务的错误。因此,我猜测处理程序包装器正在删除buffalo.Context信息。那么,除了尝试重新实现它们的包装器之外,我还需要做什么才能在Buffalo中集成Sentry和Elastic呢?
发布于 2020-04-02 11:15:04
所以,我猜处理程序的包装器正在丢弃buffalo.Context信息。
是这样的。问题是buffalo.WrapHandler (Source)丢弃了除底层http.Request/http.Response之外的所有上下文
// WrapHandler wraps a standard http.Handler and transforms it
// into a buffalo.Handler.
func WrapHandler(h http.Handler) Handler {
return func(c Context) error {
h.ServeHTTP(c.Response(), c.Request())
return nil
}
}那么,除了尝试重新实现它们的包装器之外,我还需要做些什么才能在布法罗集成
和Elastic?
我可以看到两个选项:
buffalo.WrapHandler/buffalo.WrapBuffaloHandler以停止丢弃buffalo.Context。这将涉及到将布法罗存储在底层http.Request的上下文中,然后在另一端再次取出它,而不是在不使用Wrap*函数的情况下为Sentry和Elastic APM创建一个全新的特定于context.buffalo.Context的中间件。对于后一种选择,Elastic APM代理中有一个开放的问题:elastic/apm#39。
https://stackoverflow.com/questions/60920952
复制相似问题