Skip to content

Commit

Permalink
Merge pull request #52 from ifad/bugfix/fix-frozen-string-literal-sup…
Browse files Browse the repository at this point in the history
…port

Fix instrumentation log
  • Loading branch information
tagliala authored Jan 22, 2024
2 parents 597571e + 116a838 commit fb9652d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
6 changes: 3 additions & 3 deletions .rubocop_todo.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 12 additions & 7 deletions lib/hawk/http/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def self.suppress_verbose_output(value = nil)
end

module Basic
LOG_FORMAT = ">> \033[1mHawk %<type>s: %<method>s %<url>s (%<elapsed>.2fms), cache %<cached>s\033[0m\n"

def instrument(type, payload)
if Hawk::HTTP::Instrumentation.suppress_verbose_output
yield payload
Expand All @@ -31,16 +33,19 @@ def instrument(type, payload)
ret = yield payload
elapsed = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) * 1000

url = payload[:url].to_s
url = payload[:url].to_s.dup
if payload[:params].present?
url << '?' << payload[:params].inject('') { |s, (k, v)| s << [k, '=', v, '&'].join }.chomp('&')
url << '?' << payload[:params].map { |k, v| "#{k}=#{v}" }.join('&')
end

$stderr.printf ">> \033[1mHawk #{type}: #{payload[:method]} %s (%.2fms), cache %s\033[0m\n" % [
CGI.unescape(url),
elapsed,
payload[:cached] ? 'HIT' : 'MISS'
]
$stderr.printf format(
LOG_FORMAT,
type: type,
method: payload[:method],
url: CGI.unescape(url),
elapsed: elapsed,
cached: payload[:cached] ? 'HIT' : 'MISS'
)

ret
end
Expand Down

0 comments on commit fb9652d

Please sign in to comment.