From 363f81253fac192baa52702d3f1e5170cfca7df9 Mon Sep 17 00:00:00 2001 From: Donal McBreen Date: Thu, 14 Sep 2023 08:46:18 +0100 Subject: [PATCH] Allow AR instrumentation to be disabled We want the cache queries to be fast and instrumentation can slow things down. Additionally you might not want the SolidCache queries showing up in your logs. Instrumentation is enabled by default but can we disabled with: ```ruby config.cache_store = [ :solid_cache_store, active_record_instrumentation: false ] ``` --- README.md | 1 + app/models/solid_cache/record.rb | 10 +++++++ .../cluster/connection_handling.rb | 29 ++++++++++++++++--- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5ea9d3b..eac8946 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ Solid cache supports these options in addition to the universal `ActiveSupport:: - `max_entries` - the maximum number of entries allowed in the cache (default: `2.weeks.to_i`) - `cluster` - a Hash of options for the cache database cluster, e.g { shards: [:database1, :database2, :database3] } - `clusters` - and Array of Hashes for separate cache clusters (ignored if `:cluster` is set) +- `active_record_instrumentation` - whether to instrument the cache's queries (default: `true`) For more information on cache clusters see [Sharding the cache](#sharding-the-cache) ### Cache trimming diff --git a/app/models/solid_cache/record.rb b/app/models/solid_cache/record.rb index 49a0078..7894d9a 100644 --- a/app/models/solid_cache/record.rb +++ b/app/models/solid_cache/record.rb @@ -1,8 +1,18 @@ module SolidCache class Record < ActiveRecord::Base + NULL_INSTRUMENTER = ActiveSupport::Notifications::Instrumenter.new(ActiveSupport::Notifications::Fanout.new) + self.abstract_class = true connects_to **SolidCache.connects_to if SolidCache.connects_to + + class << self + def disable_instrumentation + connection.with_instrumenter(NULL_INSTRUMENTER) do + yield + end + end + end end end diff --git a/lib/solid_cache/cluster/connection_handling.rb b/lib/solid_cache/cluster/connection_handling.rb index d60cc4a..10b0d4f 100644 --- a/lib/solid_cache/cluster/connection_handling.rb +++ b/lib/solid_cache/cluster/connection_handling.rb @@ -92,14 +92,25 @@ def reading_shard(normalized_key:) with_shard(shard_for_normalized_key(normalized_key)) { yield } end + def active_record_instrumentation? + @active_record_instrumentation + end + private attr_reader :consistent_hash def with_shard(shard) - if shard - Record.connected_to(shard: shard) { yield } - else - yield + if shard + Record.connected_to(shard: shard) do + disable_active_record_instrumentation_if_required do + yield + end + end + else + disable_active_record_instrumentation_if_required do + yield + end + end end end @@ -131,6 +142,16 @@ def async_if_required yield end end + + def disable_active_record_instrumentation_if_required + if active_record_instrumentation? + yield + else + Record.disable_instrumentation do + yield + end + end + end end end end