From 87e9e138de5f21acbda5a2d1ac9ae7c4616e0ee5 Mon Sep 17 00:00:00 2001 From: Sebastien Savater Date: Wed, 21 Aug 2024 15:55:33 +0200 Subject: [PATCH] Update instrumentation to be compatible with Rails 7.2 --- lib/caoutsearch/instrumentation/base.rb | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/caoutsearch/instrumentation/base.rb b/lib/caoutsearch/instrumentation/base.rb index 4654bc0..fdf4e8d 100644 --- a/lib/caoutsearch/instrumentation/base.rb +++ b/lib/caoutsearch/instrumentation/base.rb @@ -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}" @@ -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"] @@ -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 @@ -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