Skip to content

Commit

Permalink
Update instrumentation to be compatible with Rails 7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
inkstak committed Aug 21, 2024
1 parent a0824bc commit 87e9e13
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions lib/caoutsearch/instrumentation/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def log_request(subject, event, format: nil)
request = payload[:request]

debug do
title = color("#{payload[:klass]} #{subject}", GREEN, true)
title = color("#{payload[:klass]} #{subject}", GREEN, bold: true)
request_body = format_request_body(request, format: format)

message = " #{title} #{request_body}"
Expand All @@ -27,11 +27,11 @@ def log_response(subject, event, warn_errors: false)
return unless response

debug do
title = color("#{payload[:klass]} #{subject}", GREEN, true)
title = color("#{payload[:klass]} #{subject}", GREEN, bold: true)

duration = "#{event.duration.round(1)}ms"
duration += " / took #{response["took"]}ms" if response.key?("took")
duration = color("(#{duration})", GREEN, true)
duration = color("(#{duration})", GREEN, bold: true)

message = " #{title} #{duration}"
message += " got errors" if response["errors"]
Expand All @@ -44,7 +44,7 @@ def log_response(subject, event, warn_errors: false)

errors = response["items"].select { |k, _| k.values.first["error"] }
errors.each do |error|
warn { color(error, RED, true) }
warn { color(error, RED, bold: true) }
end
end

Expand All @@ -54,16 +54,24 @@ def format_request_body(body, format: nil)
body.ai(limit: true, index: false)
when "full"
json = MultiJson.dump(body)
color(json, BLUE, true)
color(json, BLUE, bold: true)
when "truncated"
json = MultiJson.dump(body).truncate(200, omission: "…}")
color(json, BLUE, true)
color(json, BLUE, bold: true)
end
end

def inspect_json_size(json)
ApplicationController.helpers.number_to_human_size(MultiJson.dump(json).bytesize)
end

def color(message, color, bold: false)
if Gem::Version.new(ActiveSupport::VERSION) >= Gem::Version.new("7.1.0")
super
else
super(message, color, bold)
end
end
end
end
end

0 comments on commit 87e9e13

Please sign in to comment.