How to differentiate Rails API calls in Newrelic?
Asked Answered
A

1

9

I'm using Rails 5 to serve a website and a RESTful API, and I use Newrelic (newrelic_rpm gem) to monitor the application performance.

At the moment, the gem monitor to all requests to one application name.

let's say my routes looks something like:

Rails.application.routes.draw do
  resources :users
  namespace :api do
    namespace :v1 do
      resources :users, only: :index
    end
  end
end

Here /users is the route for the web app and api/users is the route for the API. For now, once I use newrelic_rpm it won't see the different between the API and the web.

I want to make more separation for the reports so I can log web requests to a specific app name, ex: web_app and for the API another name api_app.

Any help?

Azotize answered 27/4, 2017 at 4:34 Comment(1)
I deleted my answer as it turned out I misunderstood your question. I don't know of a way to report to multiple NewRelic apps unless you used multiple rails apps, sorry.Empty
G
5

You cannot do what you have outlined here with the current version of the New Relic agent. That's not how the agent works; that's not how New Relic works.

You have a single application whose performance should be considered as a whole. If you were able to route the performance data to different apps, how could you see that a problem in one app is affecting the other app? How can you correlate events and metrics when they are in separate apps?

More specifically, someone could attack app A and negatively impact app B, and that might appear in the performance data for app A, app B, or both. That's why this isn't supported: you don't put up artificial walls in the performance data for a single app running on a single server.

Use the agent the way it was intended, and use the New Relic transactions page in APM and the Web transactions analysis report to filter down to the transactions you care about. You can also use the Insights event explorer to filter and chart your Transaction data.

Gnosticize answered 3/5, 2017 at 23:31 Comment(7)
Thanks, @anothermh, I hear you loud and clear. I agree with what you said, however sometimes the same rails app can acts like two separate apps (web application and API which can be used with mobile app). The only reason to deploy both to the same server is cost efficiency and in this case, it could be useful to have to app names as the two apps would have different characteristics (especially if we mount the API)Azotize
It sounds like you want the benefits of separating your services without actually separating your services. The reason that you can't get what you want is because that's not how applications work. There are two easy paths: make two apps (and then have two apps in New Relic), or make one app (and filter the data in New Relic). There are many impossible paths, including: what you have described trying to do. I recommend: make one app and filter the data in New Relic, for example with custom Insights eventsGnosticize
@EkiEqbal The reason why you can't treat the apps as separate: if you have two unicorn workers that are busy 95% the time servicing very long lived requests for your API at a very low throughput, and only available 5% of the time for your web app, how can you tell with two separate apps reporting to New Relic that your web app is starved for workers? By all appearances, it services requests quickly and regularly but just doesn't get them very often. You think your API is performing great, you think your web app is performing great. If you look at them as a single app, you can see the problem.Gnosticize
Thanks for the explanation @anotherm, makes perfect sense. I think I'll just run two apps and use two apps on newrelic.Azotize
Is this a rails engine implementation then?Demodulate
This might be late, but thought to ask aw, can't we just make some conditions based on the hostname in newrelic.rb. Something like app_name: <%= Socket.gethostname == "app-web" ? "Web App" : "API APP" %> ?Azotize
@EkiEqbal Your question doesn't make sense. Ruby starts up one time, and when it starts that one time it makes a single connection back to New Relic through the gem. The app name is defined once, when initialized. The gem doesn't reinitialize in between each method, connect to New Relic, send data, then disconnect.Gnosticize

© 2022 - 2024 — McMap. All rights reserved.