rack-perf 1.0.2 → 1.1.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
2
  SHA1:
3
- metadata.gz: fef2e18d1488130915f01a382eb6a43a9518d77f
4
- data.tar.gz: 131652337428f1170991a0abf162bfc174c4dc84
3
+ metadata.gz: d40a9c9b78033566ae211b1f8693ded86d5e3879
4
+ data.tar.gz: c9fc951074422528b556d4e8d4df5169e84e92cb
5
5
  SHA512:
6
- metadata.gz: 3d9403f830389e9c5191e1dd67a1e062e8dd4e77711680f771097fb2833309e587aec76a872acd518b4b5fda8c8bafee2f3e83d27f896e9e2c10c511d203ed73
7
- data.tar.gz: 9bf53c33adaa84f98abc6437dbb57f337cc18f837290d98df1e866119004c6b9bb9d00df9866c8c384e3a49a93953e712c30e9d4054e68b84de827aa2a7f9328
6
+ metadata.gz: da0b950d9dabf8a043b5cbc1b565993512a0490ae1bfd3c41658c9bbc1af77ec683a023b1dec5d40ece5a5289ff9ce32a9d9c5a71c314a528924e356a7b892c7
7
+ data.tar.gz: f5eb8a8ddd21bc86b45ef30de01fa70adfdcbcb07892558c4b6ecc59fca1b6e6f79f39b6965334daf1f6520b213f0f675de6156ff019187c2a1b6ad529f5125d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-perf.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Rack::Perf
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rack/perf`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rack-perf'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rack-perf
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rack-perf.
36
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rack/perf"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
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,5 +1,5 @@
1
1
  module Rack
2
2
  class Perf
3
- VERSION = "1.0.2"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
data/lib/rack/perf.rb CHANGED
@@ -8,39 +8,100 @@ module Rack
8
8
  attr_reader :endtime
9
9
  private :endtime
10
10
 
11
- attr_reader :label
12
- private :label
11
+ attr_reader :api_key
12
+ private :api_key
13
13
 
14
- def initialize(stack, label = "X-Perf-Timing")
14
+ attr_reader :debug
15
+ private :debug
16
+
17
+ def initialize(stack, api_key, debug = false)
15
18
  @stack = stack
16
- @label = label
19
+ @api_key = api_key
20
+ @debug = debug
17
21
  end
18
22
 
19
- def call(previous_state)
20
- @state = previous_state
23
+ def call(env)
24
+ @env = env
21
25
  @starttime = Time.now
22
26
 
23
- @status, @headers, @body = stack.call(state)
27
+ request = Rack::Request.new(env)
28
+ @status, @headers, @body = stack.call(env)
24
29
 
25
30
  @endtime = Time.now
26
31
 
27
- unless headers.has_key?(label)
28
- headers[label] = runtime
29
- end
32
+ ip_addr = request.ip
33
+ ip_addr = env["HTTP_X_FORWARDED_FOR"] if env["HTTP_X_FORWARDED_FOR"]
30
34
 
35
+ send_data(request.ip, request.request_method, request.url, get_normalized_path(request), status, runtime)
31
36
  [status, headers, body]
32
37
  end
33
38
 
39
+ private def send_data(ip_addr, request_method, request_url, normalized_uri, status_code, time_in_millis)
40
+ uri = URI.parse("https://data.perf.sh")
41
+
42
+ http = Net::HTTP.new(uri.host, uri.port)
43
+ http.use_ssl = true
44
+
45
+ request = Net::HTTP::Post.new('/ingest')
46
+ request.add_field('Content-Type', 'application/json')
47
+ request.add_field('X-Perf-Public-API-Key', api_key)
48
+ request.body = {
49
+ 'ip_addr' => ip_addr,
50
+ 'request_method' => request_method,
51
+ 'request_url' => request_url,
52
+ 'normalized_uri' => normalized_uri,
53
+ 'status_code' => status_code,
54
+ 'time_in_millis' => time_in_millis
55
+ }.to_json
56
+
57
+ puts request.body if debug
58
+
59
+ response = http.request(request)
60
+ end
61
+
62
+ private def get_normalized_path(request)
63
+ path = request.path
64
+
65
+ begin
66
+ route = Rails.application.routes.recognize_path path, method: request.request_method
67
+ params = {}
68
+ route.each{ |param, value|
69
+ if not ["controller", "action"].include?(param.to_s)
70
+ params[param.to_s] = value
71
+ end
72
+ }
73
+
74
+ normalized_path = []
75
+ path.split(/\//).each { |path_part|
76
+ params.each { |param, path_value|
77
+ if path_part == path_value
78
+ path_part = ":" + param.to_s
79
+ end
80
+
81
+ if params["format"] && path_part == path_value + "." + params["format"]
82
+ path_part = ":" + param.to_s
83
+ end
84
+ }
85
+
86
+ normalized_path.push(path_part)
87
+ }
88
+
89
+ return normalized_path.join("/")
90
+ rescue ActionController::RoutingError
91
+ return nil
92
+ end
93
+ end
94
+
34
95
  private def runtime
35
- (endtime - starttime).to_s
96
+ ((endtime - starttime) * 1000).round
36
97
  end
37
98
 
38
99
  private def stack
39
100
  @stack
40
101
  end
41
102
 
42
- private def state
43
- @state
103
+ private def env
104
+ @env
44
105
  end
45
106
 
46
107
  private def headers
data/rack-perf.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/perf/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-perf"
8
+ spec.version = Rack::Perf::VERSION
9
+ spec.authors = ["Steven Lu"]
10
+ spec.email = ["steve@perf.sh"]
11
+
12
+ spec.summary = %q{Perf middleware}
13
+ spec.description = %q{Rack middleware that records endpoint timing and status codes to Perf}
14
+ spec.homepage = "https://github.com/perflabs/rack-perf"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.13"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-perf
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steven Lu
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
11
  date: 2016-10-30 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
13
41
  description: Rack middleware that records endpoint timing and status codes to Perf
14
42
  email:
15
43
  - steve@perf.sh
@@ -17,12 +45,18 @@ executables: []
17
45
  extensions: []
18
46
  extra_rdoc_files: []
19
47
  files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - README.md
51
+ - Rakefile
52
+ - bin/console
53
+ - bin/setup
20
54
  - lib/rack-perf.rb
21
55
  - lib/rack/perf.rb
22
56
  - lib/rack/perf/version.rb
57
+ - rack-perf.gemspec
23
58
  homepage: https://github.com/perflabs/rack-perf
24
- licenses:
25
- - GPL-3.0
59
+ licenses: []
26
60
  metadata: {}
27
61
  post_install_message:
28
62
  rdoc_options: []
@@ -43,5 +77,5 @@ rubyforge_project:
43
77
  rubygems_version: 2.5.1
44
78
  signing_key:
45
79
  specification_version: 4
46
- summary: Rack middleware that records endpoint timing and status codes to Perf
80
+ summary: Perf middleware
47
81
  test_files: []