简体   繁体   English

在 Buffalo 中集成 Sentry 和 Elastic APM

[英]Integrating Sentry and Elastic APM in Buffalo

I am trying to integrate Elastic APM and Sentry into my website using Buffalo.我正在尝试使用 Buffalo 将 Elastic APM 和 Sentry 集成到我的网站中。 The interesting files are as follows:有趣的文件如下:

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
}

The problem I'm running into is if I have the Sentry/APM handlers at the top, then I get errors like application.html: line 24: "showPagePath": unknown identifier .我遇到的问题是,如果我在顶部有 Sentry/APM 处理程序,那么我会收到类似application.html: line 24: "showPagePath": unknown identifier However, if I move it to just before I set up the routes, then I get a no transaction found error.但是,如果我在设置路由之前将其移动到,则会收到未找到事务的错误。 So, I'm guessing that the handler wrappers are dropping the buffalo.Context information.所以,我猜测处理程序包装器正在丢弃buffalo.Context信息。 So, what would I need to do to be able to integrate Sentry and Elastic in Buffalo asides from trying to reimplement their wrappers?那么,除了尝试重新实现它们的包装器之外,我还需要做什么才能在 Buffalo 中集成 Sentry 和 Elastic?

So, I'm guessing that the handler wrappers are dropping the buffalo.Context information.所以,我猜测处理程序包装器正在丢弃 buffalo.Context 信息。

That's correct.没错。 The problem is that buffalo.WrapHandler ( Source ) throws away all of the context other than the underlying http.Request / http.Response :问题是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
    }
}

So, what would I need to do to be able to integrate Sentry and Elastic in Buffalo asides from trying to reimplement their wrappers?那么,除了尝试重新实现它们的包装器之外,我还需要做什么才能在 Buffalo 中集成 Sentry 和 Elastic?

I can see two options:我可以看到两个选项:

  • Reimplement buffalo.WrapHandler / buffalo.WrapBuffaloHandler to stop throwing away the buffalo.Context.重新实现buffalo.WrapHandler / buffalo.WrapBuffaloHandler以停止丢弃 buffalo.Context。 This would involve storing the buffalo.Context in the underlying http.Request 's context, and then pulling it out again on the other side instead of creating a whole new context.这将涉及将buffalo.Context存储在底层http.Request的上下文中,然后在另一端再次将其拉出,而不是创建一个全新的上下文。
  • Implement Buffalo-specific middleware for Sentry and Elastic APM without using the Wrap* functions.在不使用Wrap*函数的情况下为 Sentry 和 Elastic APM 实现 Buffalo 特定的中间件。

There's an open issue in the Elastic APM agent for the latter option: elastic/apm#39 .对于后一个选项,Elastic APM 代理中存在一个未解决的问题: elastic/apm#39

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM