Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/romul/newrelic.ex
NewRelic agent for Elixir
https://github.com/romul/newrelic.ex
Last synced: about 2 months ago
JSON representation
NewRelic agent for Elixir
- Host: GitHub
- URL: https://github.com/romul/newrelic.ex
- Owner: romul
- License: mit
- Created: 2016-10-16T10:21:41.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-12-12T20:15:58.000Z (almost 5 years ago)
- Last Synced: 2024-07-06T04:24:50.150Z (2 months ago)
- Language: Elixir
- Size: 43.9 KB
- Stars: 63
- Watchers: 5
- Forks: 19
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- freaking_awesome_elixir - Elixir - Collects metrics from your Elixir/Phoenix application and sends them to [NewRelic](https://newrelic.com/). (Instrumenting / Monitoring)
- fucking-awesome-elixir - newrelic.ex - Collects metrics from your Elixir/Phoenix application and sends them to [NewRelic](https://newrelic.com/). (Instrumenting / Monitoring)
- awesome-elixir - newrelic.ex - Collects metrics from your Elixir/Phoenix application and sends them to [NewRelic](https://newrelic.com/). (Instrumenting / Monitoring)
README
# NewRelic agent for Elixir
[![Build Status](https://travis-ci.org/romul/newrelic.ex.svg?branch=master)](https://travis-ci.org/romul/newrelic.ex)
Instrument your Phoenix applications with New Relic.
It currently supports instrumenting Phoenix controllers and Ecto repositories to record
response times for web transactions and database queries.Based on [newrelic-erlang](https://github.com/wooga/newrelic-erlang) and [new-relixir](https://github.com/TheRealReal/new-relixir)
## Why yet another?
1. `newrelic-erlang` & `new-relixir` look abandoned, so the main goal is to create a maintainable integration with NewRelic open to pull requests.
2. `newrelic-erlang` has a performance issue related to `statman` usage. Look at a real-world example of `new-relixir` usage (the project handles about 25 rps):![CPU load](https://api.monosnap.com/rpc/file/download?id=WhmimUZqDkvFkbznpaD6OmqG1tbP1G)
![Memory usage](https://api.monosnap.com/rpc/file/download?id=fI3kVrEyyebqIiIhs38yLZUQaQJkkc)The `new_relic` isn't suffer from such the leaks.
## Usage
The following instructions show how to add instrumentation with New Relic to a hypothetical
Phoenix application named `MyApp`.1. Add `new_relic` to your list of dependencies and start-up applications in `mix.exs`:
```elixir
# mix.exsdefmodule MyApp.Mixfile do
use Mix.Project# ...
def application do
[mod: {MyApp, []},
applications: [:new_relic]]
enddefp deps do
[{:new_relic, "~> 0.1.2"}]
end
end
```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:```elixir
# config/config.exsconfig :new_relic,
application_name: System.get_env("NEWRELIC_APP_NAME"),
license_key: System.get_env("NEWRELIC_LICENSE_KEY"),
poll_interval: 60_000 # push data to NewRelic once per 1 minute
```3. Define a module to wrap your repository's methods with New Relic instrumentation:
```elixir
# lib/my_app/repo.exdefmodule MyApp.Repo do
use Ecto.Repo, otp_app: :my_appdefmodule NewRelic do
use Elixir.NewRelic.Plug.Repo, repo: MyApp.Repo
end
end
```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 `NewRelic.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`:```elixir
# web/web.exdefmodule MyApp.Web do
def controller do
quote do
# ...
plug NewRelic.Plug.Phoenix
alias MyApp.Repo.NewRelic, as: Repo # Replaces `alias MyApp.Repo`
end
end
end
```5. Update your controllers to pass `conn` as an option to your New Relic repo wrapper:
```elixir
# web/controllers/users.exdefmodule MyApp.UserController do
use MyApp.Web, :controllerdef index(conn, _params) do
users = Repo.all(User, conn: conn) # Replaces `Repo.all(User)`
# ...
end
end
```### 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`:```elixir
# lib/my_app/repo.exdefmodule MyApp.Repo do
use Ecto.Repo, otp_app: :my_appdef custom_method(queryable, opts \\ []) do
# ...
enddefmodule NewRelic do
use NewRelic.Plug.Repo, repo: MyApp.Repodef custom_method(queryable, opts \\ []) do
instrument_db(:custom_method, queryable, opts, fn() ->
MyApp.Repo.custom_method(queryable, opts)
end)
end
end
end
```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.Distributed under the [MIT License](LICENSE).