Search code examples
aws-xraybuffalo

xray setup with buffalo


I'm trying to setup buffalo to send data to AWS X-Ray. I am new to buffalo/go and I'm completely lost in the docs...

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

When I curl I get the correct response from the HomeHandler, and the logs inside the middleware prints (h is not null). The init is also called correctly. On the deamon side I see nothing at all :(

Official docs have this sample code

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

and I suppose my port is incorrect..

Any suggestion on how to proceed ?

thanks


Solution

  • h := xray.Handler(xray.NewFixedSegmentNamer("WordAPI"), buffalo.WrapBuffaloHandler(next))
    

    The line creates an xray http handler but never used it. You can use Buffalo WrapHandler() to turn it back to a buffalo handler and listen to incoming request. So this will work:

    app.GET("/", 
        buffalo.WrapHandler(xray.Handler(xray.NewFixedSegmentNamer("WordAPI"), 
            buffalo.WrapBuffaloHandler(HomeHandler))))