Skip to content

Latest commit

History

19 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

New Relixir

Instrument your Phoenix applications with New Relic.

New Relixir currently supports instrumenting Phoenix controllers and Ecto repositories to record response times for web transactions and database queries.

Usage

The following instructions show how to add instrumentation with New Relixir to a hypothetical Phoenix application named MyApp.

  1. Add new_relixir to your list of dependencies and start-up applications in mix.exs:

    # mix.exsdefmoduleMyApp.MixfiledouseMix.Project# ...defapplicationdo[mod: {MyApp,[]},applications: [:new_relixir]]enddefpdepsdo[{:new_relixir,"~> 0.1.0"}]endend
  2. Add your New Relic application name and license key to config/config.exs. You may wish to use environment variables to keep production, staging, and development environments separate:

    # config/config.exsconfig:new_relixir,application_name: System.get_env("NEWRELIC_APP_NAME"),license_key: System.get_env("NEWRELIC_LICENSE_KEY")
  3. Define a module to wrap your repository's methods with New Relic instrumentation:

    # lib/my_app/repo.exdefmoduleMyApp.RepodouseEcto.Repo,otp_app: :my_appdefmoduleNewRelicdouseNewRelixir.Plug.Repo,repo: MyApp.Repoendend

    Now MyApp.Repo.NewRelic can be used as a substitute for MyApp.Repo. If a Plug.Conn is provided as the :conn option to any of the wrapper's methods, it will instrument the response time for that call. Otherwise, the repository will behave the same as the repository that it wraps.

  4. For any Phoenix controller that you want to instrument, add NewRelixir.Plug.Phoenix and replace existing aliases to your application's repository with an alias to your New Relic repository wrapper. If instrumenting all controllers, update web/web.ex:

    # web/web.exdefmoduleMyApp.Webdodefcontrollerdoquotedo# ...plugNewRelixir.Plug.PhoenixaliasMyApp.Repo.NewRelic,as: Repo# Replaces `alias MyApp.Repo`endendend
  5. Update your controllers to pass conn as an option to your New Relic repo wrapper:

    # web/controllers/users.exdefmoduleMyApp.UserControllerdouseMyApp.Web,:controllerdefindex(conn,_params)dousers=Repo.all(User,conn: conn)# Replaces `Repo.all(User)`# ...endend

Instrumenting Custom Repo Methods

If you've defined custom methods on your repository, you will need to define them on your wrapper module as well. In the wrapper module, simply call your repository's original method inside a closure that you pass to instrument_db:

# lib/my_app/repo.exdefmoduleMyApp.RepodouseEcto.Repo,otp_app: :my_appdefcustom_method(queryable,opts\\[])do# ...enddefmoduleNewRelicdouseNewRelixir.Plug.Repo,repo: MyApp.Repodefcustom_method(queryable,opts\\[])doinstrument_db(:custom_method,queryable,opts,fn()->MyApp.Repo.custom_method(queryable,opts)end)endendend

When using the wrapper module's custom_method, the time it takes to call MyApp.Repo.custom_method/2 will be recorded to New Relic.

Copyright

Copyright © 2016 The RealReal, Inc.

Distributed under the MIT License.

About

New Relic tracking for Phoenix applications.

Resources

Stars

0 stars

Watchers

8 watching

Forks

Releases

Packages

Contributors

Languages