rails_autoscale_agent 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 98ed2f90f2585a2e0ef2708353fcd3781bdee1fb
4
- data.tar.gz: 8111cdfd58133de98690a2e431c9b3c22d0eb7d7
2
+ SHA256:
3
+ metadata.gz: 835d9318705ae0bab9644578e6e39f6b29dd9c36d50671cf8651e44bfcfe2af5
4
+ data.tar.gz: c205493eb704e12e8d7da388de903e9391dfa328636118021ff5f4ca4173b127
5
5
  SHA512:
6
- metadata.gz: 98634e7e2182246c3564b37e2ef71c94c19e45d7deb7f67e3ce1099469ea6b4fc2de006be378c0e9730520466846349f88e5e44b1bedf335985ea113e37f80bb
7
- data.tar.gz: ecf32db52d110100e7146725e3481e3e43d62e13de65adea74a9391c26fc8755eda855ffe3990ef1fee0de2464a777e068d59dbc37ac897094cce5a8ec9f027f
6
+ metadata.gz: 7fe642672aa559193cd48ca32ade1acdabc3c176d8a9435b4964bb0db86752fdd2aae9ed1698d75570c015b44a87d0b6dd895925cadc831a2f4628f4c9a5efe8
7
+ data.tar.gz: f55a4a48fe981add95eb0ad497bc7d389841e0d2c9254a63b4378284f2bd14ae364b0b3a5e468783b23f453d31c34e84f716f1d27fbde56ec8c3c7f8a4d4d1c4
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5.0
data/.travis.yml CHANGED
@@ -1,5 +1,3 @@
1
- sudo: false
2
1
  language: ruby
3
2
  rvm:
4
- - 2.3.1
5
- before_install: gem install bundler -v 1.12.5
3
+ - 2.5.0
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Rails Autoscale Agent
2
2
 
