sinatra-template_metrics 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2c7e73de0572c54c0ef012af529fc6ef3662bfdc
4
+ data.tar.gz: 2837eade7f9343e1d13d50caa50b4c7ed07cf959
5
+ SHA512:
6
+ metadata.gz: bde76776ae623c3aed5dd1960bb0702a355d7efde2c637f985b117c9f4bbbcaf62020093d2abf99447454003ac4a423b01ca0bbc2a609d661eacb3629e94b2c3
7
+ data.tar.gz: 0a21aea3850d0193509d2132420b0c1e73cfadf85cc4ce6392c8813005ad260e419b6162d0873536b102f95233b2389383aed5578b13856c39d1df695dd332b1
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sinatra-metrics.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 sonots
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Sinatra::TemplateMetrics
2
+
3
+ Instrument sinatra template rendering
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'sinatra-template_metrics'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install sinatra-metrics
20
+
21
+ ## Usage
22
+
23
+ Just require `sinatra/template_metrics` in addition to `sinatra/base` as:
24
+
25
+ ```ruby
26
+ require 'sinatra/base'
27
+ require 'sinatra-template_metrics'
28
+
29
+ class App < Sinatra::Base
30
+ get '/' do
31
+ erb :index, layout: :base
32
+ end
33
+ end
34
+ ```
35
+
36
+ This will show you the measure time to render erb as:
37
+
38
+ ```
39
+ time:2014-11-02T02:39:50+09:00 template:index layout:base elapsed:0.002839445
40
+ ```
41
+
42
+ where `elapsed` is the elapsed time in seconds.
43
+
44
+ ## See also
45
+
46
+ * [rack-ltsv_logger](https://github.com/sonots/rack-ltsv_logger) (or `enable :logging`) is useful to instrument http request.
47
+ * [mysql2-metrics](https://github.com/sonots/mysql2-metrics) is useful to instrument mysql2 queries.
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it ( https://github.com/sonots/sinatra-metrics/fork )
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/example/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "sinatra"
4
+ gem "unicorn"
5
+ gem "foreman"
6
+ gem "sinatra-template_metrics", path: '../'
7
+ gem 'pry'
8
+ gem 'pry-nav'
@@ -0,0 +1,50 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ sinatra-template_metrics (0.0.1)
5
+ sinatra
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ coderay (1.1.0)
11
+ dotenv (0.11.1)
12
+ dotenv-deployment (~> 0.0.2)
13
+ dotenv-deployment (0.0.2)
14
+ foreman (0.75.0)
15
+ dotenv (~> 0.11.1)
16
+ thor (~> 0.19.1)
17
+ kgio (2.9.2)
18
+ method_source (0.8.2)
19
+ pry (0.10.1)
20
+ coderay (~> 1.1.0)
21
+ method_source (~> 0.8.1)
22
+ slop (~> 3.4)
23
+ pry-nav (0.2.4)
24
+ pry (>= 0.9.10, < 0.11.0)
25
+ rack (1.5.2)
26
+ rack-protection (1.5.3)
27
+ rack
28
+ raindrops (0.13.0)
29
+ sinatra (1.4.5)
30
+ rack (~> 1.4)
31
+ rack-protection (~> 1.4)
32
+ tilt (~> 1.3, >= 1.3.4)
33
+ slop (3.6.0)
34
+ thor (0.19.1)
35
+ tilt (1.4.1)
36
+ unicorn (4.8.3)
37
+ kgio (~> 2.6)
38
+ rack
39
+ raindrops (~> 0.7)
40
+
41
+ PLATFORMS
42
+ ruby
43
+
44
+ DEPENDENCIES
45
+ foreman
46
+ pry
47
+ pry-nav
48
+ sinatra
49
+ sinatra-template_metrics!
50
+ unicorn
data/example/Procfile ADDED
@@ -0,0 +1 @@
1
+ unicorn: bundle exec unicorn -c unicorn_config.rb -p 8080
data/example/app.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'sinatra/base'
2
+ require 'sinatra/template_metrics'
3
+
4
+ class App < Sinatra::Base
5
+ get '/' do
6
+ erb :index, layout: :base
7
+ end
8
+ end
data/example/config.ru ADDED
@@ -0,0 +1,3 @@
1
+ require_relative './app.rb'
2
+
3
+ run App
@@ -0,0 +1,2 @@
1
+ worker_processes 1
2
+ preload_app true
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <% title ||= 'test' %>
3
+ <html>
4
+ <head>
5
+ <meta charset="UTF-8">
6
+ <link rel="stylesheet" href="/stylesheets/bootstrap.min.css">
7
+ <link rel="stylesheet" href="/stylesheets/bootflat.min.css">
8
+ <link rel="stylesheet" href="/stylesheets/isucon-bank.css">
9
+ <title><%= title %></title>
10
+ </head>
11
+ <body>
12
+ <%= yield %>
13
+ </body>
14
+ </html>
@@ -0,0 +1,7 @@
1
+ <div class="page-header">
2
+ <h1>Yay!</h1>
3
+ </div>
4
+
5
+ <div class="container">
6
+ hello world
7
+ </div>
@@ -0,0 +1 @@
1
+ require 'sinatra/template_metrics'
@@ -0,0 +1,60 @@
1
+ require 'sinatra/template_metrics/ltsv_formatter'
2
+ require 'logger'
3
+
4
+ module Sinatra
5
+ class TemplateMetrics
6
+ def self.logger=(logger)
7
+ @logger = logger
8
+ end
9
+
10
+ def self.logger
11
+ @logger ||= Logger.new(STDOUT).tap {|log|
12
+ log.formatter = ::Sinatra::Metrics::LtsvFormatter.new
13
+ }
14
+ end
15
+
16
+ def self.add_tracer(method)
17
+ klass = ::Sinatra::Templates
18
+ unless klass.method_defined?("#{method}_without_metrics")
19
+ klass.__send__(:alias_method, "#{method}_without_metrics", method)
20
+ klass.__send__(:define_method, "#{method}_with_metrics") do |template, options = {}, locals = {}, &block|
21
+ ::Sinatra::TemplateMetrics.measure_time(template, options[:layout]) do
22
+ send("#{method}_without_metrics", template, options, locals, &block)
23
+ end
24
+ end
25
+ klass.__send__(:alias_method, method, "#{method}_with_metrics")
26
+ end
27
+ end
28
+
29
+ def self.measure_time(template, layout, &block)
30
+ start = Time.now
31
+ ret = yield
32
+ elapsed = Time.now - start
33
+ logger = Sinatra::TemplateMetrics.logger
34
+ logger.info({ template: template, layout: layout, elapsed: elapsed.to_f })
35
+ ret
36
+ end
37
+ end
38
+ end
39
+
40
+ # open class
41
+ module Sinatra
42
+ module Templates
43
+ # add_tracer :render was not good because it is called for layout also
44
+ ::Sinatra::TemplateMetrics.add_tracer :erb
45
+ ::Sinatra::TemplateMetrics.add_tracer :erubis
46
+ ::Sinatra::TemplateMetrics.add_tracer :haml
47
+ ::Sinatra::TemplateMetrics.add_tracer :sass
48
+ ::Sinatra::TemplateMetrics.add_tracer :scss
49
+ ::Sinatra::TemplateMetrics.add_tracer :less
50
+ ::Sinatra::TemplateMetrics.add_tracer :stylus
51
+ ::Sinatra::TemplateMetrics.add_tracer :less
52
+ ::Sinatra::TemplateMetrics.add_tracer :builder
53
+ ::Sinatra::TemplateMetrics.add_tracer :liquid
54
+ ::Sinatra::TemplateMetrics.add_tracer :markdown
55
+ ::Sinatra::TemplateMetrics.add_tracer :textile
56
+ ::Sinatra::TemplateMetrics.add_tracer :rdoc
57
+ ::Sinatra::TemplateMetrics.add_tracer :slim
58
+ end
59
+ end
60
+
@@ -0,0 +1,15 @@
1
+ module Sinatra
2
+ class Metrics
3
+ class LtsvFormatter
4
+ def call(severity, datetime, progname, msg)
5
+ "time:#{datetime.iso8601}\t#{format_msg(msg)}\n"
6
+ end
7
+
8
+ private
9
+
10
+ def format_msg(msg)
11
+ msg.map {|k, v| "#{k}:#{v}" }.join("\t")
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "sinatra-template_metrics"
3
+ spec.version = "0.0.1"
4
+ spec.authors = ["sonots"]
5
+ spec.email = ["sonots@gmail.com"]
6
+ spec.summary = %q{Instrument sinatra template rendering}
7
+ spec.description = %q{Instrument sinatra template rendering}
8
+ spec.homepage = ""
9
+ spec.license = "MIT"
10
+
11
+ spec.files = `git ls-files -z`.split("\x0")
12
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
13
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
14
+ spec.require_paths = ["lib"]
15
+
16
+ spec.add_runtime_dependency "sinatra"
17
+ spec.add_development_dependency "bundler", "~> 1.7"
18
+ spec.add_development_dependency "rake", "~> 10.0"
19
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-template_metrics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - sonots
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sinatra
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Instrument sinatra template rendering
56
+ email:
57
+ - sonots@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - example/Gemfile
68
+ - example/Gemfile.lock
69
+ - example/Procfile
70
+ - example/app.rb
71
+ - example/config.ru
72
+ - example/unicorn_config.rb
73
+ - example/views/base.erb
74
+ - example/views/index.erb
75
+ - lib/sinatra-template_metrics.rb
76
+ - lib/sinatra/template_metrics.rb
77
+ - lib/sinatra/template_metrics/ltsv_formatter.rb
78
+ - sinatra-template_metrics.gemspec
79
+ homepage: ''
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.2.2
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Instrument sinatra template rendering
103
+ test_files: []