Skip to content

Commit 27d956e

Browse files
authored
Merge pull request #1 from basecamp/address-rails-7-1-log-subscriber-deprecation-backport
Address Rails 7.1 deprecation warning in elasticsearch-rails
2 parents d6c88ce + d7a83c0 commit 27d956e

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

elasticsearch-rails/lib/elasticsearch/rails/instrumentation/log_subscriber.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,19 @@ def search(event)
2929
payload = event.payload
3030
name = "#{payload[:klass]} #{payload[:name]} (#{event.duration.round(1)}ms)"
3131
search = payload[:search].inspect.gsub(/:(\w+)=>/, '\1: ')
32+
debug %Q| #{color(name, GREEN, color_option(true))} #{colorize_logging ? "\e[2m#{search}\e[0m" : search}|
33+
end
34+
35+
private
36+
37+
def color_option(bold_value)
38+
new_color_syntax? ? { bold: bold_value } : bold_value
39+
end
40+
41+
def new_color_syntax?
42+
return @new_color_syntax if defined?(@new_color_syntax)
3243

33-
debug %Q| #{color(name, GREEN, true)} #{colorize_logging ? "\e[2m#{search}\e[0m" : search}|
44+
@new_color_syntax = ::Rails.respond_to?(:gem_version) && ::Rails.gem_version >= '7.1'
3445
end
3546
end
3647

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
require 'spec_helper'
19+
require 'elasticsearch/rails/instrumentation/log_subscriber'
20+
21+
describe Elasticsearch::Rails::Instrumentation::LogSubscriber do
22+
subject(:instance) { described_class.new }
23+
24+
let(:logger) { instance_double(Logger) }
25+
26+
before do
27+
allow(instance).to receive(:logger) { logger }
28+
end
29+
30+
describe "#search" do
31+
subject { instance.search(event) }
32+
33+
let(:event) { double("search.elasticsearch", duration: 1.2345, payload: { name: "execute", search: { query: { match_all: {}}}}) }
34+
35+
it "logs the event" do
36+
expect(instance).to receive(:color).with(" execute (1.2ms)", described_class::GREEN, { bold: true }).and_call_original
37+
expect(logger).to receive(:debug?) { true }
38+
expect(logger).to receive(:debug).with(" \e[1m\e[32m execute (1.2ms)\e[0m \e[2m{query: {match_all: {}}}\e[0m")
39+
subject
40+
end
41+
42+
context "when Rails version is older" do
43+
let(:rails_version) { "7.0.0" }
44+
45+
before do
46+
allow(::Rails).to receive(:gem_version) { Gem::Version.new(rails_version) }
47+
end
48+
49+
it "logs the event" do
50+
expect(instance).to receive(:color).with(" execute (1.2ms)", described_class::GREEN, true).and_call_original
51+
expect(logger).to receive(:debug?) { true }
52+
expect(logger).to receive(:debug).with(" \e[1m\e[32m execute (1.2ms)\e[0m \e[2m{query: {match_all: {}}}\e[0m")
53+
subject
54+
end
55+
end
56+
end
57+
end

elasticsearch-rails/spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'elasticsearch/model'
44
require 'elasticsearch/rails'
55
require 'rails/railtie'
6+
require 'rails/version'
67
require 'elasticsearch/rails/instrumentation'
78

89

0 commit comments

Comments
 (0)