From 494c5122375f2a2de697c180f66492423037fdf1 Mon Sep 17 00:00:00 2001 From: Syphax Date: Sat, 5 Oct 2024 12:56:23 +0200 Subject: [PATCH] use directly RailsPerformance::Models::CustomRecord instead of measure --- lib/ontologies_api_client/http.rb | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/ontologies_api_client/http.rb b/lib/ontologies_api_client/http.rb index 6151010..36dc67a 100644 --- a/lib/ontologies_api_client/http.rb +++ b/lib/ontologies_api_client/http.rb @@ -69,7 +69,7 @@ def self.get(path, params = {}, options = {}) begin begin response = nil - time = monitor_time(path, params, response.headers["X-Rack-Cache"]) do + time = Benchmark.realtime do response = connection.get do |req| req.url path req.params = params.dup @@ -78,6 +78,8 @@ def self.get(path, params = {}, options = {}) req.headers[:invalidate_cache] = invalidate_cache end end + + monitor_request(params, path, response, time) puts "Getting: #{path} with #{params} (t: #{time}s - cache: #{response.headers["X-Rack-Cache"]})" if $DEBUG_API_CLIENT rescue Exception => e params = Faraday::Utils.build_query(params) @@ -165,18 +167,17 @@ def self.object_from_json(json) private - def self.monitor_time(path, params, cache_hit, &block) - Benchmark.realtime do - if defined?(Rails) && RailsPerformance.enabled - RailsPerformance.measure("Getting: #{path} with #{params} - cache: #{cache_hit}", "API Call #{path}") do - block.call - end - else - block.call - end - end + def self.monitor_request(params, path, response, time) + RailsPerformance::Models::CustomRecord.new( + tag_name: "Getting: #{path} with #{params} - cache: (#{response.headers["X-Rack-Cache"]})", + namespace_name: "API call #{path}", + status: response.status, + duration: time, + datetime: Time.current.strftime(RailsPerformance::FORMAT), + datetimei: Time.current.to_i).save if defined?(Rails) && RailsPerformance.enabled end + def self.custom_req(obj, file, file_attribute, req) req.headers['Content-Type'] = 'application/json'