Skip to content

Commit

Permalink
Fix instrumentation log
Browse files Browse the repository at this point in the history
- Extract log format to a constant
- Fix error when URL is a frozen string literal
- Use `map.join` instead of `inject.join.chomp`
- Use a formatted string
  • Loading branch information
tagliala committed Jan 22, 2024
1 parent 597571e commit 116a838
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 116a838

Please sign in to comment.