From 2b1964d94eee0b20c12f5c602c427643057e787b Mon Sep 17 00:00:00 2001 From: Tom de Bruijn Date: Wed, 7 Dec 2022 10:45:33 +0100 Subject: [PATCH] Track new Ruby 3.2 VM cache metrics In Ruby 3.2 the `class_serial` and `global_constant_state` VM metrics were removed in favor of the new `constant_cache_invalidations` and `constant_cache_misses` metrics. Report these new metrics when available. This does not require an update to the "Ruby (VM) metrics" magic dashboard definition. https://github.com/ruby/ruby/commit/13bd617ea6fdf72467c593639cf33312a06c330c https://github.com/ruby/ruby/commit/6068da8937d7e4358943f95e7450dae7179a7763 --- .../track-new-ruby-3-2-vm-cache-metrics.md | 6 +++ lib/appsignal/probes/mri.rb | 43 ++++++++++++++----- spec/lib/appsignal/probes/mri_spec.rb | 11 +++-- spec/support/helpers/dependency_helper.rb | 4 ++ 4 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 .changesets/track-new-ruby-3-2-vm-cache-metrics.md diff --git a/.changesets/track-new-ruby-3-2-vm-cache-metrics.md b/.changesets/track-new-ruby-3-2-vm-cache-metrics.md new file mode 100644 index 000000000..cf08e05bf --- /dev/null +++ b/.changesets/track-new-ruby-3-2-vm-cache-metrics.md @@ -0,0 +1,6 @@ +--- +bump: "patch" +type: "change" +--- + +Track new Ruby 3.2 VM cache metrics. In Ruby 3.2 the `class_serial` and `global_constant_state` metrics are no longer reported for the "Ruby (VM) metrics" magic dashboard, because Ruby 3.2 removed these metrics. Instead we will now report the new `constant_cache_invalidations` and `constant_cache_misses` metrics reported by Ruby 3.2. diff --git a/lib/appsignal/probes/mri.rb b/lib/appsignal/probes/mri.rb index 7858b1d6c..7313090f0 100644 --- a/lib/appsignal/probes/mri.rb +++ b/lib/appsignal/probes/mri.rb @@ -18,17 +18,38 @@ def initialize(appsignal: Appsignal, gc_profiler: Appsignal::GarbageCollection.p def call stat = RubyVM.stat - set_gauge( - "ruby_vm", - stat[:class_serial], - :metric => :class_serial - ) - - set_gauge( - "ruby_vm", - stat[:constant_cache] ? stat[:constant_cache].values.sum : stat[:global_constant_state], - :metric => :global_constant_state - ) + constant_cache_invalidations = stat[:constant_cache_invalidations] + if constant_cache_invalidations + set_gauge( + "ruby_vm", + constant_cache_invalidations, + :metric => :constant_cache_invalidations + ) + end + + constant_cache_misses = stat[:constant_cache_misses] + if constant_cache_misses + set_gauge( + "ruby_vm", + constant_cache_misses, + :metric => :constant_cache_misses + ) + end + + class_serial = stat[:class_serial] + if class_serial + set_gauge("ruby_vm", class_serial, :metric => :class_serial) + end + + global_constant_state = + stat[:constant_cache] ? stat[:constant_cache].values.sum : stat[:global_constant_state] + if global_constant_state + set_gauge( + "ruby_vm", + global_constant_state, + :metric => :global_constant_state + ) + end set_gauge("thread_count", Thread.list.size) if Appsignal::GarbageCollection.enabled? diff --git a/spec/lib/appsignal/probes/mri_spec.rb b/spec/lib/appsignal/probes/mri_spec.rb index 78c8ed8fa..05d204602 100644 --- a/spec/lib/appsignal/probes/mri_spec.rb +++ b/spec/lib/appsignal/probes/mri_spec.rb @@ -42,10 +42,15 @@ def set_gauge(*args) # rubocop:disable Naming/AccessorMethodName allow(GC::Profiler).to receive(:enabled?).and_return(true) end - it "should track vm metrics" do + it "should track vm cache metrics" do probe.call - expect_gauge_value("ruby_vm", :tags => { :metric => :class_serial }) - expect_gauge_value("ruby_vm", :tags => { :metric => :global_constant_state }) + if DependencyHelper.ruby_3_2_or_newer? + expect_gauge_value("ruby_vm", :tags => { :metric => :constant_cache_invalidations }) + expect_gauge_value("ruby_vm", :tags => { :metric => :constant_cache_misses }) + else + expect_gauge_value("ruby_vm", :tags => { :metric => :class_serial }) + expect_gauge_value("ruby_vm", :tags => { :metric => :global_constant_state }) + end end it "tracks thread counts" do diff --git a/spec/support/helpers/dependency_helper.rb b/spec/support/helpers/dependency_helper.rb index b48835050..61dc16f18 100644 --- a/spec/support/helpers/dependency_helper.rb +++ b/spec/support/helpers/dependency_helper.rb @@ -13,6 +13,10 @@ def ruby_3_1_or_newer? ruby_version >= Gem::Version.new("3.1.0") end + def ruby_3_2_or_newer? + ruby_version >= Gem::Version.new("3.2.0") + end + def running_jruby? Appsignal::System.jruby? end