Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update instrumentation to be compatible with Rails 7.2 #15

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 14 additions & 0 deletions spec/caoutsearch/instrumentation/index_spec.rb
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions spec/caoutsearch/instrumentation/search_spec.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading