rack_middleware_metrics 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'thin' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("thin", "thin")
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'thor' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("thor", "thor")
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'tilt' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("tilt", "tilt")
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'yard' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("yard", "yard")
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'yardoc' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("yard", "yardoc")
data/bin/yri ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'yri' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("yard", "yri")
File without changes
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ # examples/thin_rack_app.rb
4
+ #
5
+ # Just a simple example of running with thin.
6
+ require 'lib/rack_middleware_metrics'
7
+ require 'rack'
8
+
9
+ # A simple rack app for example.
10
+ class RackApp
11
+ def self.call _env
12
+ [200, { some_header: 'a value' }, ['Hi!']]
13
+ end
14
+ end
15
+
16
+ app =
17
+ Rack::Builder.app do
18
+ use(RackMiddlewareMetrics::Reporter, logpath: 'log2.csv')
19
+ run(RackApp)
20
+ end
21
+
22
+ handler = Rack::Handler::Thin
23
+ handler.run(app, Port: 8082)
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ # lib/md5_gen.rb
4
+ #
5
+ # Provide md5 compute function that either uses native extention or Ruby digest lib.
6
+ require 'helix_runtime'
7
+ require('digest/md5')
8
+
9
+ module MD5Gen
10
+ begin
11
+ require('md5_ruby_ext/native')
12
+ def MD5Gen.compute body
13
+ MRubyExt.compute(body)
14
+ # Can only work on UTF-8 strings atm. Helix limitation.
15
+ rescue TypeError
16
+ Digest::MD5.hexdigest(body)
17
+ end
18
+ rescue LoadError
19
+ warn('Unable to load md5_ruby_ext/native. Falling back to Ruby digest/md5. ' \
20
+ 'Please run `rake build` to build native extension.')
21
+ def MD5Gen.compute body # rubocop:disable Lint/DuplicateMethods
22
+ Digest::MD5.hexdigest(body)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ # lib/md5_ruby_ext.rb
4
+ require 'helix_runtime'
5
+
6
+ begin
7
+ require('md5_ruby_ext/native')
8
+ rescue LoadError
9
+ warn('Unable to load md5_ruby_ext/native. Please run `rake build`... Requires Rust/cargo.')
10
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # lib/rack_middleware_metrics.rb
4
+ require 'rack_middleware_metrics/version'
5
+ require 'rack_middleware_metrics/reporter'
6
+
7
+ require 'rack_middleware_metrics/railtie' if defined?(Rails)
8
+
9
+ module RackMiddlewareMetrics
10
+ class Error < StandardError; end
11
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ # lib/rack_middleware_metrics/railtie.rb
4
+ module RackMiddlewareMetrics
5
+ # Rails init...
6
+ class Railtie < Rails::Railtie
7
+ config.rack_middleware_metrics = ActiveSupport::OrderedOptions.new
8
+ initializer 'rack_middleware_metrics.configure_rails_initialization' do |app|
9
+ config.rack_middleware_metrics[:logpath] =
10
+ config.rack_middleware_metrics.fetch(:logpath, Rails.root / 'rack_metrics.csv')
11
+ app.middleware.use(Reporter, config.rack_middleware_metrics)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ # lib/rack_middleware_metrics/reporter.rb
4
+ #
5
+ # A minimal rack middleware metrics reporter.
6
+ require 'time'
7
+ require 'rack'
8
+ require 'md5_gen'
9
+
10
+ # The main Gem...
11
+ module RackMiddlewareMetrics
12
+ # The metrics reporter middleware.
13
+ class Reporter
14
+ def initialize app, *args
15
+ @app = app
16
+
17
+ # Rack `use` only allows *args. Use either logpath passed via hash or default path.
18
+ options = args.last.is_a?(Hash) ? args.last : {}
19
+ @logpath = Pathname.new(options.fetch(:logpath, 'rack_metrics.csv'))
20
+ end
21
+
22
+ def uri_from env
23
+ "#{ env['rack.url_scheme'] }://#{ env['SERVER_NAME'] }"\
24
+ ":#{ env['SERVER_PORT'] }#{ env['PATH_INFO'] }"
25
+ end
26
+
27
+ def timed_call env
28
+ start_time = Time.now
29
+ response = @app.call(env)
30
+ end_time = Time.now
31
+ duration = start_time - end_time
32
+ [*response, [start_time, end_time, duration]]
33
+ end
34
+
35
+ def append_data data
36
+ logline = "#{ data.join(',') }\n"
37
+ @logpath.open(mode: 'a') { |logfile| logfile.write(logline) }
38
+ end
39
+
40
+ def call env
41
+ status, headers, body, timing = timed_call(env)
42
+
43
+ query_params = env['QUERY_STRING']
44
+ uri = uri_from(env)
45
+ thread_id = Thread.current.object_id
46
+ process_id = Process.pid
47
+ md5 = MD5Gen.compute(body.to_s)
48
+
49
+ append_data([*timing, uri, query_params, thread_id, process_id, md5])
50
+
51
+ [status, headers, body]
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ module RackMiddlewareMetrics
2
+ VERSION ||= "0.0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ # lib/tasks/helix_runtime.rake
2
+ require 'helix_runtime/build_task'
3
+
4
+ HelixRuntime::BuildTask.new()
5
+
6
+ task default: :build
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/rack_middleware_metrics/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'rack_middleware_metrics'
7
+ spec.version = RackMiddlewareMetrics::VERSION
8
+ spec.authors = ['reedjosh']
9
+ spec.email = ['reedjosh@oregonstate.edu']
10
+
11
+ spec.summary = 'A rack based middleware that does fun/silly metrics reporting.'
12
+ spec.homepage = 'https://github.com/reedjosh/RackMiddlewareMetrics'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = 'https://github.com/reedjosh/RackMiddlewareMetrics'
18
+ spec.metadata['changelog_uri'] = 'https://github.com/reedjosh/RackMiddlewareMetrics'
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files =
23
+ Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.add_dependency('helix_runtime', '~> 0.7.5')
27
+ spec.add_dependency('rack', '~> 2.2.2')
28
+ spec.add_dependency('rake', '~> 12.0')
29
+ spec.bindir = 'exe'
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ['lib']
32
+ end
@@ -0,0 +1,14 @@
1
+ #[macro_use]
2
+ extern crate helix;
3
+ extern crate md5;
4
+
5
+ ruby! {
6
+ class MRubyExt {
7
+ def compute(body: String) -> String {
8
+ let digest = md5::compute(body);
9
+ let ret = format!("{:x}", digest);
10
+ return ret;
11
+ }
12
+ }
13
+ }
14
+
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack_middleware_metrics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - reedjosh
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-10-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: helix_runtime
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.7.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.7.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.2.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.2.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
55
+ description:
56
+ email:
57
+ - reedjosh@oregonstate.edu
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".rubocop-performance.yml"
65
+ - ".rubocop-rspec.yml"
66
+ - ".rubocop.yml"
67
+ - ".solargraph.yml"
68
+ - ".travis.yml"
69
+ - ".~lock.GIST_API.odt#"
70
+ - Cargo.lock
71
+ - Cargo.toml
72
+ - Gemfile
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - bin/bundle
77
+ - bin/console
78
+ - bin/helix
79
+ - bin/htmldiff
80
+ - bin/ldiff
81
+ - bin/maruku
82
+ - bin/marutex
83
+ - bin/nokogiri
84
+ - bin/rackup
85
+ - bin/rake
86
+ - bin/reverse_markdown
87
+ - bin/rspec
88
+ - bin/rubocop
89
+ - bin/ruby-parse
90
+ - bin/ruby-rewrite
91
+ - bin/setup
92
+ - bin/solargraph
93
+ - bin/thin
94
+ - bin/thor
95
+ - bin/tilt
96
+ - bin/yard
97
+ - bin/yardoc
98
+ - bin/yri
99
+ - example/.keep
100
+ - example/thin_rack_app.rb
101
+ - lib/md5_gen.rb
102
+ - lib/md5_ruby_ext.rb
103
+ - lib/rack_middleware_metrics.rb
104
+ - lib/rack_middleware_metrics/railtie.rb
105
+ - lib/rack_middleware_metrics/reporter.rb
106
+ - lib/rack_middleware_metrics/version.rb
107
+ - lib/tasks/helix_runtime.rake
108
+ - rack_middleware_metrics.gemspec
109
+ - src/lib.rs
110
+ homepage: https://github.com/reedjosh/RackMiddlewareMetrics
111
+ licenses:
112
+ - MIT
113
+ metadata:
114
+ homepage_uri: https://github.com/reedjosh/RackMiddlewareMetrics
115
+ source_code_uri: https://github.com/reedjosh/RackMiddlewareMetrics
116
+ changelog_uri: https://github.com/reedjosh/RackMiddlewareMetrics
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: 2.5.0
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubygems_version: 3.0.1
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: A rack based middleware that does fun/silly metrics reporting.
136
+ test_files: []