Skip to content

Commit 1336a1f

Browse files
author
Philip (flip) Kromer
committed
two tweaks to tracer middleware: a) trace stats are sorted to read like a timeline; b) takes an initialize arg giving the header param to use (still 'X-Postrank' by default)
1 parent 2535a82 commit 1336a1f

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

‎lib/goliath/env.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def trace(name)
4949
#
5050
# @return [Array] Array of [name, time] pairs with a Total entry added.
5151
def trace_stats
52-
self[:trace] + [['total', self[:trace].collect { |s| s[1].to_f }.inject(:+).to_s]]
52+
self[:trace].sort_by(&:last) + [['total', self[:trace].collect { |s| s[1].to_f }.inject(:+).to_s]]
5353
end
5454

5555
# If the API is a streaming API this will send the provided data to the client.

‎lib/goliath/rack/tracer.rb‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ module Rack
88
class Tracer
99
include Goliath::Rack::AsyncMiddleware
1010

11+
def initialize(app, header_name=nil)
12+
super(app)
13+
@header_name = header_name || 'X-PostRank'
14+
end
15+
1116
def call(env)
1217
env.trace 'trace.start'
1318
shb = super(env)
@@ -16,7 +21,8 @@ def call(env)
1621
end
1722

1823
def post_process(env, status, headers, body)
19-
extra = { 'X-PostRank' => env.trace_stats.collect{|s| s.join(': ')}.join(', ')}
24+
extra = { @header_name => env.trace_stats.collect{|s| s.join(': ')}.join(', ')}
25+
env.logger.info env.trace_stats.collect{|s| s.join(':')}.join(', ')
2026
[status, headers.merge(extra), body]
2127
end
2228
end

0 commit comments

Comments
 (0)