rss_observer 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2f2a9029cb2676680915fd9536a2d5687f93a7b49f5a26f4e25408169f148645
4
- data.tar.gz: c228a4a65ecbb54993a0ad8e549293a92d89d563d6f5eb33dedfa71732966edc
3
+ metadata.gz: 01b6866b2c0c8f0f076d7d829bd95e6677df5092c49be485ef593294bfb03f3f
4
+ data.tar.gz: 4eec4fb6c71ad0d12549e3e2f7ae421546300a5a710a72eddba9a15f4c0d8a12
5
5
  SHA512:
6
- metadata.gz: 12e390587a75867a3d9bf20657fcbdf5bb6c3fd86ed4c91f2688f9f01be0a55d617d700cb43b64732a6f0d28b6c82d444be9c3c5f3bf3bbe1448a835b3b78f22
7
- data.tar.gz: bc19e017e7973c6375943fa853dc860baed1ff95058a7a5a9cff4565bda0883490dd8b6e2b3ca30af1a080701827530bb7bb75f48b7a7e1de7696c0cb392219b
6
+ metadata.gz: e21876c184ce78bee4a43008bb3264e048b1d3ee0756a3e0ae8be70b47ab831014dfb8d2e4778e0212ec67c5919afc0afa377c126cb3965e96da99aa1e1ce869
7
+ data.tar.gz: 6833c9dd2f384bf7c1ca0d80744c25cd2994e000f9663d8b819de4e8cbbefa3a013eebbac16bbba99efdda536356add11e546ff40985e8551b05761cac6f0afd
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ *.gem
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rss_observer (0.1.0)
4
+ rss_observer (0.2.0)
5
5
  get_process_mem (~> 0.2)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,9 +1,10 @@
1
1
  # RssObserver
2
2
 
