Class: ServerTiming::ResponseManipulator
- Inherits:
-
Object
- Object
- ServerTiming::ResponseManipulator
- Defined in:
- lib/server_timing/response_manipulator.rb
Overview
Adds the ‘Server-Timing’ response header w/metrics from Scout.
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#rack_body ⇒ Object
readonly
Returns the value of attribute rack_body.
-
#rack_headers ⇒ Object
readonly
Returns the value of attribute rack_headers.
-
#rack_response ⇒ Object
readonly
Returns the value of attribute rack_response.
-
#rack_status ⇒ Object
readonly
Returns the value of attribute rack_status.
Instance Method Summary collapse
- #add_header ⇒ Object
- #call ⇒ Object
-
#initialize(env, rack_response) ⇒ ResponseManipulator
constructor
A new instance of ResponseManipulator.
- #payload ⇒ Object
-
#preconditions_met? ⇒ Boolean
Checks if we should attempt to gather metrics.
- #rebuild_rack_response ⇒ Object
- #server_timing_metrics ⇒ Object
- #store ⇒ Object
- #store_metrics ⇒ Object
- #tracked_request ⇒ Object
Constructor Details
#initialize(env, rack_response) ⇒ ResponseManipulator
Returns a new instance of ResponseManipulator.
8 9 10 11 12 13 14 15 |
# File 'lib/server_timing/response_manipulator.rb', line 8 def initialize(env, rack_response) @env = env @rack_response = rack_response @rack_status = rack_response[0] @rack_headers = rack_response[1] @rack_body = rack_response[2] end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
6 7 8 |
# File 'lib/server_timing/response_manipulator.rb', line 6 def env @env end |
#rack_body ⇒ Object (readonly)
Returns the value of attribute rack_body.
5 6 7 |
# File 'lib/server_timing/response_manipulator.rb', line 5 def rack_body @rack_body end |
#rack_headers ⇒ Object (readonly)
Returns the value of attribute rack_headers.
5 6 7 |
# File 'lib/server_timing/response_manipulator.rb', line 5 def rack_headers @rack_headers end |
#rack_response ⇒ Object (readonly)
Returns the value of attribute rack_response.
4 5 6 |
# File 'lib/server_timing/response_manipulator.rb', line 4 def rack_response @rack_response end |
#rack_status ⇒ Object (readonly)
Returns the value of attribute rack_status.
5 6 7 |
# File 'lib/server_timing/response_manipulator.rb', line 5 def rack_status @rack_status end |
Instance Method Details
#add_header ⇒ Object
33 34 35 |
# File 'lib/server_timing/response_manipulator.rb', line 33 def add_header rack_headers['Server-Timing'] = payload end |
#call ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/server_timing/response_manipulator.rb', line 17 def call return rack_response unless preconditions_met? store_metrics add_header rebuild_rack_response end |
#payload ⇒ Object
64 65 66 67 68 |
# File 'lib/server_timing/response_manipulator.rb', line 64 def payload headers = server_timing_metrics.map(&:to_header) headers << TimingMetric.new('Total', server_timing_metrics.map(&:duration).reduce(0,:+)).to_header headers.join(",") end |
#preconditions_met? ⇒ Boolean
Checks if we should attempt to gather metrics.
26 27 28 29 30 31 |
# File 'lib/server_timing/response_manipulator.rb', line 26 def preconditions_met? # tracked_request.root_layer is nil for Rack apps ... unsure why. return false unless tracked_request.root_layer Auth.permitted? end |
#rebuild_rack_response ⇒ Object
70 71 72 |
# File 'lib/server_timing/response_manipulator.rb', line 70 def rebuild_rack_response [rack_status, rack_headers, rack_body] end |
#server_timing_metrics ⇒ Object
59 60 61 |
# File 'lib/server_timing/response_manipulator.rb', line 59 def server_timing_metrics @server_timing_metrics ||= store.metrics.map { |, stats| TimingMetric.from_scout(,stats)} end |
#store ⇒ Object
41 42 43 |
# File 'lib/server_timing/response_manipulator.rb', line 41 def store @store ||= ServerTiming::Store.new end |
#store_metrics ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/server_timing/response_manipulator.rb', line 45 def store_metrics layer_finder = ScoutApm::LayerConverters::FindLayerByType.new(tracked_request) converters = [ScoutApm::LayerConverters::MetricConverter] walker = ScoutApm::LayerConverters::DepthFirstWalker.new(tracked_request.root_layer) converters = converters.map do |klass| instance = klass.new(ScoutApm::Agent.instance.context, tracked_request, layer_finder, store) instance.register_hooks(walker) instance end walker.walk converters.each {|i| i.record! } end |
#tracked_request ⇒ Object
37 38 39 |
# File 'lib/server_timing/response_manipulator.rb', line 37 def tracked_request @tracked_request ||= ScoutApm::RequestManager.lookup end |