16

I've installed oink gem for monitoring the memory usage of my rails application. In order to see oink report I need to run this command in the terminal:

oink --threshold=75 /log/*

When I run it on my machine it shows the report for the development environment. The thing is that I'm more interested on seeing the report for my production environment. My app is hosted on heroku, is there a way I could run oink's terminal commands for the heroku's production environment?

Thanks

Oded Harth
  • 4,367
  • 9
  • 35
  • 62
  • Did you figure this out? also stuck.. – jalagrange Sep 25 '12 at 22:15
  • 1
    It's also worth noting that with multiple dynos there's no guaranteed log order which seems to screw up the oink parser. Out of ~100 requests with a 2 dyno heroku app oink is only parsing 7 requests. – agmin May 02 '14 at 20:46

7 Answers7

31

I got oink working on heroku doing this:

You have to change your log_level to info for the oink logs to show up:

heroku config:add LOG_LEVEL=info

Add oink middleware to production.rb with a custom stdout logger

config.middleware.use( Oink::Middleware, :logger => Hodel3000CompliantLogger.new(STDOUT))

When you want to parse your logs, tail them to a local file, then remove heroku's prefix and filter to only the oink lines.

heroku logs --tail > log/production.log
cat log/production.log | cut -c 46- | grep 'Oink\|Memory\|Instantiation' > log/production-oink.log

Then run oink on your new local log

oink --threshold=0 log/production-oink.log

You could also download logs from logentries or paperclip

sergserg
  • 21,716
  • 41
  • 129
  • 182
Gabe Coyne
  • 331
  • 3
  • 5
  • 4
    Seems that was missing an escape on pipe char like 'Oink\|Instantiation\|Memory'. However, still not processing the oink content for me, not sure if is cutting the right stuff. – brunoghisi Feb 04 '13 at 19:09
  • 2
    Needed: cat filename.log | cut -c 46- | grep 'Oink\|Memory\|Instantiation' – David Aldridge Sep 27 '13 at 12:16
16

Unfortunately I can't comment yet but in case someone else has this issue a few things seem to have changed since Gabe Coyne's (super helpful) answer above. I didn't need to change my heroku log_level, so to start generating the logs you can just use an initializer config/initializers/oink.rb like so:

YourApp::Application.middleware.use( Oink::Middleware, :logger => Hodel3000CompliantLogger.new(STDOUT))

Or put that line in production.rb if you only want the oink logs in production. Then you need to get the logs into a local log file, also mentioned in the previous answer:

heroku logs n500 --app app_name > log/production.log

Or you can grab them from Papertrail or your favorite log archiver. The local logs need to be updated to the format that oink expects, but I found that grep 'Oink|Memory|Instantiation' didn't work as that string seems to have been removed from the logs, so I used this instead:

cat log/production.log | cut -c 46- | grep 'rails\[' > log/production-oink.log

Then you can use oink --threshold=0 log/production-oink.log and it will work.

The only other thing that caught me up was the number of preceding characters to cut. Whereas cut -c 39- seems to have worked before, I had to use cut -c 46-. Obviously this varies, so in case its not obvious, you are just trying to get the lines in the raw logs that look like this:

2013-07-19T18:47:09.494475+00:00 app[web.1]: Jul 19 18:47:09 24ab5d5s-g46c-2d44-dss2-233sdfa99852wd rails[5]: Oink Action: welcome#about

To look like this:

Jul 19 18:47:09 24ab5d5s-g46c-2d44-dss2-233sdfa99852wd rails[5]: Oink Action: welcome#about

With the front part removed. Hope this helps!

hiattp
  • 2,326
  • 1
  • 20
  • 23
5

If you are using a log archive downloaded from Papertrail, you can use this one-liner to format your output:

cat log/production.log | cut -f 10- | grep 'rails\[' > log/production-oink.log
jfrprr
  • 543
  • 4
  • 11
2

You need to set up a drain for your heroku logs in order to run the oink command. For example, I have my heroku logs draining onto a local linux rsyslog server.

ylluminate
  • 12,102
  • 17
  • 78
  • 152
  • This is true, however even if you export your logs, I havent found a way for them to be in valid "hodel 3000 compliant logger" format. Which is a requirement for Oink to work. Any ideas? – jalagrange Sep 25 '12 at 22:15
2

Instead of switching the logger to Hodel3000Compliant you could also modify slightly your heroku logs with vim, for example, by using the following commands:

:%s/2013-01-25T/Jan 25 /
:%s/+00:00//
:%s/app\[web.1\]/rails\[1\]/
:%s/app\[web.2\]/rails\[2\]/

your original heroku logs like those:

2013-01-25T15:05:58+00:00 app[web.1]: Oink Action: tools#some_tool
2013-01-25T15:05:58+00:00 app[web.1]: Memory usage: 303444 | PID: 2
2013-01-25T15:05:58+00:00 app[web.1]: Oink Log Entry Complete

become:

Jan 25 15:05:58 rails[1]: Oink Action: tools#some_tool
Jan 25 15:05:58 rails[1]: Memory usage: 303444 | PID: 2
Jan 25 15:05:58 rails[1]: Oink Log Entry Complete

and Oink likes them ;) Of course, first narrow the time where possible problem occured in the log by using NewRelic.

UPDATE:

Another set of vim commands to fix Papertrail logs for oink:

:%s/\v^\d+\t//
:%s/2013-01-27T/Jan 27 /

And go to the first line, use CTRL-V to mark few other columns that are left, then type G to select those columns to the end of file. Make adjustments in your selection with 'left'/'right' arrows and then press 'c' to remove them.

januszm
  • 1,166
  • 13
  • 24
1

Got it working with hiattp's answer, but needed to add --text to the grep.

cat log/production.log | cut -c 46- | grep 'rails\[' > log/production-oink.log
ataravati
  • 8,891
  • 9
  • 57
  • 89
-3

Use New Relic - as answered in your other question.

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
John Beynon
  • 37,398
  • 8
  • 88
  • 97