Skip to content

Commit 0e7c749

Browse files
fix: require ruby_llm >= 1.8.0 via OTel compatible block
Make the requirement explicit via the standard OTel instrumentation pattern: a `compatible` block that returns false when the loaded `ruby_llm` version is below the pin (mirrors what httpx, http, http_client do in opentelemetry-ruby-contrib). When the block returns false, OpenTelemetry::Instrumentation::Base silently skips installing the instrumentation — no raise, no patch application — so apps on older `ruby_llm` continue to function without the OTel traces (and without a hard crash). Note: `gem "rspec-mocks"` test dependency added to use `stub_const`. This is aligned with other instrumentation gems in opentelemetry-ruby-contrib.
1 parent 9a97ca4 commit 0e7c749

4 files changed

Lines changed: 48 additions & 0 deletions

File tree

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ gem "opentelemetry-exporter-otlp"
1010

1111
group :test do
1212
gem "minitest"
13+
gem "rspec-mocks"
1314
gem "webmock"
1415
gem "rake"
1516
end

lib/opentelemetry/instrumentation/ruby_llm/instrumentation.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module OpenTelemetry
44
module Instrumentation
55
module RubyLLM
66
class Instrumentation < OpenTelemetry::Instrumentation::Base
7+
MINIMUM_RUBY_LLM_VERSION = "1.8.0"
8+
79
instrumentation_name "OpenTelemetry::Instrumentation::RubyLLM"
810
instrumentation_version VERSION
911

@@ -13,6 +15,22 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
1315
defined?(::RubyLLM)
1416
end
1517

18+
compatible do
19+
# The embedding patch calls `RubyLLM::Models.resolve` (class-method delegation added in 1.8.0);
20+
# Anything older than 1.8.0 would NoMethodError / NameError at install or first use.
21+
compatible = Gem::Version.new(::RubyLLM::VERSION) >= Gem::Version.new(MINIMUM_RUBY_LLM_VERSION)
22+
23+
unless compatible
24+
OpenTelemetry.logger.warn(
25+
"[OpenTelemetry::Instrumentation::RubyLLM] ruby_llm " \
26+
"#{::RubyLLM::VERSION} is below the required minimum " \
27+
"#{MINIMUM_RUBY_LLM_VERSION}; instrumentation will not be installed."
28+
)
29+
end
30+
31+
compatible
32+
end
33+
1634
install do |_config|
1735
require_relative "patches/chat"
1836
require_relative "patches/embedding"

test/instrumentation_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,34 @@ def setup
99
end
1010
end
1111

12+
def test_compatible_is_true_for_current_ruby_llm_version
13+
instrumentation = OpenTelemetry::Instrumentation::RubyLLM::Instrumentation.instance
14+
assert_equal true, instrumentation.compatible?
15+
end
16+
17+
def test_compatible_is_false_when_ruby_llm_below_minimum
18+
stub_const("::RubyLLM::VERSION", "1.7.99")
19+
20+
instrumentation = OpenTelemetry::Instrumentation::RubyLLM::Instrumentation.instance
21+
assert_equal false, instrumentation.compatible?
22+
end
23+
24+
def test_warns_when_ruby_llm_is_below_minimum
25+
stub_const("::RubyLLM::VERSION", "1.7.99")
26+
27+
logger = Logger.new(StringIO.new)
28+
allow(OpenTelemetry).to receive(:logger).and_return(logger)
29+
expect(logger).to receive(:warn).with(
30+
/ruby_llm 1\.7\.99 is below the required minimum 1\.8\.0/
31+
)
32+
33+
OpenTelemetry::Instrumentation::RubyLLM::Instrumentation.instance.compatible?
34+
end
35+
36+
def test_minimum_ruby_llm_version_is_pinned_at_1_8_0
37+
assert_equal "1.8.0", OpenTelemetry::Instrumentation::RubyLLM::Instrumentation::MINIMUM_RUBY_LLM_VERSION
38+
end
39+
1240
def test_creates_span_with_attributes
1341
stub_request(:post, "https://api.openai.com/v1/chat/completions")
1442
.to_return(

test/test_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
22

33
require "minitest/autorun"
4+
require "rspec/mocks/minitest_integration"
45
require "webmock/minitest"
56
require "ruby_llm"
67
require "opentelemetry/sdk"

0 commit comments

Comments
 (0)