Rails shows incorrect time spent in a request - ruby-on-rails

When I do-
$ time curl 'http://localhost:3000'
I get this: 0.00s user 0.00s system 0% cpu 1.270 total
Gem 'rack-mini-profiler' also show time taken almost same as curl.
But in the rails log, I see-
Completed 200 OK in 103ms (Views: 72.7ms | ActiveRecord: 5.0ms)
Why there is time difference of about 1 second?
Curl and rails server both are running on my development machine.

"time curl" is timing how long the curl client takes to fetch Rails content, ie it's the client time.
The Rails log is showing you the server time.

You did not specify that but if you are running development mode, there is classes reloading and assets compilation, which heavily impacts response time. But it does not affect production.
Here is what I got in my rails 4.2 application (first curl, than corresponding excerpts from log):
curl 'http://localhost:3000' 0.01s user 0.00s system 0% cpu 1.817 total
curl 'http://localhost:3000' 0.01s user 0.00s system 2% cpu 0.366 total
Completed 200 OK in 1254ms (Views: 1206.2ms | ActiveRecord: 13.9ms)
127.0.0.1 - - [04/Oct/2014:19:48:52 +0200] "GET / HTTP/1.1" 200 - 1.6863
Completed 200 OK in 279ms (Views: 278.7ms | ActiveRecord: 0.0ms)
127.0.0.1 - - [04/Oct/2014:19:49:41 +0200] "GET / HTTP/1.1" 200 - 0.2895
While in production:
curl 'http://localhost:3000' 0.01s user 0.00s system 3% cpu 0.329 total
curl 'http://localhost:3000' 0.00s user 0.01s system 11% cpu 0.090 total
I, [2014-10-04T19:54:02.739988 #27830] INFO -- : Completed 200 OK in 143ms (Views: 123.0ms | ActiveRecord: 6.3ms)
I, [2014-10-04T19:54:17.521175 #27830] INFO -- : Completed 200 OK in 41ms (Views: 40.9ms | ActiveRecord: 0.0ms)
So differences persist there, but it's probably due to time for send request from curl to server and then to receive the response. I still think that bigger differences in development are caused by reloading and compiling.

Related

Duplicated request info log in Rails 5 server only app

I have a Rails 5.2.3 application with Ruby 2.4.5.
I found a weird issue that the request info are logged twice in stdout.
Here is the log config in config/environments/product.rb
config.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT))
config.log_tags = [ lambda {|req| "#{req.cookie_jar["_session_id"]}" }, :remote_ip, :uuid ]
Supposedly it will tag every log with the remote ip and request uuid, and it does for most of logs, except there is a weird additional log for the request. In following example, the last line is a duplicated log for the request without the tag:
[INFO] [2019-10-28 06:11:45 UTC] [127.0.0.1] [f6de1900-a7e5-4486-8b73-7095d0cacb35] Started GET "/api/v1/nodes?pageSize=20&pageNumber=1" for 127.0.0.1 at 2019-10-28 06:11:45 +0000
[INFO] [2019-10-28 06:11:45 UTC] [127.0.0.1] [f6de1900-a7e5-4486-8b73-7095d0cacb35] Processing by Api::V1::NodesController#index as XML
...
...
[INFO] [2019-10-28 06:20:02 UTC] [127.0.0.1] [993e0db4-3995-41ef-851a-bfea1bc25781] Completed 200 OK in 1084ms (Views: 341.9ms | ActiveRecord: 150.6ms)
127.0.0.1 - - [28/Oct/2019:06:20:02 +0000] "GET /api/v1/nodes?pageSize=20&pageNumber=1 HTTP/1.1" 200 - 1.1347
I checked configurations, there is no any other logger configured.
The consequence is that there is a timer in client side to check notification status by every 5 seconds. I added a log silencer to avoid log such request:
# config/application.rb
config.middleware.insert_before Rails::Rack::Logger, LogSilencer, silenced: /notification_messages/
# lib/log_silencer.rb
class LogSilencer
def initialize(app, opts = {})
#app = app
#silenced = opts.delete(:silenced)
end
def call(env)
if #silenced.match(env['PATH_INFO'])
Rails.logger.silence do
#app.call(env)
end
else
#app.call(env)
end
end
end
It does avoid logging from the tagged logger, but the duplicated request logs are still there, then the stdout is full of this request
127.0.0.1 - - [28/Oct/2019:07:04:21 +0000] "GET /api/v1/notification_messages/to_notify HTTP/1.1" 200 - 0.0118
127.0.0.1 - - [28/Oct/2019:07:04:26 +0000] "GET /api/v1/notification_messages/to_notify HTTP/1.1" 200 - 0.0140
127.0.0.1 - - [28/Oct/2019:07:04:31 +0000] "GET /api/v1/notification_messages/to_notify HTTP/1.1" 200 - 0.0137
127.0.0.1 - - [28/Oct/2019:07:04:36 +0000] "GET /api/v1/notification_messages/to_notify HTTP/1.1" 200 - 0.0149
...
Spent a whole day try to find out who generated this log, but no any clue...
Would like to ask for help on how to turn off this log so make the log data clear...
Thanks!
Seems the logs come from the web server used when up the Rails app.
When use WEBrick, the log is like
=> Booting WEBrick
=> Rails 5.2.3 application starting in development on http://localhost:4000
=> Run `rails server -h` for more startup options
[2019-10-30 07:01:13] INFO WEBrick 1.3.1
[2019-10-30 07:01:13] INFO ruby 2.4.5 (2018-10-18) [x86_64-linux]
[2019-10-30 07:01:13] INFO WEBrick::HTTPServer#start: pid=5812 port=4000
127.0.0.1 - - [30/Oct/2019:07:01:17 UTC] "GET /test HTTP/1.1" 304 0
- -> /test
When use unicorn, the log is like:
I, [2019-10-30T07:02:47.626962 #5956] INFO -- : Refreshing Gem list
I, [2019-10-30T07:02:48.289373 #5956] INFO -- : listening on addr=0.0.0.0:4000 fd=17
I, [2019-10-30T07:02:48.393598 #5956] INFO -- : master process ready
I, [2019-10-30T07:02:48.394853 #5965] INFO -- : worker=0 ready
I, [2019-10-30T07:02:48.399878 #5968] INFO -- : worker=1 ready
127.0.0.1 - - [30/Oct/2019:07:02:52 +0000] "GET /test HTTP/1.1" 304 - 0.0724
I can find that the request log format is a bit different (the time zone info, the time consumed etc).
I use Unicorn, its logger by default uses stderr, but seems the logger configuration does not work
logger Logger.new("#{rails_root}/log/unicorn.log")
so I have to set the stderr path
stderr_path "#{rails_root}/log/unicorn.stderr.log
Then the request logs are in the unicorn.stderr.log file, and STDOUT are the rails app logs.
But still dont know how to turn it off since it is a kind of duplicated and useless log...

Rails 4.2.6 disable MD5 for FIPS compliance

How does one disable MD5 and other non-FIPS compliant algorithms in Rails 4.2.6 and Ruby 2.2+?
Started GET "/" for ::1 at 2016-04-12 11:00:52 -0400 Processing by
Rails::WelcomeController#index as ​/​ Rendered
/usr/local/rvm/gems/ruby-2.2.4/gems/railties-4.2.6/lib/rails/templates/rails/welcome/index.html.erb
(0.9ms) Completed 200 OK in 20ms (Views: 6.5ms | ActiveRecord: 0.0ms)
md5_dgst.c(78): OpenSSL internal error, assertion failed: Digest MD5
forbidden in FIPS mode! Aborted (core dumped)
This has been addressed in Rails 5.2. See https://github.com/rails/rails/issues/31203. New apps should be compliant now by default and upgraded ones can use Rails.application.config.active_support.use_sha1_digests = true

Can't login with devise on heroku, all other environments work

I got an app that runs fine locally and on production (deployed with capistrano) and I'm trying to set up a staging environment which worked so far except for devise.
When I try to log in with working user credentials I get redirected to the root path but not logged in.
Logs:
Started POST "/login" for 95.91.228.167 at 2016-01-31 13:33:51 +0000
Processing by PagesController#index as HTML
Rendered shared/_sidebar.html.erb (0.2ms)
Completed 200 OK in 2.7ms (Views: 2.1ms | ActiveRecord: 0.0ms)
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"iHo8UfHuZ9S6GVbj3hAvfzqwo67FfZwZtdhwJ2qTk3Q=", "user"=>{"email"=>"xxx#xxx.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Einloggen"}
WARNING: Can't verify CSRF token authenticity
Redirected to http://xxx.herokuapp.com/
Completed 302 Found in 81.5ms (ActiveRecord: 0.0ms)
Started GET "/" for 95.91.228.167 at 2016-01-31 13:33:51 +0000
Rendered pages/index.html.erb within layouts/application (0.5ms)
I tried all solutions posted here like changing the domain in session_store and I updated devise to the most recent version.
Any ideas? If you need more information, please let me know.
As it turned out session_store.rb had domain: :all which I thought was fine. Once I changed it to xxx.herokuapp.com, it worked. Thanks!
--- Update 1 ---
Now that it works on heroku, it doesn't work on the production server anymore - obviously because of the wrong URL in session_store. What to do now?
--- Update 2 ---
Solved this by using environment variables

Unknown slow process of requests

The behaviour is: the browser hangs keep loading without any response back.
Please see the following log.
I, [2014-07-16T14:14:09.766062 #11713] INFO -- : Started GET "/somepage" for xxx.xxx.xxx.xxx at 2014-07-16 14:14:09 +0800
I, [2014-07-16T14:14:40.872411 #31111] INFO -- : Started GET "/" for yyy.yyy.yyy.yyy at 2014-07-16 14:14:40 +0800
I, [2014-07-16T14:14:40.874968 #31111] INFO -- : Processing by HomeController#index as */*
I, [2014-07-16T14:14:40.893189 #31111] INFO -- : Rendered ...
I, [2014-07-16T14:14:40.901553 #31111] INFO -- : Completed 200 OK in 26ms (Views: 22.5ms | ActiveRecord: 2.6ms)
... more other requests handled normally
I, [2014-07-16T14:29:35.395493 #11713] INFO -- : Processing by HomeController#somepage as HTML
I, [2014-07-16T14:29:35.403614 #11713] INFO -- : Rendered home/somepage.html.erb (1.5ms)
I, [2014-07-16T14:29:35.403762 #11713] INFO -- : Completed 200 OK in 8ms (Views: 5.0ms | ActiveRecord: 0.5ms)
In between the "Started GET ..." and "Processing by" logs, the rails app can handle other requests normally. However, the 2 logs (should be from the same request) have around 15mins gap.
Server: Apache with passenger
Database: Mysql using mysql2 adapter
I completely lost and have no clues what is the root cause....
I faced this similar kind of issue where the browser kept on loading with no logs in the log file.And what i found out was that we have mistakenly written find.all in one of our model as during testing it only had 1000 records as compared to production with more than 1 million records.so changing it to batch records/pagination resolved it.
...........Hope this might help you.

Can access production database from Rails console but not Rails App

When I try to login to my Devise app using the rails console on my production database, I can with no trouble, however I cannot when using my Rails App.
I launch my Rails Console with rails c -e production, from my home environment (not SSHing int it or anything) and then proceed to execute app.post('/users/sign_in', {"user"=>{"email"=>"email", "password"=>"password"}}), which succeeds with:
Started POST "/users/sign_in" for 127.0.0.1 at 2014-03-10 05:37:31 +0000
Started POST "/users/sign_in" for 127.0.0.1 at 2014-03-10 05:37:31 +0000
Processing by DeviseSessionsController#create as HTML
Processing by DeviseSessionsController#create as HTML
Parameters: {"user"=>{"email"=>"amar#sittr.co.nz", "password"=>"[FILTERED]"}}
Parameters: {"user"=>{"email"=>"amar#sittr.co.nz", "password"=>"[FILTERED]"}}
Redirected to http://www.example.com/dashboard
Redirected to http://www.example.com/dashboard
Completed 302 Found in 1830ms (ActiveRecord: 1426.3ms)
Completed 302 Found in 1830ms (ActiveRecord: 1426.3ms)
=> 302
From which I can then use app.get '/dashboard' which is a password protected page, and then succeeds with:
Started GET "/dashboard" for 127.0.0.1 at 2014-03-10 05:38:14 +0000
Started GET "/dashboard" for 127.0.0.1 at 2014-03-10 05:38:14 +0000
Processing by StaticPagesController#home as HTML
Processing by StaticPagesController#home as HTML
Rendered static_pages/home.html.haml within layouts/application (139.6ms)
Rendered static_pages/home.html.haml within layouts/application (139.6ms)
Completed 200 OK in 6370ms (Views: 686.2ms | ActiveRecord: 5087.6ms)
Completed 200 OK in 6370ms (Views: 686.2ms | ActiveRecord: 5087.6ms)
=> 200
This is all working as expected, except for when I try and go to the production site, and sign in with the same credentials, it gives me a 500 error Errno::ECONNREFUSEDdevise_sessions#create
Connection refused - connect(2) for "localhost" port 9200. What confuses me is that this says it is trying to connect to localhost which wouldn't make any sense. The following is my database.yml file (with the sensitive bits taken out):
production:
adapter: postgresql
database: db_name
pool: 5
timeout: 5000
host: rds.database.com
port: 5432
username: username
password: password
As you can see, I have set the port and host correctly. What am I doing wrong? Thanks in advance.

Resources