server_timing 0.1.0.pre
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 +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +105 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/server_timing.rb +10 -0
- data/lib/server_timing/auth.rb +30 -0
- data/lib/server_timing/middleware.rb +21 -0
- data/lib/server_timing/railtie.rb +7 -0
- data/lib/server_timing/response_manipulator.rb +68 -0
- data/lib/server_timing/store.rb +12 -0
- data/lib/server_timing/timing_metric.rb +29 -0
- data/lib/server_timing/version.rb +3 -0
- data/server_timing.gemspec +25 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2ec7b0e1bec24e125a1e936ef05bf9388011ce24
|
4
|
+
data.tar.gz: 769e62995b294916c2669daa8ce25f798f39719e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9f039c8146fd49432ab140bf580295efa99bdc072a6c1ccd313f019e0d22b008fb2be285949ba59cc17c68e9b7b6d8e8383789530f02cedd487ad5606da8ea5f
|
7
|
+
data.tar.gz: 51022a32b1e6e208a6a26cea8ce41e767dbb25d2466cc06c795fc285f0a2c15e87bc1065ccef539137c8b9f6f88c1536504a0aacb7ce634cdb01cc8a908e9ff3
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Derek Haynes
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
# Server Timing Response Headers for Rails & Rack Apps
|
2
|
+
|
3
|
+
The `server_timing` gem brings server-side performance metrics collected by the [scout_apm gem](https://github.com/scoutapp/scout_apm_ruby) to your browser via the [Server Timing](https://w3c.github.io/server-timing/) API. Works on any Ruby framework supported by `scout_apm`. A [Scout](https://scoutapp.com) account is not required.
|
4
|
+
|
5
|
+

|
6
|
+
|
7
|
+
## Gem Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'server_timing'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
## Configuration
|
20
|
+
|
21
|
+
### Ruby on Rails
|
22
|
+
|
23
|
+
For Rails apps, the only required configuration step is setting up a minimal Scout config file. The `server_timing` gem reports metrics collected by the [scout_apm](https://github.com/scoutapp/scout_apm_ruby) gem (added as a dependency of `server_timing`).
|
24
|
+
|
25
|
+
If you don't have a Scout account, copy and paste the following minimal configuration into a `RAILS_ROOT/config/scout_apm.yml` file:
|
26
|
+
|
27
|
+
```yaml
|
28
|
+
common: &defaults
|
29
|
+
monitor: true
|
30
|
+
|
31
|
+
production:
|
32
|
+
<<: *defaults
|
33
|
+
```
|
34
|
+
|
35
|
+
If you have a Scout account, no extra configuration is required. If you wish to see server timing metrics in development, ensure `monitor: true` is set for the `development` environment in the `scout_apm.yml` file.
|
36
|
+
|
37
|
+
[See the scout_apm configuration reference](http://help.apm.scoutapp.com/#ruby-configuration-options) for more information.
|
38
|
+
|
39
|
+
### Rack
|
40
|
+
|
41
|
+
Use the `ServerTiming::Middleware`:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
# config.ru
|
45
|
+
require 'server_timing'
|
46
|
+
use ServerTiming::Middleware
|
47
|
+
```
|
48
|
+
|
49
|
+
* Add the minimal Scout config above to `APP_ROOT/scout_apm.yml`.
|
50
|
+
* Scout requires additional steps to instrument Rack applications. [See the guide](http://help.apm.scoutapp.com/#rack). Without these steps, the instrumentation will not execute and server timing metrics will not be reported.
|
51
|
+
|
52
|
+
## Usage
|
53
|
+
|
54
|
+
The `server_timing` gem exposes server-side performance metrics collected by the `scout_apm` gem inside your browser. As of this writing, [Chrome Canary](https://www.google.com/chrome/browser/canary.html) Versions 66+ display this information.
|
55
|
+
|
56
|
+
### Instrumentation
|
57
|
+
|
58
|
+
#### Auto-Instrumentation
|
59
|
+
|
60
|
+
By default, the total time consumed by each of the libraries `scout_apm` instruments is reported. This includes ActiveRecord, HTTP, Redis, and more. [View the full list of supported libraries](http://help.apm.scoutapp.com/#ruby-instrumented-libs).
|
61
|
+
|
62
|
+
#### Custom Instrumentation
|
63
|
+
|
64
|
+
Collect performance data on additional method calls by adding custom instrumentation via `scout_apm`. [See the docs for instructions](http://help.apm.scoutapp.com/#ruby-custom-instrumentation).
|
65
|
+
|
66
|
+
### Security
|
67
|
+
|
68
|
+
#### Ruby on Rails
|
69
|
+
|
70
|
+
By default, server timing response times are sent in non-development environments. In production, __the headers must be explicitly enabled__ by calling `ServerTiming::Auth.ok!`:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
# app/controllers/application_controller.rb
|
74
|
+
|
75
|
+
before_action do
|
76
|
+
if current_user && current_user.admin?
|
77
|
+
ServerTiming::Auth.ok!
|
78
|
+
end
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
#### Rack
|
83
|
+
|
84
|
+
Headers are always sent. To toggle:
|
85
|
+
|
86
|
+
```
|
87
|
+
ServerTiming::Auth.ok! # enables on this and all future requests
|
88
|
+
ServerTiming::Auth.deny! # disables on this and all future requests
|
89
|
+
```
|
90
|
+
|
91
|
+
## Development
|
92
|
+
|
93
|
+
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.
|
94
|
+
|
95
|
+
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).
|
96
|
+
|
97
|
+
## Contributing
|
98
|
+
|
99
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/scoutapp/ruby_server_timing.
|
100
|
+
|
101
|
+
|
102
|
+
## License
|
103
|
+
|
104
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
105
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "server_timing"
|
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,10 @@
|
|
1
|
+
module ServerTiming
|
2
|
+
end
|
3
|
+
|
4
|
+
require "server_timing/auth"
|
5
|
+
require "server_timing/middleware"
|
6
|
+
require "server_timing/railtie"
|
7
|
+
require "server_timing/response_manipulator"
|
8
|
+
require "server_timing/store"
|
9
|
+
require "server_timing/timing_metric"
|
10
|
+
require "server_timing/version"
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ServerTiming
|
2
|
+
# Encapsulates logic that determines whether the user is properly authorized to view server timing response headers.
|
3
|
+
class Auth
|
4
|
+
def self.required?
|
5
|
+
return false unless defined? Rails::Railtie # will send headers by default for Rack apps
|
6
|
+
|
7
|
+
return true if Rails.env.production? # Requires a call to `ServerTiming::Auth.ok!` for Rails apps in the production environment.
|
8
|
+
|
9
|
+
false
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.ok!
|
13
|
+
Thread.current[:server_timing_authorized] = true
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.deny!
|
17
|
+
Thread.current[:server_timing_authorized] = nil
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.state
|
21
|
+
Thread.current[:server_timing_authorized]
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.permitted?
|
25
|
+
return true unless required?
|
26
|
+
|
27
|
+
state
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module ServerTiming
|
2
|
+
class Middleware
|
3
|
+
def initialize(app)
|
4
|
+
@app = app
|
5
|
+
end
|
6
|
+
|
7
|
+
def call(env)
|
8
|
+
rack_response = @app.call(env)
|
9
|
+
begin
|
10
|
+
ResponseManipulator.new(env, rack_response).call
|
11
|
+
rescue Exception => e
|
12
|
+
# If anything went wrong at all, just bail out and return the unmodified response.
|
13
|
+
puts("ServerTiming: Raised an exception: #{e.message}, #{e.backtrace}")
|
14
|
+
rack_response
|
15
|
+
ensure
|
16
|
+
ServerTiming::Auth.deny!
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module ServerTiming
|
2
|
+
class ResponseManipulator
|
3
|
+
attr_reader :rack_response
|
4
|
+
attr_reader :rack_status, :rack_headers, :rack_body
|
5
|
+
attr_reader :env
|
6
|
+
|
7
|
+
def initialize(env, rack_response)
|
8
|
+
@env = env
|
9
|
+
@rack_response = rack_response
|
10
|
+
|
11
|
+
@rack_status = rack_response[0]
|
12
|
+
@rack_headers = rack_response[1]
|
13
|
+
@rack_body = rack_response[2]
|
14
|
+
end
|
15
|
+
|
16
|
+
def call
|
17
|
+
return rack_response unless preconditions_met?
|
18
|
+
|
19
|
+
store_metrics
|
20
|
+
add_header
|
21
|
+
rebuild_rack_response
|
22
|
+
end
|
23
|
+
|
24
|
+
def preconditions_met?
|
25
|
+
Auth.permitted?
|
26
|
+
end
|
27
|
+
|
28
|
+
def add_header
|
29
|
+
rack_headers['Server-Timing'] = payload
|
30
|
+
end
|
31
|
+
|
32
|
+
def tracked_request
|
33
|
+
@tracked_request ||= ScoutApm::RequestManager.lookup
|
34
|
+
end
|
35
|
+
|
36
|
+
def store
|
37
|
+
@store ||= ServerTiming::Store.new
|
38
|
+
end
|
39
|
+
|
40
|
+
def store_metrics
|
41
|
+
layer_finder = ScoutApm::LayerConverters::FindLayerByType.new(tracked_request)
|
42
|
+
converters = [ScoutApm::LayerConverters::MetricConverter]
|
43
|
+
walker = ScoutApm::LayerConverters::DepthFirstWalker.new(tracked_request.root_layer)
|
44
|
+
converters = converters.map do |klass|
|
45
|
+
instance = klass.new(ScoutApm::Agent.instance.context, tracked_request, layer_finder, store)
|
46
|
+
instance.register_hooks(walker)
|
47
|
+
instance
|
48
|
+
end
|
49
|
+
walker.walk
|
50
|
+
converters.each {|i| i.record! }
|
51
|
+
end
|
52
|
+
|
53
|
+
def server_timing_metrics
|
54
|
+
@server_timing_metrics ||= store.metrics.map { |meta, stats| TimingMetric.from_scout(meta,stats)}
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
def payload
|
59
|
+
headers = server_timing_metrics.map(&:to_header)
|
60
|
+
headers << TimingMetric.new('Total', server_timing_metrics.map(&:duration).reduce(0,:+)).to_header
|
61
|
+
headers.join(",")
|
62
|
+
end
|
63
|
+
|
64
|
+
def rebuild_rack_response
|
65
|
+
[rack_status, rack_headers, rack_body]
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module ServerTiming
|
2
|
+
# Encapsulates a single metric that should be sent inside the server timing response header.
|
3
|
+
class TimingMetric
|
4
|
+
attr_reader :name
|
5
|
+
attr_reader :duration
|
6
|
+
attr_reader :description
|
7
|
+
|
8
|
+
def self.from_scout(meta,stats)
|
9
|
+
name = meta.type
|
10
|
+
duration = stats.total_exclusive_time*1000
|
11
|
+
new(name, duration)
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(name,duration, description: nil)
|
15
|
+
@name = name
|
16
|
+
@duration = duration
|
17
|
+
@description = description
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_header
|
21
|
+
"#{name}; dur=#{duration.to_d.truncate(2).to_f}; #{description_to_header}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def description_to_header
|
25
|
+
return unless description
|
26
|
+
"desc='#{description}';"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -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 'server_timing/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "server_timing"
|
8
|
+
spec.version = ServerTiming::VERSION
|
9
|
+
spec.authors = ["Derek Haynes"]
|
10
|
+
spec.email = ["derek.haynes@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{View server-side performance metrics (ActiveRecord, external HTTP calls, Redis, etc) in your browser developer tools via server timing response headers.}
|
13
|
+
spec.homepage = "https://github.com/scoutapp/ruby_server_timing"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
|
24
|
+
spec.add_runtime_dependency "scout_apm"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: server_timing
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0.pre
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Derek Haynes
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-02 00:00:00.000000000 Z
|
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.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
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'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: scout_apm
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- derek.haynes@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
|
+
- bin/console
|
68
|
+
- bin/setup
|
69
|
+
- lib/server_timing.rb
|
70
|
+
- lib/server_timing/auth.rb
|
71
|
+
- lib/server_timing/middleware.rb
|
72
|
+
- lib/server_timing/railtie.rb
|
73
|
+
- lib/server_timing/response_manipulator.rb
|
74
|
+
- lib/server_timing/store.rb
|
75
|
+
- lib/server_timing/timing_metric.rb
|
76
|
+
- lib/server_timing/version.rb
|
77
|
+
- server_timing.gemspec
|
78
|
+
homepage: https://github.com/scoutapp/ruby_server_timing
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata: {}
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: 1.3.1
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.4.6
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: View server-side performance metrics (ActiveRecord, external HTTP calls,
|
102
|
+
Redis, etc) in your browser developer tools via server timing response headers.
|
103
|
+
test_files: []
|
104
|
+
has_rdoc:
|