diff --git a/lib/caoutsearch/instrumentation/base.rb b/lib/caoutsearch/instrumentation/base.rb index 4654bc0..3a75235 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 diff --git a/spec/caoutsearch/instrumentation/index_spec.rb b/spec/caoutsearch/instrumentation/index_spec.rb new file mode 100644 index 0000000..0fcd0e4 --- /dev/null +++ b/spec/caoutsearch/instrumentation/index_spec.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe Caoutsearch::Instrumentation::Index do + before do + Caoutsearch.instrument!(index: true) + + stub_index_class("SampleIndex") + stub_record_class("Sample") + end + + pending "TODO" +end diff --git a/spec/caoutsearch/instrumentation/search_spec.rb b/spec/caoutsearch/instrumentation/search_spec.rb new file mode 100644 index 0000000..56d27ff --- /dev/null +++ b/spec/caoutsearch/instrumentation/search_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe Caoutsearch::Instrumentation::Search do + let!(:search) { search_class.new } + let!(:search_class) { stub_search_class("SampleSearch") } + + before do + stub_elasticsearch_search_request("samples", [ + {"_id" => "135", "_source" => {"name" => "Hello World"}}, + {"_id" => "137", "_source" => {"name" => "Hello World"}} + ]) + end + + context "when setting instrumentation to full" do + before { Caoutsearch.instrument!(search: "full") } + + it "saves instrumentation options" do + expect(Caoutsearch.instrumentation_options).to eq({search: "full"}) + end + + it "instruments the search query" do + pending "TODO" + + expect { search.response }.to match_instrumentation + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f32c828..2f83df5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -54,6 +54,11 @@ end end end + + config.after :context, :instrumentation do + Caoutsearch::Instrumentation::Index.detach_from :caoutsearch_index + Caoutsearch::Instrumentation::Search.detach_from :caoutsearch_search + end end def stub_index_class(class_name, &block)