server_timing 0.1.0.pre → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +33 -32
- data/lib/server_timing.rb +4 -1
- data/lib/server_timing/auth.rb +27 -11
- data/lib/server_timing/middleware.rb +3 -2
- data/lib/server_timing/response_manipulator.rb +6 -0
- data/lib/server_timing/version.rb +1 -1
- data/server_timing.gemspec +1 -1
- metadata +5 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf99b600497254e308fd74362017f96bdd508a14
|
4
|
+
data.tar.gz: 4d4e0096db8236d9d7bf46e024e5e9bcce8934dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be6282053487a8a35044f941af1780930e317a3cf0a19432941774f4558f5e9293fd081a8232cf9ff62eab0b732d26cad6eabe1a3081081a8f68cac69777c003
|
7
|
+
data.tar.gz: 05031e19f2cbfb2baa6eab0c21d734c253050ff3ee9f109f2a229e8e1200a149423a1a4974a80b05c9b490122d470bebe07044a7243516d5fa164fdb23ad19d7
|
data/README.md
CHANGED
@@ -1,8 +1,12 @@
|
|
1
|
-
# Server Timing Response Headers for Rails
|
1
|
+
# Server Timing Response Headers for Rails
|
2
2
|
|
3
|
-
|
3
|
+
Bring Ruby on Rails server-side performance metrics 📈 to Chrome's Developer Tools via the `server_timing` gem.
|
4
4
|
|
5
|
-
|
5
|
+
`server_timing` sends server-side Rails app performance metrics collected from the [scout_apm](https://github.com/scoutapp/scout_apm_ruby) gem to the browser via the [Server Timing](https://w3c.github.io/server-timing/) API. Production-safe™.
|
6
|
+
|
7
|
+
A [Scout](https://scoutapp.com) account is not required.
|
8
|
+
|
9
|
+
![server timing screenshot](https://s3-us-west-1.amazonaws.com/scout-blog/ruby_server_timing.png?x)
|
6
10
|
|
7
11
|
## Gem Installation
|
8
12
|
|
@@ -18,9 +22,7 @@ And then execute:
|
|
18
22
|
|
19
23
|
## Configuration
|
20
24
|
|
21
|
-
|
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`).
|
25
|
+
A minimal Scout config file is required. 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
26
|
|
25
27
|
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
28
|
|
@@ -36,38 +38,26 @@ If you have a Scout account, no extra configuration is required. If you wish to
|
|
36
38
|
|
37
39
|
[See the scout_apm configuration reference](http://help.apm.scoutapp.com/#ruby-configuration-options) for more information.
|
38
40
|
|
39
|
-
|
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.
|
41
|
+
## Browser Support
|
51
42
|
|
52
|
-
|
43
|
+
As of this writing, [Chrome Canary](https://www.google.com/chrome/browser/canary.html) Versions 66+ display this information.
|
53
44
|
|
54
|
-
|
45
|
+
## Instrumentation
|
55
46
|
|
56
|
-
### Instrumentation
|
57
|
-
|
58
|
-
#### Auto-Instrumentation
|
47
|
+
### Auto-Instrumentation
|
59
48
|
|
60
49
|
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
50
|
|
62
|
-
|
51
|
+
### Custom Instrumentation
|
63
52
|
|
64
53
|
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
54
|
|
66
|
-
|
55
|
+
## Security
|
67
56
|
|
68
|
-
|
57
|
+
* Non-Production Environments (ex: development, staging) - Server timing response headers are sent by default.
|
58
|
+
* Production - The headers must be enabled.
|
69
59
|
|
70
|
-
|
60
|
+
Response headers can be enabled in production by calling `ServerTiming::Auth.ok!`:
|
71
61
|
|
72
62
|
```ruby
|
73
63
|
# app/controllers/application_controller.rb
|
@@ -79,14 +69,25 @@ before_action do
|
|
79
69
|
end
|
80
70
|
```
|
81
71
|
|
82
|
-
|
72
|
+
To only enable response headers in development and for admins in production:
|
83
73
|
|
84
|
-
|
74
|
+
```ruby
|
75
|
+
# app/controllers/application_controller.rb
|
85
76
|
|
77
|
+
before_action do
|
78
|
+
if current_user && current_user.admin?
|
79
|
+
ServerTiming::Auth.ok!
|
80
|
+
elsif Rails.env.development?
|
81
|
+
ServerTiming::Auth.ok!
|
82
|
+
else
|
83
|
+
ServerTiming::Auth.deny!
|
84
|
+
end
|
85
|
+
end
|
86
86
|
```
|
87
|
-
|
88
|
-
|
89
|
-
|
87
|
+
|
88
|
+
## Overhead
|
89
|
+
|
90
|
+
`scout_apm` is designed for use in production apps and is engineered for [low overhead](http://blog.scoutapp.com/articles/2016/02/07/overhead-benchmarks-new-relic-vs-scout) instrumentation.
|
90
91
|
|
91
92
|
## Development
|
92
93
|
|
data/lib/server_timing.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
module ServerTiming
|
2
|
+
def self.rails?
|
3
|
+
defined? Rails::Railtie
|
4
|
+
end
|
2
5
|
end
|
3
6
|
|
4
7
|
require "server_timing/auth"
|
5
8
|
require "server_timing/middleware"
|
6
|
-
require "server_timing/railtie"
|
9
|
+
require "server_timing/railtie" if ServerTiming.rails?
|
7
10
|
require "server_timing/response_manipulator"
|
8
11
|
require "server_timing/store"
|
9
12
|
require "server_timing/timing_metric"
|
data/lib/server_timing/auth.rb
CHANGED
@@ -1,30 +1,46 @@
|
|
1
1
|
module ServerTiming
|
2
2
|
# Encapsulates logic that determines whether the user is properly authorized to view server timing response headers.
|
3
3
|
class Auth
|
4
|
-
def self.
|
5
|
-
|
6
|
-
|
7
|
-
return true if Rails.env.production? # Requires a call to `ServerTiming::Auth.ok!` for Rails apps in the production environment.
|
4
|
+
def self.ok!
|
5
|
+
self.state=true
|
6
|
+
end
|
8
7
|
|
9
|
-
|
8
|
+
def self.deny!
|
9
|
+
self.state=false
|
10
10
|
end
|
11
11
|
|
12
|
-
def self.
|
13
|
-
|
12
|
+
def self.reset!
|
13
|
+
self.state=nil
|
14
14
|
end
|
15
15
|
|
16
|
-
def self.
|
17
|
-
Thread.current[:server_timing_authorized] =
|
16
|
+
def self.state=(new_state)
|
17
|
+
Thread.current[:server_timing_authorized] = new_state
|
18
18
|
end
|
19
19
|
|
20
|
+
# Can be one of three values:
|
21
|
+
# * true
|
22
|
+
# * false
|
23
|
+
# * nil (default)
|
20
24
|
def self.state
|
21
25
|
Thread.current[:server_timing_authorized]
|
22
26
|
end
|
23
27
|
|
24
28
|
def self.permitted?
|
25
|
-
|
29
|
+
if state
|
30
|
+
return true
|
31
|
+
elsif state.is_a?(FalseClass)
|
32
|
+
return false
|
33
|
+
else # implied access - state has not been set
|
34
|
+
# If not Rails, return true
|
35
|
+
return true if !ServerTiming.rails?
|
36
|
+
|
37
|
+
# If in a non-production environment, permit
|
38
|
+
return true if !Rails.env.production?
|
39
|
+
|
40
|
+
# In production, return false if no state has been set
|
41
|
+
return false if Rails.env.production?
|
42
|
+
end
|
26
43
|
|
27
|
-
state
|
28
44
|
end
|
29
45
|
end
|
30
46
|
end
|
@@ -10,10 +10,11 @@ module ServerTiming
|
|
10
10
|
ResponseManipulator.new(env, rack_response).call
|
11
11
|
rescue Exception => e
|
12
12
|
# If anything went wrong at all, just bail out and return the unmodified response.
|
13
|
-
puts
|
13
|
+
puts "ServerTiming raised an exception: #{e.message}, #{e.backtrace}"
|
14
14
|
rack_response
|
15
15
|
ensure
|
16
|
-
|
16
|
+
# Reset auth after each request
|
17
|
+
ServerTiming::Auth.reset!
|
17
18
|
end
|
18
19
|
|
19
20
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
module ServerTiming
|
2
|
+
# Adds the 'Server-Timing' response header w/metrics from Scout.
|
2
3
|
class ResponseManipulator
|
3
4
|
attr_reader :rack_response
|
4
5
|
attr_reader :rack_status, :rack_headers, :rack_body
|
@@ -21,7 +22,11 @@ module ServerTiming
|
|
21
22
|
rebuild_rack_response
|
22
23
|
end
|
23
24
|
|
25
|
+
# Checks if we should attempt to gather metrics.
|
24
26
|
def preconditions_met?
|
27
|
+
# tracked_request.root_layer is nil for Rack apps ... unsure why.
|
28
|
+
return false unless tracked_request.root_layer
|
29
|
+
|
25
30
|
Auth.permitted?
|
26
31
|
end
|
27
32
|
|
@@ -40,6 +45,7 @@ module ServerTiming
|
|
40
45
|
def store_metrics
|
41
46
|
layer_finder = ScoutApm::LayerConverters::FindLayerByType.new(tracked_request)
|
42
47
|
converters = [ScoutApm::LayerConverters::MetricConverter]
|
48
|
+
|
43
49
|
walker = ScoutApm::LayerConverters::DepthFirstWalker.new(tracked_request.root_layer)
|
44
50
|
converters = converters.map do |klass|
|
45
51
|
instance = klass.new(ScoutApm::Agent.instance.context, tracked_request, layer_finder, store)
|
data/server_timing.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Derek Haynes"]
|
10
10
|
spec.email = ["derek.haynes@gmail.com"]
|
11
11
|
|
12
|
-
spec.summary = %q{View server-side performance metrics
|
12
|
+
spec.summary = %q{View server-side performance metrics in your browser.}
|
13
13
|
spec.homepage = "https://github.com/scoutapp/ruby_server_timing"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: server_timing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Derek Haynes
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-03-
|
11
|
+
date: 2018-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -90,15 +90,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
90
90
|
version: '0'
|
91
91
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
92
|
requirements:
|
93
|
-
- - "
|
93
|
+
- - ">="
|
94
94
|
- !ruby/object:Gem::Version
|
95
|
-
version:
|
95
|
+
version: '0'
|
96
96
|
requirements: []
|
97
97
|
rubyforge_project:
|
98
98
|
rubygems_version: 2.4.6
|
99
99
|
signing_key:
|
100
100
|
specification_version: 4
|
101
|
-
summary: View server-side performance metrics
|
102
|
-
Redis, etc) in your browser developer tools via server timing response headers.
|
101
|
+
summary: View server-side performance metrics in your browser.
|
103
102
|
test_files: []
|
104
103
|
has_rdoc:
|