Skip to content

Commit

Permalink
feat: make ActiveRecord 7 compatible (#1043)
Browse files Browse the repository at this point in the history
* feat: make delegation work for active-record

* wip: add appraisal, add ruby2_keywords compat gem, change test

* wip:

* wip: make sure we require the ruby2_keywords

* Rails 7

Co-authored-by: Francis Bogsanyi <[email protected]>
  • Loading branch information
SomalianIvan and fbogsany authored Jan 10, 2022
1 parent e58fe94 commit 611be2d
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 18 deletions.
8 changes: 7 additions & 1 deletion instrumentation/active_record/Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ end

# Rails 5.2 is not compatible with Ruby 3.0
# https://github.com/rails/rails/issues/40938
if RUBY_VERSION < '3'
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3')
appraise 'activerecord-5.2' do
gem 'activerecord', '~> 5.2.0'
end
end

if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.0')
appraise 'activerecord-7.0' do
gem 'activerecord', '~> 7.0.0'
end
end
18 changes: 18 additions & 0 deletions instrumentation/active_record/gemfiles/activerecord_7.0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "opentelemetry-api", path: "../../../api"
gem "opentelemetry-instrumentation-base", path: "../../base"
gem "activerecord", "~> 7.0.0"

group :test do
gem "byebug"
gem "opentelemetry-common", path: "../../../common"
gem "opentelemetry-sdk", path: "../../../sdk"
gem "opentelemetry-semantic_conventions", path: "../../../semantic_conventions"
gem "pry-byebug"
gem "sqlite3-ruby"
end

gemspec path: "../"
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module ActiveRecord
# The Instrumentation class contains logic to detect and install the ActiveRecord instrumentation
class Instrumentation < OpenTelemetry::Instrumentation::Base
MINIMUM_VERSION = Gem::Version.new('5.2.0')
MAX_MAJOR_VERSION = 6
MAX_MAJOR_VERSION = 7

install do |_config|
require_dependencies
Expand Down Expand Up @@ -60,6 +60,7 @@ def patch
end

def require_dependencies
require 'ruby2_keywords'
require_relative 'patches/querying'
require_relative 'patches/persistence'
require_relative 'patches/persistence_class_methods'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

module OpenTelemetry
module Instrumentation
module ActiveRecord
Expand All @@ -19,39 +18,39 @@ class << base

# Contains ActiveRecord::Persistence::ClassMethods to be patched
module ClassMethods
def insert(attributes, returning: nil, unique_by: nil)
ruby2_keywords def insert(*args)
tracer.in_span("#{self}.insert") do
super
super(*args)
end
end

def insert_all(attributes, returning: nil, unique_by: nil)
ruby2_keywords def insert_all(*args)
tracer.in_span("#{self}.insert_all") do
super
super(*args)
end
end

def insert!(attributes, returning: nil)
ruby2_keywords def insert!(*args)
tracer.in_span("#{self}.insert!") do
super
super(*args)
end
end

def insert_all!(attributes, returning: nil)
ruby2_keywords def insert_all!(*args)
tracer.in_span("#{self}.insert_all!") do
super
super(*args)
end
end

def upsert(attributes, returning: nil, unique_by: nil)
ruby2_keywords def upsert(*args)
tracer.in_span("#{self}.upsert") do
super
super(*args)
end
end

def upsert_all(attributes, returning: nil, unique_by: nil)
ruby2_keywords def upsert_all(*args)
tracer.in_span("#{self}.upsert_all") do
super
super(*args)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Gem::Specification.new do |spec|

spec.add_dependency 'opentelemetry-api', '~> 1.0'
spec.add_dependency 'opentelemetry-instrumentation-base', '~> 0.19.0'
spec.add_dependency 'ruby2_keywords'

spec.add_development_dependency 'activerecord'
spec.add_development_dependency 'appraisal', '~> 2.2.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
end

it 'when a version above the maximum supported gem version is installed' do
Gem.stub(:loaded_specs, 'activerecord' => Gem::Specification.new { |s| s.version = '7.0.0' }) do
Gem.stub(:loaded_specs, 'activerecord' => Gem::Specification.new { |s| s.version = '8.0.0' }) do
_(instrumentation.compatible?).must_equal false
end
end

it 'it treats pre releases as being equivalent to a full release' do
Gem.stub(:loaded_specs, 'activerecord' => Gem::Specification.new { |s| s.version = '7.0.0.alpha' }) do
Gem.stub(:loaded_specs, 'activerecord' => Gem::Specification.new { |s| s.version = '8.0.0.alpha' }) do
_(instrumentation.compatible?).must_equal false
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

describe 'find_by_sql' do
it 'traces' do
User.first
User.find_by_sql('SELECT * FROM users')

find_span = spans.find { |s| s.name == 'User.find_by_sql' }
_(find_span).wont_be_nil
Expand Down

0 comments on commit 611be2d

Please sign in to comment.