Skip to content

Commit e61dc26

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 e61dc26

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,27 @@ 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+
original_version = ::RubyLLM::VERSION
19+
::RubyLLM.send(:remove_const, :VERSION)
20+
::RubyLLM.const_set(:VERSION, "1.7.99")
21+
22+
instrumentation = OpenTelemetry::Instrumentation::RubyLLM::Instrumentation.instance
23+
assert_equal false, instrumentation.compatible?
24+
ensure
25+
::RubyLLM.send(:remove_const, :VERSION)
26+
::RubyLLM.const_set(:VERSION, original_version)
27+
end
28+
29+
def test_minimum_ruby_llm_version_is_pinned_at_1_8_0
30+
assert_equal "1.8.0", OpenTelemetry::Instrumentation::RubyLLM::Instrumentation::MINIMUM_RUBY_LLM_VERSION
31+
end
32+
1233
def test_creates_span_with_attributes
1334
stub_request(:post, "https://api.openai.com/v1/chat/completions")
1435
.to_return(

0 commit comments

Comments
 (0)