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

Fix profiler libmysqlclient version detection with mysql2-aurora gem #2956

Merged
merged 1 commit into from
Jul 11, 2023
Merged
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
1 change: 1 addition & 0 deletions Steepfile
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ target :ddtrace do
library 'google-protobuf'
library 'protobuf-cucumber'
library 'mysql2'
library 'mysql2-aurora'
library 'opentracing'
library 'concurrent-ruby'
library 'faraday'
Expand Down
21 changes: 17 additions & 4 deletions lib/datadog/profiling/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def self.build_profiler_component(settings:, agent_settings:, optional_tracer:)
if Gem.loaded_specs['mysql2'] && incompatible_libmysqlclient_version?(settings)
Datadog.logger.warn(
'Enabling the profiling "no signals" workaround because an incompatible version of the mysql2 gem is ' \
'installed. Profiling data will have lower quality.' \
'installed. Profiling data will have lower quality. ' \
'To fix this, upgrade the libmysqlclient in your OS image to version 8.0.0 or above.'
)
return true
Expand Down Expand Up @@ -245,9 +245,22 @@ def self.build_profiler_component(settings:, agent_settings:, optional_tracer:)
begin
require 'mysql2'

return true unless defined?(Mysql2::Client) && Mysql2::Client.respond_to?(:info)

libmysqlclient_version = Gem::Version.new(Mysql2::Client.info[:version])
# The mysql2-aurora gem likes to monkey patch itself in replacement of Mysql2::Client, and uses
# `method_missing` to delegate to the original BUT unfortunately does not implement `respond_to_missing?` and
# thus our `respond_to?(:info)` below was failing.
#
# But on the bright side, the gem does stash a reference to the original Mysql2::Client class in a constant,
# so if that constant exists, we use that for our probing.
mysql2_client_class =
if defined?(Mysql2::Aurora::ORIGINAL_CLIENT_CLASS)
Mysql2::Aurora::ORIGINAL_CLIENT_CLASS
elsif defined?(Mysql2::Client)
Mysql2::Client
end

return true unless mysql2_client_class && mysql2_client_class.respond_to?(:info)

libmysqlclient_version = Gem::Version.new(mysql2_client_class.info[:version])

compatible = libmysqlclient_version >= Gem::Version.new('8.0.0')

Expand Down
26 changes: 26 additions & 0 deletions spec/datadog/profiling/component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,32 @@
no_signals_workaround_enabled?
end
end

context 'when mysql2-aurora gem is loaded and libmysqlclient < 8.0.0' do
before do
fake_original_client = double('Fake original Mysql2::Client')
stub_const('Mysql2::Aurora::ORIGINAL_CLIENT_CLASS', fake_original_client)
expect(fake_original_client).to receive(:info).and_return({ version: '7.9.9' })

client_replaced_by_aurora = double('Fake Aurora Mysql2::Client')
stub_const('Mysql2::Client', client_replaced_by_aurora)
end

it { is_expected.to be true }
end

context 'when mysql2-aurora gem is loaded and libmysqlclient >= 8.0.0' do
before do
fake_original_client = double('Fake original Mysql2::Client')
stub_const('Mysql2::Aurora::ORIGINAL_CLIENT_CLASS', fake_original_client)
expect(fake_original_client).to receive(:info).and_return({ version: '8.0.0' })

client_replaced_by_aurora = double('Fake Aurora Mysql2::Client')
stub_const('Mysql2::Client', client_replaced_by_aurora)
end

it { is_expected.to be false }
end
end
end

Expand Down
5 changes: 5 additions & 0 deletions vendor/rbs/mysql2-aurora/0/mysql2-aurora.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Mysql2
module Aurora
ORIGINAL_CLIENT_CLASS: untyped
end
end