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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ec7b0e1bec24e125a1e936ef05bf9388011ce24
4
- data.tar.gz: 769e62995b294916c2669daa8ce25f798f39719e
3
+ metadata.gz: cf99b600497254e308fd74362017f96bdd508a14
4
+ data.tar.gz: 4d4e0096db8236d9d7bf46e024e5e9bcce8934dc
5
5
  SHA512:
6
- metadata.gz: 9f039c8146fd49432ab140bf580295efa99bdc072a6c1ccd313f019e0d22b008fb2be285949ba59cc17c68e9b7b6d8e8383789530f02cedd487ad5606da8ea5f
7
- data.tar.gz: 51022a32b1e6e208a6a26cea8ce41e767dbb25d2466cc06c795fc285f0a2c15e87bc1065ccef539137c8b9f6f88c1536504a0aacb7ce634cdb01cc8a908e9ff3
6
+ metadata.gz: be6282053487a8a35044f941af1780930e317a3cf0a19432941774f4558f5e9293fd081a8232cf9ff62eab0b732d26cad6eabe1a3081081a8f68cac69777c003
7
+ data.tar.gz: 05031e19f2cbfb2baa6eab0c21d734c253050ff3ee9f109f2a229e8e1200a149423a1a4974a80b05c9b490122d470bebe07044a7243516d5fa164fdb23ad19d7
data/README.md CHANGED
@@ -1,8 +1,12 @@
1
- # Server Timing Response Headers for Rails & Rack Apps
1
+ # Server Timing Response Headers for Rails
2
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.
3
+ Bring Ruby on Rails server-side performance metrics 📈 to Chrome's Developer Tools via the `server_timing` gem.
4
4
 
5
- ![server timing screenshot](https://s3-us-west-1.amazonaws.com/scout-blog/ruby_server_timing.png)
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
- ### 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`).
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
- ### 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.
41
+ ## Browser Support
51
42
 
52
- ## Usage
43
+ As of this writing, [Chrome Canary](https://www.google.com/chrome/browser/canary.html) Versions 66+ display this information.
53
44
 
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.
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
- #### Custom Instrumentation
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
- ### Security
55
+ ## Security
67
56
 
68
- #### Ruby on Rails
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
- 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!`:
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
- #### Rack
72
+ To only enable response headers in development and for admins in production:
83
73
 
84
- Headers are always sent. To toggle:
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
- ServerTiming::Auth.ok! # enables on this and all future requests
88
- ServerTiming::Auth.deny! # disables on this and all future requests
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"
@@ -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.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.
4
+ def self.ok!
5
+ self.state=true
6
+ end
8
7
 
9
- false
8
+ def self.deny!
9
+ self.state=false
10
10
  end
11
11
 
12
- def self.ok!
13
- Thread.current[:server_timing_authorized] = true
12
+ def self.reset!
13
+ self.state=nil
14
14
  end
15
15
 
16
- def self.deny!
17
- Thread.current[:server_timing_authorized] = nil
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
- return true unless required?
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("ServerTiming: Raised an exception: #{e.message}, #{e.backtrace}")
13
+ puts "ServerTiming raised an exception: #{e.message}, #{e.backtrace}"
14
14
  rack_response
15
15
  ensure
16
- ServerTiming::Auth.deny!
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)
@@ -1,3 +1,3 @@
1
1
  module ServerTiming
2
- VERSION = "0.1.0.pre"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -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 (ActiveRecord, external HTTP calls, Redis, etc) in your browser developer tools via server timing response headers.}
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: 0.1.0.pre
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-02 00:00:00.000000000 Z
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: 1.3.1
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 (ActiveRecord, external HTTP calls,
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: