GithubHelp home page GithubHelp logo

Comments (3)

ianomad avatar ianomad commented on July 19, 2024

@antonholmquist
Roughly it would look something like this:
https://gist.github.com/ianomad/4bb66617316836131e2f62e3d0abfdbd

I didn't try to run it locally, just in case.

from go-agent.

antonholmquist avatar antonholmquist commented on July 19, 2024

Great! Seems to work :) One thing, it should be fmt.Sprintf instead of fmt.Sprint on line 21. And by the way, the path doesn't include the query params, wouldn't you recommend adding this to the transaction name?

Also, maybe this this be added to the docs somewhere?

from go-agent.

willnewrelic avatar willnewrelic commented on July 19, 2024

Hi antonholmquist

Supposing you are using negroni just like the getting started example here:
(https://github.com/urfave/negroni#getting-started) you have a couple options:

  • You can add instrumentation to select routes when registering them with your
    mux.
func main() {
    config := newrelic.NewConfig("negroni example", "__LICENSE_KEY__")
    config.Logger = newrelic.NewDebugLogger(os.Stdout)
    app, err := newrelic.NewApplication(config)
    if nil != err {
        fmt.Println(err)
        os.Exit(1)
    }

    mux := http.NewServeMux()
    mux.HandleFunc(newrelic.WrapHandleFunc(app, "/", func(w http.ResponseWriter, req *http.Request) {
        fmt.Fprintf(w, "Welcome to the home page!")
    }))

    n := negroni.Classic() // Includes some default middlewares
    n.UseHandler(mux)

    http.ListenAndServe(":3000", n)
}
  • You can replace the mux with another mux that adds the instrumentation:
type wrapMux struct {
    app      newrelic.Application
    original *http.ServeMux
}

func (mux wrapMux) Handle(p string, h http.Handler) {
    mux.original.Handle(newrelic.WrapHandle(mux.app, p, h))
}

func (mux wrapMux) HandleFunc(p string, h func(http.ResponseWriter, *http.Request)) {
    mux.original.HandleFunc(newrelic.WrapHandleFunc(mux.app, p, h))
}

func (mux wrapMux) Handler(r *http.Request) (h http.Handler, p string) {
    return mux.original.Handler(r)
}

func (mux wrapMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    mux.original.ServeHTTP(w, r)
}

func main() {
    config := newrelic.NewConfig("negroni example", "__LICENSE_KEY__")
    config.Logger = newrelic.NewDebugLogger(os.Stdout)
    app, err := newrelic.NewApplication(config)
    if nil != err {
        fmt.Println(err)
        os.Exit(1)
    }

    mux := wrapMux{
        original: http.NewServeMux(),
        app:      app,
    }
    mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
        fmt.Fprintf(w, "Welcome to the home page!")
    })

    n := negroni.Classic() // Includes some default middlewares
    n.UseHandler(mux)

    http.ListenAndServe(":3000", n)
}
  • You can introduce a negroni middleware to add the instrumentation, as
    demonstrated by the example from @ianomad. Unfortunately, this approach
    necessitates naming the transaction using properties of the request (or
    having a single transaction name). As @mikegleasonjr said, using the path as
    the transaction name has the downside that it could lead to an explosion of
    transaction names. Though is not a concern if the app is a backend service
    (and the consumers will definitely not make requests with spurious paths), we
    generally don't recommend it. For the same reason, using the query params
    in the transaction name is not recommended.

I hope this helps.

from go-agent.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.