3
- Rails middleware for observing of RSS changes around the request.
3
+ Rails middleware for observing of RSS memory changes around the request. Under the hood, it utilizes [the get_process_mem](https://github.com/schneems/get_process_mem) gem.
4
4
 
5
5
  [![CircleCI](https://circleci.com/gh/irvingwashington/rss_observer.svg?style=svg)](https://circleci.com/gh/irvingwashington/rss_observer)
6
6
 
7
+
7
8
  # Installation
8
9
 
9
10
  Add the line
@@ -15,11 +16,11 @@ to your Gemfile and run bundle install.
15
16
  To use this middleware in a Rails application, modify relevant environment file (or application.rb to log rss in all environments).
16
17
  You can use the supplied STDOUT logger and insert the RssObserver middleware after `Rails::Rack::Logger`.
17
18
  ```ruby
18
- require 'rss_observer/logger'
19
+ require 'rss_observer/logger_handler'
19
20
  module FooApp
20
21
  class Application < Rails::Application
21
22
  # ...
22
- config.middleware.insert_after Rails::Rack::Logger, RssObserver::Middleware, RssObserver::Logger.new
23
+ config.middleware.insert_after Rails::Rack::Logger, RssObserver::Middleware, RssObserver::LoggerHandler.new
23
24
  end
24
25
  end
25
26
  ```
@@ -31,8 +32,12 @@ the usage log with actual request logs.
31
32
  ```ruby
32
33
  # lib/rails_logger_handler.rb
33
34
  class RailsLoggerHandler
34
- def call(kilobytes)
35
- Rails.logger.info "RSS change: #{kilobytes} KB"
35
+ def initial_memory(kilobytes)
36
+ Thread.current[:_initial_memory] = kilobytes
37
+ end
38
+
39
+ def final_memory(kilobytes)
40
+ Rails.logger.info "Memory change: #{final_memory - Thread.current[:_initial_memory]} KB"
36
41
  end
37
42
  end
38
43
 
@@ -53,4 +58,4 @@ RssObserver vs Default Rails request logs
53
58
  ```
54
59
  I, [2019-09-03T14:56:22.735264 #7290] INFO -- : [96956f1a-7f7f-4836-a8a1-bc3c05696433] Completed 200 OK in 47ms (Views: 29.7ms | ActiveRecord: 6.3ms | Allocations: 18614)
55
60
  I, [2019-09-03T14:56:22.737113 #7290] INFO -- : [96956f1a-7f7f-4836-a8a1-bc3c05696433] Memory change: 264.0 KB
56
- ```
61
+ ```
data/lib/rss_observer.rb CHANGED
@@ -3,6 +3,13 @@
3
3
  # Top level namespace of the Gem
4
4
  module RssObserver
5
5
  Error = Class.new(StandardError)
6
+
7
+ def self.current_memory
8
+ @mem ||= GetProcessMem.new
9
+ @mem.kb
10
+ rescue StandardError
11
+ nil
12
+ end
6
13
  end
7
14
 
8
15
  require 'rss_observer/version'
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RssObserver
4
+ # Basic handler, outputting the memory change information to standard output
5
+ class LoggerHandler
6
+ KEY = :_rss_observer_initial_memory
7
+
8
+ # @param logger [Logger]
9
+ def initialize(logger = Logger.new(STDOUT))
10
+ @logger = logger
11
+ end
12
+
13
+ # @param kilobytes [Float]
14
+ def initial_memory(kilobytes)
15
+ Thread.current[KEY] = kilobytes
16
+ end
17
+
18
+ # @param kilobytes [Float]
19
+ def final_memory(kilobytes)
20
+ initial_memory = Thread.current[KEY]
21
+ return unless initial_memory
22
+
23
+ change = kilobytes - initial_memory
24
+ initial_memory(nil)
25
+ logger.info "Memory change: #{change} KB"
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :logger
31
+ end
32
+ end
@@ -11,8 +11,11 @@ module RssObserver
11
11
  # @param handler [Object] Handler that accepts memory change updates
12
12
  def initialize(app, handler)
13
13
  @app = app
14
- unless handler.respond_to?(:call)
15
- raise UnsupportedHandlerError, 'Handler must respond to the #call(kilobytes) method'
14
+ unless handler.respond_to?(:initial_memory)
15
+ raise UnsupportedHandlerError, 'Handler must respond to the #initial_memory(kilobytes) method'
16
+ end
17
+ unless handler.respond_to?(:final_memory)
18
+ raise UnsupportedHandlerError, 'Handler must respond to the #final_memory(kilobytes) method'
16
19
  end
17
20
 
18
21
  @handler = handler
@@ -21,11 +24,9 @@ module RssObserver
21
24
  # @param env [Hash] Full application environment hash
22
25
  # @return [Array] Status code, hash of headers, response body
23
26
  def call(env)
24
- before_memory = current_memory
25
-
27
+ handler.initial_memory(current_memory)
26
28
  app.call(env).tap do
27
- memory_change = current_memory - before_memory
28
- handler.call(memory_change)
29
+ handler.final_memory(current_memory)
29
30
  end
30
31
  end
31
32
 
@@ -34,8 +35,7 @@ module RssObserver
34
35
  attr_reader :app, :handler
35
36
 
36
37
  def current_memory
37
- @mem ||= GetProcessMem.new
38
- @mem.kb
38
+ RssObserver.current_memory
39
39
  end
40
40
  end
41
41
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RssObserver
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
data/rss_observer.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  # Specify which files should be added to the gem when it is released.
19
19
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
20
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|.circleci|bin)/}) }
22
22
  end
23
23
  spec.bindir = 'exe'
24
24
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rss_observer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maciek Dubiński
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-03 00:00:00.000000000 Z
11
+ date: 2019-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: get_process_mem
@@ -101,22 +101,17 @@ executables: []
101
101
  extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
- - ".circleci/config.yml"
105
104
  - ".gitignore"
106
105
  - ".rspec"
107
106
  - ".rubocop.yml"
108
- - ".travis.yml"
109
107
  - CODE_OF_CONDUCT.md
110
108
  - Gemfile
111
109
  - Gemfile.lock
112
110
  - LICENSE.txt
113
111
  - README.md
114
112
  - Rakefile
115
- - bin/console
116
- - bin/rubocop.sh
117
- - bin/setup
118
113
  - lib/rss_observer.rb
119
- - lib/rss_observer/logger.rb
114
+ - lib/rss_observer/logger_handler.rb
120
115
  - lib/rss_observer/middleware.rb
121
116
  - lib/rss_observer/version.rb
122
117
  - rss_observer.gemspec
data/.circleci/config.yml DELETED
@@ -1,61 +0,0 @@
1
- # Ruby CircleCI 2.0 configuration file
2
- #
3
- # Check https://circleci.com/docs/2.0/language-ruby/ for more details
4
- #
5
- version: 2
6
- jobs:
7
- build:
8
- docker:
9
- # specify the version you desire here
10
- - image: circleci/ruby:2.4.1-node-browsers
11
-
12
- # Specify service dependencies here if necessary
13
- # CircleCI maintains a library of pre-built images
14
- # documented at https://circleci.com/docs/2.0/circleci-images/
15
- # - image: circleci/postgres:9.4
16
-
17
- working_directory: ~/repo
18
-
19
- steps:
20
- - checkout
21
-
22
- # Download and cache dependencies
23
- - restore_cache:
24
- keys:
25
- - v1-dependencies-{{ checksum "Gemfile.lock" }}
26
- # fallback to using the latest cache if no exact match is found
27
- - v1-dependencies-
28
-
29
- - run:
30
- name: install dependencies
31
- command: |
32
- bundle install --jobs=4 --retry=3 --path vendor/bundle
33
-
34
- - save_cache:
35
- paths:
36
- - ./vendor/bundle
37
- key: v1-dependencies-{{ checksum "Gemfile.lock" }}
38
-
39
- # run tests!
40
- - run:
41
- name: run tests
42
- command: |
43
- mkdir /tmp/test-results
44
- TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | \
45
- circleci tests split --split-by=timings)"
46
-
47
- bundle exec rspec \
48
- --format progress \
49
- --format RspecJunitFormatter \
50
- --out /tmp/test-results/rspec.xml \
51
- --format progress \
52
- $TEST_FILES
53
- - run:
54
- name: run rubocop
55
- command: bash bin/rubocop.sh
56
- # collect reports
57
- - store_test_results:
58
- path: /tmp/test-results
59
- - store_artifacts:
60
- path: /tmp/test-results
61
- destination: test-results
data/.travis.yml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- sudo: false
3
- language: ruby
4
- cache: bundler
5
- rvm:
6
- - 2.6.3
7
- before_install: gem install bundler -v 1.17.2
data/bin/console DELETED
@@ -1,15 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require 'bundler/setup'
5
- require 'rss_observer'
6
-
7
- # You can add fixtures and/or initialization code here to make experimenting
8
- # with your gem easier. You can also use a different console, if you like.
9
-
10
- # (If you use this, don't forget to add pry to your Gemfile!)
11
- # require "pry"
12
- # Pry.start
13
-
14
- require 'irb'
15
- IRB.start(__FILE__)
data/bin/rubocop.sh DELETED
@@ -1,41 +0,0 @@
1
- #!/usr/bin/env bash
2
-
3
- # Inspired by http://takemikami.com/2018/01/30/RubocopPullRequestCI.html
4
-
5
- BASE_REMOTE=origin
6
- BASE_BRANCH=master
7
-
8
- git fetch $BASE_REMOTE $BASE_BRANCH
9
-
10
- diff_list=()
11
- commit_list=`git --no-pager log --no-merges $BASE_REMOTE/$BASE_BRANCH...HEAD | grep -e '^commit' | sed -e "s/^commit \(.\{8\}\).*/\1/"`
12
-
13
- for f in `git --no-pager diff $BASE_REMOTE/$BASE_BRANCH...HEAD --name-only`; do
14
- for c in $commit_list; do
15
- diffs=`git --no-pager blame --follow --show-name -s $f | grep $c | sed -e "s/^[^ ]* *\([^ ]*\) *\([0-9]*\)*).*$/\1:\2/"`
16
- for ln in $diffs; do
17
- diff_list+=("$ln")
18
- done
19
- done
20
- done
21
-
22
- err_count=0
23
- err_lines=()
24
-
25
- while read line; do
26
- for m in ${diff_list[@]}; do
27
- if [[ "$line" =~ "$m" ]]; then
28
- err_count=$(($err_count + 1))
29
- err_lines+=("$line")
30
- break
31
- fi
32
- done
33
- done < <(bundle exec rubocop --format emacs)
34
-
35
- if [ $err_count -ne 0 ]; then
36
- echo -e "\033[31m$err_count Lint Errors\033[0;39m"
37
- echo -e "\033[31m-----------------------------------\033[0;39m\n"
38
- printf '\033[31m%s\n' "${err_lines[@]}"
39
- echo -e "\033[31mPlease resolve them before merging.\033[0;39m"
40
- exit 1
41
- fi
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RssObserver
4
- # Basic handler, outputting the memory change information to standard output
5
- class Logger
6
- # @param logger [Logger]
7
- def initialize(logger = Logger.new(STDOUT))
8
- @logger = logger
9
- end
10
-
11
- # @param kilobytes [Float]
12
- def call(kilobytes)
13
- logger.info "Memory change: #{kilobytes} KB"
14
- end
15
-
16
- private
17
-
18
- attr_reader :logger
19
- end
20
- end