From a0e9dc07cd1622b34ec8bb16b7b6f7414d5cfac5 Mon Sep 17 00:00:00 2001 From: Albert Bertram Date: Thu, 6 Feb 2025 16:28:38 -0500 Subject: [PATCH] EXPERIMENTAL Add metrics for api_response_duration_seconds histograms. --- config.ru | 2 ++ config/puma.rb | 6 ++++++ lib/alma_rest_client/monkey_patch.rb | 16 ++++++++++++++++ .../lib/spectrum/search_engines/primo/engine.rb | 16 +++++++++++++++- .../lib/spectrum/search_engines/solr.rb | 6 +++++- 5 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 lib/alma_rest_client/monkey_patch.rb diff --git a/config.ru b/config.ru index a689b3ab..deee8645 100644 --- a/config.ru +++ b/config.ru @@ -13,6 +13,8 @@ end Bundler.require Spectrum::Json.configure(__dir__, ENV["RAILS_RELATIVE_URL_ROOT"]) +require 'alma_rest_client/monkey_patch' + use Rack::ReverseProxy do reverse_proxy %r{^/browse.css}, "https://#{ENV["BROWSE_HOST"]}/$1" reverse_proxy %r{^/catalog/browse/(.*)$}, "https://#{ENV["BROWSE_HOST"]}/$1" diff --git a/config/puma.rb b/config/puma.rb index 71eb4286..f0aa03b4 100644 --- a/config/puma.rb +++ b/config/puma.rb @@ -65,6 +65,12 @@ labels: %i[method path], buckets: Prometheus::Client::Histogram::DEFAULT_BUCKETS + [15, 20, 30] ) + Prometheus::Client.registry.histogram( + :api_response_duration_seconds, + docstring: "The API response duration for requests made by Spectrum.", + labels: %i[source], + buckets: Prometheus::Client::Histogram::DEFAULT_BUCKETS + [15, 20, 30] + ) Yabeda.configure! plugin :yabeda diff --git a/lib/alma_rest_client/monkey_patch.rb b/lib/alma_rest_client/monkey_patch.rb new file mode 100644 index 00000000..27c9bf90 --- /dev/null +++ b/lib/alma_rest_client/monkey_patch.rb @@ -0,0 +1,16 @@ +module AlmaRestClient + class Client + [:get, :post, :delete, :put].each do |name| + old_method = instance_method(name) + define_method(name) do |url, options = {}| + @metrics ||= Prometheus::Client.registry.get(:api_response_duration_seconds) + response = nil + duration = Benchmark.realtime do + response = old_method.bind(self).(url, options) + end + @metrics.observe(duration, labels: {source: "alma"}) + response + end + end + end +end diff --git a/local-gems/spectrum-config/lib/spectrum/search_engines/primo/engine.rb b/local-gems/spectrum-config/lib/spectrum/search_engines/primo/engine.rb index a88410b6..aa702eeb 100644 --- a/local-gems/spectrum-config/lib/spectrum/search_engines/primo/engine.rb +++ b/local-gems/spectrum-config/lib/spectrum/search_engines/primo/engine.rb @@ -24,6 +24,7 @@ def initialize( if defined?(Rails) && Rails.respond_to?(:logger) @logger = Rails.logger end + @metrics = Prometheus::Client.registry.get(:api_response_duration_seconds) end def results @@ -36,8 +37,21 @@ def total_items def search + return @results if @results @logger&.info { url } - @results ||= Response.for_json(HTTParty.get(url)).with_libkey(libkey) + + response = nil + duration = Benchmark.realtime do + response = Response.for_json(HTTParty.get(url)) + end + @metrics.observe(duration, labels: {source: 'primo'}) + + duration = Benchmark.realtime do + @results = response.with_libkey(libkey) + end + @metrics.observe(duration, labels: {source: 'libkey'}) + + @results end def url diff --git a/local-gems/spectrum-config/lib/spectrum/search_engines/solr.rb b/local-gems/spectrum-config/lib/spectrum/search_engines/solr.rb index 420b05bc..a39d6670 100644 --- a/local-gems/spectrum-config/lib/spectrum/search_engines/solr.rb +++ b/local-gems/spectrum-config/lib/spectrum/search_engines/solr.rb @@ -32,7 +32,11 @@ def initialize(original_options) @params[:qq] ||= '"' + RSolr.solr_escape(@params[:q]) + '"' - @response = Response.for(@solr.post("select", params: @params)) + @metrics = Prometheus::Client.registry.get(:api_response_duration_seconds) + duration = Benchmark.realtime do + @response = Response.for(@solr.post("select", params: @params)) + end + @metrics.observe(duration, labels: {source: @source.id}) end def total_items