Skip to content

Commit

Permalink
EXPERIMENTAL
Browse files Browse the repository at this point in the history
Add metrics for api_response_duration_seconds histograms.
  • Loading branch information
bertrama committed Feb 6, 2025
1 parent 295a116 commit a0e9dc0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
2 changes: 2 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions config/puma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions lib/alma_rest_client/monkey_patch.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a0e9dc0

Please sign in to comment.