3
+ [![Build Status](https://travis-ci.org/adamlogic/rails_autoscale_agent.svg?branch=master)](https://travis-ci.org/adamlogic/rails_autoscale_agent)
4
+
3
5
  This gem works together with the Rails Autoscale Heroku add-on
4
6
  to automatically scale your web dynos as needed.
5
7
  It gathers a minimal set of metrics for each request,
@@ -24,6 +26,19 @@ which happens automatically when you install the Heroku add-on.
24
26
  In development (or anytime the ENV var is missing), the middleware will still produce
25
27
  `INFO`-level log output to your Rails log.
26
28
 
29
+ ## Non-Rails Rack apps
30
+
31
+ You'll need to insert the `RailsAutoscaleAgent::Middleware` manually. Insert it
32
+ before `Rack::Runtime` to ensure accuracy of request queue timings.
33
+
34
+ ## Changing the logger
35
+
36
+ If you wish to use a different logger you can set it on the configuration object:
37
+
38
+ ```ruby
39
+ RailsAutoscaleAgent::Config.instance.logger = MyLogger.new
40
+ ```
41
+
27
42
  ## Development
28
43
 
29
44
  After checking out the repo, run `bin/setup` to install dependencies.
@@ -42,4 +57,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/adamlo
42
57
  ## License
43
58
 
44
59
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
45
-
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/gem_tasks"
2
4
  require "rspec/core/rake_task"
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RailsAutoscaleAgent
2
4
  end
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'net/http'
2
4
  require 'uri'
3
5
  require 'json'
@@ -1,25 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'singleton'
2
4
 
3
5
  module RailsAutoscaleAgent
4
6
  class Config
5
7
  include Singleton
6
8
 
7
- attr_reader :api_base_url, :dyno, :pid, :fake_mode, :max_request_size
8
- attr_accessor :report_interval
9
- alias_method :fake_mode?, :fake_mode
9
+ attr_accessor :report_interval, :logger, :api_base_url, :max_request_size,
10
+ :dyno, :pid
10
11
 
11
12
  def initialize
12
13
  @api_base_url = ENV['RAILS_AUTOSCALE_URL']
13
14
  @pid = Process.pid
14
15
  @max_request_size = 100_000 # ignore request payloads over 100k since they skew the queue times
15
16
  @report_interval = 60 # this default will be overwritten during Reporter#register!
16
- @fake_mode = true if ENV['RAILS_AUTOSCALE_FAKE_MODE'] == 'true'
17
-
18
- if fake_mode?
19
- @dyno = 'web.123'
20
- else
21
- @dyno = ENV['DYNO']
22
- end
17
+ @logger ||= defined?(Rails) ? Rails.logger : ::Logger.new(STDOUT)
18
+ @dyno = ENV['DYNO']
23
19
  end
24
20
 
25
21
  def to_s
@@ -27,7 +23,7 @@ module RailsAutoscaleAgent
27
23
  end
28
24
 
29
25
  def ignore_large_requests?
30
- @max_request_size.present?
26
+ @max_request_size
31
27
  end
32
28
 
33
29
  end
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RailsAutoscaleAgent
2
4
  module Logger
3
5
 
4
6
  def logger
5
- @logger ||= Rails.logger
7
+ @logger ||= Config.instance.logger
6
8
  end
7
9
 
8
10
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RailsAutoscaleAgent
2
4
  class Measurement < Struct.new(:time, :value)
3
5
  def initialize(time, value)
@@ -1,6 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails_autoscale_agent/logger'
2
4
  require 'rails_autoscale_agent/store'
3
- require 'rails_autoscale_agent/collector'
4
5
  require 'rails_autoscale_agent/reporter'
5
6
  require 'rails_autoscale_agent/config'
6
7
  require 'rails_autoscale_agent/request'
@@ -22,7 +23,12 @@ module RailsAutoscaleAgent
22
23
 
23
24
  store = Store.instance
24
25
  Reporter.start(config, store)
25
- Collector.collect(request, store) unless request.ignore?
26
+
27
+ if !request.ignore? && queue_time = request.queue_time
28
+ # NOTE: Expose queue time to the app
29
+ env['queue_time'] = queue_time
30
+ store.push queue_time
31
+ end
26
32
  end
27
33
 
28
34
  @app.call(env)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails_autoscale_agent/middleware'
2
4
 
3
5
  module RailsAutoscaleAgent
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails_autoscale_agent/version'
2
4
 
3
5
  module RailsAutoscaleAgent
@@ -8,7 +10,7 @@ module RailsAutoscaleAgent
8
10
  dyno: config.dyno,
9
11
  pid: config.pid,
10
12
  ruby_version: RUBY_VERSION,
11
- rails_version: Rails.version,
13
+ rails_version: defined?(Rails) && Rails.version,
12
14
  gem_version: VERSION,
13
15
  }
14
16
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RailsAutoscaleAgent
2
4
  class Report
3
5
 
@@ -15,12 +17,12 @@ module RailsAutoscaleAgent
15
17
  end
16
18
 
17
19
  def to_csv
18
- ''.tap do |result|
20
+ String.new.tap do |result|
19
21
  @measurements.each do |measurement|
20
22
  result << measurement.time.to_i.to_s
21
- result << ','.freeze
23
+ result << ','
22
24
  result << measurement.value.to_s
23
- result << "\n".freeze
25
+ result << "\n"
24
26
  end
25
27
  end
26
28
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'singleton'
2
4
  require 'rails_autoscale_agent/logger'
3
5
  require 'rails_autoscale_agent/autoscale_api'
@@ -1,5 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RailsAutoscaleAgent
2
4
  class Request
5
+ include Logger
6
+
3
7
  attr_reader :id, :entered_queue_at, :path, :method, :size
4
8
 
5
9
  def initialize(env, config)
@@ -8,16 +12,29 @@ module RailsAutoscaleAgent
8
12
  @path = env['PATH_INFO']
9
13
  @method = env['REQUEST_METHOD'].downcase
10
14
  @size = env['rack.input'].respond_to?(:size) ? env['rack.input'].size : 0
11
- @entered_queue_at = if unix_millis = env['HTTP_X_REQUEST_START']
12
- Time.at(unix_millis.to_f / 1000)
13
- elsif config.fake_mode?
14
- Time.now - rand(1000) / 1000.0 # 0-1000 ms ago
15
- end
15
+
16
+ if unix_millis = env['HTTP_X_REQUEST_START']
17
+ @entered_queue_at = Time.at(unix_millis.to_f / 1000)
18
+ end
16
19
  end
17
20
 
18
21
  def ignore?
19
22
  @config.ignore_large_requests? && @size > @config.max_request_size
20
23
  end
21
24
 
25
+ def queue_time
26
+ if entered_queue_at
27
+ if entered_queue_at < (Time.now - 60 * 10)
28
+ # ignore unreasonable values
29
+ logger.info "request queued for more than 10 minutes... skipping collection"
30
+ else
31
+ queue_time = ((Time.now - entered_queue_at) * 1000).to_i
32
+ queue_time = 0 if queue_time < 0
33
+ logger.info "Collected queue_time=#{queue_time}ms request_id=#{id} request_size=#{size}"
34
+
35
+ queue_time
36
+ end
37
+ end
38
+ end
22
39
  end
23
40
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'singleton'
2
4
  require 'rails_autoscale_agent/time_rounder'
3
5
  require 'rails_autoscale_agent/measurement'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RailsAutoscaleAgent
2
4
  class TimeRounder
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RailsAutoscaleAgent
2
- VERSION = "0.3.1"
4
+ VERSION = "0.4.0"
3
5
  end
data/log/.gitkeep ADDED
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_autoscale_agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam McCrea
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-26 00:00:00.000000000 Z
11
+ date: 2018-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -131,6 +131,7 @@ extra_rdoc_files: []
131
131
  files:
132
132
  - ".gitignore"
133
133
  - ".rspec"
134
+ - ".ruby-version"
134
135
  - ".travis.yml"
135
136
  - Gemfile
136
137
  - LICENSE.txt
@@ -140,7 +141,6 @@ files:
140
141
  - bin/setup
141
142
  - lib/rails_autoscale_agent.rb
142
143
  - lib/rails_autoscale_agent/autoscale_api.rb
143
- - lib/rails_autoscale_agent/collector.rb
144
144
  - lib/rails_autoscale_agent/config.rb
145
145
  - lib/rails_autoscale_agent/logger.rb
146
146
  - lib/rails_autoscale_agent/measurement.rb
@@ -153,6 +153,7 @@ files:
153
153
  - lib/rails_autoscale_agent/store.rb
154
154
  - lib/rails_autoscale_agent/time_rounder.rb
155
155
  - lib/rails_autoscale_agent/version.rb
156
+ - log/.gitkeep
156
157
  - rails_autoscale_agent.gemspec
157
158
  homepage: https://github.com/adamlogic/rails_autoscale_agent
158
159
  licenses:
@@ -174,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
174
175
  version: '0'
175
176
  requirements: []
176
177
  rubyforge_project:
177
- rubygems_version: 2.5.2
178
+ rubygems_version: 2.7.3
178
179
  signing_key:
179
180
  specification_version: 4
180
181
  summary: This gem works with the Rails Autoscale Heroku add-on to automatically scale
@@ -1,22 +0,0 @@
1
- require 'rails_autoscale_agent/logger'
2
-
3
- module RailsAutoscaleAgent
4
- class Collector
5
- extend Logger
6
-
7
- def self.collect(request, store)
8
- if request.entered_queue_at
9
- if request.entered_queue_at < (Time.now - 60 * 10)
10
- # ignore unreasonable values
11
- logger.info "request queued for more than 10 minutes... skipping collection"
12
- else
13
- queue_time_millis = ((Time.now - request.entered_queue_at) * 1000).to_i
14
- queue_time_millis = 0 if queue_time_millis < 0
15
- store.push(queue_time_millis)
16
- logger.info "Collected queue_time=#{queue_time_millis}ms request_id=#{request.id}"
17
- end
18
- end
19
- end
20
-
21
- end
22
- end